/1.h
Created
September 4, 2012 00:30
lesson 7: Window class gist
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
#ifndef WINDOW_H | |
#define WINDOW_H | |
//What we think our window class should look like | |
class Window { | |
public: | |
//Start SDL and TTF, create the window and renderer | |
static void Init(); | |
//Quit SDL and TTF | |
static void Quit(); | |
//Draw an SDL_Texture | |
static void Draw(SDL_Texture*, ...); | |
//Load an image | |
static SDL_Texture* LoadImage(std::string file); | |
//Render some text | |
static SDL_Texture* RenderText(std::string, std::string, SDL_Color, int); | |
//Clear window | |
static void Clear(); | |
//Present renderer | |
static void Present(); | |
//Get the window's box | |
static SDL_Rect Box(); | |
private: | |
static SDL_Window* mWindow; | |
static SDL_Renderer* mRenderer; | |
static SDL_Rect mBox; | |
}; | |
#endif |
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
private: | |
static std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> mWindow; | |
static std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> mRenderer; | |
static SDL_Rect mBox; | |
}; |
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
//Initialize the unique_ptr's deleters here | |
std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> Window::mWindow | |
= std::unique_ptr<SDL_Window, void (*)(SDL_Window*)>(nullptr, SDL_DestroyWindow); | |
std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> Window::mRenderer | |
= std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)>(nullptr, SDL_DestroyRenderer); | |
//Other static members | |
SDL_Rect Window::mBox; |
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
//In window.cpp | |
void Window::Init(std::string title){ | |
//initialize all SDL subsystems | |
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) | |
throw std::runtime_error("SDL Init Failed"); | |
if (TTF_Init() == -1) | |
throw std::runtime_error("TTF Init Failed"); | |
//Setup our window size | |
mBox.x = 0; | |
mBox.y = 0; | |
mBox.w = 640; | |
mBox.h = 480; | |
//Create our window | |
mWindow.reset(SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, | |
SDL_WINDOWPOS_CENTERED, mBox.w, mBox.h, SDL_WINDOW_SHOWN)); | |
//Make sure it created ok | |
if (mWindow == nullptr) | |
throw std::runtime_error("Failed to create window"); | |
//Create the renderer | |
mRenderer.reset(SDL_CreateRenderer(mWindow.get(), -1, | |
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)); | |
//Make sure it created ok | |
if (mRenderer == nullptr) | |
throw std::runtime_error("Failed to create renderer"); | |
} |
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
//In window.h | |
/** | |
* Initialize SDL, setup the window and renderer | |
* @param title The window title | |
*/ | |
static void Init(std::string title = "Window"); |
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
//In window.cpp | |
void Window::Quit(){ | |
TTF_Quit(); | |
SDL_Quit(); | |
} |
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
//In window.h | |
///Quit SDL and destroy the window and renderer | |
static void Quit(); |
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
void Window::Draw(SDL_Texture *tex, SDL_Rect &dstRect, SDL_Rect *clip, float angle, | |
int xPivot, int yPivot, SDL_RendererFlip flip) | |
{ | |
//Convert pivot pos from relative to object's center to screen space | |
xPivot += dstRect.w / 2; | |
yPivot += dstRect.h / 2; | |
//SDL expects an SDL_Point as the pivot location | |
SDL_Point pivot = { xPivot, yPivot }; | |
//Draw the texture | |
SDL_RenderCopyEx(mRenderer.get(), tex, clip, &dstRect, angle, &pivot, flip); | |
} |
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
//In window.h | |
/** | |
* Draw a SDL_Texture to the screen at dstRect with various other options | |
* @param tex The SDL_Texture to draw | |
* @param dstRect The destination position and width/height to draw the texture with | |
* @param clip The clip to apply to the image, if desired | |
* @param angle The rotation angle to apply to the texture, default is 0 | |
* @param xPivot The x coordinate of the pivot, relative to (0, 0) being center of dstRect | |
* @param yPivot The y coordinate of the pivot, relative to (0, 0) being center of dstRect | |
* @param flip The flip to apply to the image, default is none | |
*/ | |
static void Draw(SDL_Texture *tex, SDL_Rect &dstRect, SDL_Rect *clip = NULL, | |
float angle = 0.0, int xPivot = 0, int yPivot = 0, | |
SDL_RendererFlip flip = SDL_FLIP_NONE); |
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
//In window.h | |
/** | |
* Loads an image directly to texture using SDL_image's | |
* built in function IMG_LoadTexture | |
* @param file The image file to load | |
* @return SDL_Texture* to the loaded texture | |
*/ | |
static SDL_Texture* LoadImage(const std::string &file); | |
/** | |
* Generate a texture containing the message we want to display | |
* @param message The message we want to display | |
* @param fontFile The font we want to use to render the text | |
* @param color The color we want the text to be | |
* @param fontSize The size we want the font to be | |
* @return An SDL_Texture* to the rendered message | |
*/ | |
static SDL_Texture* RenderText(const std::string &message, const std::string &fontFile, | |
SDL_Color color, int fontSize); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment