Skip to content

Instantly share code, notes, and snippets.

@BrentFarris
Created April 24, 2015 02:57
Show Gist options
  • Save BrentFarris/ba7ced930cc5422e5ff9 to your computer and use it in GitHub Desktop.
Save BrentFarris/ba7ced930cc5422e5ff9 to your computer and use it in GitHub Desktop.
SDL Tutorial 02 Renderer and Texture Code
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
const int SCREEN_WIDTH = 720;
const int SCREEN_HEIGHT = 480;
bool Initialize();
void Close();
SDL_Texture* LoadTexture(std::string file);
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Texture* texture = NULL;
int main(int argc, char* args[])
{
if (!Initialize())
{
printf("Could not initialize!\n");
return -1;
}
texture = LoadTexture("img2.png");
if (texture == NULL)
{
printf("Failed to load the image %s\n", "img2.png");
return -1;
}
bool exit = false;
SDL_Event e;
while (!exit)
{
while (SDL_PollEvent(&e) != 0)
{
if (e.type == SDL_QUIT || e.key.keysym.sym == SDLK_ESCAPE)
exit = true;
}
// Clear the renderer
SDL_RenderClear(renderer);
// Render the image
SDL_RenderCopy(renderer, texture, NULL, NULL);
// Show the final render
SDL_RenderPresent(renderer);
}
Close();
return 0;
}
bool Initialize()
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not be initialized! SDL_Error: %s\n", SDL_GetError());
return false;
}
window = SDL_CreateWindow("My SDL Game",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL)
{
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
return false;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL)
{
printf("We were not able to create the renderer! SDL Error: %s\n", SDL_GetError());
return false;
}
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
int imgFlags = IMG_INIT_PNG;
if (!(IMG_Init(imgFlags) & imgFlags))
{
printf("SDL_image could not be initialized! SDL_image Error: %s\n", IMG_GetError());
return false;
}
return true;
}
void Close()
{
SDL_DestroyWindow(window);
window = NULL;
SDL_Quit();
}
SDL_Texture* LoadTexture(std::string file)
{
SDL_Texture* newTexture = NULL;
SDL_Surface* loadedSurface = IMG_Load(file.c_str());
if (loadedSurface == NULL)
printf("Unable to load the image %s! SDL_image Error: %s\n", file.c_str(), IMG_GetError());
else
{
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
if (newTexture == NULL)
printf("Unable to create the texture from %s! SDL Error: %s\n", file.c_str(), SDL_GetError());
SDL_FreeSurface(loadedSurface);
}
return newTexture;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment