1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
#include "SDL.h"
#include "SDL/SDL_image.h"
#include <string>
using namespace std;
void applySurface(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect &clip);
SDL_Surface *loadImage(string filename);
bool init();
bool loadFiles();
bool cleanUp();
const int scrWidth = 640;
const int scrHeight = 480;
const int scrBPP = 32;
SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
SDL_Surface *image = NULL;
SDL_Rect redRect;
SDL_Rect imageRect;
Uint32 RectColor = SDL_MapRGB(screen->format, 234, 0, 45);
SDL_Event event;
int main(int argc, char **argv)
{
bool quit = false;
if(init()==false){return 1;}
if(loadFiles()==false){return 1;}
//To move the rectangle on the screen.
int xVel = 10; yVel = 10;
//Tracks the position of the image rectangle following the image.
int imageX = 10; imageY = 10;
//This stuff can go anywhere, but for time's sake I'm putting it here.
redRect.x = 320;
redRect.y = 240;
redRect.w = 100;
redRect.h = 100;
imageRect.w = 50;
imageRect.h = 50;
while(quit==false)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{ quit = true; }
if(event.type==SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_w: imageY -= yVel; break;
case SDLK_s: imageY += yVel; break;
case SDLK_a: imageX -= xVel; break;
case SDLK_d: imageX += xVel; break;
}
}
if(event.type==SDLK_KEYUP)
{
switch(event.key.keysym.sym)
{
case SDLK_w: imageY += yVel; break;
case SDLK_s: imageY -= yVel; break;
case SDLK_a: imageX += xVel; break;
case SDLK_d: imageX -= xVel; break;
}
}
//Updates the image rect to the current image position.
//This way we can detect collisions.
imageRect.x = imageX;
imageRect.y = imageY;
//Check for X collisions on right side of red rect.
if((imageRect.x+imageRect.w)==redRect.x)
{ imageX-=xVel; }
//Check for X collision on left side of red rect.
if((redRect.x+redRect.w)==imageRect.x)
{ imageX+=xVel; }
//Y collision on bottom of box.
if((imageRect.y+imageRect.h)==redRect.y)
{ imageY+=yVel; }
//Y collision on top of box.
if((redRect.y+=redRect.h)==imageRect.y)
{ imageY-=yVel; }
applySurface(0,0,background,screen);
applySurface(imageX,imageY,image,screen);
SDL_FillRect(screen,&redRect,RectColor);
}
cleanUp();
return 0;
}
void applySurface(int x,int y,SDL_Surface *source,SDL_Surface *destination,SDL_Rect *clip)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source,clip,destination,&offset);
}
SDL_Surface *loadImage(string filename)
{
SDL_Surface *loadedImage = NULL;
SDL_Surface *optimizedImage = NULL;
loadedImage = IMG_Load (filename.c_str());
if(loadedImage!=NULL) {
optimizedImage = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage);
}
if(optimizedImage != NULL) {
Uint32 colorKey = SDL_MapRGB(optimizedImage->format,0xFF,0xFF,0xFF);
SDL_SetColorKey(optimizedImage,SDL_SRCCOLORKEY,colorKey);
return optimizedImage;
}
}
bool init()
{
if(SDL_Init(SDL_INIT_EVERYTHING)==-1) {return false;}
screen = SDL_SetVideoMode(scrWidth,scrHeight,scrBPP,SDL_SWSURFACE);
if(screen==NULL){return false;}
SDL_WM_SetCaption(title.c_str(),NULL);
return true;
}
void cleanUp()
{
SDL_FreeSurface(background);
SDL_FreeSurface(image);
SDL_Quit();
}
bool loadFiles()
{
background = loadImage("background.bmp");
image = loadImage("image.bmp");
if((background==NULL)||(image==NULL))
{ return false; }
return true;
}
|