Allegro5 Cant get Tilemaps to work.
EnClave

Hello everyone, im really new to Programming, i'm reading books and also follow Online Tutorials to create my own RPG game as a "Learning" project.

Ive runned into this Problem tho, ive tried to search it up but havent found any usefull help so i'm posting here to see if anyone could crack this problem to help me out.

I got this beginning of a RPG game with a Character with Sprite Animation, also Screen Scrolling, now im trying to add Tile maps and im not applying them directly in my code, i have a seperate .txt file that im trying to load it from, but somehow i cant get this to work, when i run the code i dont get ANY errors, just that the tiles are invisible... The online tutorial ive been follow did the exacly same thing and its working for him, i tried after hours and hours to copy his code and just run it to see what happend, and same thing happend nothing... code runns perfectly in my compiler with no erros but nothing shows up... is it something wrong with my Compiler? my installation of Allegro5? I mean everything els works in the Allegro 5 Library so i dont really understand..

id preciate if anyone could look over my code and see if im doing something wrong!

Here is the code:

#SelectExpand
1 2#include<allegro5\allegro5.h> 3#include<allegro5\allegro_native_dialog.h> 4#include<allegro5\allegro_primitives.h> 5#include<allegro5\allegro_image.h> 6#include<fstream> 7#include<string> 8#include<sstream> 9#include<vector> 10 11 12#define ScreenWidth 1024 13#define ScreenHeight 768 14#define BlockSize 40 15 16int loadCounterX = 0, loadCounterY = 0, mapSizeX, mapSizeY; 17 18 19// %%%%% TILE MAP LOADING TXT FILES %%%%%// 20void LoadMap(const char *filename, std::vector< std::vector <int> > &map) 21{ 22 std::fstream openfile(filename); 23 if(openfile.is_open()) 24 { 25 std::string line, value; 26 int space; 27 28 while(!openfile.eof()) 29 { 30 std::getline(openfile, line); 31 32 std::stringstream str(line); 33 std::vector<int> tempVector; 34 35 while(!str.eof()) 36 { 37 std::getline(str, value, ' '); 38 if(value.length() > 0) 39 tempVector.push_back(atoi(value.c_str())); 40 } 41 map.push_back(tempVector); 42 } 43 } 44 else 45 { 46 } 47 48} 49 50 // %%%%% CAMERA SCROLLING %%%%%// 51void cameraUpdate(float *cameraPosition, float x, float y, int width, int height) 52{ 53 cameraPosition[0] = -(ScreenWidth / 2) + (x + width / 2); 54 cameraPosition[1] = -(ScreenWidth / 2) + (y + height / 2); 55 56 if(cameraPosition[0] < 0) 57 cameraPosition[0] = 0; 58 if(cameraPosition[1] < 0) 59 cameraPosition[1] = 0; 60 61} 62void DrawMap(std::vector <std::vector <int> > map); 63 64int main() 65{ 66 // %%%%% IMPORTANT SETTINGS %%%%%// 67 ALLEGRO_DISPLAY *display; 68 const float FPS = 60.0; 69 const float frameFPS = 10.0; 70 enum Direction { DOWN, LEFT, RIGHT, UP }; 71 72 73 74 75 // %%%%% ERROR CODES, ALLEGRO AND DISPLAY %%%%%// 76 if(!al_init()) 77 al_show_native_message_box(NULL, "Error", NULL, "Could not Initialize Allegro", NULL, NULL); 78 79 display = al_create_display(ScreenWidth, ScreenHeight); 80 81 if(!display) 82 al_show_native_message_box(NULL, "Error", NULL, "Could not create Allegro Display", NULL, NULL); 83 84 85 // %%%%% WINDOW SETTINGS %%%%%// 86 87 al_set_new_display_flags(ALLEGRO_WINDOWED); 88 al_set_window_position(display, 200, 200); 89 al_set_window_title(display, "PRE-alpha Test"); 90 91 ALLEGRO_COLOR playerColor = al_map_rgb(255, 0, 255); 92 93 94 // %%%%% Movement SPEED %%%%%// 95 bool done = false, draw = true, active = false; 96 float x = 512, y = 384, moveSpeed = 3; 97 int dir = DOWN, sourceX = 64, sourceY = 96; 98 float cameraPosition[2] = { 0, 0 }; 99 100 101 // %%%%% ADDONS %%%%%// 102 al_init_primitives_addon(); 103 al_install_keyboard(); 104 al_init_primitives_addon(); 105 al_install_mouse(); 106 al_init_image_addon(); 107 108 109 110 111 ALLEGRO_BITMAP *player = al_load_bitmap("player.png"); 112 /*ALLEGRO_BITMAP *background = al_load_bitmap("backgroundtest.png");*/ 113 al_convert_mask_to_alpha(player, al_map_rgb(255, 0, 255)); 114 ALLEGRO_KEYBOARD_STATE KeyState; 115 ALLEGRO_TRANSFORM camera; 116 117 118 // %%%%% EVENTS installation & TIMERS %%%%%// 119 ALLEGRO_TIMER *timer = al_create_timer(1.0 / FPS); 120 ALLEGRO_TIMER *frametimer = al_create_timer(1.0 / frameFPS); 121 122 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); 123 al_register_event_source(event_queue, al_get_keyboard_event_source()); 124 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 125 al_register_event_source(event_queue, al_get_timer_event_source(frametimer)); 126 al_register_event_source(event_queue, al_get_display_event_source(display)); 127 al_register_event_source(event_queue, al_get_mouse_event_source()); 128 129 130 131 132 133 // %%%%% TIMER, NO installations UNDER THIS %%%%%// 134 al_start_timer(timer); 135 al_start_timer(frametimer); 136 137 // %%%%% Beginning of Events %%%%%// 138 139 140 std::vector< std::vector <int> > map; 141 LoadMap("map.txt", map); 142 143 while(!done) 144 { 145 ALLEGRO_EVENT events; 146 al_wait_for_event(event_queue, &events); 147 al_get_keyboard_state(&KeyState); 148 149 150 // %%%%% TILE MAP IN THE EVENT & TIMER CODE %%%%%// 151 152 if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 153 { 154 done = true; 155 } 156 else if (events.type == ALLEGRO_EVENT_TIMER) 157 { 158 159 } 160 161 // %%%%% KEYBOARD INPUT AND EVENTS AND PLAYER IMAGE AND CAMERA SCROLLING FOR CHARACTER %%%%%// 162 if(events.type == ALLEGRO_EVENT_KEY_UP) 163 { 164 switch(events.keyboard.keycode) 165 { 166 case ALLEGRO_KEY_ESCAPE: 167 done = true; 168 } 169 } 170 else if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 171 { 172 done = true; 173 } 174 else if (events.type = ALLEGRO_EVENT_TIMER) 175 { 176 if(events.timer.source == timer) 177 { 178 active = true; 179 if(al_key_down(&KeyState, ALLEGRO_KEY_S)) 180 { 181 y += moveSpeed; 182 dir = DOWN; 183 } 184 else if(al_key_down(&KeyState, ALLEGRO_KEY_W)) 185 { 186 y -= moveSpeed; 187 dir = UP; 188 } 189 else if(al_key_down(&KeyState, ALLEGRO_KEY_D)) 190 { 191 x += moveSpeed; 192 dir = RIGHT; 193 } 194 else if(al_key_down(&KeyState, ALLEGRO_KEY_A)) 195 { 196 x -= moveSpeed; 197 dir = LEFT; 198 } 199 else 200 active = false; 201 202 cameraUpdate(cameraPosition, x, y, 64, 96); 203 al_identity_transform(&camera); 204 al_translate_transform(&camera, -cameraPosition[0], -cameraPosition[1]); 205 al_use_transform(&camera); 206 207 } 208 209 else if(events.timer.source == frametimer) 210 { 211 212 if(active) 213 sourceX += al_get_bitmap_width(player) / 4; 214 215 else 216 sourceX = 64; 217 218 if(sourceX >= al_get_bitmap_width(player)) 219 sourceX = 0; 220 221 sourceY = dir; 222 } 223 draw = true; 224 } 225 226 if(draw) 227 { 228 229 230 /*al_draw_bitmap(background, 0, 0, NULL);*/ 231 232 al_draw_bitmap_region(player, sourceX, dir * al_get_bitmap_height(player) / 4, 64, 96, x, y, NULL); 233 234 DrawMap(map); 235 al_flip_display(); 236 al_clear_to_color(al_map_rgb(0, 0, 0)); 237 238 239 } 240 241 242 // %%%%% GAME UI SHELL %%%%%// 243 /*al_draw_rectangle(1, 1, 800, 100, al_map_rgb(255, 50, 50), 9.0);*/ 244 245 // %%%%% Destroy ENDING %%%%%// 246 } 247 248 249 al_destroy_display(display); 250 al_destroy_timer(timer); 251 al_destroy_bitmap(player); 252 /*al_destroy_bitmap(background);*/ 253 al_destroy_event_queue(event_queue); 254 255 256 return 0; 257} 258 259 260 //%%%%% RITAR UPP TILES / FÄRG PÅ FYRKANTERNA %%%%%// 261void DrawMap(std::vector <std::vector <int> > map) 262 { 263 for(int i = 0; i < map.size(); i++) 264 { 265 for(int j = 0; j < map.size(); j++) 266 { 267 if(map[i][j] == 0) 268 al_draw_filled_rectangle(j * BlockSize, i * BlockSize, 269 j * BlockSize + BlockSize, i = BlockSize + BlockSize, al_map_rgb(255, 100, 255)); 270 else 271 al_draw_filled_rectangle(j * BlockSize, i * BlockSize, 272 j * BlockSize + BlockSize, i * BlockSize + BlockSize, al_map_rgb(0, 255, 0)); 273 274 } 275 } 276 }

LennyLen

If you want to post code, just place it between <code></code> tags.

#SelectExpand
1#include<allegro5\allegro5.h> 2#include<allegro5\allegro_native_dialog.h> 3#include<allegro5\allegro_primitives.h> 4#include<allegro5\allegro_image.h> 5#include<fstream> 6#include<string> 7#include<sstream> 8#include<vector> 9 10 11#define ScreenWidth 1024 12#define ScreenHeight 768 13#define BlockSize 40 14 15int loadCounterX = 0, loadCounterY = 0, mapSizeX, mapSizeY; 16 17 18// %%%%% TILE MAP LOADING TXT FILES %%%%%// 19void LoadMap(const char *filename, std::vector< std::vector <int> > &map) 20{ 21 std::fstream openfile(filename); 22 if(openfile.is_open()) 23 { 24 std::string line, value; 25 int space; 26 27 while(!openfile.eof()) 28 { 29 std::getline(openfile, line); 30 31 std::stringstream str(line); 32 std::vector<int> tempVector; 33 34 while(!str.eof()) 35 { 36 std::getline(str, value, ' '); 37 if(value.length() > 0) 38 tempVector.push_back(atoi(value.c_str())); 39 } 40 map.push_back(tempVector); 41 } 42 } 43 else 44 { 45 } 46 47} 48 49 // %%%%% CAMERA SCROLLING %%%%%// 50void cameraUpdate(float *cameraPosition, float x, float y, int width, int height) 51{ 52 cameraPosition[0] = -(ScreenWidth / 2) + (x + width / 2); 53 cameraPosition[1] = -(ScreenWidth / 2) + (y + height / 2); 54 55 if(cameraPosition[0] < 0) 56 cameraPosition[0] = 0; 57 if(cameraPosition[1] < 0) 58 cameraPosition[1] = 0; 59 60} 61void DrawMap(std::vector <std::vector <int> > map); 62 63int main() 64{ 65 // %%%%% IMPORTANT SETTINGS %%%%%// 66 ALLEGRO_DISPLAY *display; 67 const float FPS = 60.0; 68 const float frameFPS = 10.0; 69 enum Direction { DOWN, LEFT, RIGHT, UP }; 70 71 72 73 74 // %%%%% ERROR CODES, ALLEGRO AND DISPLAY %%%%%// 75 if(!al_init()) 76 al_show_native_message_box(NULL, "Error", NULL, "Could not Initialize Allegro", NULL, NULL); 77 78 display = al_create_display(ScreenWidth, ScreenHeight); 79 80 if(!display) 81 al_show_native_message_box(NULL, "Error", NULL, "Could not create Allegro Display", NULL, NULL); 82 83 84 // %%%%% WINDOW SETTINGS %%%%%// 85 86 al_set_new_display_flags(ALLEGRO_WINDOWED); 87 al_set_window_position(display, 200, 200); 88 al_set_window_title(display, "PRE-alpha Test"); 89 90 ALLEGRO_COLOR playerColor = al_map_rgb(255, 0, 255); 91 92 93 // %%%%% Movement SPEED %%%%%// 94 bool done = false, draw = true, active = false; 95 float x = 512, y = 384, moveSpeed = 3; 96 int dir = DOWN, sourceX = 64, sourceY = 96; 97 float cameraPosition[2] = { 0, 0 }; 98 99 100 // %%%%% ADDONS %%%%%// 101 al_init_primitives_addon(); 102 al_install_keyboard(); 103 al_init_primitives_addon(); 104 al_install_mouse(); 105 al_init_image_addon(); 106 107 108 109 110 ALLEGRO_BITMAP *player = al_load_bitmap("player.png"); 111 /*ALLEGRO_BITMAP *background = al_load_bitmap("backgroundtest.png");*/ 112 al_convert_mask_to_alpha(player, al_map_rgb(255, 0, 255)); 113 ALLEGRO_KEYBOARD_STATE KeyState; 114 ALLEGRO_TRANSFORM camera; 115 116 117 // %%%%% EVENTS installation & TIMERS %%%%%// 118 ALLEGRO_TIMER *timer = al_create_timer(1.0 / FPS); 119 ALLEGRO_TIMER *frametimer = al_create_timer(1.0 / frameFPS); 120 121 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); 122 al_register_event_source(event_queue, al_get_keyboard_event_source()); 123 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 124 al_register_event_source(event_queue, al_get_timer_event_source(frametimer)); 125 al_register_event_source(event_queue, al_get_display_event_source(display)); 126 al_register_event_source(event_queue, al_get_mouse_event_source()); 127 128 129 130 131 132 // %%%%% TIMER, NO installations UNDER THIS %%%%%// 133 al_start_timer(timer); 134 al_start_timer(frametimer); 135 136 // %%%%% Beginning of Events %%%%%// 137 138 139 std::vector< std::vector <int> > map; 140 LoadMap("map.txt", map); 141 142 while(!done) 143 { 144 ALLEGRO_EVENT events; 145 al_wait_for_event(event_queue, &events); 146 al_get_keyboard_state(&KeyState); 147 148 149 // %%%%% TILE MAP IN THE EVENT & TIMER CODE %%%%%// 150 151 if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 152 { 153 done = true; 154 } 155 else if (events.type == ALLEGRO_EVENT_TIMER) 156 { 157 158 } 159 160 // %%%%% KEYBOARD INPUT AND EVENTS AND PLAYER IMAGE AND CAMERA SCROLLING FOR CHARACTER %%%%%// 161 if(events.type == ALLEGRO_EVENT_KEY_UP) 162 { 163 switch(events.keyboard.keycode) 164 { 165 case ALLEGRO_KEY_ESCAPE: 166 done = true; 167 } 168 } 169 else if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 170 { 171 done = true; 172 } 173 else if (events.type = ALLEGRO_EVENT_TIMER) 174 { 175 if(events.timer.source == timer) 176 { 177 active = true; 178 if(al_key_down(&KeyState, ALLEGRO_KEY_S)) 179 { 180 y += moveSpeed; 181 dir = DOWN; 182 } 183 else if(al_key_down(&KeyState, ALLEGRO_KEY_W)) 184 { 185 y -= moveSpeed; 186 dir = UP; 187 } 188 else if(al_key_down(&KeyState, ALLEGRO_KEY_D)) 189 { 190 x += moveSpeed; 191 dir = RIGHT; 192 } 193 else if(al_key_down(&KeyState, ALLEGRO_KEY_A)) 194 { 195 x -= moveSpeed; 196 dir = LEFT; 197 } 198 else 199 active = false; 200 201 cameraUpdate(cameraPosition, x, y, 64, 96); 202 al_identity_transform(&camera); 203 al_translate_transform(&camera, -cameraPosition[0], -cameraPosition[1]); 204 al_use_transform(&camera); 205 206 } 207 208 else if(events.timer.source == frametimer) 209 { 210 211 if(active) 212 sourceX += al_get_bitmap_width(player) / 4; 213 214 else 215 sourceX = 64; 216 217 if(sourceX >= al_get_bitmap_width(player)) 218 sourceX = 0; 219 220 sourceY = dir; 221 } 222 draw = true; 223 } 224 225 if(draw) 226 { 227 228 229 /*al_draw_bitmap(background, 0, 0, NULL);*/ 230 231 al_draw_bitmap_region(player, sourceX, dir * al_get_bitmap_height(player) / 4, 64, 96, x, y, NULL); 232 233 DrawMap(map); 234 al_flip_display(); 235 al_clear_to_color(al_map_rgb(0, 0, 0)); 236 237 238 } 239 240 241 // %%%%% GAME UI SHELL %%%%%// 242 /*al_draw_rectangle(1, 1, 800, 100, al_map_rgb(255, 50, 50), 9.0);*/ 243 244 // %%%%% Destroy ENDING %%%%%// 245 } 246 247 248 al_destroy_display(display); 249 al_destroy_timer(timer); 250 al_destroy_bitmap(player); 251 /*al_destroy_bitmap(background);*/ 252 al_destroy_event_queue(event_queue); 253 254 255 return 0; 256} 257 258 259 //%%%%% RITAR UPP TILES / FÄRG PÅ FYRKANTERNA %%%%%// 260void DrawMap(std::vector <std::vector <int> > map) 261 { 262 for(int i = 0; i < map.size(); i++) 263 { 264 for(int j = 0; j < map.size(); j++) 265 { 266 if(map[i][j] == 0) 267 al_draw_filled_rectangle(j * BlockSize, i * BlockSize, 268 j * BlockSize + BlockSize, i = BlockSize + BlockSize, al_map_rgb(255, 100, 255)); 269 else 270 al_draw_filled_rectangle(j * BlockSize, i * BlockSize, 271 j * BlockSize + BlockSize, i * BlockSize + BlockSize, al_map_rgb(0, 255, 0)); 272 273 } 274 } 275 }

EnClave

Thanks alot LennyLen,

for some reason my mind set was set at [] instead of <> :P

Angeljruiz
#SelectExpand
1//main.cpp 2 // 3 // cPlayer.h 4 // Allegro 5 // 6 // Created by Angel Bates on 10/27/12. 7 // Copyright (c) 2012 Angel Bates. All rights reserved. 8 // 9#include <iostream> 10#include <fstream> 11#include <allegro5/allegro.h> 12#include <allegro5/allegro_primitives.h> 13#include <allegro5/allegro_image.h> 14#include "cPlayer.h" 15 16using namespace std; 17 18void loadmap(int Tiles[][30]); 19void updateBitTile(int Tiles[][30], ALLEGRO_BITMAP* BitTile, ALLEGRO_BITMAP* tiles, ALLEGRO_DISPLAY* display); 20bool Collision(int Tiles[][30], const int& x, const int& y); 21 22int main(int argc, char** argv) 23{ 24 ALLEGRO_DISPLAY* display = NULL; 25 ALLEGRO_EVENT_QUEUE* queue = NULL; 26 ALLEGRO_TIMER* timer = NULL; 27 ALLEGRO_BITMAP* player = NULL; 28 ALLEGRO_BITMAP* tiles = NULL; 29 ALLEGRO_BITMAP* BitTile = NULL; 30 31 al_init(); 32 al_install_keyboard(); 33 al_init_primitives_addon(); 34 al_init_image_addon(); 35 36 display = al_create_display(640, 480); 37 queue = al_create_event_queue(); 38 timer = al_create_timer(1.0 / 120); 39 player = al_load_bitmap("Knight.png"); 40 tiles = al_load_bitmap("Tiles.png"); 41 BitTile = al_create_bitmap(1280, 960); 42 43 44 bool Exit = false; 45 bool Redraw = true; 46 bool KeyDown = false; 47 48 bool kLeft = false; 49 bool kRight = false; 50 bool kUp = false; 51 bool kDown = false; 52 53 54 55 int Counter = 0; 56 57 int OffsetX = 0; 58 int OffsetY = 0; 59 60 int Tiles[40][30]; 61 62 for (int x = 0; x < 40; ++x) 63 { 64 for (int y = 0; y < 30; ++y) 65 { 66 Tiles[x][y] = 0; 67 } 68 } 69 70 loadmap(Tiles); 71 updateBitTile(Tiles, BitTile, tiles, display); 72 73 cPlayer Player(640/2-16, 480/2-16); 74 75 al_register_event_source(queue, al_get_display_event_source(display)); 76 al_register_event_source(queue, al_get_keyboard_event_source()); 77 al_register_event_source(queue, al_get_timer_event_source(timer)); 78 79 al_start_timer(timer); 80 81 while (!Exit) 82 { 83 ALLEGRO_EVENT ev; 84 al_wait_for_event(queue, &ev); 85 86 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) 87 { 88 switch (ev.keyboard.keycode) 89 { 90 case ALLEGRO_KEY_ESCAPE: 91 Exit = true; 92 break; 93 case ALLEGRO_KEY_LEFT: 94 Player.SetDir(LEFT); 95 KeyDown = true; 96 kLeft = true; 97 Redraw = true; 98 break; 99 case ALLEGRO_KEY_RIGHT: 100 Player.SetDir(RIGHT); 101 KeyDown = true; 102 kRight = true; 103 Redraw = true; 104 break; 105 case ALLEGRO_KEY_UP: 106 Player.SetDir(UP); 107 KeyDown = true; 108 kUp = true; 109 Redraw = true; 110 break; 111 case ALLEGRO_KEY_DOWN: 112 Player.SetDir(DOWN); 113 KeyDown = true; 114 kDown = true; 115 Redraw = true; 116 break; 117 } 118 } 119 120 if (ev.type == ALLEGRO_EVENT_KEY_UP) 121 { 122 switch (ev.keyboard.keycode) 123 { 124 case ALLEGRO_KEY_LEFT: 125 kLeft = false; 126 break; 127 case ALLEGRO_KEY_RIGHT: 128 kRight = false; 129 break; 130 case ALLEGRO_KEY_UP: 131 kUp = false; 132 break; 133 case ALLEGRO_KEY_DOWN: 134 kDown = false; 135 break; 136 } 137 if (!kRight && !kUp && !kDown && !kLeft) 138 KeyDown = false; 139 } 140 141 if (ev.type == ALLEGRO_EVENT_TIMER) 142 { 143 switch (Player.GetDir()) 144 { 145 case LEFT: 146 if (KeyDown && !Collision(Tiles, (Player.GetX() - 1) / 32, (Player.GetY()) / 32 ) && 147 !Collision(Tiles, (Player.GetX() - 1) / 32, (Player.GetY() + 31 ) / 32 ) ) 148 { 149 Player.Move(); 150 ++Counter; 151 --OffsetX; 152 Redraw = true; 153 } 154 break; 155 case RIGHT: 156 if (KeyDown && !Collision(Tiles, (Player.GetX() + 32) / 32, (Player.GetY()) / 32 ) && 157 !Collision(Tiles, (Player.GetX() + 32) / 32, (Player.GetY() + 31) / 32 ) ) 158 { 159 Player.Move(); 160 ++Counter; 161 ++OffsetX; 162 Redraw = true; 163 } 164 break; 165 case UP: 166 if (KeyDown && !Collision(Tiles, (Player.GetX() + 1) / 32, (Player.GetY() - 1) / 32 ) && 167 !Collision(Tiles, (Player.GetX() + 31) / 32, (Player.GetY() + -1) / 32 ) ) 168 { 169 Player.Move(); 170 ++Counter; 171 --OffsetY; 172 Redraw = true; 173 } 174 break; 175 case DOWN: 176 if (KeyDown && !Collision(Tiles, (Player.GetX() + 1) / 32, (Player.GetY() + 32) / 32 ) && 177 !Collision(Tiles, (Player.GetX() + 31) / 32, (Player.GetY() + 32) / 32 ) ) 178 { 179 Player.Move(); 180 ++Counter; 181 ++OffsetY; 182 Redraw = true; 183 } 184 break; 185 } 186 if (Counter == 30) 187 { 188 Player.Tick(); 189 Redraw = true; 190 Counter = 0; 191 } 192 193 } 194 195 if (al_event_queue_is_empty(queue) && Redraw) 196 { 197 al_clear_to_color(al_map_rgb(250, 250, 250)); 198 al_draw_bitmap(BitTile, 0-OffsetX, 0-OffsetY, NULL); 199// switch (Player.GetDir()) { 200// case LEFT: 201// al_draw_bitmap_region(player, Player.GetFrame() ? 32 : 0, 64, 32, 32, Player.GetX()-OffsetX, Player.GetY()-OffsetY, NULL); 202// break; 203// 204// case RIGHT: 205// al_draw_bitmap_region(player, Player.GetFrame() ? 32 : 0, 96, 32, 32, Player.GetX()-OffsetX, Player.GetY()-OffsetY, NULL); 206// break; 207// 208// case UP: 209// al_draw_bitmap_region(player, Player.GetFrame() ? 32 : 0, 32, 32, 32, Player.GetX()-OffsetX, Player.GetY()-OffsetY, NULL); 210// break; 211// 212// case DOWN: 213// al_draw_bitmap_region(player, Player.GetFrame() ? 32 : 0, 0, 32, 32, Player.GetX()-OffsetX, Player.GetY()-OffsetY, NULL); 214// break; 215// 216// } 217 switch (Player.GetDir()) { 218 case LEFT: 219 al_draw_bitmap_region(player, Player.GetFrame() ? 20 : 13 , 64, 20, 32, Player.GetX()-OffsetX, Player.GetY()-OffsetY, NULL); 220 break; 221 222 case RIGHT: 223 al_draw_bitmap_region(player, Player.GetFrame() ? 32 : 0, 96, 32, 32, Player.GetX()-OffsetX, Player.GetY()-OffsetY, NULL); 224 break; 225 226 case UP: 227 al_draw_bitmap_region(player, Player.GetFrame() ? 32 : 0, 32, 32, 32, Player.GetX()-OffsetX, Player.GetY()-OffsetY, NULL); 228 break; 229 230 case DOWN: 231 al_draw_bitmap_region(player, Player.GetFrame() ? 62 : 14, 8, 20, 32, Player.GetX()-OffsetX, Player.GetY()-OffsetY, NULL); 232 break; 233 234 } 235 236 al_draw_pixel((304-Player.GetX())/32, (224-Player.GetY())/32, al_map_rgb(0, 0, 250)); 237 al_flip_display(); 238 Redraw = false; 239 } 240 } 241 242 al_destroy_timer(timer); 243 al_destroy_display(display); 244 al_destroy_event_queue(queue); 245 al_destroy_bitmap(player); 246 al_destroy_bitmap(tiles); 247 al_destroy_bitmap(BitTile); 248 249 return 0; 250} 251void loadmap(int Tiles[][30]) 252{ 253 int x(0), y(0); 254 int temp; 255 ifstream Map("Map1.txt"); 256 if (Map.is_open()) 257 { 258 while(Map >> temp) 259 { 260 Tiles[x][y] = temp; 261 ++x; 262 if (x == 40) 263 { 264 x = 0; 265 ++y; 266 } 267 } 268 } 269 Map.close(); 270} 271void updateBitTile(int Tiles[][30], ALLEGRO_BITMAP* BitTile, ALLEGRO_BITMAP* tiles, ALLEGRO_DISPLAY* display) 272{ //creates a bitmap from tiles instead of drawing all tiles seperately, fucking awesome ryte? 273 al_set_target_bitmap(BitTile); 274 for (int x = 0; x < 40; ++x) 275 { 276 for (int y = 0; y < 30; ++y) 277 { 278 /*switch (Tiles[x][y]) 279 { 280 case 0: 281 al_draw_bitmap_region(tiles, 0, 0, 32, 32, x*32, y*32, NULL); 282 break; 283 case 1: 284 al_draw_bitmap_region(tiles, 32, 0, 32, 32, x*32, y*32, NULL); 285 break; 286 } */ 287 al_draw_bitmap_region(tiles, Tiles[x][y]*32, 0, 32, 32, x*32, y*32, NULL); 288 } 289 } 290 al_set_target_bitmap(al_get_backbuffer(display)); 291 292 // al_save_bitmap("Screenshot.png", BitTile); 293} 294bool Collision(int Tiles[][30], const int& x, const int& y) 295{ 296 if (Tiles[x][y] == 0) 297 return true; 298 return false; 299} 300 301//cPlayer.h 302// 303// cPlayer.h 304// Allegro 305// 306// Created by Angel Bates on 10/27/12. 307// Copyright (c) 2012 Angel Bates. All rights reserved. 308// 309 310#ifndef Allegro_cPlayer_h 311#define Allegro_cPlayer_h 312 313enum Directions { LEFT, RIGHT, UP, DOWN }; 314 315 316class cPlayer { 317public: 318 cPlayer(const int& i_X, const int& i_Y):m_X(i_X), m_Y(i_Y), m_SmallFrame(0), m_Dir(LEFT) { } 319 320 void Tick() { (m_SmallFrame == 1) ? m_SmallFrame = 0 : m_SmallFrame = 1; } 321 322 int GetFrame() { return m_SmallFrame; } 323 324 int GetX() { return m_X; } 325 int GetY() { return m_Y; } 326 327 void SetCords(const int& i_X, const int& i_Y) { m_X = i_X; m_Y = i_Y; } 328 329 void Move() 330 { 331 switch(m_Dir) 332 { 333 case LEFT: 334 --m_X; 335 break; 336 case RIGHT: 337 ++m_X; 338 break; 339 case UP: 340 --m_Y; 341 break; 342 case DOWN: 343 ++m_Y; 344 break; 345 } 346 } 347 348 Directions GetDir() { return m_Dir; } 349 350 void SetDir(const Directions& i_Dir) { m_Dir = i_Dir; } 351 352 353 354private: 355 int m_X, m_Y; 356 int m_SmallFrame; 357 Directions m_Dir; 358 359 360}; 361 362 363#endif

Heres my code to a simple Tilemap application thingy, it loads a map thats 32*40, 32*30 and instead of drawing each tile separately (like you do) it makes a bitmap from the tiles, which can be easily updated/drawn - reducing cpu% from 30-50 to around 4%
You can edit it/do whatever you want with it if it helps :P
http://i.imgur.com/11tVr.png

EnClave

Thanks alot Angeljruiz!

I will have to read through this code for a VERY long time :)
i recognize alot of it but im very new to programming as you can probebly see, thanks alot tho for sharing your code with me to help me with my problem :)

ifstream Map("Map1.txt");
Hmm, getting error, dont really know where you are going with it, think its a typo but cant figure out what it should say.

Kris Asick

I was thinking about going with this approach with my current game, but my maps are 1024x1024 tiles big and the tiles are 16x16 pixels. This translates to a 16384x16384 image which would take up 1 GB of video RAM. :o

...yeah, had to take a different approach, more like how the NES works. ;)

EnClave

