Tilemap Editor - Nothing Happening

Started by
3 comments, last by Robbie Woods 11 years, 10 months ago

#include <allegro5.h>
#include <fstream>
#include <iostream>
#include <allegro_primitives.h>

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define FPS 60
#define MAP_HEIGHT 15
#define MAP_WIDTH 20

ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *evq = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_BITMAP *buffer = NULL;
ALLEGRO_BITMAP *map = NULL;
ALLEGRO_COLOR black = al_map_rgb(0, 0, 0), purple = al_map_rgb(255, 0, 255), blue = al_map_rgb(0, 0, 255), red = al_map_rgb(255, 0, 0), white = al_map_rgb(255, 255, 255), green = al_map_rgb(0, 255, 0);
ALLEGRO_EVENT ev;

bool mapRedraw = false, wallMode = false, floorMode = false, help = false, done = false;
int otherCount = 0, mousePosX, mousePosY;
int mapArray[MAP_HEIGHT][MAP_WIDTH];

int destroy(ALLEGRO_DISPLAY *display, ALLEGRO_EVENT_QUEUE *evqueue, ALLEGRO_TIMER *timer);
void draw();
void drawmap();
void edit();
void load();
void save();
int main(void)
{
if (!al_init())
return -1;
display = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT);
evq = al_create_event_queue();
timer = al_create_timer(1.0 / FPS);
al_install_keyboard();
al_install_mouse();
al_init_primitives_addon();
al_register_event_source(evq, al_get_mouse_event_source());
al_register_event_source(evq, al_get_keyboard_event_source());
al_register_event_source(evq, al_get_timer_event_source(timer));
al_register_event_source(evq, al_get_display_event_source(display));
buffer = al_create_bitmap(SCREEN_WIDTH, SCREEN_HEIGHT);
map = al_create_bitmap(SCREEN_WIDTH, SCREEN_HEIGHT);
for (int i = 0; i < MAP_HEIGHT; i++)
{
for (int j = 0; j < MAP_WIDTH; j++)
{
mapArray[j] = 0;
}
}
drawmap();
al_start_timer(timer);
while (!done)
{
al_wait_for_event(evq, &ev);
if (ev.type == ALLEGRO_EVENT_TIMER)
{
if (mapRedraw)
{
drawmap();
mapRedraw = false;
}
draw();
}
else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
done = true;
}
else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch (ev.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
case ALLEGRO_KEY_F1:
if (!help)
help = true;
else if (help)
help = false;
break;
case ALLEGRO_KEY_W:
if (!wallMode)
{
wallMode = true;
floorMode = false;
}
else if (wallMode)
{
wallMode = false;
}
break;
case ALLEGRO_KEY_F:
if (!floorMode)
{
floorMode = true;
wallMode = false;
}
else if (floorMode)
{
floorMode = false;
}
break;
case ALLEGRO_KEY_S:
save();
break;

case ALLEGRO_KEY_L:
load();
break;
}
}
else if (ev.type = ALLEGRO_EVENT_MOUSE_AXES)
{
mousePosX = ev.mouse.x;
mousePosY = ev.mouse.y;
}
if (ev.type = ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
{
if (ev.mouse.button & 1)
{
edit();
}
}
}

destroy(display, evq, timer);
}

int destroy(ALLEGRO_DISPLAY *display1, ALLEGRO_EVENT_QUEUE *evqueue, ALLEGRO_TIMER *timer1)
{
al_destroy_display(display);
al_destroy_event_queue(evqueue);
al_destroy_timer(timer);

if (!display && !evqueue && !timer)
return 0;
else
return -1;
}
void draw()
{
al_set_target_bitmap(buffer);
al_clear_to_color(al_map_rgb(0, 0, 0));
al_draw_bitmap(map, 1.0f, 1.0f, NULL);

if(help) //do we need to display help?
{
std::cout << "Press S to save the map";
std::cout << "Press L to load a map ";
std::cout << "Press W to place a Wall";
std::cout << "Press F to place a Floor";
help = false;
}
else if (!help)
{
system("cls");
}
if (wallMode)
{
al_draw_filled_rectangle(mousePosX - 16, mousePosY - 16, mousePosX + 16, mousePosY + 16, al_map_rgb(0, 255, 0));
al_draw_rectangle(mousePosX-16,mousePosY-16,mousePosX+16,mousePosY+16, al_map_rgb(255, 0, 0), 2);
}
else if (floorMode)
{
al_draw_filled_rectangle(mousePosX - 16, mousePosY - 16, mousePosX + 16, mousePosY + 16, al_map_rgb(255, 255, 255));
al_draw_rectangle(mousePosX-16,mousePosY-16,mousePosX+16,mousePosY+16, al_map_rgb(255, 0, 0), 2);
}
else
{
al_draw_filled_circle(mousePosX, mousePosY, 4, al_map_rgb(255, 0, 0));
}
al_set_target_bitmap(al_get_backbuffer(display));
al_draw_bitmap(buffer, 0, 0, NULL);
al_flip_display();
}
void drawmap()
{
al_set_target_bitmap(map);
for (int i = 0; i < MAP_HEIGHT; i++)
{
for (int j = 0; j < MAP_WIDTH; j++)
{
if (mapArray[j] == 0)
{
al_draw_filled_rectangle(j*32, i*32, j*32+32, i*32+32, al_map_rgb(0, 0, 0));
}
else if (mapArray[j])
{
al_draw_filled_rectangle(j*32, i*32, j*32+32, i*32+32, al_map_rgb(255, 0, 0));
}
}
}
}
void edit()
{

if (wallMode)
{
mapArray[mousePosY / 32][mousePosX / 32] = 1;
}
if (floorMode)
{
mapArray[mousePosY / 32][mousePosX / 32] = 0;
}

mapRedraw = true;
}
void save()
{
std::ofstream mapOut;
mapOut.open("map1.amp");
for (int i = 0; i < MAP_HEIGHT; i++)
{
for (int j = 0; j < MAP_WIDTH; j++)
{
mapOut << mapArray[j] << " ";
}
mapOut << std::endl;
}
mapOut.close();
}
void load()
{
std::ifstream mapIn;
mapIn.open("map1.amp");
for (int i = 0; i < MAP_HEIGHT; i++)
{
for (int j = 0; j < MAP_WIDTH; j++)
{
mapIn >> mapArray[j];
}
}
mapRedraw = true;
mapIn.close();
}

Hi guys, I've finished converting a tile engine from Allegro 4 to 5 and am having a little trouble.

It compiles and runs fine, just everything is laggy.

Here's the code



I know it's not the most efficient but it should work. If someone could help me get it to work that'd be much appreciated.
Advertisement
Hey, did you try debugging it?
Set a breakpoint at all the places where you draw something and check if it executes everything like it's supposed to. If not, find the point where it stops working as intended. That should give you enough information to specify the problem or fix it yourself.
I managed to get it to work but its really laggy. Any ideas?
What exactly do you mean by laggy? Input lag (a delay between when you press a key and when it's registered by the game) or low framerate (everything seems to stutter, comes usually with input lag)?
Both, it takes a while to register that there's been input and when it does it skips.

This topic is closed to new replies.

Advertisement