Showing posts with label Allegro. Show all posts
Showing posts with label Allegro. Show all posts

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, 30 March 2013

Elephant Algorithm: They Never Forget


At the moment the elephant memories are represented by a 2 dimensional array but I was actually thinking I might put different types of geographical locations in other that just the oasis, and use a 3 dimensional array for the elephant memories. The 3rd index being a 'tag' to distinguish between the type of location. This might end up as polish though as I want all the basics in by the end of this weekend so I can start on the crowd  algorithm.

Elephants can now store information on where the oasis' are located. This is done by calling a remember function on collision, which stores the current x and y position. An elephant will continue to store coordinates as long as there is collision. The result is the storage of not only the geographical location but also its partial coverage (the area in which the elephant travelled). Originally only one coordinate was going to be stored but this way has given the memories more depth and given the algorithm more functionality, without increasing the complexity. I'm very pleased with how this has worked out even though it isn't as planned. However it is currently using arrays 3600 in size, eventually this should be changed to a vector to increase efficiency.  

I would also rather the arrays size was set on the fly at run time based on the number of various locations, which would be randomly placed and in randomly set quantities. Probably with a limit to the amount of locations present based on the screen resolution. The amount of elephants could be generated at run time too. It's likely that these features won't be in the build I hand in in May but I have enjoyed working on this project and so I will carry on working on next semester.

Sunday, 24 March 2013

Elephant Algorithm Project


It was my intention to finish the 1st algorithm for my innovation project by the end of the month. This leaves me with this weekend and next weekend to complete it (at least the code, write up can come later). Managed to get the collision between elephants and oasis in with no problems. Elephants now know when they are at an oasis or not, they just don't do anything with the information. Next I have to store the information so the elephants can use it.

Saturday, 23 March 2013

Tutorial: 2D Box Collision

2D Box Collision

Theory

If the corner of one square is inside the other square the collision has occurred. For easier reading write 4 functions which check for possible collision on each side (left, right, top, bottom). Then write another function that compares which sides have a possible collision to figure out if collision has actually occurred.

Example

Write a function that checks collision with a single side. Note: sprite is 30 pixels large and getX and getY return the origin point of the sprite which in this case is the top left corner.

bool Blue::possibleCollisionLeft(Orange* orange)
{
        //get necessary coordinates
        int blueLeftSide = getX();
        int orangeLeftSide =  orange->getX();
        int orangeRightSide =  orange->getX() + 30;

        //return true if collision could occur on left side
        if((blueLeftSide <= orangeRightSide) && (blueLeftSide >= orangeLeftSide))
        {
                return true;
        }
        else
        {
                return false;
        }
}

Write a function like this for all 4 sides.
Then bring them all together in another function to compare which ones return true. And don't forget to check if its even colliding with the object you want. If its not then return true, doing this at the beginning of the function will stop the function comparing all the edge functions for no reason.

bool Blue::orangeCollision(Orange* orange)
{
            if(!orange)
            {
                       return false;
            }

        //compare collision function results, return true if a collision has occured
        if((possibleCollisionRight(orange) == true && possibleCollisionBottom(orange) == true)
        || (possibleCollisionLeft(orange) == true && possibleCollisionBottom(orange) == true)
        || (possibleCollisionLeft(orange) == true && possibleCollisionTop(orange) == true)
        || (possibleCollisionRight(orange) == true && possibleCollisionTop(orange) == true))
        {
                return true;   
        }
        else
        {
                return false;
        }
}

Tuesday, 12 March 2013

Elephant Algorith Project


Started on my innovation project, it's all going to plan so far. I've got the basics for the elephant based algorithm demo in. Elephants move around on screen on their own and the geographical locations (oasis) are in too. The next step is to give the elephants a memory back which will probably be made out of an array. Once that's done I have to work out how to transfer data both ways when two elephants have collided. This shouldn't be too hard anyway as the elephants have their own class and so a change to one will change them all. It's more a case of, arrays can confuse me a lot and collision can be a nightmare. Once these two features are in though the rest should be straight forward. Then that's one algorithm and demo down and one more to go. The aim is to get this one finished by the end of this month, and the second one finished by the end of next month, leaving me 17 days until the hand in for polish, paper work and to work on the 4 other modules that are due in on the same day.