i working on little game im making
now i've made map via tiled map editor , put on screen via al_draw_bitmap_region loop. map shown on screen, yet when try scroll view, takes ages until goes forward. caused "large" map (100x100) -> in allegro 4 used method once, , worked fine me (i used blit in doublebuffer , blit on screen)
yet seem not find solution problem in allegro 5 far i'm sorry if board has been open, didnt found solution problem far.
i'd thankfull help!
code:
main.cpp:
#include<allegro5\allegro.h> #include<allegro5\allegro_native_dialog.h> #include<allegro5\allegro_primitives.h> #include<allegro5\allegro_image.h> #include<allegro5\allegro_font.h> #include<allegro5\allegro_ttf.h> #include "global.h" #include "map.h" using namespace std; int main() { allegro_display *display = null; al_init_primitives_addon(); al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); al_init(); allegro_font *font18 = al_load_font("arial.ttf", 18, 0); if (!al_init()) { al_show_native_message_box(null,null,null, "could not initialize allegro 5", null, null); } bool keys[] = {false, false, false, false}; enum keys{up,down,left,right}; allegro_timer *timer = null; bool done=false; bool redraw=true; int xoff =0; int yoff =0; int count = 0; int fps = 5; int x=10, y=10; int movespeed = 5; display = al_create_display(screenwidth,screenheight); if(!display) al_show_native_message_box(null,null,null, "could not create allegro window", null,null); al_set_window_title(display,"outfall-rpg"); al_install_keyboard(); allegro_event_queue *event_queue = null; event_queue = al_create_event_queue(); timer = al_create_timer(1.0 / fps); // 1.0 = sekunden; / fps = frames per second al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_start_timer(timer); while(!done) { allegro_event ev; al_wait_for_event(event_queue, &ev); map map; map.init(); map.loadmap("map1.txt"); if(ev.type == allegro_event_key_down) { switch(ev.keyboard.keycode) { case allegro_key_escape: done=true; break; case allegro_key_left: keys[left] = true; break; case allegro_key_right: keys[right] = true; break; case allegro_key_up: keys[up] = true; break; case allegro_key_down: keys[down] = true; break; } } else if(ev.type == allegro_event_key_up) { switch(ev.keyboard.keycode) { case allegro_key_escape: done=true; break; case allegro_key_left: keys[left] = false; break; case allegro_key_right: keys[right] = false; break; case allegro_key_up: keys[up] = false; break; case allegro_key_down: keys[down] = false; break; } } else if(ev.type == allegro_event_display_close) { break; } else if(ev.type == allegro_event_timer) { xoff -= keys[right] * 3; xoff += keys[left] * 3; yoff -= keys[down] * 3; yoff += keys[up] * 3; redraw=true; } if(redraw && al_is_event_queue_empty(event_queue)) { map.draw(xoff, yoff); //al_set_target_bitmap(al_get_backbuffer(display)); al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); redraw=false; } } al_destroy_event_queue(event_queue); al_destroy_timer(timer); al_destroy_display(display); return 0; }
map.h
#include <fstream> #include <string> #include <algorithm> #include <iostream> #include "global.h" #include<allegro5\allegro.h> #include<allegro5\allegro_native_dialog.h> #include<allegro5\allegro_primitives.h> using namespace std; class map { public: map(); ~map(); void init(); void update(); allegro_bitmap *buffer; void loadmap(const char *filename); void draw(int &xoff, int &yoff); private: int loadcounterx, loadcountery; int mapsizex, mapsizey; int mapfile[200][200]; int blocksize; allegro_bitmap *bgsheet; int mapcolumns; int mapsize; int y; int sheetsize; };
map.cpp:
#include "map.h" map::map() { mapsizex = 101; mapsizey = 101; y=0; sheetsize = 13; } map::~map() { } void map::init() { loadcounterx = loadcountery = 0; map::loadmap("maptest.txt"); blocksize = 32; int mapfile[100][100]; mapcolumns = 100; mapsize = 1000; } void map::update() { } void map::draw(int &x, int &q) { int xoff = x; int yoff = q; bgsheet = al_load_bitmap("bilder/testas1.png"); buffer = al_create_bitmap(800,600); //al_set_target_bitmap(buffer); (int i=0; i<mapsizex; i++) { (int j=0; j<mapsizey; j++) { y=0; while(mapfile[i][j] > sheetsize) { mapfile[i][j] = mapfile[i][j] - sheetsize; y++; } // al_draw_bitmap_region(bgsheet, blocksize*(mapfile[i][j]-1),blocksize*y, blocksize, blocksize, xoff + blocksize * (i%mapcolumns), yoff + blocksize * (j%mapcolumns),0); //al_set_target_bitmap(buffer); al_draw_bitmap_region(bgsheet, blocksize*(mapfile[i][j]-1),blocksize*y, blocksize, blocksize, xoff + (blocksize *i), yoff + (blocksize * j),0); //al_set_target_backbuffer(display); // generell gucken, wie ich schneller bitmaps abbilden kann! //allegro_bitmap *al_get_backbuffer(allegro_display *display) // al_set_target_bitmap(display); // nur abgebildeter bereich + puffer blitten (20x20?) } } } void map::loadmap(const char *filename) { ifstream openfile(filename); if (openfile.is_open()) { string line; getline(openfile, line); line.erase(remove(line.begin(),line.end(),' '), line.end()); mapsizex = 100; openfile.seekg(0,ios::beg); //openfile >> mapsizex >> mapsizey; while(!openfile.eof()) { openfile >> mapfile[loadcounterx][loadcountery]; loadcounterx ++; if (loadcounterx >= mapsizex) { loadcounterx = 0; loadcountery++; } } mapsizey = loadcountery; } //file opend else { //al_show_native_message_box(null,null,null, "could not load map", null,null); } }
the tiles im using:
these lines of map::draw
suspicious:
bgsheet = al_load_bitmap("bilder/testas1.png"); buffer = al_create_bitmap(800,600);
map::draw
re-loads bitmap every time call it. not put needless load on draw loop, leaking loaded bitmap every time.
try loading bgsheet
once in map::init
, using same bitmap every draw loop.
you allocate , subsequently leak new buffer
every draw call. appears aren't using buffer, remove al_create_bitmap
call entirely. if intent double-buffer, isn't needed in allegro5.
also, looks map::init
called in game loop, means repeatedly loading map , spending lot of time waiting on i/o. call map::init
once before game loop begins.
Comments
Post a Comment