Frames
AleX-G Squadron

When i try to create a timer using a counter, whatever button i type in, it will generate an extra frame.
If i try to move the mouse, it increments too.
I find it using a variable and incrementing it.
The code for the timer is this one:

#SelectExpand
1bool quit = false; 2const FPS = 80; 3int count = 0; 4ALLEGRO_TIMER *timer = NULL; 5timer = al_create_timer(1.0 / FPS); 6al_start_timer(timer); 7 8while(!quit) 9{ 10//create events 11count++; 12//create a font and display it 13}

Neil Walker

Sorry, I have no idea what you are talking about.

How about posting all the code, or the parts of the code that show the error.

AleX-G Squadron

It is not that it doesnt work. If you try this program, when you move your mouse, the frames will increment much faster.
The question is why?

#SelectExpand
1#include <allegro5\allegro.h> 2#include <allegro5\allegro_primitives.h> 3#include <allegro5\allegro_image.h> 4#include <allegro5\allegro_ttf.h> 5#include <allegro5\allegro_font.h> 6 7enum BUTON{A,D,S,W}; 8 9const int width = 800, height = 600, FPS = 1; 10 11int main() 12{ 13 al_init(); 14 al_init_image_addon(); 15 al_init_primitives_addon(); 16 al_init_font_addon(); 17 al_init_ttf_addon(); 18 19 al_install_keyboard(); 20 al_install_mouse(); 21 22 bool quit = false; 23 bool buton[4] = {false,false,false,false}; 24 25 int x = width / 2; 26 int y = height / 2; 27 28 int count = 0; 29 30 ALLEGRO_DISPLAY *window = al_create_display(width, height); 31 ALLEGRO_BITMAP *icon = al_load_bitmap("image/1.png"); 32 ALLEGRO_FONT *font16 = al_load_ttf_font("fonts/1.ttf", 86, ALLEGRO_ALIGN_CENTRE); 33 ALLEGRO_TIMER *timer = NULL; 34 ALLEGRO_EVENT_QUEUE *event = NULL; 35 36 al_set_display_icon(window, icon); 37 al_set_window_title(window, "Game programming"); 38 39 timer = al_create_timer(1.0 / FPS); 40 event = al_create_event_queue(); 41 42 al_register_event_source(event, al_get_keyboard_event_source()); 43 al_register_event_source(event, al_get_mouse_event_source()); 44 al_register_event_source(event, al_get_display_event_source(window)); 45 al_register_event_source(event, al_get_timer_event_source(timer)); 46 47 al_flip_display(); 48 al_hide_mouse_cursor(window); 49 50 al_start_timer(timer); 51 52 while(!quit) 53 { 54 ALLEGRO_EVENT e; 55 al_wait_for_event(event, &e); 56 57 count++; 58 59 60 if(e.type == ALLEGRO_EVENT_KEY_DOWN) 61 { 62 switch(e.keyboard.keycode) 63 { 64 case ALLEGRO_KEY_A: 65 buton[A] = true; 66 break; 67 68 case ALLEGRO_KEY_S: 69 buton[S] = true; 70 break; 71 72 case ALLEGRO_KEY_D: 73 buton[D] = true; 74 break; 75 76 case ALLEGRO_KEY_W: 77 buton[W] = true; 78 break; 79 80 case ALLEGRO_KEY_ESCAPE: 81 quit = true; 82 } 83 } 84 85 else if(e.type == ALLEGRO_EVENT_KEY_UP) 86 { 87 switch(e.keyboard.keycode) 88 { 89 case ALLEGRO_KEY_A: 90 buton[A] = false; 91 break; 92 93 case ALLEGRO_KEY_S: 94 buton[S] = false; 95 break; 96 97 case ALLEGRO_KEY_D: 98 buton[D] = false; 99 break; 100 101 case ALLEGRO_KEY_W: 102 buton[W] = false; 103 break; 104 } 105 } 106 107 else if(e.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 108 { 109 quit = true; 110 } 111 112 else if(e.type == ALLEGRO_EVENT_MOUSE_AXES) 113 { 114 x = e.mouse.x; 115 y = e.mouse.y; 116 } 117 118 119 x -= buton[A] * 10; 120 x += buton[D] * 10; 121 y -= buton[W] * 10; 122 y += buton[S] * 10; 123 124 al_draw_rectangle(x-15,y-15,x+15,y+15,al_map_rgb(0,255,0), 3.0); 125 al_draw_textf(font16, al_map_rgb(0,255,0), 300, 100, ALLEGRO_ALIGN_CENTRE, "Total frames: %i", count); 126 127 al_flip_display(); 128 al_clear_to_color(al_map_rgb(0,0,0)); 129 130 } 131 132 al_destroy_bitmap(icon); 133 al_destroy_display(window); 134 135}

Arthur Kalliokoski

I take the previous edit back, you need to set up a queue.

J-Gamer

That code increments counter at each event. Mouse movements generate lots of events, so it is to be expected that counter increases faster.

CodeStepper

Use this:

#SelectExpand
1bool quit = false; 2 3redraw = true; 4 5const FPS = 80; 6int count = 0; 7ALLEGRO_TIMER *timer = NULL; 8ALLEGRO_EVENT_QUEUE *queue = al_create_event_queue( ); 9timer = al_create_timer(1.0 / FPS); 10al_start_timer(timer); 11 12while(!quit) 13{ 14//create events 15// if timer event "tick", set redraw to true 16if( event.type == ALLEGRO_EVENT_TIMER ) 17redraw = true; 18 19// On every FPS turn off redraw and wait for timer event 20if( al_event_queue_empty( queue ) && redraw ) 21count++, redraw = false; 22 23//create a font and display it 24}

This will stop events other than timer

In your code do this:

#SelectExpand
1#include <allegro5\allegro.h> 2#include <allegro5\allegro_primitives.h> 3#include <allegro5\allegro_image.h> 4#include <allegro5\allegro_ttf.h> 5#include <allegro5\allegro_font.h> 6 7enum BUTON{A,D,S,W}; 8 9const int width = 800, height = 600, FPS = 1; 10 11int main() 12{ 13 al_init(); 14 al_init_image_addon(); 15 al_init_primitives_addon(); 16 al_init_font_addon(); 17 al_init_ttf_addon(); 18 19 al_install_keyboard(); 20 al_install_mouse(); 21 22 bool quit = false; 23 bool buton[4] = {false,false,false,false}; 24 25 // Redraw 26 // This will stop other events 27 bool redraw = false; 28 29 int x = width / 2; 30 int y = height / 2; 31 32 int count = 0; 33 34 ALLEGRO_DISPLAY *window = al_create_display(width, height); 35 ALLEGRO_BITMAP *icon = al_load_bitmap("image/1.png"); 36 ALLEGRO_FONT *font16 = al_load_ttf_font("fonts/1.ttf", 86, ALLEGRO_ALIGN_CENTRE); 37 ALLEGRO_TIMER *timer = NULL; 38 ALLEGRO_EVENT_QUEUE *event = NULL; 39 40 al_set_display_icon(window, icon); 41 al_set_window_title(window, "Game programming"); 42 43 timer = al_create_timer(1.0 / FPS); 44 event = al_create_event_queue(); 45 46 al_register_event_source(event, al_get_keyboard_event_source()); 47 al_register_event_source(event, al_get_mouse_event_source()); 48 al_register_event_source(event, al_get_display_event_source(window)); 49 al_register_event_source(event, al_get_timer_event_source(timer)); 50 51 al_flip_display(); 52 al_hide_mouse_cursor(window); 53 54 al_start_timer(timer); 55 56 while(!quit) 57 { 58 ALLEGRO_EVENT e; 59 al_wait_for_event(event, &e); 60 61 // Better to do this on SWITCH 62 switch(e.type) 63 { 64 case ALLEGRO_EVENT_KEY_DOWN: 65 switch(e.keyboard.keycode) 66 { 67 case ALLEGRO_KEY_A: 68 buton[A] = true; 69 break; 70 71 case ALLEGRO_KEY_S: 72 buton[S] = true; 73 break; 74 75 case ALLEGRO_KEY_D: 76 buton[D] = true; 77 break; 78 79 case ALLEGRO_KEY_W: 80 buton[W] = true; 81 break; 82 83 case ALLEGRO_KEY_ESCAPE: 84 quit = true; 85 } 86 break; 87 88 case ALLEGRO_EVENT_KEY_UP: 89 switch(e.keyboard.keycode) 90 { 91 case ALLEGRO_KEY_A: 92 buton[A] = false; 93 break; 94 95 case ALLEGRO_KEY_S: 96 buton[S] = false; 97 break; 98 99 case ALLEGRO_KEY_D: 100 buton[D] = false; 101 break; 102 103 case ALLEGRO_KEY_W: 104 buton[W] = false; 105 break; 106 } 107 break; 108 109 case ALLEGRO_EVENT_DISPLAY_CLOSE: 110 quit = true; 111 break; 112 113 case ALLEGRO_EVENT_MOUSE_AXES: 114 x = e.mouse.x; 115 y = e.mouse.y; 116 break; 117 // On timer "tick" event, turn on redraw 118 case ALLEGRO_EVENT_TIMER: 119 redraw = true; 120 break; 121 } 122 123 // If you want wait only for timer event 124 if( redraw ) 125 count++; 126 127 128 // Waiting for timer event and empty event queue 129 if( al_is_event_queue_empty( event ) && redraw ) 130 { 131 // Turn off redraw 132 redraw = false; 133 x -= buton[A] * 10; 134 x += buton[D] * 10; 135 y -= buton[W] * 10; 136 y += buton[S] * 10; 137 138 al_draw_rectangle(x-15,y-15,x+15,y+15,al_map_rgb(0,255,0), 3.0); 139 al_draw_textf(font16, al_map_rgb(0,255,0), 300, 100, ALLEGRO_ALIGN_CENTRE, "Total frames: %i", count); 140 141 al_flip_display(); 142 al_clear_to_color(al_map_rgb(0,0,0)); 143 } 144 145 } 146 147 al_destroy_bitmap(icon); 148 al_destroy_display(window); 149 150}

Luiji99

Alternatively you could move your frame drawing code into your timer event handler.

AleX-G Squadron

Thanks for the help!
The square now runs incredibly smoother!
As i was making some practice by learning all the program by heart... I found a very nice program
Showing coordinates of the square! :)

This is it with all mods codestepper gave

#SelectExpand
1#include <allegro5\allegro.h> 2#include <allegro5\allegro_primitives.h> 3#include <allegro5\allegro_image.h> 4#include <allegro5\allegro_ttf.h> 5#include <allegro5\allegro_font.h> 6 7enum BUTON{A,D,S,W}; 8 9const int width = 800, height = 600, FPS = 80; 10 11int main() 12{ 13 al_init(); 14 al_init_image_addon(); 15 al_init_primitives_addon(); 16 al_init_font_addon(); 17 al_init_ttf_addon(); 18 19 al_install_keyboard(); 20 al_install_mouse(); 21 22 bool quit = false; 23 bool buton[4] = {false,false,false,false}; 24 25 // Redraw 26 // This will stop other events 27 bool redraw = false; 28 29 int x = width / 2; 30 int y = height / 2; 31 32 int count = 0; 33 34 ALLEGRO_DISPLAY *window = al_create_display(width, height); 35 ALLEGRO_BITMAP *icon = al_load_bitmap("image/1.png"); 36 ALLEGRO_FONT *font16 = al_load_ttf_font("fonts/1.ttf", 86, ALLEGRO_ALIGN_CENTRE); 37 ALLEGRO_TIMER *timer = NULL; 38 ALLEGRO_EVENT_QUEUE *event = NULL; 39 40 al_set_display_icon(window, icon); 41 al_set_window_title(window, "Game programming"); 42 43 timer = al_create_timer(1.0 / FPS); 44 event = al_create_event_queue(); 45 46 al_register_event_source(event, al_get_keyboard_event_source()); 47 al_register_event_source(event, al_get_mouse_event_source()); 48 al_register_event_source(event, al_get_display_event_source(window)); 49 al_register_event_source(event, al_get_timer_event_source(timer)); 50 51 al_flip_display(); 52 al_hide_mouse_cursor(window); 53 54 al_start_timer(timer); 55 56 while(!quit) 57 { 58 ALLEGRO_EVENT e; 59 al_wait_for_event(event, &e); 60 61 // Better to do this on SWITCH 62 switch(e.type) 63 { 64 case ALLEGRO_EVENT_KEY_DOWN: 65 switch(e.keyboard.keycode) 66 { 67 case ALLEGRO_KEY_A: 68 buton[A] = true; 69 break; 70 71 case ALLEGRO_KEY_S: 72 buton[S] = true; 73 break; 74 75 case ALLEGRO_KEY_D: 76 buton[D] = true; 77 break; 78 79 case ALLEGRO_KEY_W: 80 buton[W] = true; 81 break; 82 83 case ALLEGRO_KEY_ESCAPE: 84 quit = true; 85 } 86 break; 87 88 case ALLEGRO_EVENT_KEY_UP: 89 switch(e.keyboard.keycode) 90 { 91 case ALLEGRO_KEY_A: 92 buton[A] = false; 93 break; 94 95 case ALLEGRO_KEY_S: 96 buton[S] = false; 97 break; 98 99 case ALLEGRO_KEY_D: 100 buton[D] = false; 101 break; 102 103 case ALLEGRO_KEY_W: 104 buton[W] = false; 105 break; 106 } 107 break; 108 109 case ALLEGRO_EVENT_DISPLAY_CLOSE: 110 quit = true; 111 break; 112 113 case ALLEGRO_EVENT_MOUSE_AXES: 114 x = e.mouse.x; 115 y = e.mouse.y; 116 break; 117 // On timer "tick" event, turn on redraw 118 case ALLEGRO_EVENT_TIMER: 119 redraw = true; 120 break; 121 } 122 123 // If you want wait only for timer event 124 if( redraw ) 125 count++; 126 127 128 // Waiting for timer event and empty event queue 129 if( al_is_event_queue_empty( event ) && redraw ) 130 { 131 // Turn off redraw 132 redraw = false; 133 x -= buton[A] * 10; 134 x += buton[D] * 10; 135 y -= buton[W] * 10; 136 y += buton[S] * 10; 137 138 al_draw_rectangle(x-15,y-15,x+15,y+15,al_map_rgb(0,255,0), 3.0); 139 140 //Coordinates 141 al_draw_textf(font16, al_map_rgb(0,255,0), 300, 100, ALLEGRO_ALIGN_CENTRE, "Coordinates X: %i Y: %i", x, y); 142 143 al_flip_display(); 144 al_clear_to_color(al_map_rgb(0,0,0)); 145 } 146 147 } 148 149 al_destroy_bitmap(icon); 150 al_destroy_display(window); 151 152}

Thomas Fjellstrom

Personally I'd put the count++ inside the if() at the end there. Since a frame isn't actually drawn till that if is entered, it's more accurate to only count frames if that code has executed.

But then you're not doing anything with the count variable at the moment anyway, so its a moot point.

AleX-G Squadron

Since i added FPS, what is the lowest and highest FPS the eye can see?
I believe that 80 is perfect, because when i played sometimes in 60 it wasnt that nicely smooth.

Also, i learned that only with 4 lines of code the game becomes smoother

bool redraw = false;
redraw = true;
if(redraw){
redraw = false;}

And normally you need to put them in logic order because like this is a waste.

With the count variable, since it is put count++, i would be greatly amazed of not doing anything like you said, as it increments.
But normally, if you mean for the coordinates, i know it is a waste, but i had no time to delete the timer

Luiji99

I'm pretty sure the human eye's not supposed to be able to perceive more than 24 FPS or so...

taronĀ 
Luiji99 said:

I'm pretty sure the human eye's not supposed to be able to perceive more than 24 FPS or so...

I'm pretty sure that's bullshit.

Movies (and games?) have motion blur all over to look smoother.
Because I think someone is going to say, "But movies are played back at 24 fps and they look smooth!".

AleX-G Squadron

I mean a FPS about gaming, because even if human eye cannot perceive 1 FPS,
human brain can...

Luiji99

Alex-G: if the eye wasn't able to accept more than two frames per second, the brain wouldn't get more than two frames per second, thus the brain would not be able to process more than two frames per second, thus your statement is nonsensical.

taron: It would appear you're correct. I've got the chance to look it up, and (annoyingly) some sources say 10 FPS and others say hundreds. Some resources say it depends completely on the object being perceived, its motion and brightness. I.e. slow-moving fog doesn't need as many FPS as a fast-moving bullet.

Though, I found information that states Halo 3 is locked at 30 FPS. AFAIK that game looks relatively smooth. ???

AleX-G Squadron

If you see a frame(a photo) for each second, than the brain can perceive it.
(I mean you can calculate the box path if you know two movements of the box, but that said it is totally absolute, not relative)
It is just like in math, where if you find 2 points, you get the whole plane.
(You mean if you stand still the brain wont perceive you!)

I tried this program and compared 30 with 80 and it was a nice difference,
but i guess that 30 is quite normal

Luiji99

This is the flaw I see in using ALLEGRO_TIMER for logic: you can't max out FPS so you have to make sure that you use a high enough tick rate so that you don't notice anything.

gnolam

The limiting factor isn't what the eye can see (24 FPS is indeed utter bollocks) but what the user's monitor can and will render. There's no use rendering at a higher frequency than the user's refresh rate.

AleX-G Squadron

This is a better version of the coordinates version without the counter and with a nice "player" name ;)

I wanted to ask if anyone of you on this forum, has thought of creating a game together in a post, even thought it could sound silly...
So each member can add what he thinks is better like in github

#SelectExpand
1#include <allegro5\allegro.h> 2#include <allegro5\allegro_primitives.h> 3#include <allegro5\allegro_image.h> 4#include <allegro5\allegro_ttf.h> 5#include <allegro5\allegro_font.h> 6 7enum BUTON{A,S,D,W}; 8 9const int width = 800, height = 600, FPS = 60; 10 11int main() 12{ 13 al_init(); 14 al_init_primitives_addon(); 15 al_init_image_addon(); 16 al_init_font_addon(); 17 al_init_ttf_addon(); 18 19 al_install_keyboard(); 20 al_install_mouse(); 21 22 int x = width / 2; 23 int y = height / 2; 24 25 bool quit = false; 26 bool redraw = false; 27 bool buton[4] = {false,false,false,false}; 28 29 ALLEGRO_DISPLAY *window = al_create_display(width, height); 30 ALLEGRO_BITMAP *icon = al_load_bitmap("image/1.png"); 31 ALLEGRO_FONT *font16 = al_load_ttf_font("fonts/1.ttf", 80, ALLEGRO_ALIGN_CENTRE); 32 ALLEGRO_TIMER *timer = NULL; 33 ALLEGRO_EVENT_QUEUE *event = NULL; 34 35 36 timer = al_create_timer(1.0 / FPS); 37 event = al_create_event_queue(); 38 39 al_register_event_source(event, al_get_keyboard_event_source()); 40 al_register_event_source(event, al_get_mouse_event_source()); 41 al_register_event_source(event, al_get_display_event_source(window)); 42 al_register_event_source(event, al_get_timer_event_source(timer)); 43 44 al_set_display_icon(window, icon); 45 al_set_window_title(window, "Game programming"); 46 47 al_flip_display(); 48 49 al_hide_mouse_cursor(window); 50 51 al_start_timer(timer); 52 53 while(!quit) 54 { 55 ALLEGRO_EVENT e; 56 al_wait_for_event(event, &e); 57 58 switch(e.type) 59 { 60 case ALLEGRO_EVENT_KEY_DOWN: 61 switch(e.keyboard.keycode) 62 { 63 case ALLEGRO_KEY_A: 64 buton[A] = true; 65 break; 66 67 case ALLEGRO_KEY_S: 68 buton[S] = true; 69 break; 70 71 case ALLEGRO_KEY_D: 72 buton[D] = true; 73 break; 74 75 case ALLEGRO_KEY_W: 76 buton[W] = true; 77 break; 78 79 case ALLEGRO_KEY_ESCAPE: 80 quit = true; 81 } 82 } 83 84 switch(e.type) 85 { 86 case ALLEGRO_EVENT_KEY_UP: 87 switch(e.keyboard.keycode) 88 { 89 case ALLEGRO_KEY_A: 90 buton[A] = false; 91 break; 92 93 case ALLEGRO_KEY_D: 94 buton[D] = false; 95 break; 96 97 case ALLEGRO_KEY_S: 98 buton[S] = false; 99 break; 100 101 case ALLEGRO_KEY_W: 102 buton[W] = false; 103 break; 104 } 105 } 106 107 if(e.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 108 { 109 quit = true; 110 } 111 112 switch(e.type) 113 { 114 case ALLEGRO_EVENT_MOUSE_AXES: 115 x = e.mouse.x; 116 y = e.mouse.y; 117 } 118 119 if(e.type == ALLEGRO_EVENT_TIMER) 120 { 121 redraw = true; 122 x -= buton[A] * 10; 123 x += buton[D] * 10; 124 y -= buton[W] * 10; 125 y += buton[S] * 10; 126 127 } 128 129 if(redraw && al_event_queue_is_empty(event)) 130 { 131 redraw = false; 132 133 al_draw_rectangle(x-15,y-15,x+15,y+15,al_map_rgb(0,255,0),3.0); 134 135 al_draw_textf(font16, al_map_rgb(255,0,0), 300, 100, ALLEGRO_ALIGN_CENTRE, "Coordinates X: %i Y: %i", x, y); 136 al_draw_textf(font16, al_map_rgb(0,0,255), x,y, ALLEGRO_ALIGN_CENTRE, "Player"); 137 138 al_flip_display(); 139 al_clear_to_color(al_map_rgb(0,0,0)); 140 } 141 142 } 143 al_destroy_bitmap(icon); 144 al_destroy_display(window); 145}

Thomas Fjellstrom
gnolam said:

The limiting factor isn't what the eye can see (24 FPS is indeed utter bollocks) but what the user's monitor can and will render. There's no use rendering at a higher frequency than the user's refresh rate.

^^^ That.

Fishcake

I wanted to ask if anyone of you on this forum, has thought of creating a game together in a post, even thought it could sound silly...
So each member can add what he thinks is better like in github

I think there has been several attempts to do such project here in Allegro. I found this one after searching for "project Monday". And I'm pretty sure I came across a project where everyone takes turn contributing exactly one line of code (or several), though I couldn't find it. I wonder what happened to that project.. :-/

AleX-G Squadron

Must see video... i accidentally found it!
http://www.youtube.com/watch?v=SoHeWgLvlXI&feature=related

Luiji99

gnolam: yeah, that sounds about right. I think I threw some factoids into a vat and smelted bullshit.

Thread #610937. Printed from Allegro.cc