Sign in to follow this  
GameDevDrox

Help on tile map creator

Recommended Posts

Hello, thanks for the help in advance I'm currently createing a mini tower defense game and I have a little problem. I don't have any errors but my game freezes and I can't do anything except turn off the computer. I think it's my math but I don't know... It won't let me even Alt + tab or Ctrl + alt + delete... well it does but I can't see it... and to complicate things even more it works the first time( lets you esc still doesn't draw anything besides the mouse) and stops responding when I exit out and then after I do it the second time it don't let me see anything like I said.... anyways heres the source code. #include <allegro.h> // this is my tower defense game map creator class grid{ public: grid(); ~grid(); int Add_tile(int x, int y, int *type); int Remove_tile(int x, int y); int Draw_grid(int x,int y); int grid_array[63][63]; }; grid::grid(){ } grid::~grid(){ } int grid::Add_tile(int x, int y, int *type){ grid_array[x][y] = *type; } int grid::Remove_tile(int x, int y){ grid_array[x][y] = 0 ; } int grid::Draw_grid(int x,int y){ int z; z = grid_array[x][y]; return z; } int main() { allegro_init(); install_mouse(); install_keyboard(); set_color_depth(16); set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0); acquire_screen(); BITMAP* Buffer; BITMAP* grass, *platform_top,*platform_middle,*platform_corner_left,*platform_corner_right,*platform_right,*platform_left,*road; grass = load_bitmap("tiles\\grass.bmp",NULL); platform_top = load_bitmap("tiles\\platform front top.bmp",NULL); platform_middle = load_bitmap("tile\\platform middle front.bmp",NULL); platform_corner_left = load_bitmap("tiles\\platform corner top left.bmp",NULL); platform_corner_right = load_bitmap("tiles\\platform corner top right.bmp",NULL); platform_right = load_bitmap("tiles\\platform right.bmp",NULL); platform_left = load_bitmap("tiles\\platform left.bmp", NULL); road = load_bitmap("tiles\\road.bmp",NULL); Buffer = create_bitmap(640,480); grid map; int selected_tile; int screen_sel_x = 0; int screen_sel_y = 0; show_mouse(Buffer); int cursor_x = 20, cursor_y = 20; int current_tile; while(!key[KEY_ESC]) { cursor_x = mouse_x; cursor_y = mouse_y; if(key[KEY_1]) { selected_tile = 1; } if(key[KEY_2]) { selected_tile = 2; } if(key[KEY_3]) { selected_tile = 3; } if(key[KEY_4]) { selected_tile = 4; } if(key[KEY_5]) { selected_tile = 5; } if(key[KEY_6]) { selected_tile = 6; } if(key[KEY_7]) { selected_tile = 7; } if(key[KEY_8]) { selected_tile = 8; } if(key[KEY_UP]) { if(screen_sel_y != 0) { screen_sel_y = screen_sel_y--; } } if(key[KEY_DOWN]) { if(screen_sel_y != 34) { screen_sel_y = screen_sel_y++; } } if(key[KEY_RIGHT]) { if(screen_sel_x != 24) { screen_sel_x++; } } if(key[KEY_LEFT]) { if(screen_sel_x != 0) { screen_sel_x++; } } if(mouse_b & 1) { map.Add_tile(screen_sel_x + cursor_x / 16, screen_sel_y + cursor_y / 16, &selected_tile); } if(mouse_b & 2) { map.Remove_tile(screen_sel_x + cursor_x / 16, screen_sel_y + cursor_y / 16); } for(int x = 0; x == 40;x++) { for(int y = 0; y == 30; y++) { if(map.Draw_grid(screen_sel_x + mouse_x / 16, screen_sel_y + mouse_y / 16) == 1) { draw_sprite(Buffer,grass,screen_sel_x + mouse_x / 16, screen_sel_y + mouse_y / 16); } } } draw_sprite(screen,Buffer,0,0); } release_screen(); return 0; } END_OF_MAIN() also if you know of anything that might help me out such as tutorials on the topic or a tutorial on network programming I would greatly appricate it... also I know theres a few things that i can do to make it more compact and mabey faster but i haven't programed in a few years so I'm out of my niche. Still a begginer even after 3 years of this crap !

Share this post


Link to post
Share on other sites


A very old debugging tool is called the 'print statement method' which can help trace a problem that freezes a program (or the whole machine).

You create a log file and a routine that prints a given msg string (maybe a _line_ number also) to a logfile and does a flush to make sure the ouput is written (sometiems you even need to open/close the file every time...).

The killer error will always be somewhere after the last printed log message and the real culprit that corrupted your data will be before that.

You then do a divide and conquer search on where the error is happening by inserting these logger calls in the area where you think the problem is (expand the scope if it doesnt encompass it) and then add more logger calls each time you run it (and it fails) to narrow down the problem even more.

After you narrow it down enough you will know what data is involved and then can look thru the code to find what set it that way (and now the debugger with variable watches and breakpoints can be used effectively)

You may have to be careful to remove unneeded logger calls to let you program run faster. I even before had to create a state saver for the program to save/load the game situation (data state) because it took minutes to play thru back to the exact situation that precipitated the error.

Share this post


Link to post
Share on other sites
my guess is that you mistyped one of the filenames for the images or one of the files doesnt exist. does allegro support long filenames? I cant remember. also there is a forum feature that makes your source look better, the tag is used like this: [ source lang="cpp"]Code here[ /source]

Share this post


Link to post
Share on other sites
its difficult to read, but it could be because you didnt install_timer() which is required to use the mouse routines.

there might be other errors, but thats the first thing I'd try.

the best thing to do is to keep taking out code and re-running it until it works, then slowly put it all back in.

formatting the code better will also help you to find mistakes.

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