Skip to content

Instantly share code, notes, and snippets.

@marchelzo
Created December 20, 2014 03:58
Show Gist options
  • Save marchelzo/660bf677a329ce37df6b to your computer and use it in GitHub Desktop.
Save marchelzo/660bf677a329ce37df6b to your computer and use it in GitHub Desktop.
#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();
}
#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);
}
#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