[A5]Tilemap scrolling [Revisited]
Specter Phoenix

Well I've not done it the way you recommended in my last thread. That is for me to do later, I just wanted to get a camera working, and after following CodingMadeEasy's tutorial, I have done that. I will have to go back and change a few things, but figured I'd get input on it in case someone has advice that wasn't given in the previous thread or if they have advice after seeing this take. This code is horrendous, but fine for my experimenting right now.

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_image.h> 3#include <fstream> 4#include <vector> 5#include <iostream> 6 7using namespace std; 8 9const int TILE_SIZE = 32; 10const int PLAYER_SIZE = 32; 11const int SCREEN_W = 640; 12const int SCREEN_H = 480; 13const int MAP_X_SIZE = 30; 14const int MAP_Y_SIZE = 30; 15const float FPS = 60.0; 16 17enum MYKEYS{KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN}; 18 19void cameraUpdate(float *cameraPosition, float x, float y, int width, int height) 20{ 21 cameraPosition[0] = -(SCREEN_W / 2) + (x + width / 2); 22 cameraPosition[1] = -(SCREEN_H / 2) + (y + height / 2); 23 24 if(cameraPosition[0] < 0){ cameraPosition[0] = 0; } 25 if(cameraPosition[0] + SCREEN_W > TILE_SIZE * MAP_X_SIZE) { cameraPosition[0] = (TILE_SIZE * MAP_X_SIZE) - SCREEN_W; } 26 if(cameraPosition[1] < 0){ cameraPosition[1] = 0; } 27 if(cameraPosition[1] + SCREEN_H > TILE_SIZE * MAP_Y_SIZE){ cameraPosition[1] = (TILE_SIZE * MAP_Y_SIZE) - SCREEN_H; } 28} 29 30vector<ALLEGRO_BITMAP*> tiles; 31int map[MAP_X_SIZE][MAP_Y_SIZE] = { 32 {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4}, 33 {8, 3, 3, 3, 3, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 8}, 34 {8, 3, 3, 3, 3, 5,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56 {8, 3, 3, 3, 3, 3, 5, 3, 7, 7, 3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 7, 7, 7, 7, 3, 3, 3, 3, 3, 8}, 57 {8, 3, 3, 3, 3, 3, 5, 3, 7, 7, 3, 3, 3, 3, 3, 7, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 8}, 58 {8, 3, 3, 3, 3, 5, 5, 3, 7, 7, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 8}, 59 {8, 3, 3, 3, 3, 5, 5, 3, 7, 7, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 8}, 60 {8, 3, 3, 3, 3, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 8}, 61 {6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2} 62}; 63 64int main() 65{ 66 if(!al_init()) 67 { 68 cout << "Failed to init allegro\n"; 69 } 70 if(!al_install_keyboard()) 71 { 72 cout << "Failed to install keyboad\n"; 73 } 74 if(!al_init_image_addon()) 75 { 76 cout << "Failed to init image addon\n"; 77 } 78 79 ALLEGRO_BITMAP *sprite, *player; 80 ALLEGRO_DISPLAY *display; 81 ALLEGRO_EVENT_QUEUE *event_queue; 82 ALLEGRO_TIMER *timer; 83 84 ALLEGRO_TRANSFORM camera; 85 86 bool doexit = false; 87 bool redraw = false; 88 bool key[4] = {false, false, false, false}; 89 90 float cameraPosition[2] = {0, 0}; 91 92 timer = al_create_timer(1.0/FPS); 93 display = al_create_display(SCREEN_W, SCREEN_H); 94 event_queue = al_create_event_queue(); 95 96 al_set_window_title(display, "Tilemap Engine"); 97 al_set_window_position(display, 20, 20); 98 99 al_register_event_source(event_queue, al_get_display_event_source(display)); 100 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 101 al_register_event_source(event_queue, al_get_keyboard_event_source()); 102 103 sprite = al_load_bitmap("tilesheet.png"); 104 int tileOffset = 0; 105 106 for(int i = 0; i < 10; i++) 107 { 108 for(int j = 0; j < 2; j++) 109 { 110 tiles.push_back(al_create_sub_bitmap(sprite, i * TILE_SIZE, j * TILE_SIZE, TILE_SIZE, TILE_SIZE)); 111 tileOffset = tileOffset + TILE_SIZE; 112 } 113 } 114 115 player = al_load_bitmap("player.png"); 116 int playerX = SCREEN_W / 2, playerY = SCREEN_H / 2; 117 118 al_clear_to_color(al_map_rgb(0, 0, 0)); 119 al_flip_display(); 120 al_start_timer(timer); 121 122 while(!doexit) 123 { 124 ALLEGRO_EVENT ev; 125 al_wait_for_event(event_queue, &ev); 126 127 if(ev.type == ALLEGRO_EVENT_TIMER) 128 { 129 // logic 130 if(key[KEY_UP] && playerY >= 0) 131 { 132 playerY--; 133 } 134 if(key[KEY_DOWN] && playerY <= MAP_Y_SIZE * TILE_SIZE - PLAYER_SIZE) 135 { 136 playerY++; 137 } 138 if(key[KEY_LEFT] && playerX >= 0) 139 { 140 playerX--; 141 } 142 if(key[KEY_RIGHT] && playerX <= MAP_X_SIZE * TILE_SIZE - PLAYER_SIZE) 143 { 144 playerX++; 145 } 146 147 cameraUpdate(cameraPosition, playerX, playerY, PLAYER_SIZE, PLAYER_SIZE); 148 al_identity_transform(&camera); 149 al_translate_transform(&camera, -cameraPosition[0], -cameraPosition[1]); 150 al_use_transform(&camera); 151 152 redraw = true; 153 } 154 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 155 { 156 break; 157 } 158 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) 159 { 160 switch(ev.keyboard.keycode) 161 { 162 case ALLEGRO_KEY_UP: 163 key[KEY_UP] = true; 164 break; 165 case ALLEGRO_KEY_DOWN: 166 key[KEY_DOWN] = true; 167 break; 168 case ALLEGRO_KEY_LEFT: 169 key[KEY_LEFT] = true; 170 break; 171 case ALLEGRO_KEY_RIGHT: 172 key[KEY_RIGHT] = true; 173 break; 174 } 175 } 176 else if(ev.type == ALLEGRO_EVENT_KEY_UP) 177 { 178 switch(ev.keyboard.keycode) 179 { 180 case ALLEGRO_KEY_UP: 181 key[KEY_UP] = false; 182 break; 183 case ALLEGRO_KEY_DOWN: 184 key[KEY_DOWN] = false; 185 break; 186 case ALLEGRO_KEY_LEFT: 187 key[KEY_LEFT] = false; 188 break; 189 case ALLEGRO_KEY_RIGHT: 190 key[KEY_RIGHT] = false; 191 break; 192 case ALLEGRO_KEY_ESCAPE: 193 doexit = true; 194 break; 195 } 196 } 197 198 if(redraw && al_is_event_queue_empty(event_queue)) 199 { 200 redraw = false; 201 al_clear_to_color(al_map_rgb(0, 0, 0)); 202 // draw map 203 for(int i = 0; i < MAP_X_SIZE; i++) 204 { 205 for(int j = 0; j < MAP_Y_SIZE; j++) 206 { 207 al_draw_bitmap(tiles[map[j][i]], i * TILE_SIZE, j * TILE_SIZE, 0); 208 } 209 } 210 al_draw_bitmap(player, playerX, playerY, 0); 211 al_flip_display(); 212 } 213 } 214 215 return 0; 216 217}

Thread #612099. Printed from Allegro.cc