Code: Select all
#include "SDL.h"
#include <string>
const int Grass = 0;
const int Wall = 1;
int xPos = 200;
int yPos = 300;
int map[12][16] =
{
{1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
{1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
SDL_Surface *load_image(std::string filename)
{
SDL_Surface *loadedImage = NULL;
SDL_Surface *optimizedImage = NULL;
loadedImage = SDL_LoadBMP(filename.c_str());
if(loadedImage != NULL)
{
optimizedImage = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage);
}
return optimizedImage;
}
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, NULL, destination, &offset);
}
int main(int argc, char *args[])
{
SDL_Event event;
bool quit = false;
SDL_Surface *screen;
SDL_Surface *player;
SDL_Surface *wall;
SDL_Surface *grass;
player = load_image("gfx/player.bmp");
wall = load_image("gfx/wall.bmp");
grass = load_image("gfx/grass.bmp");
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
SDL_WM_SetCaption("My RPG", NULL);
while(quit == false)
{
while(SDL_PollEvent(&event))
{
for(int y = 0; y < 12; y++)
{
for(int x = 0; x < 16; x++)
{
if(map[x][y] == Wall)
{
apply_surface(x*40, y*40, wall, screen);
}
if(map[x][y] == Grass)
{
apply_surface(x*40, y*40, grass, screen);
}
}
}
switch(event.key.keysym.sym)
{
case SDLK_UP: yPos--; break;
case SDLK_DOWN: yPos++; break;
case SDLK_LEFT: xPos--; break;
case SDLK_RIGHT: xPos++; break;
}
apply_surface(xPos, yPos, player, screen);
if(event.type == SDL_QUIT)
{
quit = true;
}
SDL_Flip(screen);
}
}
SDL_FreeSurface(player);
SDL_FreeSurface(wall);
SDL_FreeSurface(grass);
SDL_Quit();
return 0;
}
The problem is that when I compile the project I get a send error report and I have to close it.
Any ideas why that would happen?
PS: I know this code is ugly as all living and breathing f*ck but I was speeding through this code and I will touch up on it later...
PSS: Oh and I've not seen the result of this program yet because it closes before I get a chance to see it.