Yeah my code wasent really optimal, but thats fine i love to learn, so people that could improve my knowledge i really appreciate it, tho right now im having problem with the ifstream Map("Map1.txt"); in Angel's Code, hmm like i said my Knowledge is really thin im learning new things everyday!

Angeljruiz

Yeahh making it into a bitmap only works if the map isnt that big :p
But im not at my house right now so i cant give you the map but the format is really easy its just 0-4 40 number wide and 30 numbers high, tiles with the #0 are solid the rest arent
I guess ill give you the sprites to go with it too :p

@Kris - how did you load/display your tiles?

EnClave

yeah that would be great Angel so i could have a look how everything is working together, its just in the code the ifstream map("map1.txt");

The ifstream is a typo isent it? because i get an error,if stream map,fstream map, doesent work either,ive tried to think what it should say but im stuck there :(

Angeljruiz

I dont know why you would be getting a error like that. If you didnt already know ifstream is a (i)nput (f)ile stream. Its used to open read/write to files. Its functions are declared in the fstream header so if you have #include <fstream> in your code it should work

EnClave

That is extremly odd, yeah i got <fstream> included, and thank you for explaining what ifstream does, hmm... this is very odd...

void loadmap(int Tiles[][30])
{
    int x(0), y(0);
    int temp;
  ifstream map("map.txt");
    if (Map.is_open())

ifstream map("map.txt"); <---- identifier ifstream is Undefined
if (Map.is_open()) <---- identifier "map" is undefined

Angeljruiz

Im not sure exactly... If you have the header file included and using namespace std right under it and you're compiling it as a c++ file, not a c file, it should work

EnClave

Alright i got the ifstream to work now, the problem was i forgot the "using namespace std;" :P stupid noob mistake, now i get the 'map' undeclared identifier, so i gotta look why it is undeclared, i must have forgot something in the code, Anyway if you could attache the sprite image and the .txt so i could just have a look at it how it works i would appreciate it a ton! When your at your home that is. :) thanks again Angel

Angeljruiz

You have ifstream map and Map.is_open() , there capitalized differently :p
Ill be home i about 2 hours but you could easily make the map.txt file by just filling 40x30 with ones and just use any sprite for the character

EnClave

Yeah thank you i missed the small M so now my code runns into NO errors, tho nothing shows up when i use the txt file, to be exact im writing the 1's like this:

1 1 1 1 1 1
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 1 1 1

Anyway the same problem like my own code, nothing shows up.
and im sorry i meant the tiles not the sprites before, i think im messing something up in my .txt file or my tiles.png file and thats why nothin shows up, so ill just wait until you get the time to send me your files so i can have a look what i'm doing wrong here :(

Angeljruiz

map1.txt

#SelectExpand
10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 0 30 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 0 40 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 0 50 2 2 2 0 0 0 0 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 0 60 2 2 2 0 0 0 0 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 0 70 2 2 2 2 2 0 0 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1 0 80 2 2 2 2 2 0 0 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1 0 90 2 2 2 2 2 0 0 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1 1 0 100 2 2 2 2 2 0 0 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1 1 1 0 110 2 2 0 2 2 0 0 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1 1 1 0 120 2 2 0 0 0 0 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1 1 1 1 0 130 2 2 0 0 0 0 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1 1 1 1 1 0 140 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 150 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 160 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 170 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 180 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 190 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 1 1 1 1 1 1 1 1 1 1 1 0 200 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 1 1 1 1 1 1 1 1 1 1 0 210 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 1 1 1 1 1 1 1 1 1 0 220 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 1 1 1 1 1 1 1 1 1 0 230 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 1 1 1 1 1 1 1 1 0 240 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 1 1 1 1 1 1 0 250 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 1 1 1 1 1 0 260 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 1 1 1 1 0 270 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 1 1 1 0 280 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 1 1 0 290 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 1 0 300 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

http://art.devsader.com/content/contributions/sprites/BoyKnight_Moosader.png <- Save as Player.png
http://art.devsader.com/content/contributions/textures/FloorTiles_Moosader.png <- Save as Tiles.png

And uncomment the first set of switch statements and comment out the 2nd set

EDIT:
Also go check out moosader.com , its where i got the sprites from (they have no copy right so you can do whatever with them) , it has alot of free sprites and tutorials, its actually what got me coding

EnClave

Alright, thanks alot Angel, trying everything out right now, i get the concept now ive learned ALOT by starting this Topic, i see now that i have to make every tiles 32x32 and then the program will just crop 1 image 32x32 and assigning a numer to the tile right so lets say, 1 = grass tile, if i assigne it to be the first 32x32 in the png to 1?

Also i get this error now, Debug error R6010 Abort. i have no idea why im getting it i guess something has the wrong value in my code, ill just post my new code if anyone ever got time to throw an eye on it.

[EDIT] When i switch the position of the :
loadmap(Tiles);
updateBitTile(Tiles, BitTile, tiles, display);
to higher up in the code the Error R6010 abort is gone, but still no Tiles are shown, just a black window with my lonely Character sprite :(, tho if i uncomment the Switch code Case 0 and Case 1, and then comment out the al_draw_bitmap_region(tiles, Tiles[x][y]*32, 0, 32, 32, x*32, y*32, NULL);
i get no error but still no tile map, if i uncomment the:
al_draw_bitmap_region(tiles, Tiles[x][y]*32, 0, 32, 32, x*32, y*32, NULL);
i get Error again, same one R6010, hmmm

#SelectExpand
1#include <allegro5\allegro_native_dialog.h> 2#include <allegro5\allegro_primitives.h> 3#include <allegro5\allegro_image.h> 4#include <fstream> 5#include <string> 6#include <sstream> 7#include <iostream> 8#include <allegro5\allegro5.h> 9 10using namespace std; 11 12#define ScreenWidth 1024 13#define ScreenHeight 768 14 15int loadCounterX = 0, loadCounterY = 0, mapSizeX, mapSizeY; 16 17void loadmap(int Tiles[][30]); 18void updateBitTile(int Tiles[][30], ALLEGRO_BITMAP* BitTile, ALLEGRO_BITMAP* tiles, ALLEGRO_DISPLAY* display); 19 20// %%%%% TILE MAP LOADING TXT FILES %%%%%// 21 22 // %%%%% CAMERA SCROLLING %%%%%// 23void cameraUpdate(float *cameraPosition, float x, float y, int width, int height) 24{ 25 cameraPosition[0] = -(ScreenWidth / 2) + (x + width / 2); 26 cameraPosition[1] = -(ScreenWidth / 2) + (y + height / 2); 27 28 if(cameraPosition[0] < 0) 29 cameraPosition[0] = 0; 30 if(cameraPosition[1] < 0) 31 cameraPosition[1] = 0; 32 33} 34 35 36int main() 37{ 38 // %%%%% IMPORTANT SETTINGS %%%%%// 39 ALLEGRO_DISPLAY *display; 40 ALLEGRO_BITMAP* tiles = NULL; 41 ALLEGRO_BITMAP* BitTile = NULL; 42 const float FPS = 60.0; 43 const float frameFPS = 10.0; 44 enum Direction { DOWN, LEFT, RIGHT, UP }; 45 46 47 48 49 // %%%%% ERROR CODES, ALLEGRO AND DISPLAY %%%%%// 50 if(!al_init()) 51 al_show_native_message_box(NULL, "Error", NULL, "Could not Initialize Allegro", NULL, NULL); 52 53 display = al_create_display(ScreenWidth, ScreenHeight); 54 55 if(!display) 56 al_show_native_message_box(NULL, "Error", NULL, "Could not create Allegro Display", NULL, NULL); 57 58 59 // %%%%% WINDOW SETTINGS %%%%%// 60 61 al_set_new_display_flags(ALLEGRO_WINDOWED); 62 al_set_window_position(display, 200, 200); 63 al_set_window_title(display, "PRE-alpha Test"); 64 65 ALLEGRO_COLOR playerColor = al_map_rgb(255, 0, 255); 66 67 68 // %%%%% Movement SPEED %%%%%// 69 bool done = false, draw = true, active = false; 70 float x = 512, y = 384, moveSpeed = 3; 71 int dir = DOWN, sourceX = 64, sourceY = 96; 72 float cameraPosition[2] = { 0, 0 }; 73 74 75 // %%%%% ADDONS %%%%%// 76 al_init_primitives_addon(); 77 al_install_keyboard(); 78 al_init_primitives_addon(); 79 al_install_mouse(); 80 al_init_image_addon(); 81 82 83 BitTile = al_create_bitmap(1280, 960); 84 tiles = al_load_bitmap("Tiles.png"); 85 86 ALLEGRO_BITMAP *player = al_load_bitmap("player.png"); 87 /*ALLEGRO_BITMAP *background = al_load_bitmap("backgroundtest.png");*/ 88 al_convert_mask_to_alpha(player, al_map_rgb(255, 0, 255)); 89 ALLEGRO_KEYBOARD_STATE KeyState; 90 ALLEGRO_TRANSFORM camera; 91 92 int Tiles[40][30]; 93 94 for (int x = 0; x < 40; ++x) 95 { 96 for (int y = 0; y < 30; ++y) 97 { 98 Tiles[x][y] = 0; 99 } 100 } 101 102 loadmap(Tiles); 103 updateBitTile(Tiles, BitTile, tiles, display); 104 105 // %%%%% EVENTS installation & TIMERS %%%%%// 106 ALLEGRO_TIMER *timer = al_create_timer(1.0 / FPS); 107 ALLEGRO_TIMER *frametimer = al_create_timer(1.0 / frameFPS); 108 109 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); 110 al_register_event_source(event_queue, al_get_keyboard_event_source()); 111 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 112 al_register_event_source(event_queue, al_get_timer_event_source(frametimer)); 113 al_register_event_source(event_queue, al_get_display_event_source(display)); 114 al_register_event_source(event_queue, al_get_mouse_event_source()); 115 116 117 118 119 120 // %%%%% TIMER, NO installations UNDER THIS %%%%%// 121 al_start_timer(timer); 122 al_start_timer(frametimer); 123 124 // %%%%% Beginning of Events %%%%%// 125 126 127 128 while(!done) 129 { 130 ALLEGRO_EVENT events; 131 al_wait_for_event(event_queue, &events); 132 al_get_keyboard_state(&KeyState); 133 134 135 // %%%%% TILE MAP IN THE EVENT & TIMER CODE %%%%%// 136 137 if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 138 { 139 done = true; 140 } 141 else if (events.type == ALLEGRO_EVENT_TIMER) 142 { 143 144 } 145 146 // %%%%% KEYBOARD INPUT AND EVENTS AND PLAYER IMAGE AND CAMERA SCROLLING FOR CHARACTER %%%%%// 147 if(events.type == ALLEGRO_EVENT_KEY_UP) 148 { 149 switch(events.keyboard.keycode) 150 { 151 case ALLEGRO_KEY_ESCAPE: 152 done = true; 153 } 154 } 155 else if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 156 { 157 done = true; 158 } 159 else if (events.type = ALLEGRO_EVENT_TIMER) 160 { 161 if(events.timer.source == timer) 162 { 163 active = true; 164 if(al_key_down(&KeyState, ALLEGRO_KEY_S)) 165 { 166 y += moveSpeed; 167 dir = DOWN; 168 } 169 else if(al_key_down(&KeyState, ALLEGRO_KEY_W)) 170 { 171 y -= moveSpeed; 172 dir = UP; 173 } 174 else if(al_key_down(&KeyState, ALLEGRO_KEY_D)) 175 { 176 x += moveSpeed; 177 dir = RIGHT; 178 } 179 else if(al_key_down(&KeyState, ALLEGRO_KEY_A)) 180 { 181 x -= moveSpeed; 182 dir = LEFT; 183 } 184 else 185 active = false; 186 187 cameraUpdate(cameraPosition, x, y, 64, 96); 188 al_identity_transform(&camera); 189 al_translate_transform(&camera, -cameraPosition[0], -cameraPosition[1]); 190 al_use_transform(&camera); 191 192 } 193 194 else if(events.timer.source == frametimer) 195 { 196 197 if(active) 198 sourceX += al_get_bitmap_width(player) / 4; 199 200 else 201 sourceX = 64; 202 203 if(sourceX >= al_get_bitmap_width(player)) 204 sourceX = 0; 205 206 sourceY = dir; 207 } 208 draw = true; 209 } 210 211 if(draw) 212 { 213 214 215 /*al_draw_bitmap(background, 0, 0, NULL);*/ 216 217 al_draw_bitmap_region(player, sourceX, dir * al_get_bitmap_height(player) / 4, 64, 96, x, y, NULL); 218 219 al_flip_display(); 220 al_clear_to_color(al_map_rgb(0, 0, 0)); 221 222 223 } 224 225 226 // %%%%% GAME UI SHELL %%%%%// 227 /*al_draw_rectangle(1, 1, 800, 100, al_map_rgb(255, 50, 50), 9.0);*/ 228 229 // %%%%% Destroy ENDING %%%%%// 230 } 231 232 al_destroy_bitmap(tiles); 233 al_destroy_bitmap(BitTile); 234 al_destroy_display(display); 235 al_destroy_timer(timer); 236 al_destroy_timer(frametimer); 237 al_destroy_bitmap(player); 238 /*al_destroy_bitmap(background);*/ 239 al_destroy_event_queue(event_queue); 240 241 242 return 0; 243} 244 245 246void loadmap(int Tiles[][30]) 247{ 248 int x(0), y(0); 249 int temp; 250 ifstream Map("map.txt"); 251 252 if (Map.is_open()) 253 { 254 while(Map >> temp) 255 { 256 Tiles[x][y] = temp; 257 ++x; 258 if (x == 40) 259 { 260 x = 0; 261 ++y; 262 } 263 } 264 } 265 Map.close(); 266} 267 268void updateBitTile(int Tiles[][30], ALLEGRO_BITMAP* BitTile, ALLEGRO_BITMAP* tiles, ALLEGRO_DISPLAY* display) 269{ //creates a bitmap from tiles instead of drawing all tiles seperately, fucking awesome ryte? 270 al_set_target_bitmap(BitTile); 271 for (int x = 0; x < 40; ++x) 272 { 273 for (int y = 0; y < 30; ++y) 274 { 275 switch (Tiles[x][y]) 276 { 277 case 0: 278 al_draw_bitmap_region(tiles, 0, 0, 32, 32, x*32, y*32, NULL); 279 break; 280 case 1: 281 al_draw_bitmap_region(tiles, 32, 0, 32, 32, x*32, y*32, NULL); 282 break; 283 } 284 /* al_draw_bitmap_region(tiles, Tiles[x][y]*32, 0, 32, 32, x*32, y*32, NULL);*/ 285 } 286 } 287 al_set_target_bitmap(al_get_backbuffer(display)); 288 289 // al_save_bitmap("Screenshot.png", BitTile); 290}

Kris Asick

@Kris - how did you load/display your tiles?

Loading's easy. Just have them all together on a single bitmap and load it in, then create sub-bitmaps off of it for sake of simplicity for each kind of tile.

The maps themselves are procedurally generated at present, though eventually I have to incorporate saving and loading of them since a single run through my game will take several hours.

But as for displaying them, I took a page out of the book of the NES. Older game consoles don't have the power to constantly refresh the tiles on-screen, nor the memory to store an active bitmap of an entire map/level, but because these older systems were specifically designed around a tile/sprite concept, they have a tile page (sometimes referred to as the "name page") where a series of tiles can be stored and this page can be drawn to the screen from any spot, wrapping around at its edges. This way, only tiles that have freshly come on screen need to actually be updated, and the rest are simply there already from previous drawing operations.

This is essentially the same approach I've taken. I have a tile page which is essentially a 4096x4096 texture (largest safe size, takes up 64 MB of video RAM) that wraps around and only has tiles written to it when they change or come into view. At present, the only way to pull this off with Allegro is using the primitives add-on, as textures will not wrap with the standard bitmap functions. To know how many rows/columns to draw, I track positional data relative to the edges of a camera frame and as this frame moves, the system tracks how many new rows and columns worth of tiles to draw. I can also request the system to redraw the entire tile page, which is necessary when first booting in or if the camera needs to jump to a new spot immediately.

This is definitely the hardest way to do it, but offers the best performance possible. With very small tile sizes like I'm using, this method is a must to avoid the overhead of thousands of tile drawing calls per frame. With typical tile sizes though, it doesn't make a huge difference on modern hardware.

Angeljruiz

Ohh thats really cool, ill have to check that out. Could you give some sample code?

As for EnClave it seems like you never actually draw the tiles, just the character
Just draw the BitTiles bitmap as you would normally with its x and y positions as 0 minus the offset values (OffsetX and OffsetY in my code)

EnClave

Its wierd tho Angel even if i copy your code straight off, if i save the Tiles, and Knight png to my project folder, if i create a cPlayer class in Header file and check that no errors accure when i debug the program i get ERROR R6010 Abort, and i cant do anything with it, thats what is very wierd...I know im being a Pain in the ass right now but i seriously cant figure it out... been trying to fix the Tilemaps 3 different ways now i either get nothing or Error R6010... i dont get it :(

Angeljruiz

Alright try this

#SelectExpand
1//main.cpp 2#include <iostream> 3#include <fstream> 4#include <allegro5/allegro.h> 5#include <allegro5/allegro_primitives.h> 6#include <allegro5/allegro_image.h> 7#include "cPlayer.h" 8 9using namespace std; 10 11int loadmap(int Tiles[][30]); 12void updateBitTile(int Tiles[][30], ALLEGRO_BITMAP* BitTile, ALLEGRO_BITMAP* tiles, ALLEGRO_DISPLAY* display); 13bool Collision(int Tiles[][30], const int& x, const int& y); 14 15int main(int argc, char** argv) 16{ 17 ALLEGRO_DISPLAY* display = NULL; 18 ALLEGRO_EVENT_QUEUE* queue = NULL; 19 ALLEGRO_TIMER* timer = NULL; 20 ALLEGRO_BITMAP* player = NULL; 21 ALLEGRO_BITMAP* tiles = NULL; 22 ALLEGRO_BITMAP* BitTile = NULL; 23 24 al_init(); 25 al_install_keyboard(); 26 al_init_primitives_addon(); 27 al_init_image_addon(); 28 29 display = al_create_display(640, 480); 30 queue = al_create_event_queue(); 31 timer = al_create_timer(1.0 / 120); 32 player = al_load_bitmap("Player.png"); 33 if (!player) { 34 cout << "Bad Player.png\n"; 35 return -1; 36 } 37 tiles = al_load_bitmap("Tiles.png"); 38 if (!tiles) { 39 cout << "Bad Tiles.png\n"; 40 return -1; 41 } 42 BitTile = al_create_bitmap(1280, 960); 43 44 45 bool Exit = false; 46 bool Redraw = true; 47 bool KeyDown = false; 48 49 bool kLeft = false; 50 bool kRight = false; 51 bool kUp = false; 52 bool kDown = false; 53 54 55 56 int Counter = 0; 57 58 int OffsetX = 0; 59 int OffsetY = 0; 60 61 int Tiles[40][30]; 62 63 for (int x = 0; x < 40; ++x) 64 { 65 for (int y = 0; y < 30; ++y) 66 { 67 Tiles[x][y] = 0; 68 } 69 } 70 71 if (loadmap(Tiles) == -1) 72 { 73 return -1; 74 } 75 updateBitTile(Tiles, BitTile, tiles, display); 76 77 cPlayer Player(640/2-16, 480/2-16); 78 79 al_register_event_source(queue, al_get_display_event_source(display)); 80 al_register_event_source(queue, al_get_keyboard_event_source()); 81 al_register_event_source(queue, al_get_timer_event_source(timer)); 82 83 al_start_timer(timer); 84 85 while (!Exit) 86 { 87 ALLEGRO_EVENT ev; 88 al_wait_for_event(queue, &ev); 89 90 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) 91 { 92 switch (ev.keyboard.keycode) 93 { 94 case ALLEGRO_KEY_ESCAPE: 95 Exit = true; 96 break; 97 case ALLEGRO_KEY_LEFT: 98 Player.SetDir(LEFT); 99 KeyDown = true; 100 kLeft = true; 101 Redraw = true; 102 break; 103 case ALLEGRO_KEY_RIGHT: 104 Player.SetDir(RIGHT); 105 KeyDown = true; 106 kRight = true; 107 Redraw = true; 108 break; 109 case ALLEGRO_KEY_UP: 110 Player.SetDir(UP); 111 KeyDown = true; 112 kUp = true; 113 Redraw = true; 114 break; 115 case ALLEGRO_KEY_DOWN: 116 Player.SetDir(DOWN); 117 KeyDown = true; 118 kDown = true; 119 Redraw = true; 120 break; 121 } 122 } 123 124 if (ev.type == ALLEGRO_EVENT_KEY_UP) 125 { 126 switch (ev.keyboard.keycode) 127 { 128 case ALLEGRO_KEY_LEFT: 129 kLeft = false; 130 break; 131 case ALLEGRO_KEY_RIGHT: 132 kRight = false; 133 break; 134 case ALLEGRO_KEY_UP: 135 kUp = false; 136 break; 137 case ALLEGRO_KEY_DOWN: 138 kDown = false; 139 break; 140 } 141 if (!kRight && !kUp && !kDown && !kLeft) 142 KeyDown = false; 143 } 144 145 if (ev.type == ALLEGRO_EVENT_TIMER) 146 { 147 switch (Player.GetDir()) 148 { 149 case LEFT: 150 if (KeyDown && !Collision(Tiles, (Player.GetX() - 1) / 32, (Player.GetY()) / 32 ) && 151 !Collision(Tiles, (Player.GetX() - 1) / 32, (Player.GetY() + 31 ) / 32 ) ) 152 { 153 Player.Move(); 154 ++Counter; 155 --OffsetX; 156 Redraw = true; 157 } 158 break; 159 case RIGHT: 160 if (KeyDown && !Collision(Tiles, (Player.GetX() + 32) / 32, (Player.GetY()) / 32 ) && 161 !Collision(Tiles, (Player.GetX() + 32) / 32, (Player.GetY() + 31) / 32 ) ) 162 { 163 Player.Move(); 164 ++Counter; 165 ++OffsetX; 166 Redraw = true; 167 } 168 break; 169 case UP: 170 if (KeyDown && !Collision(Tiles, (Player.GetX() + 1) / 32, (Player.GetY() - 1) / 32 ) && 171 !Collision(Tiles, (Player.GetX() + 31) / 32, (Player.GetY() + -1) / 32 ) ) 172 { 173 Player.Move(); 174 ++Counter; 175 --OffsetY; 176 Redraw = true; 177 } 178 break; 179 case DOWN: 180 if (KeyDown && !Collision(Tiles, (Player.GetX() + 1) / 32, (Player.GetY() + 32) / 32 ) && 181 !Collision(Tiles, (Player.GetX() + 31) / 32, (Player.GetY() + 32) / 32 ) ) 182 { 183 Player.Move(); 184 ++Counter; 185 ++OffsetY; 186 Redraw = true; 187 } 188 break; 189 } 190 if (Counter == 30) 191 { 192 Player.Tick(); 193 Redraw = true; 194 Counter = 0; 195 } 196 197 } 198 199 if (al_event_queue_is_empty(queue) && Redraw) 200 { 201 al_clear_to_color(al_map_rgb(250, 250, 250)); 202 al_draw_bitmap(BitTile, 0-OffsetX, 0-OffsetY, NULL); 203 switch (Player.GetDir()) { //These switch statements are for Player.png 204 case LEFT: 205 al_draw_bitmap_region(player, Player.GetFrame() ? 32 : 0, 64, 32, 32, Player.GetX()-OffsetX, Player.GetY()-OffsetY, NULL); 206 break; 207 208 case RIGHT: 209 al_draw_bitmap_region(player, Player.GetFrame() ? 32 : 0, 96, 32, 32, Player.GetX()-OffsetX, Player.GetY()-OffsetY, NULL); 210 break; 211 212 case UP: 213 al_draw_bitmap_region(player, Player.GetFrame() ? 32 : 0, 32, 32, 32, Player.GetX()-OffsetX, Player.GetY()-OffsetY, NULL); 214 break; 215 216 case DOWN: 217 al_draw_bitmap_region(player, Player.GetFrame() ? 32 : 0, 0, 32, 32, Player.GetX()-OffsetX, Player.GetY()-OffsetY, NULL); 218 break; 219 220 } 221// switch (Player.GetDir()) { //these switch statements are for Knight.png 222// case LEFT: 223// al_draw_bitmap_region(player, Player.GetFrame() ? 20 : 13 , 64, 20, 32, Player.GetX()-OffsetX, Player.GetY()-OffsetY, NULL); 224// break; 225// 226// case RIGHT: 227// al_draw_bitmap_region(player, Player.GetFrame() ? 32 : 0, 96, 32, 32, Player.GetX()-OffsetX, Player.GetY()-OffsetY, NULL); 228// break; 229// 230// case UP: 231// al_draw_bitmap_region(player, Player.GetFrame() ? 32 : 0, 32, 32, 32, Player.GetX()-OffsetX, Player.GetY()-OffsetY, NULL); 232// break; 233// 234// case DOWN: 235// al_draw_bitmap_region(player, Player.GetFrame() ? 62 : 14, 8, 20, 32, Player.GetX()-OffsetX, Player.GetY()-OffsetY, NULL); 236// break; 237// 238// } 239 240 al_draw_pixel((304-Player.GetX())/32, (224-Player.GetY())/32, al_map_rgb(0, 0, 250)); 241 al_flip_display(); 242 Redraw = false; 243 } 244 } 245 246 al_destroy_timer(timer); 247 al_destroy_display(display); 248 al_destroy_event_queue(queue); 249 al_destroy_bitmap(player); 250 al_destroy_bitmap(tiles); 251 al_destroy_bitmap(BitTile); 252 253 return 0; 254} 255int loadmap(int Tiles[][30]) 256{ 257 int x(0), y(0); 258 int temp; 259 ifstream Map("Map1.txt"); 260 if (Map.is_open()) 261 { 262 while(Map >> temp) 263 { 264 Tiles[x][y] = temp; 265 ++x; 266 if (x == 40) 267 { 268 x = 0; 269 ++y; 270 } 271 } 272 } else { 273 cout << "Bad Map1.txt\n"; 274 return -1; 275 } 276 Map.close(); 277 return 0; 278} 279void updateBitTile(int Tiles[][30], ALLEGRO_BITMAP* BitTile, ALLEGRO_BITMAP* tiles, ALLEGRO_DISPLAY* display) 280{ //creates a bitmap from tiles instead of drawing all tiles seperately, fucking awesome ryte? 281 al_set_target_bitmap(BitTile); 282 for (int x = 0; x < 40; ++x) 283 { 284 for (int y = 0; y < 30; ++y) 285 { 286 /*switch (Tiles[x][y]) 287 { 288 case 0: 289 al_draw_bitmap_region(tiles, 0, 0, 32, 32, x*32, y*32, NULL); 290 break; 291 case 1: 292 al_draw_bitmap_region(tiles, 32, 0, 32, 32, x*32, y*32, NULL); 293 break; 294 } */ 295 al_draw_bitmap_region(tiles, Tiles[x][y]*32, 0, 32, 32, x*32, y*32, NULL); 296 } 297 } 298 al_set_target_bitmap(al_get_backbuffer(display)); 299 300 // al_save_bitmap("Screenshot.png", BitTile); 301} 302bool Collision(int Tiles[][30], const int& x, const int& y) 303{ 304 if (Tiles[x][y] == 0) 305 return true; 306 return false; 307}

#SelectExpand
1// 2// cPlayer.h 3// Allegro 4// 5// Created by Angel Bates on 10/27/12. 6// Copyright (c) 2012 Angel Bates. All rights reserved. 7// 8 9#ifndef Allegro_cPlayer_h 10#define Allegro_cPlayer_h 11 12enum Directions { LEFT, RIGHT, UP, DOWN }; 13 14 15class cPlayer { 16public: 17 cPlayer(const int& i_X, const int& i_Y):m_X(i_X), m_Y(i_Y), m_SmallFrame(0), m_Dir(LEFT) { } 18 19 void Tick() { (m_SmallFrame == 1) ? m_SmallFrame = 0 : m_SmallFrame = 1; } 20 21 int GetFrame() { return m_SmallFrame; } 22 23 int GetX() { return m_X; } 24 int GetY() { return m_Y; } 25 26 void SetCords(const int& i_X, const int& i_Y) { m_X = i_X; m_Y = i_Y; } 27 28 void Move() 29 { 30 switch(m_Dir) 31 { 32 case LEFT: 33 --m_X; 34 break; 35 case RIGHT: 36 ++m_X; 37 break; 38 case UP: 39 --m_Y; 40 break; 41 case DOWN: 42 ++m_Y; 43 break; 44 } 45 } 46 47 Directions GetDir() { return m_Dir; } 48 49 void SetDir(const Directions& i_Dir) { m_Dir = i_Dir; } 50 51 52 53private: 54 int m_X, m_Y; 55 int m_SmallFrame; 56 Directions m_Dir; 57 58 59}; 60 61 62#endif

This code checks for bad sprites, which is something i shoulda done in the the first place, and the collision is better now.
Just make sure that the sprites are in the same directory as the executable, just because its in the project folder doesnt mean its going to put it in right directory. Like in xcode you have to specifically tell it to copy the sprites to the executable folder. Hopefully this works X)

EnClave

Angel....You are my HERO! thank you for being so PATIENT with me, it FINNALY worked... it was such a stupid mistake that its to embarassing to tell ;D... Anyway Thank you.

:):):)

Angeljruiz

Haha no problem :D

Kris Asick

Ohh thats really cool, ill have to check that out. Could you give some sample code?

Nope! ;D

The big reason why I can't really is because the system is specifically tailored for the game I'm making. In order for the code to make sense, I would have to provide you with the entire rendering, mapping, and tile processing source code, which is not going to happen. :P

It also still has some minor bugs which I'm going to be addressing as development of my game continues. Also, because it's inteded to be a commercial indie title, I don't want to share routines that took an extreme amount of effort to code. At least, not until the game's out. ;)

What I can say though is that the key to making it work is creating a large texture for your tile page and treat camera positions on it with modulo values. (IE: For a 4096x4096 texture, your camera's XY coordinates on it would have fmod(coord,4096.0f) applied.) Then just use Allegro's primitives add-on to draw from the texture WITHOUT applying fmod to your source coordinates so that the texture wraps around its edges when drawing it to the screen.

If you only use the primitive functions to draw to the texture you can avoid having to fmod anything, but Allegro's bitmap drawing functions are far-better optimized for drawing lots of things at once, whereas with the primitives add-on you have to queue up all of your drawing ops into an array before you make them. (Though you can draw a ton more stuff per frame this way if you feel up to the challenge.)

It's a lot more complicated than that though and again, unless you're working with tile sizes so small that you have to draw over a thousand or two per frame, the performance boost is not worth the effort. Since I have to render 15,000 tiles per frame... yeah... ;D

EnClave

Kris or Angel or anyone?, i have a question, when i draw tiles from my TXT document it follows the 0 1 2 3 4 5 6 depending on my PNG file, the first 64x64 is grass so its = 0, then the other is a stone tile, then it automaticly = 1 because they are like that in the Tilesheet, but it only reads horizontal and now ive runned out of tiles in my png file because it only reads horizontal, i got 17 tiles right now each is 64x64, is there anyway to read the tiles that are under it so for an example:

The numbers represent a Tile in the PNG file.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 [STOP at the PNG file]
18 19 20 21 22 23 24 25 26 27 28 29 30 31 [is there anyway to continue like this?]

Thanks in advance,

EnClave

Kris Asick
  _tile_sheet = VZ_LoadBitmap("Textures","vz_tiles_1.png",ALLEGRO_VIDEO_BITMAP|ALLEGRO_MIN_LINEAR|ALLEGRO_MAG_LINEAR|ALLEGRO_NO_PREMULTIPLIED_ALPHA);
  if (_tile_sheet == NULL) FatalErrorMsg(8,display);
  for (z = 0; z < VZ_MAX_FRAMES; z++)
    if ((tile_bmp[z] = al_create_sub_bitmap(_tile_sheet,(z%51)*20+2,(z/51)*20+2,16,16)) == NULL) FatalErrorMsg(9,display);

This is my tile loading code. Typically, you want to modulo one coordinate by the number of tiles per col/row, and divide the other. In my case, each column goes up by 1 and each row by 51, so I modulo the X by 51 and divide the Y by 51, then just multiply each by the space between each tile. 8-)

