Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » trying to move font....

This thread is locked; no one can reply to it. rss feed Print
trying to move font....
PCwizard200
Member #16,127
December 2015
avatar

Hy everybody, instead of doing the typical bitmap moving experiment, I am trying to move a font phrase. I am simply doing this for training, but I am stuck and although it would be easier for me to just move a bitmap, I figure It would be more educational if I learn how to do more unique things with fonts, like move it. I even tried putting my font_x and font_y variables in the parenthasies of my al_draw_text statement. Any help would be very good and thank you in advance.

#define ALLEGRO_STATICLINK
#include <iostream>
#include <allegro5\allegro.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_ttf.h>
#include <allegro5\allegro_native_dialog.h>
#include <allegro5\allegro_primitives.h>
#include <allegro5\allegro_image.h>
using namespace std;

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

int main(void){
bool done = false;
int font_x = 0;
int font_y = 0;
bool keys[5] = {false, false, false, false};

ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_DISPLAY_MODE disp_data;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;

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

ALLEGRO_FONT *font10 = al_load_font("ARDESTINE.ttf", 24, 0);

al_get_display_mode(al_get_num_display_modes() - 1, &disp_data);
al_set_new_display_flags(ALLEGRO_FULLSCREEN);
display = al_create_display(disp_data.width, disp_data.height);
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_display_event_source(display));
while(!done){
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);

if (ev.type == ALLEGRO_EVENT_KEY_DOWN){
switch (ev.keyboard.keycode){

case ALLEGRO_KEY_UP:
keys[UP] = true;
break;

case ALLEGRO_KEY_DOWN:
keys[DOWN] = true;
break;

case ALLEGRO_KEY_LEFT:
keys[LEFT] = true;
break;

case ALLEGRO_KEY_RIGHT:
keys[RIGHT] = true;
break;

case ALLEGRO_KEY_ESCAPE:
done = true;
}
}
else if (ev.type == ALLEGRO_EVENT_KEY_UP){
switch (ev.keyboard.keycode){

case ALLEGRO_KEY_UP:
keys[UP] = false;
break;

case ALLEGRO_KEY_DOWN:
keys[DOWN] = false;
break;

case ALLEGRO_KEY_LEFT:
keys[LEFT] = true;
break;

case ALLEGRO_KEY_RIGHT:
keys[RIGHT] = false;
break;

case ALLEGRO_KEY_ESCAPE:
done = false;
}

}
else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE){
done = true;
}

if (keys[UP]){
font_y -= 4.0;
}
if (keys[DOWN]){
font_y += 4.0;
}

if (keys[LEFT]){
font_x -= 4.0;
}

if (keys[RIGHT]){
font_x += 4.0;
}

al_draw_text(font10, al_map_rgb(233,100,90),50,50,0,"Hy! Move me around please!");

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

al_rest(15);
al_flip_display();
al_destroy_display(display);
al_destroy_font(font10);
return 0;
}

Mark Oates
Member #1,146
March 2001
avatar

Awe man, you're so close! :D

Right now you have:

al_draw_text(font10, al_map_rgb(233,100,90),50,50,0,"Hy! Move me around please!");

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

So you are 1) drawing the text, then 2) completely clearing the screen, then 3) flipping the cleared screen to the display.

Fix that and I think you'll be able to solve the rest no problem. :)

PCwizard200
Member #16,127
December 2015
avatar

Thanks! Sorry for the late reply I was very busy, any way I shall now fix my code in a jiff.

Go to: