Sign in to follow this  
XDaWNeDX

Tile based game's killing FPS?

Recommended Posts

XDaWNeDX    113
So, I copied a bunch of code from the allegro's wiki. For version 5.0. I then added in a little bit of my own, to make it load tiles. Which works, it loads tiles now. The problem is, I have a 15x20 array, and it is SLOW to run this program...

The code is as follows:

main.cpp:
[code]
[font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]#include[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] [/size][/font][/size][/font][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"]<stdio.h>

[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]#include[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] [/size][/font][/size][/font][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"]"MapData.h"

[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]#include[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] [/size][/font][/size][/font][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"]<allegro.h>

[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]#include[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] [/size][/font][/size][/font][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"]<allegro5/allegro_image.h>

[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]const[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]float[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] FPS = 60;

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]const[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] SCREEN_W = 640;

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]const[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] SCREEN_H = 480;

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]const[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] BOUNCER_SIZE = 32;

ALLEGRO_BITMAP* Tiles[8];

CMapData MapData;

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]enum[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] MYKEYS {

KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT

};

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]void[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] DrawMap(){

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]for[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] ([/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] i1 = 0; i1 < 15; i1++)

{

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]for[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] ([/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] j1 = 0; j1 < 20; j1++)

{

al_draw_bitmap(Tiles[MapData.Map[i1][j1]], (j1 * 32), (i1 * 32),0);

}

}

?

}



[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] main([/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] argc, [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]char[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] **argv)

{

ALLEGRO_DISPLAY *display = NULL;

ALLEGRO_EVENT_QUEUE *event_queue = NULL;

ALLEGRO_TIMER *timer = NULL;

ALLEGRO_BITMAP *bouncer = NULL;

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]float[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]float[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0;

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]bool[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] key[4] = { [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]false[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"], [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]false[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"], [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]false[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"], [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]false[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] };

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]bool[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] redraw = [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]true[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]bool[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] doexit = [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]false[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];

al_init_image_addon();

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"](!al_init()) {

fprintf(stderr, [/size][/font][/size][/font][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"]"failed to initialize allegro!\n"[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"]);

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]return[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] -1;

}

Tiles[0] = al_load_bitmap([/size][/font][/size][/font][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"]"bitmap1.bmp"[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"]);





[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"](!al_install_keyboard()) {

fprintf(stderr, [/size][/font][/size][/font][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"]"failed to initialize the keyboard!\n"[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"]);

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]return[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] -1;

}



timer = al_create_timer(1.0 / FPS);

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"](!timer) {

fprintf(stderr, [/size][/font][/size][/font][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"]"failed to create timer!\n"[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"]);

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]return[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] -1;

}



display = al_create_display(SCREEN_W, SCREEN_H);

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"](!display) {

fprintf(stderr, [/size][/font][/size][/font][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"]"failed to create display!\n"[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"]);

al_destroy_timer(timer);

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]return[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] -1;

}



bouncer = al_load_bitmap([/size][/font][/size][/font][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"]"Test BitMap.bmp"[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"]);

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"](!bouncer) {

fprintf(stderr, [/size][/font][/size][/font][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"]"failed to create bouncer bitmap!\n"[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"]);

al_destroy_display(display);

al_destroy_timer(timer);

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]return[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] -1;

}

al_convert_mask_to_alpha(bouncer, (al_map_rgb(255, 0, 255)));

al_convert_mask_to_alpha(Tiles[0], (al_map_rgb(255,0,255)));

al_set_target_bitmap(al_get_backbuffer(display));



event_queue = al_create_event_queue();

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"](!event_queue) {

fprintf(stderr, [/size][/font][/size][/font][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"]"failed to create event_queue!\n"[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"]);

al_destroy_bitmap(bouncer);

al_destroy_display(display);

al_destroy_timer(timer);

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]return[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] -1;

}



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_register_event_source(event_queue, al_get_keyboard_event_source());



al_clear_to_color(al_map_rgb(200,200,200));

al_flip_display();



al_start_timer(timer);



