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
|
#include <iostream>
#include <SDL.h>
SDL_Surface *optimizedSurface(std::string filePath, SDL_Surface *windowSurface)
{
SDL_Surface *optimizedSurface = nullptr;
SDL_Surface *surface = SDL_LoadBMP(filePath.c_str());
if(surface == NULL)
std::cout << "Surface Error: " << SDL_GetError() << std::endl;
else
{
optimizedSurface = SDL_ConvertSurface(surface, windowSurface->format, 0);
if(optimizedSurface == NULL)
std::cout << "OptimizedSurface Error: " << SDL_GetError() << std::endl;
}
SDL_FreeSurface(surface);
return optimizedSurface;
}
int main(int argc, char** argv)
{
SDL_Window *window = nullptr; //Prepare a spot in memory for window
SDL_Surface *windowSurface = nullptr; //Prepare a spot in memory for the windows builtin surface
SDL_Surface *currentImage = nullptr;
SDL_Init(SDL_INIT_VIDEO); //Make SDL set itself up for use on my system
//Setup the window parameters
window = SDL_CreateWindow("SDL MY CODING MADE DIFFICULT", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
windowSurface = SDL_GetWindowSurface(window); //Window Created
currentImage = optimizedSurface("testsmall.bmp", windowSurface);
SDL_Rect drawingRect;
drawingRect.x = drawingRect.y = 60;
drawingRect.w = 240;
drawingRect.h = 180;
bool isRunning = true;
SDL_Event ev;
//Start of game loop
while(isRunning)
{
while(SDL_PollEvent(&ev) != 0)
{
else if(ev.type == SDL_KEYDOWN)
{
switch(ev.key.keysym.sym)
{
case SDLK_LEFT:
drawingRect.x -= 20;
break;
case SDLK_RIGHT:
drawingRect.x += 20;
break;
case SDLK_x:
isRunning = false;
break;
}
}
}
//SDL_BlitSurface(currentImage, NULL, windowSurface, NULL); //place imageSurface on windowSurface
//SDL_FreeSurface(currentImage);
SDL_BlitScaled(currentImage, NULL, windowSurface, &drawingRect);
SDL_UpdateWindowSurface(window); //Show updated surface on screen
}
SDL_FreeSurface(currentImage);
currentImage = nullptr;
SDL_DestroyWindow(window);
window = nullptr;
SDL_Quit(); //Quit SDL2
return 0;
}
|