none
Cant load font - ALLEGRO 5 RRS feed

  • Question

  • Hello Everyone !

    I have problem with allegro libarry, i find snake game in the web and try to run it, but its always return - Can't Load font.

    I added path to solve this problem, but dont know that i did it well.

    i have font addon and ttf addon in properties... help me ! 

    Here is code.

    #include <allegro5\allegro5.h>
    #include <allegro5\allegro_primitives.h>
    #include <allegro5\allegro_image.h>
    #include <cmath>
    #include <time.h>
    #include <allegro5\allegro_font.h>
    #include <allegro5\allegro_ttf.h>
    #include <allegro5\allegro_audio.h> 
    #include <allegro5\allegro_acodec.h> 
    #include <stdio.h>

    using namespace std;

    int dolzina = 800;
    int visina = 600;

    int main() {

        ALLEGRO_DISPLAY* display;
        const float FPS = 60.0;
        const float frameFPS = 15.0;

        if (!al_init()) return -1;
        display = al_create_display(dolzina, visina);
        if (!display) return -1;
        al_set_window_position(display, 200, 200);


        bool done = false, active = false;
        bool draw = true;
        int x = 0, y = 0, moveSpeed = 5;


        al_init_primitives_addon();
        al_install_keyboard();
        al_init_image_addon();
        al_init_font_addon();
        al_init_ttf_addon();

        ALLEGRO_BITMAP* glava = al_load_bitmap("C:/Users/Windica/source/repos/snaakka/glava.png");
        ALLEGRO_BITMAP* telo = al_load_bitmap("C:/Users/Windica/source/repos/snaakka/telo.png");

        ALLEGRO_BITMAP* coin1 = al_load_bitmap("C:/Users/Windica/source/repos/snaakka/coin.png");

        ALLEGRO_PATH* path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
        al_append_path_component(path, "C:/Users/Windica/source/repos/snaakka");

        al_set_path_filename(path, "C:/Users/Windica/source/repos/snaakka/glava.png");
        glava = al_load_bitmap(al_path_cstr(path, '/'));
        if (glava == NULL)
        {
            fprintf(stderr, "Can't load font\n");
            exit(1);
        }
        al_set_path_filename(path, "C:/Users/Windica/source/repos/snaakka/telo.png");
        telo = al_load_bitmap(al_path_cstr(path, '/'));
        if (telo == NULL)
        {
            fprintf(stderr, "Can't load font\n");
            exit(1);
        }
        al_set_path_filename(path, "C:/Users/Windica/source/repos/snaakka/coin.png");
        coin1 = al_load_bitmap(al_path_cstr(path, '/'));
        if (coin1 == NULL)
        {
            fprintf(stderr, "Can't load font\n");
            exit(1);
        }
        al_set_path_filename(path, "snake.ttf");
        ALLEGRO_FONT *mcfont = al_load_ttf_font(al_path_cstr(path, '/'),39,0);

        if (mcfont == NULL)
        {
            fprintf(stderr, "Can't load font\n");
            exit(1);
        }
        al_set_path_filename(path, "snake.ttf");
        ALLEGRO_FONT *font1 = al_load_ttf_font(al_path_cstr(path, '/'), 39,0);
        if (font1 == NULL)
        {
            fprintf(stderr, "Can't load font\n");
            exit(1);
        }
        al_destroy_path(path);



        ALLEGRO_TIMER* timer = al_create_timer(1.0 / 10);
        ALLEGRO_TIMER* frameTimer = al_create_timer(1.0 / frameFPS);
        ALLEGRO_TIMER* VREME = al_create_timer(1);
        ALLEGRO_KEYBOARD_STATE keyState;

        ALLEGRO_EVENT_QUEUE* event_queue = al_create_event_queue();
        al_register_event_source(event_queue, al_get_keyboard_event_source());
        al_register_event_source(event_queue, al_get_timer_event_source(timer));
        al_register_event_source(event_queue, al_get_timer_event_source(frameTimer));
        al_register_event_source(event_queue, al_get_timer_event_source(VREME));
        al_register_event_source(event_queue, al_get_display_event_source(display));

        al_start_timer(timer);
        al_start_timer(frameTimer);
        al_start_timer(VREME);
        srand(time(NULL));

        const int maxF = 8;
        int curF = 0;
        int frameC = 0;
        int frameD = 2;
        int frameW = 40;
        int frameH = 40;
        int timeS = 0;
        int timeF = 0;

        enum Direction { DOWN, LEFT, RIGHT, UP };

        int dir = DOWN;
        int score = 1;
        int lastX;
        int lastY;

        int coinX = 40 * (rand() % 20);
        int coinY = 40 * (rand() % 15);

        int snakeT[50];
        for (int i = 0; i <= 50; i++) {
            snakeT[i] = 0;
        }
        int snakeX[50], snakeY[50];

        bool menu = true;
        bool dead = false;
        while (!done) {



            lastX = x;
            lastY = y;


            ALLEGRO_EVENT events;
            al_wait_for_event(event_queue, &events);


            if (events.type == ALLEGRO_EVENT_KEY_UP)
            {
                switch (events.keyboard.keycode) {
                case ALLEGRO_KEY_ESCAPE:
                    done = true;
                    break;
                case ALLEGRO_KEY_ENTER:
                    if (menu) menu = false, score = 1, timeS = 0, x = 0, y = 0;
                    break;
                }
            }
            else if (events.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
                done = true;

            if (events.type == ALLEGRO_EVENT_TIMER) {
                if (events.timer.source == VREME) timeS++;
                if (events.timer.source == timer) {

                    al_get_keyboard_state(&keyState);
                    if (al_key_down(&keyState, ALLEGRO_KEY_RIGHT) && dir != LEFT)
                        dir = RIGHT;
                    else if (al_key_down(&keyState, ALLEGRO_KEY_LEFT) && dir != RIGHT)
                        dir = LEFT;
                    else if (al_key_down(&keyState, ALLEGRO_KEY_UP) && dir != DOWN)
                        dir = UP;
                    else if (al_key_down(&keyState, ALLEGRO_KEY_DOWN) && dir != UP)
                        dir = DOWN;
                    else if (al_key_down(&keyState, ALLEGRO_KEY_A))
                        score++;
                    else if (al_key_down(&keyState, ALLEGRO_KEY_ENTER) && menu == true)
                        menu = false, score = 1, timeS = 0, x = 0, y = 0;
                    if (menu == false) {
                        if (score != 0) {
                            for (int i = score; i > 0; i--) {
                                snakeX[i] = snakeX[i - 1];
                                snakeY[i] = snakeY[i - 1];
                            }
                            snakeX[0] = lastX;
                            snakeY[0] = lastY;
                        }
                    }

                    switch (dir) {
                    case RIGHT: x = x + 40;
                        break;
                    case LEFT: x = x - 40;
                        break;
                    case UP: y = y - 40;
                        break;
                    case DOWN: y = y + 40;
                        break;
                    }

                    if (x == coinX && y == coinY) {
                        score++;
                        coinX = 40 * (rand() % 20);
                        coinY = 40 * (rand() % 15);
                        snakeT[score] = 1;
                    }

                    if (menu == false) {
                        for (int i = 0; i < score; i++) {
                            if (x == snakeX[i] && y == snakeY[i] && menu == false) dead = true;
                        }
                        if (x < 0 || x >= 800 || y < 0 || y >= 600 && menu == false) dead = true;
                    }

                    draw = true;

                }
            }
            if (++frameC >= frameD) {
                if (++curF >= maxF)
                    curF = 0;

                frameC = 0;
            }

            if (dead && menu == false) {
                menu = true;
                timeF = timeS;
                x = 0, y = 0;
                for (int i = 0; i <= 50; i++) {
                    snakeT[i] = 0;
                }
                dead = false;
                dir = DOWN;
            }



            if (draw == true) {
                draw = false;
                if (menu) {
                    x = 0, y = 0;
                    for (int i = 0; i <= 50; i++) {
                        snakeT[i] = 0;
                    }
                        al_draw_text(mcfont, al_map_rgb(100, 50, 250), 170, 100, 0,
                        "Press Enter to Start");
                        al_draw_text(mcfont, al_map_rgb(100, 50, 250), 205, 200, 0,
                        "Press Esc to Exit");
                        al_draw_text(mcfont, al_map_rgb(250, 0, 250), 120, 350, 0,
                        "Coins: %i");
                        al_draw_text(mcfont, al_map_rgb(250, 0, 250), 470, 350, 0,
                        "Time: %i sec");
                }
                else {
                    //   al_draw_bitmap(coin, coinX, coinY, NULL);
                    al_draw_bitmap_region(coin1, curF * frameW, 0, frameW, frameH, coinX, coinY, 0);
                    for (int i = 0; i < score; i++) {
                        al_draw_bitmap(telo, snakeX[i], snakeY[i], NULL);
                    }

                    al_draw_bitmap(glava, x, y, NULL);
                    al_draw_textf(font1, al_map_rgb(250, 0, 250), 5, 5, 0,
                        "Coins: %i", score - 1);
                    al_draw_textf(font1, al_map_rgb(250, 0, 250), 705, 5, 0,
                        "Time: %i", timeS);
                }

                al_flip_display();
                al_clear_to_color(al_map_rgb(0, 0, 0));
            }




        }//while


        al_destroy_display(display);
        al_destroy_timer(timer);
        al_destroy_event_queue(event_queue);
        return 0;
    }


    Friday, January 17, 2020 10:36 AM

All replies

  • up

    Friday, January 17, 2020 4:03 PM
  • Hi Windica1,

    Welcome to MSDN forum.

    >> its always return – Can’t load font.

    ## I notice that the error words are from “fprintf(stderr,”Can’t load font\n”);”. And there are almost 5 of them, so the causes should be narrowed to these codes.

    So there are several possibilities, first one is, the path of the resources maybe not wright, please check it. Secondly, you could refer to this doc and make sure that the required files(like .ttf file) have been copied to the same directory as your executable.

    Please try to add different marks in “Can’t load font\n” to debug and locate where the program is blocked, like “Can’t load font 1\n” and “Can’t load font 2\n”.

    If possible, could you share us with more detailed information include necessary(simple one is enough) resources and reproduce steps, I will then test and try to solve it on my side.(you could use OneDrive and share us with the link)

    BTW, I think your issue is more related to the development of program with Allegro 5, actually it is out of our support range, Stack Overflow tagged allegro5 may be helpful, and you could get more professional help there.

    Hope all above could help you and any feedback will be expected.

    Best Regards,

    Tianyu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.




    Monday, January 20, 2020 4:19 AM