[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]while[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"](!doexit)

{

ALLEGRO_EVENT ev;

al_wait_for_event(event_queue, &ev);



[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"](ev.type == ALLEGRO_EVENT_TIMER) {

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"](key[KEY_UP] && bouncer_y >= 4.0) {

bouncer_y -= 4.0;

}



[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"](key[KEY_DOWN] && bouncer_y <= SCREEN_H - BOUNCER_SIZE - 4.0) {

bouncer_y += 4.0;

}



[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"](key[KEY_LEFT] && bouncer_x >= 4.0) {

bouncer_x -= 4.0;

}



[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"](key[KEY_RIGHT] && bouncer_x <= SCREEN_W - BOUNCER_SIZE - 4.0) {

bouncer_x += 4.0;

}



redraw = [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]true[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];

}

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]else[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"](ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]break[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];

}

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]else[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"](ev.type == ALLEGRO_EVENT_KEY_DOWN) {

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]switch[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"](ev.keyboard.keycode) {

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]case[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] ALLEGRO_KEY_UP:

key[KEY_UP] = [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]true[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]break[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];



[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]case[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] ALLEGRO_KEY_DOWN:

key[KEY_DOWN] = [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]true[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]break[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];



[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]case[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] ALLEGRO_KEY_LEFT:

key[KEY_LEFT] = [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]true[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]break[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];



[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]case[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] ALLEGRO_KEY_RIGHT:

key[KEY_RIGHT] = [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]true[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]break[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];

}

}

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]else[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"](ev.type == ALLEGRO_EVENT_KEY_UP) {

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]switch[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"](ev.keyboard.keycode) {

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]case[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] ALLEGRO_KEY_UP:

key[KEY_UP] = [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]false[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]break[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];



[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]case[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] ALLEGRO_KEY_DOWN:

key[KEY_DOWN] = [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]false[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]break[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];



[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]case[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] ALLEGRO_KEY_LEFT:

key[KEY_LEFT] = [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]false[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]break[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];



[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]case[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] ALLEGRO_KEY_RIGHT:

key[KEY_RIGHT] = [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]false[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]break[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];



[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]case[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] ALLEGRO_KEY_ESCAPE:

doexit = [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]true[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]break[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];

}

}



[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"](redraw && al_is_event_queue_empty(event_queue)) {

redraw = [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]false[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"];



al_clear_to_color(al_map_rgb(200,200,200));

DrawMap();

al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);

al_flip_display();



}

}



al_destroy_bitmap(bouncer);

al_destroy_timer(timer);

al_destroy_display(display);

al_destroy_event_queue(event_queue);



[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]return[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] 0;

}
[/code]

MapData.h:
[code]
[font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]

#pragma[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] [/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]once

class[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] CMapData

