#include<string>
#include<SDL.h>
#include <conio.h>
using namespace std;
//declare surfaces.
SDL_Surface* floor = NULL; //background tile.
SDL_Surface* player = NULL; //player tile
SDL_Surface* zombie = NULL; //enemy tile
SDL_Surface* colonist = NULL; //friendly tile
SDL_Surface* screen = NULL; //display layer.
//declare attributes of the screen
const int SCREEN_HEIGHT = 500;
const int SCREEN_WIDTH = 1000;
const int SCREEN_BPP = 32;
bool quit;
//array containing the coordinates for all entities.
SDL_Rect object[50];
//array containing type of unit
int type[50]; // player = 1, colonists = 2, zombies = 3
//amount of baddies
int enemyAmount = 4;
//whether colliding
bool isCol[50];
int amount;
bool collide;
//event
SDL_Event move;
//function for conversion of 24 bit bmps into 32 bit bmps
SDL_Surface *load_image( std::string filename ) //load bmp
{
//loaded 24 bit bmp is placed here:
SDL_Surface* loadedImage = NULL;
//optimized 32 bit bmp is placed here:
SDL_Surface* optimizedImage = NULL;
//the bmp is loaded as "filename"
loadedImage = SDL_LoadBMP( filename.c_str() );
//if the image loads without errors:
if( loadedImage != NULL )
{
//convert to 32 bit bmp
optimizedImage = SDL_DisplayFormat( loadedImage );
//clear RAM of 24 bit image
SDL_FreeSurface( loadedImage );
}
if( optimizedImage != NULL )
{
//Map the color key
Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0xFF, 0, 0xFF );
//Set all pixels of colour transparent
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
}
//send the new 32 bit file back to the program
return optimizedImage;
}
//Function to blit the surface
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
//temporary rectangle is created to hold offset values.
//SDL_Rect declares a rectangle with four attributes: offsets( x, y ) and dimensions( h, w )
SDL_Rect offset;
//declare rectangle offsets:
offset.x = x;
offset.y = y;
//blit the surface. ( surface used in function, ?, surface that is blitted to, offsets )
SDL_BlitSurface ( source, NULL, destination, &offset );
}
void drawFloor()
{
int n = 1;
int nl = 1;
int xl = 20;
int yl = 40;
while( nl < 23 )
{
if( n == 37 )
{
xl = 20;
yl += 20;
n = 1;
nl++;
}
else
{
apply_surface( xl, yl, floor, screen );
n++;
xl += 20;
}
}
}
bool start()
{
//Start SDL and end program if errors are encountered.
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1)
{
return -1;
}
//sets up screen, ends program if issues.
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
if( screen == NULL )
{
return -1;
}
//Title of the window
SDL_WM_SetCaption( "ROOM MOVER", NULL );
//start position
object[1].x = 240;
object[1].y = 240;
//declare player as player
type[1] = 3;
//test sci
object[2].x = 240;
object[2].y = 100;
type[2] = 2;
//test zamb
object[3].x = 240;
object[3].y = 340;
type[3] = 1;
return true;
}
bool stop()
{
//clear the RAM
SDL_FreeSurface( floor );
//end SDL
SDL_Quit();
//end program
return 0;
}
//function that blits the objects onto the screen
int drawGuys()
{
int objCount = 1;
while( objCount < enemyAmount )
{
if( type[objCount] == 1 ) //if its the player
{
apply_surface( object[objCount].x, object[objCount].y, player, screen ); //draw as player
objCount ++;//check next thing
}
else if( type[objCount] == 2 ) //if its a colonist
{
apply_surface( object[objCount].x, object[objCount].y, colonist, screen ); //draw as colonist
objCount ++;//check next thing
}
else if( type[objCount] == 3 ) //if its a zombie
{
apply_surface( object[objCount].x, object[objCount].y, zombie, screen ); //draw as zombie
objCount ++;//check next thing
}
else //if its none of the above
{
objCount ++;//check next thing
}
}
}
int draw()
{
drawFloor();
drawGuys();
SDL_Flip( screen );
}
//checks two objects colliding and determines zombification and game loss.
int actCollision( SDL_Rect offender, SDL_Rect defender, int offenderType, int defenderType )
{
//if the one moving into the space is a zombie
if ( offender.y == defender.y && offender.x == defender.x && offenderType == 3 && defenderType == 2 )
{
defenderType = 3;// the one occupying the space is now a zombie
return (defenderType);
}
// if player
else if ( offender.y == defender.y && offender.x == defender.x && offenderType == 3 && defenderType == 1 )
{
stop(); // game over
}
}
int checkCollision()
{
int compare1 = 1;
int compare2 = 1;
while( compare1 < 49 )
{
if( compare2 == 49 )
{
compare1 ++;
compare2 = 1;
}
else
{
if( compare1 != compare2 )
{
actCollision( object[compare1], object[compare2], type[compare1], type[compare2] );
compare2 ++;
}
else
{
compare2 ++;
}
}
}
}
int main( int argc, char* args[] )
{
start();
//Load Images to surfaces
floor = load_image( "tile.bmp" );
player = load_image( "man.bmp" );
colonist = load_image( "sci.bmp" );
zombie = load_image( "zomb.bmp" );
while( quit == false )
{
while( SDL_PollEvent( &move ) )
{
if (move.type == SDL_KEYDOWN)
{
if (move.key.keysym.sym == SDLK_w)
{
object[1].y -= 20;
checkCollision();
draw();
}
else if (move.key.keysym.sym == SDLK_s)
{
object[1].y += 20;
checkCollision();
draw();
}
else if (move.key.keysym.sym == SDLK_d)
{
object[1].x += 20;
checkCollision();
draw();
}
else if (move.key.keysym.sym == SDLK_a)
{
object[1].x -= 20;
checkCollision();
draw();
}
}
//looks for the 'x' of the window to be closed, the acts accordingly
else if (move.type == SDL_QUIT)
{
quit = true;
}
}
}
stop();
}