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
|
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
#include <iostream>
// SDL Surfaces
SDL_Surface *surfaces[5] = {0};
// Global Constants
const int scrWidth = 640;
const int scrHeight = 400;
const int scrBitDepth = 32;
// SDL Prequisites
SDL_Event event;
SDL_Surface *loadImage(std::string filename)
{
SDL_Surface *loadedIMG = 0;
SDL_Surface *optimizedIMG = 0;
loadedIMG = IMG_Load(filename.c_str());
if(loadedIMG)
{
optimizedIMG = SDL_DisplayFormatAlpha(loadedIMG);
SDL_FreeSurface(loadedIMG);
}
return(optimizedIMG);
}
void applySurface(int x, int y, SDL_Surface *source, SDL_Surface *destination,SDL_Rect *sprShSel = 0)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source,sprShSel,destination,&offset);
}
bool init()
{
SDL_Init(SDL_INIT_EVERYTHING);
surfaces[0] = SDL_SetVideoMode(scrWidth,scrHeight,scrBitDepth,SDL_SWSURFACE);
surfaces[1] = loadImage("upImg.png");
surfaces[2] = loadImage("downImg.png");
surfaces[3] = loadImage("leftImg.png");
surfaces[4] = loadImage("rightImg.png");
for (int i=0;i<5;i++) if(!surfaces[i]) return(0);
return(1);
}
void exit()
{
for (int i=0;i<5;i++) SDL_FreeSurface(surfaces[i]);
}
int main(int argc,char *argv[])
{
if(!init()) return(1);
bool quit = 0;
while(!quit)
{
if(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT) {quit=1;}
else if(event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_UP: applySurface(0,0,surfaces[1],surfaces[0]); break;
case SDLK_DOWN: applySurface(0,0,surfaces[2],surfaces[0]); break;
case SDLK_LEFT: applySurface(0,0,surfaces[3],surfaces[0]); break;
case SDLK_RIGHT: applySurface(0,0,surfaces[4],surfaces[0]); break;
default: surfaces[0] = SDL_SetVideoMode(scrWidth,scrHeight,scrBitDepth,SDL_SWSURFACE);
}
}
}
SDL_Flip(surfaces[0]);
}
exit();
return(0);
}
int WinMain (int argc,char *argv[])
{
return(main(argc,argv));
}
|