Created
March 2, 2015 02:40
-
-
Save vinipsmaker/884ce0587b8dd0e2cfde to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
#include <SDL.h> | |
#include <SDL_image.h> | |
#include "res_path.h" | |
#include "cleanup.h" | |
/* | |
* Lesson 4: Handling Events | |
*/ | |
//Screen attributes | |
const int SCREEN_WIDTH = 640; | |
const int SCREEN_HEIGHT = 480; | |
/* | |
* Log an SDL error with some error message to the output stream of our choice | |
* @param os The output stream to write the message too | |
* @param msg The error message to write, format will be msg error: SDL_GetError() | |
*/ | |
void logSDLError(std::ostream &os, const std::string &msg){ | |
os << msg << " error: " << SDL_GetError() << std::endl; | |
} | |
/* | |
* Loads an image into a texture on the rendering device | |
* @param file The image file to load | |
* @param ren The renderer to load the texture onto | |
* @return the loaded texture, or nullptr if something went wrong. | |
*/ | |
SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren){ | |
SDL_Texture *texture = IMG_LoadTexture(ren, file.c_str()); | |
if (texture == nullptr){ | |
logSDLError(std::cout, "LoadTexture"); | |
} | |
return texture; | |
} | |
/* | |
* Draw an SDL_Texture to an SDL_Renderer at position x, y, with some desired | |
* width and height | |
* @param tex The source texture we want to draw | |
* @param rend The renderer we want to draw too | |
* @param x The x coordinate to draw too | |
* @param y The y coordinate to draw too | |
* @param w The width of the texture to draw | |
* @param h The height of the texture to draw | |
*/ | |
void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y, int w, int h){ | |
//Setup the destination rectangle to be at the position we want | |
SDL_Rect dst; | |
dst.x = x; | |
dst.y = y; | |
dst.w = w; | |
dst.h = h; | |
SDL_RenderCopy(ren, tex, NULL, &dst); | |
} | |
/* | |
* Draw an SDL_Texture to an SDL_Renderer at position x, y, preserving | |
* the texture's width and height | |
* @param tex The source texture we want to draw | |
* @param rend The renderer we want to draw too | |
* @param x The x coordinate to draw too | |
* @param y The y coordinate to draw too | |
*/ | |
void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y){ | |
int w, h; | |
SDL_QueryTexture(tex, NULL, NULL, &w, &h); | |
renderTexture(tex, ren, x, y, w, h); | |
} | |
int main(int, char**){ | |
//Start up SDL and make sure it went ok | |
if (SDL_Init(SDL_INIT_VIDEO) != 0){ | |
logSDLError(std::cout, "SDL_Init"); | |
return 1; | |
} | |
//Setup our window and renderer, this time let's put our window in the center | |
//of the screen | |
SDL_Window *window = SDL_CreateWindow("Lesson 4", SDL_WINDOWPOS_CENTERED, | |
SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); | |
if (window == nullptr){ | |
logSDLError(std::cout, "CreateWindow"); | |
SDL_Quit(); | |
return 1; | |
} | |
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); | |
if (renderer == nullptr){ | |
logSDLError(std::cout, "CreateRenderer"); | |
cleanup(window); | |
SDL_Quit(); | |
return 1; | |
} | |
//The texture we'll be using | |
const std::string resPath = getResourcePath("Lesson4"); | |
SDL_Texture *image = loadTexture(resPath + "image.png", renderer); | |
if (image == nullptr){ | |
cleanup(image, renderer, window); | |
IMG_Quit(); | |
SDL_Quit(); | |
return 1; | |
} | |
//Our texture size won't change, so we can get it here | |
//instead of constantly allocating/deleting ints in the loop | |
int iW, iH; | |
SDL_QueryTexture(image, NULL, NULL, &iW, &iH); | |
int x = SCREEN_WIDTH / 2 - iW / 2; | |
int y = SCREEN_HEIGHT / 2 - iH / 2; | |
//Our event structure | |
SDL_Event e; | |
//For tracking if we want to quit | |
bool quit = false; | |
while (!quit){ | |
//Read any events that occured, for now we'll just quit if any event occurs | |
while (SDL_PollEvent(&e)){ | |
//If user closes the window | |
if (e.type == SDL_QUIT){ | |
quit = true; | |
} | |
//If user presses any key | |
if (e.type == SDL_KEYDOWN){ | |
quit = true; | |
} | |
//If user clicks the mouse | |
if (e.type == SDL_MOUSEBUTTONDOWN){ | |
quit = true; | |
} | |
} | |
//Rendering | |
SDL_RenderClear(renderer); | |
//Draw the image | |
renderTexture(image, renderer, x, y); | |
//Update the screen | |
SDL_RenderPresent(renderer); | |
} | |
//Destroy the various items | |
cleanup(image, renderer, window); | |
IMG_Quit(); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment