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
|
#include <SDL.h>
#include <iostream>
//Declare variables
const unsigned int SCREEN_WIDTH = 640;
const unsigned int SCRREN_HEIGHT = 480;
bool succsess;
bool quit = false;
unsigned int colorRed,colorGreen,colorBlue;
int spriteDstX = 0;
int spriteDstY = 0;
//Create SDL_Surfaces and SDL_Window
SDL_Surface *spriteSurface = NULL;
SDL_Surface *screenSurface = NULL;
SDL_Window *window = NULL;
//Declare functions
bool init();
void drawSprite(int spriteDstX,int spriteDstY,SDL_Surface *sprite,SDL_Surface *screen);
bool loadMedia();
void close();
int main(int argc,char* args[])
{
init();
SDL_Event event;
spriteSurface = SDL_LoadBMP("sprite.bmp");
//Create game loop
while(!quit)
{
while(SDL_PollEvent(&event)!= 0)
{
if (event.type == SDL_QUIT)
{
quit = true;
}
else if (event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_UP:
spriteDstY-=10;
break;
case SDLK_DOWN:
spriteDstY+=10;
break;
case SDLK_LEFT:
spriteDstX-=10;
break;
case SDLK_RIGHT:
spriteDstX+=10;
break;
}
}
}
drawSprite(spriteDstX,spriteDstY,spriteSurface,screenSurface);
SDL_UpdateWindowSurface(window);
}
close();
return 0;
}
bool init()
{
succsess = true;
//Create window
window = SDL_CreateWindow("SDL TEST",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCRREN_HEIGHT,
SDL_WINDOW_SHOWN);
if (window == 0)
{
succsess = false;
std::cout << "Could not create window: " << SDL_GetError();
}
else
{
screenSurface = SDL_GetWindowSurface(window);
//Give the background a color
SDL_FillRect(screenSurface,NULL,SDL_MapRGB(screenSurface->format,colorRed,colorGreen,colorBlue));
}
return succsess;
}
void close()
{
SDL_FreeSurface(spriteSurface);
SDL_FreeSurface(screenSurface);
SDL_DestroyWindow(window);
SDL_Quit();
}
void drawSprite(int spriteDstX,int spriteDstY,SDL_Surface *sprite,SDL_Surface *screen)
{
SDL_Rect spriteDst;
spriteDst.x = spriteDstX;
spriteDst.y = spriteDstY;
SDL_BlitSurface(sprite,NULL,screen,&spriteDst);
}
|