{

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]public[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"]:

CMapData([/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]void[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"]);

~CMapData([/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]void[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"]);

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] tilesize;

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] Map[15][20];

};

?[/code]

MapData.cpp

[code]
[font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]

#include[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] [/size][/font][/size][/font][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"][font="Courier New"][size="2"][color="#a31515"]"MapData.h"

[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"]?

CMapData::CMapData([/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]void[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"])

{

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] tilesize = 32;

[/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"] map1[15][20] ={{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};



}

?

CMapData::~CMapData([/size][/font][/size][/font][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"][font="Courier New"][size="2"][color="#0000ff"]void[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="2"][font="Courier New"][size="2"])

{

}
[/code]

Yes, the map is hardcoded in and only has 0's (was too lazy to mAKE more bitmaps. Although 0 was probably a bad choice for number as if it returns NULL, that's what it'll display... Should've went with 2/3...

But yeah, when I run this it is REALLY slow... Why? Is it because it's redrawing the entire 300 bitmaps. (I believe...) every frame?
Without the tiles, it runs at 60 FPS just fine. (remove DrawMap(); to run without tiles. And it runs quickly...)

*NOTE. I won't be responding until late tomorrow. Gonna go to sleep now, and then school tomorrow.

G'Night all.

[/size][/font][/size][/font][/size][/font][/size][/font][/size][/font][/size][/font]

Share this post


Link to post
Share on other sites
Hodgman    51323
You've got to be very careful with this forum, or it will mangle your posted code like that... :(

Here you go:[hr]main.cpp:
[source lang=cpp]#include <stdio.h>
#include "MapData.h"
#include <allegro.h>
#include <allegro5/allegro_image.h>
const float FPS = 60;
const int SCREEN_W = 640;
const int SCREEN_H = 480;
const int BOUNCER_SIZE = 32;
ALLEGRO_BITMAP* Tiles[8];
CMapData MapData;
enum MYKEYS {
KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT
};
void DrawMap(){
for (int i1 = 0; i1 < 15; i1++)
{
for (int j1 = 0; j1 < 20; j1++)
{
al_draw_bitmap(Tiles[MapData.Map[i1][j1]], (j1 * 32), (i1 * 32),0);
}
}
?
}

int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_BITMAP *bouncer = NULL;
float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;
float bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0;
bool key[4] = { false, false, false, false };
bool redraw = true;
bool doexit = false;
al_init_image_addon();
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
Tiles[0] = al_load_bitmap("bitmap1.bmp");


if(!al_install_keyboard()) {
fprintf(stderr, "failed to initialize the keyboard!\n");
return -1;
}

timer = al_create_timer(1.0 / FPS);
if(!timer) {
fprintf(stderr, "failed to create timer!\n");
return -1;
}

display = al_create_display(SCREEN_W, SCREEN_H);
if(!display) {
fprintf(stderr, "failed to create display!\n");
al_destroy_timer(timer);
return -1;
}

bouncer = al_load_bitmap("Test BitMap.bmp");
if(!bouncer) {
fprintf(stderr, "failed to create bouncer bitmap!\n");
al_destroy_display(display);
al_destroy_timer(timer);
return -1;
}
al_convert_mask_to_alpha(bouncer, (al_map_rgb(255, 0, 255)));
al_convert_mask_to_alpha(Tiles[0], (al_map_rgb(255,0,255)));
al_set_target_bitmap(al_get_backbuffer(display));

event_queue = al_create_event_queue();
if(!event_queue) {
fprintf(stderr, "failed to create event_queue!\n");
al_destroy_bitmap(bouncer);
al_destroy_display(display);
al_destroy_timer(timer);
return -1;
}

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_register_event_source(event_queue, al_get_keyboard_event_source());

al_clear_to_color(al_map_rgb(200,200,200));
al_flip_display();

al_start_timer(timer);

while(!doexit)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);

if(ev.type == ALLEGRO_EVENT_TIMER) {
if(key[KEY_UP] && bouncer_y >= 4.0) {
bouncer_y -= 4.0;
}

if(key[KEY_DOWN] && bouncer_y <= SCREEN_H - BOUNCER_SIZE - 4.0) {
bouncer_y += 4.0;
}

if(key[KEY_LEFT] && bouncer_x >= 4.0) {
bouncer_x -= 4.0;
}

if(key[KEY_RIGHT] && bouncer_x <= SCREEN_W - BOUNCER_SIZE - 4.0) {
bouncer_x += 4.0;
}

redraw = true;
}
else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
break;
}
else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
switch(ev.keyboard.keycode) {
case ALLEGRO_KEY_UP:
key[KEY_UP] = true;
break;

case ALLEGRO_KEY_DOWN:
key[KEY_DOWN] = true;
break;

case ALLEGRO_KEY_LEFT:
key[KEY_LEFT] = true;
break;

case ALLEGRO_KEY_RIGHT:
key[KEY_RIGHT] = true;
break;
}
}
else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
switch(ev.keyboard.keycode) {
case ALLEGRO_KEY_UP:
key[KEY_UP] = false;
break;

case ALLEGRO_KEY_DOWN:
key[KEY_DOWN] = false;
break;

case ALLEGRO_KEY_LEFT:
key[KEY_LEFT] = false;
break;

case ALLEGRO_KEY_RIGHT:
key[KEY_RIGHT] = false;
break;

case ALLEGRO_KEY_ESCAPE:
doexit = true;
break;
}
}

if(redraw && al_is_event_queue_empty(event_queue)) {
redraw = false;

al_clear_to_color(al_map_rgb(200,200,200));
DrawMap();
al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
al_flip_display();

}
}

al_destroy_bitmap(bouncer);
al_destroy_timer(timer);
al_destroy_display(display);
al_destroy_event_queue(event_queue);

return 0;
}[/source]MapData.h:[source lang=cpp]#pragma once
class CMapData
{
public:
CMapData(void);
~CMapData(void);
int tilesize;
int Map[15][20];
};[/source]MapData.cpp[source lang=cpp]#include "MapData.h"
?
CMapData::CMapData(void)
{
int tilesize = 32;
int map1[15][20] ={{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
,{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};

}
?
CMapData::~CMapData()
{
}[/source]

Share this post


Link to post
Share on other sites
kauna    2922
Hi,

Are all 300 tiles visible actually in the same time? If not, do not draw tiles outside of screen.

However, I cannot see how come 300 drawing calls slow down that much. Even the most inefficient way of drawing all the tiles shouldn't slow down much.

Do you have debug or release build?

al_draw_bitmap function may not be designed for drawing lots of tiles.

Good luck!

Share this post


Link to post
Share on other sites
XDaWNeDX    113
Yeah, it draws all 300 tiles at the same time. But what else am I supposed to use, if not al_draw_bitmap...


Ok, thanks for that note.. didn't even notice that last night.

Share this post


Link to post
Share on other sites
kauna    2922
[quote name='XDaWNeDX' timestamp='1307629247' post='4821318']
Yeah, it draws all 300 tiles at the same time. But what else am I supposed to use, if not al_draw_bitmap...


Ok, thanks for that note.. didn't even notice that last night.
[/quote]

I have to admit that I don't know about Allegro to guide you much further. I found some topics on the net about slow calls to the particular bitmap function, but I cannot guess what is the matter really. A wild guess would be that there is something wrong with creating your tile bitmaps. Something that forces the system to non-accelerated mode.

Good luck anyway!

Share this post


Link to post
Share on other sites
Ravyne    14300
Not Alegro-specific, but a few general issues to be aware of --

[list][*]Make sure that bit-depth and format of your tiles matches that of your rendering surface, otherwise a per-pixel, real-time conversion is probably taking place for each draw call.[*]If Allegro is OpenGL/Direct3D accelerated dig around and look for batched drawing calls. IIRC, using OpenGL/Direct3D you get around 10k draw calls per second, at best.[*]If you have control over it, make sure your tiles and rendering surface are aligned on nice boundaries, are power-of-2, or at least a multiple of a power of two.[*]Make sure you're not doing anything hair-brained like loading multiple copies of the same tile image for each place on the map that it's used, or that you aren't loading from disk every frame.[*]Make sure your map-access is cache-friendly -- a two-dimensional map array in C should be defined [map_height][map_width], not the other way around (a common mistake for a beginner).[/list]

Share this post


Link to post
Share on other sites
Hodgman    51323
You're actually very luck that your code isn't crashing right now. Check this out:[source]class CMapData
{
public:
CMapData()
{
int map1[15][20] = ...;
}
int Map[15][20];
};
ALLEGRO_BITMAP* Tiles[8];
Tiles[0] = al_load_bitmap("bitmap1.bmp");
CMapData MapData;
al_draw_bitmap( Tiles[MapData.Map[i1][j1]], ...);[/source]Only "[font="'Courier New"]Tiles[0][/font]" contains a valid pointer - using any tile index other than [font="'Courier New"]0[/font] should crash.
The tile index is fetched from "[font="'Courier New"]MapData.Map[/font]".
"[font="'Courier New"]MapData.Map[/font]" is never initialized (it should contain unpredictable random garbage numbers).

Share this post


Link to post
Share on other sites
XDaWNeDX    113
[quote name='Ravyne' timestamp='1307652420' post='4821477']
Not Alegro-specific, but a few general issues to be aware of --

[list][*]Make sure that bit-depth and format of your tiles matches that of your rendering surface, otherwise a per-pixel, real-time conversion is probably taking place for each draw call.[*]If Allegro is OpenGL/Direct3D accelerated dig around and look for batched drawing calls. IIRC, using OpenGL/Direct3D you get around 10k draw calls per second, at best.[*]If you have control over it, make sure your tiles and rendering surface are aligned on nice boundaries, are power-of-2, or at least a multiple of a power of two.[*]Make sure you're not doing anything hair-brained like loading multiple copies of the same tile image for each place on the map that it's used, or that you aren't loading from disk every frame.[*]Make sure your map-access is cache-friendly -- a two-dimensional map array in C should be defined [map_height][map_width], not the other way around (a common mistake for a beginner).[/list]
[/quote]

I don't know what bit-depth is. :S New to this whole graphics API thing.
It should only be drawing 60 times a second, but it's drawing about once a second...
My tiles are 32x32, screen is 640x480.
I don't know how that'd happen. The only time I DrawMap() is right after clearing the backbuffer... So it shouldn't be loading multiple copies. And I'm pretty sure I'm loading from memory.
I did some research, and apparently I should be loading a VIDEO_BITMAP instead of from memory, but I'm not sure HOW do to that. I'll try it out, and see if it works.
I copied this from the allegro's official wiki... Well, the int array at least. Most everything else is outdated there.

Also, @hodgman, it IS declared in map.h, but it is not created in the constructor. I'll see if changing that does anything at all...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this