Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Drawing moving text

This thread is locked; no one can reply to it. rss feed Print
Drawing moving text
miso3367
Member #15,168
June 2013

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
Member #8,643
May 2007

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

In capitalist America bank robs you.

Arthur Kalliokoski
Second in Command
February 2005
avatar

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

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

miso3367
Member #15,168
June 2013

#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
Member #12,491
January 2011
avatar

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

" There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo
"If your body was a business, thought would be like micro-management and emotions would be like macro-management. If you primarily live your life with emotions, then you are prone to error on the details. If you over-think things all the time you tend to lose scope of priorities." - Mark Oates

miso3367
Member #15,168
June 2013

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

Edgar Reynaldo
Member #8,592
May 2007
avatar

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
Member #15,168
June 2013

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

Edgar Reynaldo
Member #8,592
May 2007
avatar

taron 
Member #10,584
January 2009
avatar

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
Member #1,425
July 2001
avatar

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}

"Can't a man even talk to himself without being interrupted?" -Krull(1983)
"Through vengence I was born. Through war I was trained. Through love I was found. Through death I was released. Through release I was given a purpose." -- Specter Phoenix
"Programming == AWESOME the rest is just tools to accomplish it."
END OF LINE

Go to: