Izvorni tehnički članak,
Piše: Amar Tufo
Opis zadatka:
Allegro_Timer?
Dok igrate neke zahtjevne video igre, podrazumjeva se da vam je HD grafička karta i da je vaš računar barem 2 do 4 GB RAM-a jak. To znači da ćete za ove performanse dobiti podjednaku brzinu gameplay-a vaše igre i da ćete biti u stanju primjetiti velike razlike u FPS-u koji vaša igrica koristi. No, kada dođe do 2D video igara, jako je neophodno da takve igrice imaju svoj FPS jer će se večinom pokretati na slabijim računalima, to se i podrazumjeva. U programiranju video igre koristeči se Allegro bibliotekom unutar koje postoji funkcija Allegro_Timer koja developeru omogučava da vidi brzinu svoje igrice koju izrađuje i igra, a u isto vrijeme mu omogučava da igračima svoje igre dostavi brzinu od recimo 60 FPS-ova u sekundi bez obzira na slabe performanse test računala.
Ovaj lab je dio moje Allegro2D game dev serije pa ja stoga neću govoriti u detalje kako i na koji način kreirati cio projekat, jer podrazumjevam da ste ranije radili u C++-u ili u Visual C++ 2008 razvojnom okruženju i da ste već dosada napravili neke svoje aplikacije makar to bio i HelloWorld ili desktop aplikacija koja pritisko na taster pozdravlja korisnika nekom porukom. Ono što ću iznjeti u daljem djelu ovog članka jeste while petlja kao glavni dio ovog laba. Vidjet ćemo dvije while petlje i ukazati na njihove razlike kako bismo rješili naš problem sa pomjeranjem sprajta na zaslonu. Primjer veoma jednostavne while petlje je dat dolje ispod ovog teksta.
//main loop while(!exit) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: exit = true; break; } }
//Allegro_Bitmap_App //Developer: Amar Tufo //8. October, 2015 //This sample demonstrates the use of bitmaps in Allegro //and way of manipulating the bitmaps in actual programs #include <allegro5\allegro.h> #include <allegro5\allegro_primitives.h> #include <allegro5\allegro_image.h> int main() { //variables int width = 640; int height = 480; bool exit = false; int imageWidth = 0; int imageHeight = 0; int x = 0; int y = 0; //allegro variable ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_BITMAP *image = NULL; //program init if(!al_init()) //initialize Allegro return -1; display = al_create_display(width, height); //create our display object if(!display) //test display object return -1; //addon init al_install_keyboard(); al_init_image_addon(); image = al_load_bitmap("slika.png"); //local folder imageWidth = al_get_bitmap_width(image); imageHeight = al_get_bitmap_height(image); x = width / 2 - imageWidth / 2; y = height / 2 - imageHeight / 2; //installing event sources event_queue = al_create_event_queue(); al_register_event_source(event_queue, al_get_keyboard_event_source()); //main loop starts here while(!exit) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { //allow user to exit the program case ALLEGRO_KEY_ESCAPE: done = true; break; case ALLEGRO_KEY_UP: y -= 10; break; case ALLEGRO_KEY_DOWN: y += 10; break; case ALLEGRO_KEY_LEFT: x -= 10; break; case ALLEGRO_KEY_RIGHT: x += 10; break; } } //main loop ends here al_draw_bitmap(image, width / 2 - imageWidth / 2, height / 2 - imageHeight / 2, 0); al_draw_tinted_bitmap(image, al_map_rgba(255, 0, 0, .2), x, y, 0); al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); } al_destroy_bitmap(image); al_destroy_event_queue(event_queue); al_destroy_display(display); //destroy our display object return 0; }
//while loop start's here while(!exit) { //creating allegro event ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); //if keyboard key is pressed then 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_RIGHT: keys[RIGHT] = true; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = true; break; } } //keep track of my keys 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_RIGHT: keys[RIGHT] = false; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = false; break; //if user press escape key case ALLEGRO_KEY_ESCAPE: exit = true; //than exit break; //if user press space key case ALLEGRO_KEY_SPACE: exit = true;//than exit break; } } //closing window by pressing red X else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { //while loop end's here exit = true; } //using allegro_timer event handler else if(ev.type == ALLEGRO_EVENT_TIMER) { //move my square every 10 pixels y -= keys[UP] * 10; y += keys[DOWN] * 10; x -= keys[LEFT] * 10; x += keys[RIGHT] * 10; }
//Allegro_Bitmap_App //Developer: Amar Tufo //12. October, 2015 //This sample demonstrates the use of bitmaps in Allegro //and way of manipulating the bitmaps in actual programs //by using FPS #include <allegro5\allegro.h> #include <allegro5\allegro_primitives.h> #include <allegro5\allegro_image.h> enum KEYS{UP, DOWN, LEFT, RIGHT}; int main() { int width = 700; int height = 650; bool exit = false; bool keys[4] = {false, false, false, false}; int imageWidth = 0; int imageHeight = 0; int x = 0; int y = 0; //int pos_x = width / 2; //int pos_y = height / 2; //creating speed regulation int FPS = 60; //float scale = 2; //float degree = 0; //Creating allegro variables ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_BITMAP *image = NULL; //Creating timer event handler ALLEGRO_TIMER *timer = NULL; //allegro init check if(!al_init()) return -1; display = al_create_display(width, height); //check display initialisation if(!display) return -0; //addon init al_install_keyboard(); al_init_primitives_addon(); al_init_image_addon(); //register timer event timer = al_create_timer(1.0 / FPS); //load image into memory image = al_load_bitmap("slika.png"); //determine width & height of image imageWidth = al_get_bitmap_width(image); imageHeight = al_get_bitmap_height(image); //initialising variables x = width / 2 - imageWidth / 2; y = height / 2 - imageHeight / 2; //registering event sources 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_timer_event_source(timer)); al_register_event_source(event_queue, al_get_display_event_source(display)); //start timer al_start_timer(timer); //while loop start's here while(!exit) { //creating allegro event ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); //if keyboard key is pressed then 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_RIGHT: keys[RIGHT] = true; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = true; break; } } //keep track of my keys 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_RIGHT: keys[RIGHT] = false; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = false; break; //if user press escape key case ALLEGRO_KEY_ESCAPE: exit = true; //than exit break; //if user press space key case ALLEGRO_KEY_SPACE: exit = true;//than exit break; } } //closing window by pressing red X else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { //while loop end's here exit = true; } //using allegro_timer event handler else if(ev.type == ALLEGRO_EVENT_TIMER) { //move my square every 10 pixels y -= keys[UP] * 10; y += keys[DOWN] * 10; x -= keys[LEFT] * 10; x += keys[RIGHT] * 10; } //draw on the screen if there are no other event to process al_draw_bitmap(image, width / 2 - imageWidth / 2, height / 2 - imageHeight / 2, 0); //drawing tinted image on screen al_draw_tinted_bitmap(image, al_map_rgba(125,100,100, .5), x, y, 0); //draw scaled image on the screen //al_draw_scaled_bitmap(image, 0, 0, imageWidth, imageHeight, x, y, imageWidth * scale, imageHeight * scale, 0); //draw rotated image to the center of the screen //al_draw_rotated_bitmap(image, imageWidth / 2, imageHeight / 2, width / 2, height / 2, degree * 3.14159 / 180, 0); //draw image on center of the screen //al_draw_bitmap(image, width / 2 - imageWidth / 2, height / 2 - imageHeight / 2, 0); al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); } al_destroy_bitmap(image); al_destroy_event_queue(event_queue); al_destroy_display(display); return 0; }