Last active
March 1, 2018 02:17
-
-
Save xeekworx/1f16ab71c1f4d8579b4c6ee387a96bca to your computer and use it in GitHub Desktop.
SDL Renderer + A Moving Image
This file contains hidden or 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 <SDL.h> | |
#include <iostream> | |
#include <sstream> | |
#include <string> | |
int main(int argc, char *argv[]) | |
{ | |
int screen_width = 1024, screen_height = 768; | |
SDL_Window* main_window = NULL; | |
SDL_Renderer* renderer = NULL; | |
SDL_Event event = { 0 }; | |
bool should_quit = false; | |
const std::string image_file = "..\\img\\image.bmp"; | |
SDL_Surface* image_surface = NULL; | |
SDL_Texture* image_texture = NULL; | |
SDL_Rect image_destination = { 0 }; | |
int image_width = 0; | |
int image_height = 0; | |
std::stringstream error; | |
try { | |
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) < 0) { | |
error << "Failed to initialize SDL: " << SDL_GetError(); | |
throw(error.str()); | |
} | |
if ((main_window = SDL_CreateWindow("Use the arrow keys!", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screen_width, screen_height, 0)) == 0) { | |
error << "Failed to create a window: " << SDL_GetError(); | |
throw(error.str()); | |
} | |
if ((renderer = SDL_CreateRenderer(main_window, -1, 0)) == 0) { | |
error << "Failed to create the renderer: " << SDL_GetError(); | |
throw(error.str()); | |
} | |
// LOAD THE BACKGROUND BITMAP INTO A SURFACE & CONVERT IT TO A TEXTURE: | |
// ... the surface is only temporary, but rendering requires a texture. | |
if ((image_surface = SDL_LoadBMP(image_file.c_str())) == NULL) { | |
// Failure ... | |
error << "Failed load image \"" << image_file << "\": " << SDL_GetError(); | |
throw(error.str()); | |
} | |
else { | |
// Image will initially appear centered on the screen: | |
image_width = image_surface->w; | |
image_height = image_surface->h; | |
image_destination.x = (screen_width / 2) - (image_width / 2); | |
image_destination.y = (screen_height / 2) - (image_height / 2); | |
image_destination.w = image_width; | |
image_destination.h = image_height; | |
// Success (Continue converting it to a texture so it's usable) ... | |
image_texture = SDL_CreateTextureFromSurface(renderer, image_surface); | |
SDL_FreeSurface(image_surface); // The back surface is no longer needed now that we have a texture for it. | |
image_surface = NULL; | |
// If texture creation failed, now is the time to do something about it: | |
if (image_texture == NULL) { | |
// Failure ... | |
error << "Failed to create texture from surface: " << SDL_GetError(); | |
throw(error.str()); | |
} | |
} | |
// PRIMARY & EVENT LOOP: | |
while (!should_quit) { | |
while (SDL_PollEvent(&event)) { | |
switch (event.type) { | |
case SDL_QUIT: | |
should_quit = 1; | |
break; | |
case SDL_KEYDOWN: | |
switch (event.key.keysym.sym) { | |
case SDLK_ESCAPE: | |
should_quit = 1; | |
break; | |
} | |
break; | |
} | |
} | |
// CHECK THE KEYBOARD STATE FOR MOVEMENT KEYS: | |
// If you had used events for this instead (SDL_KEYDOWN/UP) you | |
// would have only been able to act on one key press at a time. | |
const Uint8 *state = SDL_GetKeyboardState(NULL); | |
if (state[SDL_SCANCODE_LEFT]) { | |
if (image_destination.x > 0) image_destination.x -= 1; | |
} | |
if (state[SDL_SCANCODE_RIGHT]) { | |
if (image_destination.x + image_width < screen_width) image_destination.x += 1; | |
} | |
if (state[SDL_SCANCODE_UP]) { | |
if (image_destination.y > 0) image_destination.y -= 1; | |
} | |
if (state[SDL_SCANCODE_DOWN]) { | |
if (image_destination.y + image_height < screen_height) image_destination.y += 1; | |
} | |
// RENDERING: | |
// Clear the background: | |
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | |
SDL_RenderClear(renderer); | |
// Render the entire logo to the center of the screen: | |
SDL_RenderCopy(renderer, image_texture, NULL, &image_destination); | |
SDL_RenderPresent(renderer); | |
} | |
} | |
catch (std::string error_str) { | |
std::cout << error_str << std::endl; | |
} | |
if (image_texture) SDL_DestroyTexture(image_texture); | |
if (renderer) SDL_DestroyRenderer(renderer); | |
if (main_window) SDL_DestroyWindow(main_window); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment