Search code examples
allegro5

Allegro5 showing text doesnt work


I have this kind of problem: I want to show the message "Hello!" near the centre of the Allegro screen before the game starts. Don't know why there is always only full window white colour after I compile the program not message "Hello!". I don't know why it doesn't show message "Hello!" . But if I erase the code between comment lines //***** program show the message "Hello!" well. Can someone can tell me how to solve this problem?

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

#include<iostream>
using namespace std;
int main(){
    int ScreenWidth = 800, ScreenHeight = 600;

    if (!al_init())
    {
        al_show_native_message_box(NULL, NULL, NULL, "Could not initialize Allegro 5", NULL, NULL);
        return -1;
    }

    al_set_new_display_flags(ALLEGRO_WINDOWED);
    ALLEGRO_DISPLAY *display = al_create_display(ScreenWidth, ScreenHeight);//creating window with dimensions: 800:600 px
    al_set_window_position(display, 200, 100);//place from left top positioning the frame of display
    al_set_window_title(display, "Try to catch me!");

    if (!display)//show this message if sth wrong with display
    {
        al_show_native_message_box(display, "Sample Title", "Display Settings", "Display window was not created succesfully", NULL, ALLEGRO_MESSAGEBOX_ERROR);
        return -1;
    }

    al_init_font_addon();//initialization font addon
    al_init_ttf_addon();//initialization ttf(true type font) addon

    //INTRO
    ALLEGRO_FONT *fontOrbionBlack36 = al_load_font("fonts/Orbitron Black.ttf", 36, NULL);
    al_draw_text(fontOrbionBlack36, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 2, ALLEGRO_ALIGN_CENTRE, "Hello!" );
    al_rest(5.0);

    //********************************************************************************
    al_init_primitives_addon();
    al_install_keyboard();

    ALLEGRO_COLOR electricBlue = al_map_rgb(44, 117, 255);
    ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();

    al_register_event_source(event_queue, al_get_keyboard_event_source());

    bool done = false;
    int x = 10, y = 10;
    int moveSpeed = 5;

    while (!done)
    {
        ALLEGRO_EVENT events;
        al_wait_for_event(event_queue, &events);
        if (events.type = ALLEGRO_EVENT_KEY_DOWN)
        {
            switch (events.keyboard.keycode)
            {
            case ALLEGRO_KEY_DOWN:
                y += moveSpeed;
                break;
            case ALLEGRO_KEY_UP:
                y -= moveSpeed;
                break;          
            case ALLEGRO_KEY_ESCAPE:
                done = true;
                break;
            }
        }
        al_draw_rectangle(x, y, x + 20, y + 20, electricBlue, 2.0);
        al_flip_display();
        al_clear_to_color(al_map_rgb(0, 0, 0));
    }
    //*****************************************************************************************

    al_flip_display();
    al_rest(10.0);//rest is very simmilar to Sleep() it is waitg function, waiting 3s then close the allegro display
    //al_destroy_font(fontOrbionBlack18);
    al_destroy_font(fontOrbionBlack36);
    al_destroy_display(display);//destroy the display at the end of the programm


    return 0;
}

Solution

  • There are a few problems here that are preventing you from seeing the text.

    1. You've set up a 'wait-for-event then draw' loop that is normally associated with the use of a timer, but you have no timer. This means that unless you trigger some sort of event (e.g. by pressing a keyboard key), al_wait_for_event will stall indefinitely and your program will never reach the call to al_flip_display. You can read more about timers here, but for the time being just know you will have to press a keyboard key to get this program to progress.

    2. The ordering of your clear/draw/flip calls is a bit confusing.

    Try something like this within your main loop:

    al_clear_to_color(al_map_rgb(0, 0, 0));`
    al_draw_rectangle(x, y, x + 20, y + 20, electricBlue, 2.0);
    al_draw_text(fontOrbionBlack36, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 2, ALLEGRO_ALIGN_CENTRE, "Hello!" );
    al_flip_display();
    

    In other words, clear first, then draw, then flip. Currently you only draw the text once (before your main loop). This means that clearing the screen will clear the text -- you need to draw the text every frame after clearing the screen but before flipping the display.

    If you change those two things, you should be able to see the text after pressing a keyboard button. However, there are a few other changes that might help:

    1. I'd remove those calls to al_rest unless they serve a useful purpose. Currently they just make you wait 5 seconds before you start your main loop (meaning you won't be able to see the text immediately even if you do press a key).

    2. There's no reason to flip the display after your main loop exits, although I guess this was left over from your earlier experiments with showing text.

    3. When checking the event type, use events.type == ALLEGRO_EVENT_KEY_DOWN instead of events.type = ALLEGRO_EVENT_KEY_DOWN. Currently, you're actually assigning the value ALLEGRO_EVENT_KEY_DOWN to events.type, overwriting whatever its original value was. Your compiler may (and probably should) have warned you about this.