Well I kind of accomplished what I wanted so far, of course it is without graphics, but you can push another tile around like the Sokoban game without any game winning condition...
I am looking for feedback and whether or not I did anything in a bad practice
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
using namespace std;
const int SCREEN_W = 800;
const int SCREEN_H = 600;
const int TILE_SIZE = 32;
const int MAP_W = 15;
const int MAP_H = 15;
const int TOTAL_TILES = 225;
SDL_Surface* display = NULL;
SDL_Event event;
vector<int> Map;
vector<int> PlayerPos;
void CreateVector() {
// file load mock
int tiles[TOTAL_TILES] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, };
for(int i = 0; i < TOTAL_TILES; i++ ) {
Map.push_back(tiles[i]);
}
for(int i = 0; i < TOTAL_TILES; i++ ) {
PlayerPos.push_back(0);
}
PlayerPos[17] = 1;
PlayerPos[32] = 2;
}
void DrawTiles() {
for(int i = 0; i < Map.size(); i++) {
SDL_Rect offset;
offset.w = TILE_SIZE;
offset.h = TILE_SIZE;
offset.x = (1 + TILE_SIZE) * (i % MAP_W);
offset.y = (1 + TILE_SIZE) * (i / MAP_H);
if(PlayerPos[i] == 1) {
SDL_FillRect( display, &offset, SDL_MapRGB( display->format, 0xFF, 0x00, 0x00) );
} else if (PlayerPos[i] == 2) {
SDL_FillRect( display, &offset, SDL_MapRGB( display->format, 0x00, 0xFF, 0x00) );
} else {
switch(Map.at(i)) {
case 0:
SDL_FillRect( display, &offset, SDL_MapRGB( display->format, 0x00, 0x00, 0x00) );
break;
case 1:
SDL_FillRect( display, &offset, SDL_MapRGB( display->format, 0x00, 0x00, 0xFF) );
break;
default:break;
}
}
}
}
void MovePlayerRight() {
for(int i = 0; i < Map.size(); i++) {
if(PlayerPos.at(i) == 1) {
if(((i+1)<= Map.size()) && (Map.at(i+1) != 1)) {
if(PlayerPos.at(i+1) == 2) {
if(((i+2)<= Map.size()) && (Map.at(i+2) != 1)) {
PlayerPos[i+1] = 0;
PlayerPos[i+2] = 2;
} else break;
}
PlayerPos[i] = 0;
PlayerPos[i+1] = 1;
}
break;
} else continue;
}
}
void MovePlayerLeft() {
for(int i = 0; i < Map.size(); i++) {
if(PlayerPos.at(i) == 1) {
if(PlayerPos.at(i-1) == 2) {
if(((i-2)> -1) && (Map.at(i-2) != 1)) {
PlayerPos[i-1] = 0;
PlayerPos[i-2] = 2;
} else break;
}
if(((i-1) > -1) && (Map.at(i-1) != 1)) {
PlayerPos[i] = 0;
PlayerPos[i-1] = 1;
}
break;
} else continue;
}
}
void MovePlayerDown() {
for(int i = 0; i < Map.size(); i++) {
if(PlayerPos.at(i) == 1) {
if(PlayerPos.at(i + MAP_W) == 2) {
if((i + (MAP_W * 2) <= Map.size()) && (Map.at(i + (MAP_W * 2)) != 1)) {
PlayerPos[i + MAP_W] = 0;
PlayerPos[i + (MAP_W * 2)] = 2;
} else break;
}
if((i + MAP_W) <= Map.size() && (Map.at(i + MAP_W) != 1)) {
PlayerPos[i] = 0;
PlayerPos[i + MAP_W] = 1;
}
break;
} else continue;
}
}
void MovePlayerUp() {
for(int i = 0; i < Map.size(); i++) {
if(PlayerPos.at(i) == 1) {
if(PlayerPos.at(i - MAP_W) == 2) {
if((i - (MAP_W * 2) <= Map.size()) && (Map.at(i - (MAP_W * 2)) != 1)) {
PlayerPos[i - MAP_W] = 0;
PlayerPos[i - (MAP_W * 2)] = 2;
} else break;
}
if((i - MAP_W) > -1 && (Map.at(i - MAP_W) != 1)) {
PlayerPos[i] = 0;
PlayerPos[i - MAP_W] = 1;
}
break;
} else continue;
}
}
int main( int argc, char* args[] ) {
bool done = false;
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1) return 1;
display = SDL_SetVideoMode(SCREEN_W, SCREEN_H, 32, SDL_SWSURFACE);
if(!display) return 2;
CreateVector();
while(!done) {
while(SDL_PollEvent( &event )) {
if(event.type == SDL_KEYDOWN) {
switch(event.key.keysym.sym) {
case SDLK_ESCAPE:
done = true;
break;
case SDLK_RIGHT:
MovePlayerRight();
break;
case SDLK_LEFT:
MovePlayerLeft();
break;
case SDLK_DOWN:
MovePlayerDown();
break;
case SDLK_UP:
MovePlayerUp();
break;
default:break;
}
}
}
DrawTiles();
SDL_Flip( display );
SDL_FillRect( display, &display->clip_rect, SDL_MapRGB(display->format, 0xFF, 0xFF, 0xFF) );
}
SDL_FreeSurface( display );
SDL_Quit();
return 0;
}