I also extend my tiles by 2 pixels in both directions since I have to be prepared to scale them. In fact, this a good practice to do with ALL 2D graphics using hardware acceleration that need to be joined together out of pieces, like status bars and whatnot.

Audric

Kris, I see you use things like FatalErrorMsg(8,display); apparently with 8 or 9 to get information about the point where the problem was raised.
In case you don't know, compilers provide macros like _FILE_ and _LINE_ that expand into source filename and line number.
This allows writing macros like

#define FATAL_ERR Error_handler(__FILE__, __LINE__)

Then you can use the macro any number of time, each occurence will pass a different filename + line number.

Edit: GCC additionally provides _func_ which is the function's name.

Angeljruiz

Thats understandable kris :p Thanks for introducing me to a interesting concept tho :D

@Enclave what do you mean?

Kris Asick
Audric said:

In case you don't know, compilers provide macros like FILE and LINE that expand into source filename and line number.

Didn't know that... but it wouldn't really help me much anyways since I can actually use the MSVC10 debugger with A5 projects and the FatalErrorMsg() routine I developed is intended for end-users to know what went wrong if something does go wrong in the completed product. ;)

My only problem at the moment is that the popups produced by the FatalErrorMsg() routine are appearing BEHIND the Allegro window, and when I try to shut the program down with an exit() call it's turning Allegro off before calling my additional uninitialization routines, which crashes the program. Meh, I'll figure out how to make that all work soon enough, it's not a high priority right now. :P

Raidho36

Why don't use allegro native dialogs? They work fine. Unless there's a crash already happened, of course.

Kris Asick
Raidho36 said:

Why don't use allegro native dialogs? They work fine. Unless there's a crash already happened, of course.

I am. :(

void FatalErrorMsg (int errornum, ALLEGRO_DISPLAY *display)
{
  if (display != NULL) al_show_mouse_cursor(display);
  al_show_native_message_box(display, "Vectorzone - Fatal Error", _errormsg[errornum], NULL, NULL, ALLEGRO_MESSAGEBOX_ERROR);
  exit(1);
}

EnClave

this is my code to load tiles right now,

BlockSize = 64

#SelectExpand
1 2void LoadMap(const char *filename, std::vector< std::vector <int> > &map) 3{ 4 std::ifstream openfile(filename); 5 if(openfile.is_open()) 6 { 7 std::string line, value; 8 int space; 9 10 while(!openfile.eof()) 11 { 12 std::getline(openfile, line); 13 14 if(line.find("[TileSet]") != std::string::npos) 15 { 16 state = TileSet; 17 continue; 18 } 19 else if (line.find("[Map]") != std::string::npos) 20 { 21 state = Map; 22 continue; 23 } 24 25 switch(state) 26 { 27 case TileSet: 28 if(line.length() > 0) 29 tileSet = al_load_bitmap(line.c_str()); 30 break; 31 case Map: 32 std::stringstream str(line); 33 std::vector<int> tempVector; 34 35 while(!str.eof()) 36 { 37 std::getline(str, value, ' '); 38 if(value.length() > 0) 39 tempVector.push_back(atoi(value.c_str())); 40 } 41 map.push_back(tempVector); 42 break; 43 } 44 } 45 } 46 else 47 { 48 } 49} 50 51 52 53 //%%%%% RITAR UPP TILES %%%%%// 54 55void DrawMap(std::vector <std::vector <int> > map) 56{ 57 for(int i = 0; i < map.size(); i++) 58 { 59 for(int j = 0; j < map[i].size(); j++) 60 { 61al_draw_bitmap_region(tileSet, map[i][j] * BlockSize, 0, BlockSize, 64, j * BlockSize, 62 i * BlockSize, NULL); 63 } 64 } 65}

and I'm kind of loading the pngs from the .txt files, like this:

[TileSet]
test.png
[Map]
3 3 3 5 5 5 5 5 5
3 3 3 5 5 5 5 5 5
4 3 3 5 5 5 5 5 5

my problem is tho that in my Tilesheet my program can only load tiles horizontal right now my .png file is 1088x64 because it can only load horizontal but now i have runned out of space in the .png file, i want it to be example 1088x128 and the program can load the tiles on the second row aswell. :-/

Angeljruiz

Well in my code i fix that by keeping the x value and increasing it every time it reads a value and once it gets to the maximum tiles per row or whatever i reset it to 0 then increment the y value. Why dont you just do that ? :p
(obviously im not very good at explaining)

EnClave

The thing was i couldent implement your "Loading maps code" into my own code and i dont want to like straight copy your code because i dont recognize anything there hehe, hmm i really have water above my head right now, this is extremly complicated i might need to change this code, because i followed some guys tutorial but i dont understand it so well, damn, i just want a nice working Mapload function :(

Angeljruiz

But mine is so much simpler than yours, albeit maybe a little bit more cryptic looking X)
Heres a deep explaination of my code
To keep track of all the tiles i just use a simple 2d array of ints

int tiles[40][30];

I then open up a file then keep looping until it is the end of the file, keeping track of how many values have been stored, once 40 values (the number of how many tiles per row) has been stored it must mean that its time to increase the y value because all the tiles in that row are filled up.

//openfile bla bla bla
while (File >> temp) //loops until end
{
  Tiles[x][y] = temp;
  x++; //next tile in the row
  if (x == TILES_PER_ROW /*40*/) //all the rows are filled up
  {
    x = 0; //reset it
    y++; //next column
  }
}

Then, as ive already explained, i just make a bitmap out of the tiles then i just draw the bitmap as you would with any other bitmap.
Im not saying this is the best way to do this but its definately a good way to start off and get a feel of things.
Also i didnt compile this code, its only for example so if there are anything syntax errors thats why

EnClave

Yeah, actully i borrowed your code and implemented in my code, it runs good but now it just creates a bitmap and only using the:

BitTile = al_create_bitmap(800, 600);

so it doesent matter what i do in my .txt document it takes like the first tile in my png file and just copys it to 800 x 600, here is a screenshot

http://i.imgur.com/g7NkN.jpg

do you know why it does that?

Angeljruiz

Emm im not sure, maybe bad input? I know when you give bad input it just keeps repeating the last good input. Also check the updateBitMap function and make sure its actually drawing the tile that the input corrisponds to.

EnClave

Yeah this is extremly wierd can it have something to do with i changed your [40][30] to [100][100] because i want to make bigger maps?

anyway with some touching of the code i end up with this,

http://i.imgur.com/qq0KG.jpg

also there is the png file im using and also the update code, hmm

Kris Asick

One of the ways I debug a problem like this is to actually do all the logic, step by step, in my head, following along in my code. You can either do this in a forwards motion to determine what the result should be, or a backwards motion to determine what you need to start with to get the result you want.

However, I just noticed something obvious: You're only going up by increments of 32 when pulling tile references, not 64 like your tile sizes are. :P

I also noticed that tile #0 is a brick wall. Usually it's best to make tile #0 your empty, undefined tile for sake of if statements. ;)

EnClave

I actully havent had any luck on the Working TileMap yet, its still doesent update as it should do, it only creates a wierd big Tile of 800, 600 because its set like that in the BitTile = al_create_bitmap(800, 600);

Its extremly wierd, i dont know what to do anymore ive been experimenting with my code changed alot of values, switching locations of the updates but still nothing, just does the same thing, if anyone with sharper eyes and knowledge could spot something out i would really appreciate it, been working with this for 2 days now withouth any luck, also when i have Screen scrolling on my Game my Mouse event doesent work either, it works fine if i stay in place in the beginning of the game but when i start to move it doesent update as it should so it just lacks behind all the time, here is my code if anyone have the time to just look over it a quickie! :)

#SelectExpand
1 2#include<fstream> 3#include<string> 4#include<sstream> 5#include<iostream> 6#include<allegro5\allegro5.h> 7#include<allegro5\allegro_native_dialog.h> 8#include<allegro5\allegro_primitives.h> 9#include<allegro5\allegro_image.h> 10 11using namespace std; 12 13int loadmap(int Tiles[][30]); 14void updateBitTile(int Tiles[][30], ALLEGRO_BITMAP* BitTile, ALLEGRO_BITMAP* tiles, ALLEGRO_DISPLAY* display); 15bool Collision(int Tiles[][30], const int& x, const int& y); 16 17 18#define ScreenWidth 1024 19#define ScreenHeight 768 20 21int pos_y = ScreenWidth / 2; 22int pos_x = ScreenHeight / 2; 23 24 25// %%%%% COLLISION OF MOVEMENT HITTING BOX %%%%%// 26bool Collision(float x, float y, float ex, float ey, int width, int height) 27{ 28 if(x + width < ex || x > ex + width || y + height < ey || y > ey + height) 29 { 30 return false; 31 } 32 return true; 33} 34 35int state = NULL; 36 37// %%%%% CAMERA SCROLLING %%%%%// 38 39void cameraUpdate(float *cameraPosition, float x, float y, int width, int height) 40{ 41 cameraPosition[0] = -(ScreenWidth / 2) + (x + width / 2); 42 cameraPosition[1] = -(ScreenWidth / 2) + (y + height / 2); 43 44 if(cameraPosition[0] < 0) 45 cameraPosition[0] = 0; 46 if(cameraPosition[1] < 0) 47 cameraPosition[1] = 0; 48 49} 50 51 52int main() 53{ 54 // %%%%% IMPORTANT SETTINGS %%%%%// 55 ALLEGRO_DISPLAY *display; 56 const float FPS = 60.0; 57 const float frameFPS = 10.0; 58 enum Direction { DOWN, LEFT, RIGHT, UP }; 59 60 // %%%%% ERROR CODES, ALLEGRO AND DISPLAY %%%%%// 61 if(!al_init()) 62 al_show_native_message_box(NULL, "Error", NULL, "Could not Initialize Allegro", NULL, NULL); 63 64 display = al_create_display(ScreenWidth, ScreenHeight); 65 66 if(!display) 67 al_show_native_message_box(NULL, "Error", NULL, "Could not create Allegro Display", NULL, NULL); 68 69 70 // %%%%% WINDOW SETTINGS %%%%%// 71 72 al_set_new_display_flags(ALLEGRO_WINDOWED); 73 al_set_window_position(display, 200, 200); 74 al_set_window_title(display, "PRE-alpha Test"); 75 76 ALLEGRO_COLOR playerColor = al_map_rgb(255, 0, 255); 77 78 // %%%%% Movement SPEED %%%%%// 79 bool done = false, draw = true, active = false; 80 float x = 512, y = 384, moveSpeed = 3; 81 int dir = DOWN, sourceX = 64, sourceY = 96; 82 float cameraPosition[2] = { 0, 0 }; 83 84 // %%%%% ADDONS %%%%%// 85 al_init_primitives_addon(); 86 al_install_keyboard(); 87 al_init_primitives_addon(); 88 al_install_mouse(); 89 al_init_image_addon(); 90 91 92 // %%%%% PNGS AND OTHER THINGS TO INSTALL %%%%%// 93 94 ALLEGRO_EVENT_QUEUE* queue = NULL; 95 ALLEGRO_BITMAP *enemy1 = al_load_bitmap("Zanm1.png"); 96 ALLEGRO_BITMAP *player = al_load_bitmap("player.png"); 97 al_convert_mask_to_alpha(player, al_map_rgb(255, 0, 255)); 98 al_convert_mask_to_alpha(enemy1, al_map_rgb(255, 0, 255)); 99 100 queue = al_create_event_queue(); 101 ALLEGRO_KEYBOARD_STATE KeyState; 102 ALLEGRO_TRANSFORM camera; 103 ALLEGRO_BITMAP* BitTile = NULL; 104 ALLEGRO_BITMAP* tiles = NULL; 105 106 107 tiles = al_load_bitmap("test.png"); 108 if (!tiles) 109 { 110 cout << "Bad Tiles.png\n"; 111 return -1; 112 } 113 114 int Counter = 0; 115 int OffsetX = 0; 116 int OffsetY = 0; 117 118 int Tiles[40][30]; 119 120 for (int x = 0; x < 40; ++x) 121 { 122 for (int y = 0; y < 30; ++y) 123 { 124 Tiles[x][y] = 0; 125 } 126 } 127 128 if (loadmap(Tiles) == -1) 129 { 130 return -1; 131 } 132 BitTile = al_create_bitmap(800, 600); 133 134 updateBitTile(Tiles, BitTile, tiles, display); 135 136 137 138 // %%%%% EVENTS installation & TIMERS %%%%%// 139 ALLEGRO_TIMER *timer = al_create_timer(1.0 / FPS); 140 ALLEGRO_TIMER *frametimer = al_create_timer(1.0 / frameFPS); 141 142 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); 143 al_register_event_source(event_queue, al_get_keyboard_event_source()); 144 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 145 al_register_event_source(event_queue, al_get_timer_event_source(frametimer)); 146 al_register_event_source(event_queue, al_get_display_event_source(display)); 147 al_register_event_source(event_queue, al_get_mouse_event_source()); 148 149 // %%%%% TIMER, NO installations UNDER THIS %%%%%// 150 al_start_timer(timer); 151 al_start_timer(frametimer); 152 153 // %%%%% Beginning of Events %%%%%// 154 while(!done) 155 { 156 ALLEGRO_EVENT events; 157 al_wait_for_event(event_queue, &events); 158 al_get_keyboard_state(&KeyState); 159 160 // %%%%% TILE MAP IN THE EVENT & TIMER CODE %%%%%// 161 162 if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 163 { 164 done = true; 165 } 166 else if (events.type == ALLEGRO_EVENT_TIMER) 167 { 168 169 } 170 171 // %%%%% KEYBOARD INPUT AND EVENTS AND PLAYER IMAGE AND CAMERA SCROLLING FOR CHARACTER %%%%%// 172 173 if(events.type == ALLEGRO_EVENT_KEY_UP) 174 { 175 switch(events.keyboard.keycode) 176 177 { 178 case ALLEGRO_KEY_ESCAPE: 179 done = true; 180 } 181 } 182 183 184 else if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 185 { 186 done = true; 187 } 188 189 // %%%%% MOUSE %%%%%// 190 else if(events.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) 191 192 { 193 if(events.mouse.button & 1) 194 draw = !draw; 195 else if (events.mouse.button & 2) 196 draw = !draw; 197 } 198 199 else if(events.type == ALLEGRO_EVENT_MOUSE_AXES) 200 { 201 pos_x = events.mouse.x; 202 pos_y = events.mouse.y; 203 } 204 205 // %%%%% KEYBOARD %%%%%// 206 else if (events.type = ALLEGRO_EVENT_TIMER) 207 { 208 if(events.timer.source == timer) 209 { 210 active = true; 211 if(al_key_down(&KeyState, ALLEGRO_KEY_S)) 212 { 213 y += moveSpeed; 214 dir = DOWN; 215 } 216 else if(al_key_down(&KeyState, ALLEGRO_KEY_W)) 217 { 218 y -= moveSpeed; 219 dir = UP; 220 } 221 else if(al_key_down(&KeyState, ALLEGRO_KEY_D)) 222 { 223 x += moveSpeed; 224 dir = RIGHT; 225 } 226 else if(al_key_down(&KeyState, ALLEGRO_KEY_A)) 227 { 228 x -= moveSpeed; 229 dir = LEFT; 230 } 231 232 else 233 active = false; 234 235 if(Collision(x, y, 400, 400, 32, 32)) 236 { 237 if(dir == 0) 238 y -= moveSpeed; 239 else if(dir == 1) 240 x += moveSpeed; 241 else if(dir == 2) 242 x -= moveSpeed; 243 else if(dir == 3) 244 y += moveSpeed; 245 } 246 247 248 cameraUpdate(cameraPosition, x, y, 96, 64); 249 al_identity_transform(&camera); 250 al_translate_transform(&camera, -cameraPosition[0], -cameraPosition[1]); 251 al_use_transform(&camera); 252 253 } 254 255 256 else if(events.timer.source == frametimer) 257 { 258 259 if(active) 260 sourceX += al_get_bitmap_width(player) / 4; 261 262 else 263 sourceX = 64; 264 265 if(sourceX >= al_get_bitmap_width(player)) 266 sourceX = 0; 267 268 sourceY = dir; 269 } 270 271 draw = true; 272 } 273 274 275 if(al_event_queue_is_empty(queue) &&draw) 276 277 { 278 279 al_clear_to_color(al_map_rgb(0, 0, 0)); 280 al_draw_bitmap(BitTile, 0-OffsetX, 0-OffsetY, NULL); 281 al_draw_bitmap_region(enemy1, 0, 0, 92, 150, 400, 400, NULL); 282 al_draw_bitmap_region(player, sourceX, dir * al_get_bitmap_height(player) / 4, 64, 96, x, y, NULL); 283 al_draw_filled_rectangle(pos_x, pos_y, pos_x + 30, pos_y + 30, al_map_rgb(150, 150, 150)); 284 285 al_flip_display(); 286 } 287 288 289 290 // %%%%% GAME UI SHELL %%%%%// 291 /*al_draw_rectangle(1, 1, 800, 100, al_map_rgb(255, 50, 50), 9.0);*/ 292 293 // %%%%% Destroy ENDING %%%%%// 294 } 295 al_destroy_display(display); 296 al_destroy_timer(timer); 297 al_destroy_bitmap(player); 298 al_destroy_bitmap(enemy1); 299 al_destroy_event_queue(event_queue); 300 al_destroy_bitmap(tiles); 301 al_destroy_bitmap(BitTile); 302 303 return 0; 304} 305 306int loadmap(int Tiles[][30]) 307{ 308 int x(0), y(0); 309 int temp; 310 ifstream Map("Map1.txt"); 311 if (Map.is_open()) 312 { 313 while(Map >> temp) 314 { 315 Tiles[x][y] = temp; 316 ++x; 317 if (x == 40) 318 { 319 x = 0; 320 ++y; 321 } 322 } 323 } else { 324 cout << "Bad Map1.txt\n"; 325 return -1; 326 } 327 Map.close(); 328 return 0; 329} 330 331void updateBitTile(int Tiles[][30], ALLEGRO_BITMAP* BitTile, ALLEGRO_BITMAP* tiles, ALLEGRO_DISPLAY* display) 332{ //creates a bitmap from tiles instead of drawing all tiles seperately, fucking awesome ryte? 333 al_set_target_bitmap(BitTile); 334 for (int x = 0; x < 40; ++x) 335 { 336 for (int y = 0; y < 30; ++y) 337 { 338 /*switch (Tiles[x][y]) 339 { 340 case 0: 341 al_draw_bitmap_region(tiles, 0, 0, 32, 32, x*32, y*32, NULL); 342 break; 343 case 1: 344 al_draw_bitmap_region(tiles, 32, 0, 32, 32, x*32, y*32, NULL); 345 break; 346 } */ 347 al_draw_bitmap_region(tiles, Tiles[x][y]*32, 0, 32, 32, x*32, y*32, NULL); 348 } 349 } 350 al_set_target_bitmap(al_get_backbuffer(display)); 351 352 // al_save_bitmap("Screenshot.png", BitTile); 353} 354bool Collision(int Tiles[][30], const int& x, const int& y) 355{ 356 if (Tiles[x][y] == 0) 357 return true; 358 return false; 359}

Kris Asick

When I get EXTREMELY stuck with a problem like this, I like to add keyboard-sensitive stepping in my code.

In your case, what this would mean is adding some routines to your tilemap rendering code so that it only draws one tile, shows the result on-screen, then waits for you to press a key. Then it draws the next tile, waits for a keypress, repeat until you press a specific key to signal the program to stop running.

This would at least let you visually see how your tiles are being rendered one by one and you may be able to tell what the trouble is as a result.

J-Gamer

You don't need extra code to do that, just a debugger :D

Thread #611343. Printed from Allegro.cc