Created
December 20, 2014 03:58
-
-
Save marchelzo/660bf677a329ce37df6b to your computer and use it in GitHub Desktop.
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_wrapper.hh" | |
#include <SDL2/SDL.h> | |
int main(int argc, char *argv[]) | |
{ | |
SDL::init(); | |
SDL_Event e; | |
bool quit{}; | |
while (!quit) { | |
while (SDL_PollEvent(&e)) | |
if (e.type == SDL_QUIT) | |
quit = true; | |
SDL::render_clear(); | |
SDL::render_present(); | |
} | |
SDL::quit(); | |
} |
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 <cstdio> | |
#include <string> | |
#include <SDL2/SDL.h> | |
#include <SDL2/SDL_image.h> | |
#include "sdl_wrapper.hh" | |
/* Global SDL State */ | |
static SDL_Window *window; | |
static SDL_Renderer *renderer; | |
static SDL_Texture *current_texture; | |
static constexpr char WINDOW_TITLE[] = "Motherload"; | |
static constexpr int WINDOW_HEIGHT = 480; | |
static constexpr int WINDOW_WIDTH = 640; | |
static constexpr int FPS = 60; | |
/* static helper functions */ | |
static SDL_Texture* load_texture(const std::string& path) | |
{ | |
SDL_Texture *new_texture = nullptr; | |
SDL_Surface *loaded_surface = IMG_Load(path.c_str()); | |
if (!loaded_surface) { | |
fprintf(stderr, "Unable to load image %s:\n%s\n", path.c_str(), IMG_GetError()); | |
} else { | |
new_texture = SDL_CreateTextureFromSurface(renderer, loaded_surface); | |
if (!new_texture) { | |
fprintf(stderr, "Unable to create texture from %s:\n%s\n", path.c_str(), SDL_GetError()); | |
} | |
SDL_FreeSurface(loaded_surface); | |
} | |
return new_texture; | |
} | |
bool SDL::init() | |
{ | |
if (SDL_Init(SDL_INIT_VIDEO) < 0) { | |
fprintf(stderr, "SDL could not initialize: %s\n", SDL_GetError()); | |
return false; | |
} | |
window = SDL_CreateWindow(WINDOW_TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, | |
WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN); | |
if (!window) { | |
fprintf(stderr, "Failed to create window: %s\n", SDL_GetError()); | |
return false; | |
} | |
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); | |
if (!renderer) { | |
fprintf(stderr, "Could not create renderer: %s\n", SDL_GetError()); | |
return false; | |
} | |
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); | |
if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)) { | |
fprintf(stderr, "SDL_image could not initialize: %s\n", IMG_GetError()); | |
return false; | |
} | |
return true; | |
} | |
void SDL::quit() | |
{ | |
SDL_DestroyTexture(current_texture); | |
SDL_DestroyRenderer(renderer); | |
SDL_DestroyWindow(window); | |
IMG_Quit(); | |
SDL_Quit(); | |
} | |
void SDL::render_clear() | |
{ | |
SDL_RenderClear(renderer); | |
} | |
void SDL::render_present() | |
{ | |
SDL_RenderPresent(renderer); | |
} |
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
#pragma once | |
/* public functions */ | |
namespace SDL { | |
bool init(); | |
void quit(); | |
void render_clear(); | |
void render_present(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment