Cant load font - ALLEGRO 5

  • Thread starter Thread starter Windica1
  • Start date Start date
W

Windica1

Guest
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 = 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 = snakeX[i - 1];
snakeY = 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 && y == snakeY && 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 = 0;
}
dead = false;
dir = DOWN;
}



if (draw == true) {
draw = false;
if (menu) {
x = 0, y = 0;
for (int i = 0; i <= 50; i++) {
snakeT = 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, snakeY, 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;
}

Continue reading...
 
Back
Top