Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] Ghost trail when moving primitives

This thread is locked; no one can reply to it. rss feed Print
[A5] Ghost trail when moving primitives
walnut100
Member #5,592
March 2005

Hi,

I am new to Allegro and my game programming skills are spotty in general. I'm trying to migrate my game from XNA to Allegro and so I set off to write a sample "game" in Allegro to get a framework in place.

My problem is whenever I move my test object (A blue triangle) around, it leaves behind a trail the exact length of the movement speed. I thought it was just my computer possibly since I'm on an integrated graphics rig right now but my friends have confirmed it happens on their computers.

I have the sample uploaded to see what I mean here: (Press Enter to start it otherwise you'll only see a green screen)
https://rapidshare.com/files/1255978564/PwnSquadPCTest.zip

And of course my source code

#SelectExpand
1#include <allegro5\allegro.h> 2#include <allegro5\allegro_primitives.h> 3#include <allegro5\allegro_font.h> 4#include <allegro5\allegro_ttf.h> 5#include <allegro5\allegro_native_dialog.h> 6 7// Load pointers 8ALLEGRO_DISPLAY *display = NULL; 9ALLEGRO_EVENT_QUEUE *event_queue = NULL; 10ALLEGRO_TIMER *timer = NULL; 11ALLEGRO_FONT *nicocyan36 = NULL; 12ALLEGRO_FONT *lucy24 = NULL; 13 14const int screenWidth = 1280; 15const int screenHeight = 720; 16enum KEYS {UP, DOWN, LEFT, RIGHT}; 17 18 // Parameters :D 19 // These are variable depending on the actual current set resolution 20 int screen_w; 21 int screen_h; 22 // Game loop 23 //bool redraw = false; 24 bool isDone = false; 25 bool showVashIsGay = false; 26 27 // Default positions for the triangle 28 double pos_x = 300; 29 double pos_y = 475; 30 31 // Timers LOL 32 double lastTime = 0.0; 33 double currentTime = 0.0; 34 double frameTime = 0.0; 35 double cumulativeTime = 0.0; 36 37 // Movement :D 38 bool keys[4] = {false, false, false, false}; 39 40void Update(const double targetFPS) 41{ 42 lastTime = currentTime; 43 currentTime = al_get_time(); 44 frameTime = (currentTime - lastTime); 45 46 // Recheck the targetFPS might not be a good idea 47 pos_y -= keys[UP] * 10 * targetFPS * frameTime; 48 pos_y += keys[DOWN] * 10 * targetFPS * frameTime; 49 pos_x -= keys[LEFT] * 10 * targetFPS * frameTime; 50 pos_x += keys[RIGHT] * 10 * targetFPS * frameTime; 51} 52 53void Draw() 54{ 55 al_clear_to_color(al_map_rgb(0,100,0)); 56 57 if (al_is_event_queue_empty(event_queue)) 58 { 59 if (showVashIsGay == true) 60 { 61 screen_w = al_get_display_width(display); 62 screen_h = al_get_display_height(display); 63 64 al_draw_filled_triangle(pos_x, pos_y, pos_x + 100, pos_y - 200, pos_x + 200, pos_y, al_map_rgb(0, 0, 255)); 65 al_draw_text(nicocyan36, al_map_rgb(255, 127, 127), screen_w / 2, screen_h / 2, ALLEGRO_ALIGN_CENTRE, "VASH IS GAY"); 66 al_draw_text(lucy24, al_map_rgb(255, 255, 255), screen_w / 2, (screen_h / 2 + 50), ALLEGRO_ALIGN_CENTRE, "Love, the Pwn Squad"); 67 } 68 69 // Flip the display and clear it 70 al_flip_display(); 71 //redraw = false; 72 } 73} 74 75int main(int argc, char **argv) 76{ 77 // Initialize Allegro 78 if(!al_init()) 79 { 80 al_show_native_message_box(display, "ERROR", "CRITICAL ERROR", "Failed to initialize allegro!", NULL, ALLEGRO_MESSAGEBOX_ERROR); 81 return -1; 82 } 83 84 // Create the display 85 display = al_create_display(screenWidth, screenHeight); 86 al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_SUGGEST); 87 const int targetFPS = 60; 88 if(!display) 89 { 90 al_show_native_message_box(display, "ERROR", "CRITICAL ERROR", "Failed to initialize display!", NULL, ALLEGRO_MESSAGEBOX_ERROR); 91 return -1; 92 } 93 94 // Create a timer 95 timer = al_create_timer(1.0 / (targetFPS * 2)); 96 if(!timer) 97 { 98 al_show_native_message_box(display, "ERROR", "CRITICAL ERROR", "Failed to initialize game timer!", NULL, ALLEGRO_MESSAGEBOX_ERROR); 99 return -1; 100 } 101 102 // Initialize primitives (Shapes lol) 103 al_init_primitives_addon(); 104 105 // Initialize the keyboard 106 al_install_keyboard(); 107 108 // Initialize the mouse 109 al_install_mouse(); 110 111 // Initialize font functionality and .ttf support 112 al_init_font_addon(); 113 al_init_ttf_addon(); 114 115 // Define the fonts 116 nicocyan36 = al_load_font("Resources//Fonts//Nicocyan.ttf", 36, 0); 117 if(!nicocyan36) 118 { 119 al_show_native_message_box(display, "ERROR", "CRITICAL ERROR", "Failed to load Nicocyan.ttf!\nCheck if the file is missing", NULL, ALLEGRO_MESSAGEBOX_ERROR); 120 return -1; 121 } 122 123 lucy24 = al_load_font("Resources//Fonts//Lucy.ttf", 24, 0); 124 if (!lucy24) 125 { 126 al_show_native_message_box(display, "ERROR", "CRITICAL ERROR", "Failed to load Lucy.ttf!\nCheck if the file is missing", NULL, ALLEGRO_MESSAGEBOX_ERROR); 127 return -1; 128 } 129 130 // Setup the event queue 131 event_queue = al_create_event_queue(); 132 al_register_event_source(event_queue, al_get_display_event_source(display)); 133 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 134 al_register_event_source(event_queue, al_get_keyboard_event_source()); 135 al_register_event_source(event_queue, al_get_mouse_event_source()); 136 137 // Hide the mouse 138 al_hide_mouse_cursor(display); 139 140 // Start the timer! 141 al_start_timer(timer); 142 143 while(!isDone) 144 { 145 ALLEGRO_EVENT ev; 146 al_wait_for_event(event_queue, &ev); 147 148 Update(targetFPS); 149 150 if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 151 { 152 isDone = true; 153 } 154 // If a key is pressed... 155 if(ev.type == ALLEGRO_EVENT_KEY_DOWN) 156 { 157 // Decide what to do based on which key 158 switch(ev.keyboard.keycode) 159 { 160 case ALLEGRO_KEY_UP: 161 keys[UP] = true; 162 break; 163 case ALLEGRO_KEY_DOWN: 164 keys[DOWN] = true; 165 break; 166 case ALLEGRO_KEY_LEFT: 167 keys[LEFT] = true; 168 break; 169 case ALLEGRO_KEY_RIGHT: 170 keys[RIGHT] = true; 171 break; 172 173 case ALLEGRO_KEY_ENTER: 174 showVashIsGay? showVashIsGay=false : showVashIsGay=true; 175 break; 176 } 177 } 178 else if(ev.type == ALLEGRO_EVENT_KEY_UP) 179 { 180 switch(ev.keyboard.keycode) 181 { 182 case ALLEGRO_KEY_UP: 183 keys[UP] = false; 184 break; 185 case ALLEGRO_KEY_DOWN: 186 keys[DOWN] = false; 187 break; 188 case ALLEGRO_KEY_LEFT: 189 keys[LEFT] = false; 190 break; 191 case ALLEGRO_KEY_RIGHT: 192 keys[RIGHT] = false; 193 break; 194 195 case ALLEGRO_KEY_ESCAPE: 196 isDone = true; 197 break; 198 } 199 } 200 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) 201 { 202 if(ev.mouse.button & 1) 203 showVashIsGay? showVashIsGay=false : showVashIsGay=true; 204 else if (ev.mouse.button & 2) 205 isDone = true; 206 } 207 else if(ev.type == ALLEGRO_EVENT_TIMER) 208 { 209 Draw(); 210 } 211 } 212 213 // Cleanup, in reverse order from initialization 214 al_destroy_font(lucy24); 215 al_destroy_font(nicocyan36); 216 al_destroy_timer(timer); 217 al_destroy_event_queue(event_queue); 218 al_destroy_display(display); 219 220 return 0; 221}

If anybody can help me figure out how to fix this I would be extremely grateful

Luiji99
Member #12,254
September 2010

I don't understand why you're clearing the display every Draw() call, but only flipping the display when the event queue is empty. Shouldn't al_clear_to_color be within the al_is_event_queue_empty statement? Also, you should probably move that check outside of the function call so you can avoid the overhead of a function call when it is not needed.

I don't think it's the source of your problem, but it'd probably be good to adjust it anyway.

Also, who is VASH and why do you proclaim his sexuality in your program?

Programming should be fun. That's why I hate Java.

torhu
Member #2,727
September 2002
avatar

I built your game, and get the same result. MSVC 9 on Windows 7. Tried both Allegro 5.0.7 and 5.0.5, Direct3D and OpenGL. Tried drawing a rectangle instead of a triangle, still the same problem. Tried a couple of other things, didn't help. :-/

Dennis
Member #1,090
July 2003
avatar

Works without problems here: MSVC2010 Windows 7 64bit, Allegro 5.1.x (from GIT).

(I only changed the font to the builtin font because I didn't have a TTF font ready for testing.)

Luiji99
Member #12,254
September 2010

Uh...modified the forward slashes in the #includes and switched "showVashIsGay? showVashIsGay=false : showVashIsGay=true" calls to "showVashIsGay = !showVashIsGay" so that it would compile on my Arch Linux machine with the GNU Compiler Collection...it works perfectly. No trails or anything. ???

Programming should be fun. That's why I hate Java.

walnut100
Member #5,592
March 2005

Luiji99 said:

I don't understand why you're clearing the display every Draw() call, but only flipping the display when the event queue is empty.

It's a mistake, I was moving stuff around trying to see if I could diagnose the blur before I posted this thread

Quote:

Also, who is VASH and why do you proclaim his sexuality in your program?

It's an inside joke with some of my friends, I honestly forgot I had that in there because I tend to ignore it

Dennis said:

Works without problems here: MSVC2010 Windows 7 64bit, Allegro 5.1.x (from GIT).

I'm hoping this will fix my issue since my setup is fairly similar (Although I was using 5.0.7) I'll give it a go later today and update with results

Thanks guys!

Go to: