May 14, 2014 at 6:57pm UTC
Hi guys, i am trying to write my first SDL programme. I am making use of two cpp files,a main.cpp a sdl.cpp and a header file. There seems to be no errors in the code and it builds however i cannot seem to run this project. I am using eclipse kepler. I have put the bmp images in the project folder but by .exe shows blank and closes.
[main]
#include <iostream>
#include "SDL/SDL.h"
#include "SDL.H"
using namespace std;
int main( int argc, char* args[] )
{
makeWorld();
return 0;
}
[main]
[SDL.cpp]
#include "SDL.h"
#include <cstdlib>
#include <ctime>
using namespace std;
char** makeWorld()
{
char** world= new char* [NUM_ROWS];
for (int r=0; r<NUM_ROWS; r++)
{
world[r]=new char[NUM_COLS];
for (int c=0; c<NUM_COLS;c++)
{
if(rand()%100+1 <=CHANCE && r>0 && r < (NUM_ROWS-1))
world[r][c]=DEF;
else
{
world[r][c]=EMPTY;
}
}
}
int PCOL=rand() % NUM_COLS;
world[0][PCOL]=BOY;
int GCOL= rand() % NUM_COLS;
world[NUM_ROWS-1][GCOL]=GOAL;
return world;
}
void destroyWorld(char**& world)
{
for(int r=0; r <NUM_ROWS;r++)
{
delete [] world [r];
world=NULL;
}
}
void findPlayer(char** world,int& PROW,int& PCOL)
{
bool find=false;
for(PROW=0; (PROW<NUM_ROWS); PROW++)
{
for(PCOL=0; (PCOL<NUM_COLS); PCOL++)
{
if( world[PROW][PCOL]==BOY)
find=true;
if(find)
break;
}
if (find)
break;
}
}
SDL_Surface* loadImage(const char* fileName)
{
SDL_Surface* surfLoad=NULL;
SDL_Surface* surfReturn=NULL;
if((surfLoad=SDL_LoadBMP(fileName))==NULL)
{
return NULL;
}
surfReturn=SDL_DisplayFormat(surfLoad);
SDL_FreeSurface(surfLoad);
return surfReturn;
}
void updateWorld(char** world, char move)
{
int pr;
int pc;
findPlayer(world,pr,pc);
int nr=pr;
int nc=pc;
switch(move)
{
case 'U':
if(pr>0)
nr=pr-1;
break;
case 'D':
if(pr<NUM_ROWS-1)
nr=pr+1;
break;
case'L':
if(pc>0)
nc=pc-1;
break;
case'R':
if(pc<NUM_COLS)
nc=pc+1;
break;
}
if (world[nr][nc]==DEF)
world[0][0]=LOSE;
else if (world[nr][nc]==GOAL)
world[0][0]=WIN;
else
{
world[pr][pc]=EMPTY;
world[nr][nc]=BOY;
}
}
bool initWorld(char**& world,SDL_Surface*& surface,bool& run)
{
srand(time(0));
if(SDL_Init(SDL_INIT_EVERYTHING) <0)
{
return false;
}
SDL_WM_SetCaption("SOCCER GAME","PRAC10");
surface=SDL_SetVideoMode(SPRITE_SIZE* NUM_COLS,SPRITE_SIZE* NUM_ROWS,32,SDL_HWSURFACE | SDL_DOUBLEBUF);
if(surface == NULL)
{
return false;
}
world=makeWorld();
run=true;
return true;
}
void clean(char**& world,SDL_Surface*& surface,SDL_Surface* sprites[])
{
destroyWorld(world);
SDL_FreeSurface(surface);
for(int v=0;v <SPRITES;v++)
{
SDL_FreeSurface(sprites[v]);
sprites[v]=NULL;
}
SDL_Quit();
}
int makeGame()
{
char** world=NULL;
SDL_Surface* surface=NULL;
SDL_Surface* sprites[SPRITES];
bool run=false;
if(!initWorld(world,surface,run))
return -1;
SDL_Event event;
sprites[EMPTY_IMAGE] = loadImage("grass.bmp");
sprites[BOY_IMAGE] = loadImage("ronaldo.bmp");
sprites[DEF_IMAGE] = loadImage("defend.bmp");
sprites[GOAL_IMAGE] = loadImage("ball.bmp");
sprites[LOSE_IMAGE] = loadImage("sad.bmp");
sprites[WIN_IMAGE] = loadImage("win.bmp");
SDL_SetColorKey(sprites[BOY_IMAGE],SDL_SRCCOLORKEY,SDL_MapRGB(sprites[BOY_IMAGE]->format, 255, 0, 255));
SDL_SetColorKey(sprites[DEF_IMAGE],SDL_SRCCOLORKEY,SDL_MapRGB(sprites[DEF_IMAGE]->format, 255, 0, 255));
SDL_SetColorKey(sprites[LOSE_IMAGE],SDL_SRCCOLORKEY,SDL_MapRGB(sprites[LOSE_IMAGE]->format, 255, 0, 255));
SDL_SetColorKey(sprites[WIN_IMAGE],SDL_SRCCOLORKEY,SDL_MapRGB(sprites[WIN_IMAGE]->format, 255, 0, 255));
while(run)
{
render(world, surface, sprites);
while(SDL_PollEvent(&event))
handleEvent(world, &event, surface,run);
}
clean(world,surface,sprites);
return 0;
}
void handleEvent(char** world, SDL_Event* event, SDL_Surface* surface, bool& run)
{
switch(event->type)
{
case SDL_QUIT:
run = false;
break;
case SDL_KEYDOWN:
{
switch(event->key.keysym.sym)
{
case SDLK_UP:
updateWorld(world,'U');
break;
case SDLK_DOWN:
updateWorld(world,'D');
break;
case SDLK_LEFT:
updateWorld(world,'L');
break;
case SDLK_RIGHT:
updateWorld(world,'R');
break;
default:
break;
}
break;
}
}
}
void render(char** world, SDL_Surface* surface, SDL_Surface* sprites[])
{
SDL_Rect rectTarget;
bool win = (world[0][0] == WIN);
bool defeat = (world[0][0] == LOSE);
for(int r = 0; r < NUM_ROWS; r++)
{
rectTarget.y = r * SPRITE_SIZE;
for(int c = 0; c < NUM_COLS; c++)
{
rectTarget.x = c * SPRITE_SIZE;
int draw = 0;
if(win)
draw = WIN_IMAGE;
else if(defeat)
draw = LOSE_IMAGE;
else
{
switch(world[r][c])
{
case BOY:
draw = BOY_IMAGE;
break;
case DEF:
draw = DEF_IMAGE;
break;
case GOAL:
draw = GOAL_IMAGE;
break;
case EMPTY:
draw = EMPTY_IMAGE;
break;
default:
exit(ERROR_INVALID_STATE);
}
}
SDL_BlitSurface(sprites[EMPTY_IMAGE], NULL,surface, &rectTarget);
SDL_BlitSurface(sprites[draw], NULL, surface, &rectTarget);
}
}
SDL_Flip(surface);
}
[SDL.cpp]
[Sdl.h]
#ifndef SDL_H_
#define SDL_H_
#include <SDL/SDL.h>
const int NUM_ROWS=10;
const int NUM_COLS=15;
const int CHANCE=15;
const char BOY='B';
const char DEF= 'T';
const char EMPTY=' ';
const char GOAL= 'G';
const char WIN='W';
const char LOSE='D';
const int SPRITES=6;
const int SPRITE_SIZE=64;
const int BOY_IMAGE=0;
const int DEF_IMAGE=1;
const int EMPTY_IMAGE=2;
const int GOAL_IMAGE=3;
const int LOSE_IMAGE=4;
const int WIN_IMAGE=5;
const int ERROR_INVALID_STATE=-1;
char** makeWorld();
void destroyWorld(char**& world);
void updateWorld(char** world, char move);
void findPlayer(char** world,int& PROW,int& PCOL);
int makeGame();
bool initWorld(char**& world,SDL_Surface*& surface,bool& run);
void render(char** world,SDL_Surface* surface,SDL_Surface* sprites[]);
void handleEvent(char** world,SDL_Event* event,SDL_Surface* surface,bool& run);
void clean(char**& world,SDL_Surface*& surface,SDL_Surface* sprites[]);
#endif /* SDL_H_ */
[sdl.h]