Drawing moving text
miso3367

hi, i'm trying to draw a moving text using al_draw_textf, from y = 0 to y = HEIGHT , i've got y += velY; but al_draw_textf is not moving but drawing it on every single position. Could you help me?

Vanneto

Please provide a snippet of code so that we can help you better.

Arthur Kalliokoski

Just guessing here, but Y is an int and velY is < 0.5?

miso3367
#SelectExpand
1#include "NewLevelAnimation.h" 2 3NewLevelAnimation::NewLevelAnimation(ALLEGRO_FONT *font, int levelNumber) 4{ 5 Objects::Init(WIDTH/2, -20, 0, 8, 0, 1, 0, 0); 6 // Init(x, y, velX, velY, dirX, dirY, boundX, boundY); 7 8 NewLevelAnimation::font = font; 9 NewLevelAnimation::levelNumber = levelNumber; 10 11 12 SetCollision(false); 13 SetAlive(true); 14} 15 16void NewLevelAnimation::Update() 17{ 18 Objects::Update(); 19 /* Update() 20 { 21 x += velX * dirX; 22 Y += velY * dirY; 23 } 24 */ 25 26 if (y >= HEIGHT) 27 SetAlive(false); 28 29} 30void NewLevelAnimation::Render() 31{ 32 al_draw_textf(font, al_map_rgb(255,255,255), x, y, ALLEGRO_ALIGN_CENTRE, "LEVEL %i", levelNumber); 33 34}

The logic is that everytime I start a new level there should move a text "LEVEL 1" etc. But it looks like the image i added.

img src="http://s23.postimg.org/nhfr4j9kb/error.jpg"/>

J-Gamer

You need to clear the screen every frame. al_clear_to_color ;)

miso3367

I think that's not a problem. I think that al_draw_textf is static or it draw on every position.

Edgar Reynaldo
miso3367 said:

I think that's not a problem. I think that al_draw_textf is static or it draw on every position.

If you draw the text more than once without clearing the screen, those are the results you will get. What do you expect if you don't erase the old text output?

miso3367

Are we talking abound al_clear_to_color in main.cpp in my render zone? because i have it there.

Edgar Reynaldo

How would we know that? You haven't posted enough code.

taronĀ 

What type is x? What type is y?
What does Objects::Update() do?

I don't see you increasing y at all.
Also it does look like you are drawing the text without first clearing the screen.

Specter Phoenix

Well I coded a quick one that just has the words Level # scroll the screen. Maybe my code can help you understand where your code is messing up at.

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_image.h> 3#include <allegro5/allegro_font.h> 4#include <allegro5/allegro_ttf.h> 5#include <allegro5/allegro_primitives.h> 6 7#include <iostream> 8 9using namespace std; 10 11const int SCREEN_W = 640; 12const int SCREEN_H = 480; 13const float FPS = 60.0; 14const int yVel = 2; 15 16int main() 17{ 18 int fps = 0, fps_accum = 0; 19 double fps_time = 0; 20 int lvlX = SCREEN_W / 2, lvlY = 0, lvlNum = 1; 21 22 if(!al_init()) 23 { 24 cout << "Failed to init allegro\n"; 25 } 26 if(!al_install_keyboard()) 27 { 28 cout << "Failed to install keyboad\n"; 29 } 30 31 if(!al_init_primitives_addon()) 32 { 33 cout << "Failed to init primitives addon\n"; 34 } 35 36 if(!al_init_image_addon()) 37 { 38 cout << "Failed to init image addon\n"; 39 } 40 41 al_init_font_addon(); 42 al_init_ttf_addon(); 43 44 ALLEGRO_DISPLAY *display; 45 ALLEGRO_EVENT_QUEUE *event_queue; 46 ALLEGRO_TIMER *timer; 47 ALLEGRO_FONT *fnt, *lvltxt; 48 49 fnt = al_load_ttf_font("pirulen.ttf", 12, 0); 50 lvltxt = fnt; 51 52 bool doexit = false; 53 bool redraw = false; 54 55 timer = al_create_timer(1.0/FPS); 56 display = al_create_display(SCREEN_W, SCREEN_H); 57 event_queue = al_create_event_queue(); 58 //al_set_display_flag(display, ALLEGRO_FULLSCREEN_WINDOW, 1); 59 al_set_window_title(display, "Scrolling Text"); 60 al_set_window_position(display, 20, 20); 61 62 al_register_event_source(event_queue, al_get_display_event_source(display)); 63 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 64 al_register_event_source(event_queue, al_get_keyboard_event_source()); 65 66 al_clear_to_color(al_map_rgb(0, 0, 0)); 67 al_flip_display(); 68 al_start_timer(timer); 69 70 while(!doexit) 71 { 72 ALLEGRO_EVENT ev; 73 al_wait_for_event(event_queue, &ev); 74 75 if(ev.type == ALLEGRO_EVENT_TIMER) 76 { 77 // logic 78 if(lvlY < 0 || lvlY > SCREEN_H) 79 { 80 lvlY = 0; 81 lvlNum++; 82 } 83 else lvlY += yVel; 84 85 86 87 redraw = true; 88 } 89 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 90 { 91 break; 92 } 93 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) 94 { 95 switch(ev.keyboard.keycode) 96 { 97 break; 98 } 99 } 100 else if(ev.type == ALLEGRO_EVENT_KEY_UP) 101 { 102 switch(ev.keyboard.keycode) 103 { 104 case ALLEGRO_KEY_ESCAPE: 105 doexit = true; 106 break; 107 } 108 } 109 110 if(redraw && al_is_event_queue_empty(event_queue)) 111 { 112 // draw code here 113 redraw = false; 114 al_clear_to_color(al_map_rgb(0, 0, 0)); 115 double t = al_get_time(); 116 117 al_draw_filled_rounded_rectangle(4, 4, 100, 22, 8, 8, al_map_rgb(255, 0, 255)); 118 al_draw_textf(fnt, al_map_rgb(255, 255, 255), 8, 8, 0, "FPS: %d", fps); 119 al_draw_textf(lvltxt, al_map_rgb(255, 255, 255), lvlX, lvlY, 0, "Level %d", lvlNum); 120 al_flip_display(); 121 122 fps_accum++; 123 if(t - fps_time >= 1) 124 { 125 fps = fps_accum; 126 fps_accum = 0; 127 fps_time = t; 128 } 129 } 130 } 131 132 return 0; 133 134}

Thread #612851. Printed from Allegro.cc