Wednesday, 26 February 2014

Allegro FPS Limiter

Start with a boolean, this will be used for an update statement at the end. Another variable was used for the FPS in case you change it later, so you don't have to change it throughout the program.

bool draw = true;
const int FPS = 60;

Then make an event queue pointer and a timer pointer. 1 / 60 means 60 times a second.

ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
ALLEGRO_TIMER *timer = al_create_timer(1.0 / FPS);

Register the timer into the event queue and start the timer. Putting anything other than the game loop after this statement might fuck up the timer.

al_register_event_source(event_queue, al_get_timer_event_source(timer));//put the timer into the queue
al_start_timer(timer);

Inside the game loop, make the event.

ALLEGRO_EVENT events;
al_wait_for_event(event_queue, &events);

If the event timer ticks into the queue, update the draw variable.

if(events.type == ALLEGRO_EVENT_TIMER)
{
     draw = true;//only draw if something updated
}

If something changed the update the graphics.

if(draw)
{
     do all your graphics stuff here
}

Allegro Colour Variables

Rather than using the rgb colour every time you need a colour argument you can use ALLEGRO_COLOR to set a global colour.

ALLEGRO_COLOR electricBlue = al_map_rgb(44, 117, 255);

This was you only need the variable name rather than the hex code.

Tuesday, 25 February 2014

Allegro Fonts

The first thing you'll need to do is include the font files and the true type font (ttf) file.

#include<allegro5\allegro_ttf.h>
#include<allegro5\allegro_font.h>

Both modules need to be added to the program too.

al_init_font_addon();
al_init_ttf_addon();

Now you'll need to acquire a font file. I got mine from this website for free.
http://www.webpagepublicity.com/free-fonts.html
Once downloaded, put the ttf file in your assets folder of the program.

To load the font into the program you'll need the following a font pointer. The first argument is where you put the file location of the font file. The second argument is the size of the text and the third is for any flags.

ALLEGRO_FONT *font = al_load_font("Assets/Fonts/Airmole Shaded.ttf", 36, NULL);

To display the text on screen use the following function, using the font pointer as the first argument. Next is colour, x position and y position. Fifth is a flag to centralize the text, you could also use ALIGN_LEFT or ALIGN_RIGHT. Lastly, the message you want displayed.

al_draw_text(font, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 2, ALLEGRO_ALIGN_CENTRE, "HELLO");

Allegro Message Boxes

To start using message boxes in Allegro 5 you'll need to include this file:

#include<allegro5\allegro_native_dialog.h>

This will allow you to use boxes that match your operating system.

The following function sets up the message box.  Using display as the first argument will place the window in front of the display. Leaving it as NULL will leave the message box behind the display. The second argument is the box title and the third is the message heading that will display inside the box. Forth argument is where you put your message.

al_show_native_message_box(display, "Error Message", "Error", "Could not initialize Allegro 5", NULL, ALLEGRO_MESSAGEBOX_ERROR);

The last argument represents the type of message box. The following is a list of available options.

ALLEGRO_MESSAGEBOX_WARN          
ALLEGRO_MESSAGEBOX_ERROR          
ALLEGRO_MESSAGEBOX_OK_CANCEL      
ALLEGRO_MESSAGEBOX_YES_NO      
ALLEGRO_MESSAGEBOX_QUESTION

You can use two options at once for example ALLEGRO_MESSAGEBOX_WARN | ALLEGRO_MESSAGEBOX_YES_NO.

The al_show_native_message_box function returns an int that represents which answer the user chose. To access it allocate a variable to it.

int answer = al_show_native_message_box(display, "Error Message", "Error", "Could not initialize Allegro 5", NULL, ALLEGRO_MESSAGEBOX_ERROR);

If the answer is no the return value will be 0. If yes then 1.

Saturday, 11 May 2013

P.U.N.C.H


So this is the showcase for the game we've been working on for our 2nd semester project. The team is called Good Lord! What a lot of biscuits! The team name has a long story about it, maybe a story for another post. P.U.N.C.H is a product of 7 programmers, 1 artist, 1 designer and a producer and was developed using the Havok physics engine.





Limbo: A beautiful and horrible experience

(Artical was originally part of assessment for a narrantology/ludology module for my postgraduate)

I started playing a game called Limbo; it’s a small indie game that’s gotten a good reputation from fans. It’s a beautiful looking game that sets the environment up perfectly, with its minimalist scenery and monotone colours. Everything on screen matches the beautiful but depressing path the boy character follows.

But what surprised me the most was the amount of attachment I had gained in the first 60seconds of gameplay. A boy wakes up in a forest. The smooth running animation glides you across the grass to the next scene. My first reaction was ‘awww how sweet, I’m going to love this game’. I see something on the floor, I go to investigate but get too close. The bear trap snaps shut around the boy’s body which ricochets like a rag doll with the force, decapitating him with a small squirt of blood. It happens so fast it makes me jump. My mouth just dropped, I was in complete shock and within 60seconds of me starting the game, it had managed to make me feel guilty. I shouted ‘IM SO SORRY!’ at the little boy. This theme continues throughout the game but never seems to get old. I felt so attached to this boy; I felt so much responsibility towards his survival. When he fell from a tree and broke his legs, when he got flattened by a rolling boulder, when his convulsing body drowned in a small pond, I had exactly the same reaction each time.



It’s got nothing to do with the graphic deaths. I've played games with much more gore than this. Nor has it anything to do with the death of a child, this concept isn’t one of much horror to me. It is however completely down to the atmosphere the environment makes. The simplicity of each scene draws all your attention to the death, it allows for no visual dilution. The smoothness and fluidity of the boy’s animations are in contrast with the large, sparse surroundings making him seem far more vulnerably and human like. The death events can happen so quick that in less than a second you’re left with nothing, and the boy always dies in one hit. I think this is a fantastic example as to how effective spatial use can be in games, compared to any other medium in which it barely exists at all. This game would not be viable in any other format.

Saturday, 20 April 2013

Error: Stack Overflow

The stack is a part of memory used by your program at run time. It has a predefined size allocated to your program (based on different things like language etc). A stack overflow is when a program uses too much memory from the stack by trying to go outside its allocated area.

The most common reason for this error is infinite recursion:

int function()
{
   return function();
}

Or a deep recursion function that compiles but overflows on use:

void function (argument)
{
    if (condition)
   {
      function (argument.next);
   }
}

Which can be fixed by using stack loops:

stack.push(argument);

while (!stack.empty()) 
{
   argument = stack.pop();

   if (condition)
   {
      stack.push(argument.next);
   }
}

Another possible cause could be by using stupidly large variables, the array below takes up 8 megabytes!

double x[1000000];

Previous examples I've accidentally coded:


Stack overflow caused by infinite recursion:

bool ifCollision()
{
    if(oasisCollision)
   {
   }
}

bool oasisCollision()
{
   if(condition)
   {
       ifCollision();
   }
}