Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro5 Cant get Tilemaps to work.

This thread is locked; no one can reply to it. rss feed Print
 1   2 
Allegro5 Cant get Tilemaps to work.
EnClave
Member #14,661
October 2012

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
Member #5,313
December 2004
avatar

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
Member #14,661
October 2012

Thanks alot LennyLen,

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

Angeljruiz
Member #14,553
September 2012

#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
Member #14,661
October 2012

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
Member #1,424
July 2001

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. ;)

--- Kris Asick (Gemini)
--- http://www.pixelships.com

EnClave
Member #14,661
October 2012

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
Member #14,553
September 2012

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
Member #14,661
October 2012

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
Member #14,553
September 2012

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
Member #14,661
October 2012

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
Member #14,553
September 2012

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
Member #14,661
October 2012

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
Member #14,553
September 2012

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
Member #14,661
October 2012

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
Member #14,553
September 2012

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
Member #14,661
October 2012

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
Member #1,424
July 2001

@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.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Angeljruiz
Member #14,553
September 2012

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
Member #14,661
October 2012

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
Member #14,553
September 2012

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
Member #14,661
October 2012

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
Member #14,553
September 2012

Haha no problem :D

Kris Asick
Member #1,424
July 2001

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

--- Kris Asick (Gemini)
--- http://www.pixelships.com

EnClave
Member #14,661
October 2012

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

 1   2 


Go to: