Skip to content

Instantly share code, notes, and snippets.

@riptl
Created March 26, 2018 14:37
Show Gist options
  • Save riptl/33b397ab3f648fabb504566e896f6ca7 to your computer and use it in GitHub Desktop.
Save riptl/33b397ab3f648fabb504566e896f6ca7 to your computer and use it in GitHub Desktop.
Mediocre SDL C++ Wrappers
#include "sdlwrappers.h"
#include <SDL2/SDL.h>
#define CHECK_OBJ {if (obj == nullptr) throw std::string(SDL_GetError());}
using namespace sdl;
Context::Context()
{
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
throw "Could not init SDL.";
}
}
Context::~Context()
{
SDL_Quit();
}
DisplayMode::DisplayMode(const Context &, int index)
{
SDL_GetCurrentDisplayMode(index, &obj);
}
Window::Window(const Context &, const char *title, int x, int y, int w, int h, Uint32 flags)
: obj(SDL_CreateWindow(title, x, y, w, h, flags))
CHECK_OBJ
Window::~Window()
{
if (!moved)
SDL_DestroyWindow(obj);
}
Window::Window(Window &&w) noexcept
: obj(w.obj)
{
w.moved = true;
}
Surface::Surface(SDL_Surface *srf)
: obj(srf)
{
moved = true;
}
static inline SDL_Surface *create_surface_from_png(const png::image<png::rgb_pixel> &img)
{
auto w = static_cast<unsigned>(img.get_width());
auto h = static_cast<unsigned>(img.get_height());
auto numPixels = w * h;
auto pixelSize = 3;
auto pitch = w * pixelSize;
auto numBytes = numPixels * pixelSize;
const auto &pbuf = img.get_pixbuf();
auto *buf = new uint8_t[numBytes];
auto *ptr = buf;
for (unsigned y = 0; y < h; ++y) {
for (unsigned x = 0; x < w; ++x) {
auto pixel = pbuf.get_pixel(x, y);
*(ptr++) = pixel.red;
*(ptr++) = pixel.green;
*(ptr++) = pixel.blue;
}
}
auto srf = SDL_CreateRGBSurfaceWithFormatFrom(buf, w, h, 24, pitch, SDL_PIXELFORMAT_RGB24);
delete [] buf;
return srf;
}
Surface::Surface(const png::image<png::rgb_pixel> &img)
: obj(create_surface_from_png(img))
CHECK_OBJ
Surface::Surface(Uint32 flags, int w, int h, int depth, Uint32 format)
: obj(SDL_CreateRGBSurfaceWithFormat(flags, w, h, depth, format))
CHECK_OBJ
Surface::Surface(const Surface &srf, const SDL_PixelFormat *fmt, Uint32 flags)
: obj(SDL_ConvertSurface(srf.obj, fmt, flags))
CHECK_OBJ
Surface::Surface(Surface &&o) noexcept
: obj(o.obj)
, moved(o.moved)
{
o.moved = true;
}
Surface::~Surface()
{
if (!moved)
SDL_FreeSurface(obj);
}
int blitSurface(const Surface &src, const SDL_Rect &srcRect, const Surface &dst, SDL_Rect &dstRect)
{
return SDL_BlitSurface(src.obj, &srcRect, dst.obj, &dstRect);
}
Renderer::Renderer(const Window &window, Uint32 flags)
: obj(SDL_CreateRenderer(window.obj, -1, flags))
CHECK_OBJ
Renderer::~Renderer()
{
if (!moved)
SDL_DestroyRenderer(obj);
}
Renderer::Renderer(Renderer &&o) noexcept
: obj(o.obj)
{
o.moved = true;
}
Texture::Texture(const Renderer &r, const Surface &s)
: obj(SDL_CreateTextureFromSurface(r.obj, s.obj))
CHECK_OBJ
Texture::~Texture()
{
SDL_DestroyTexture(obj);
}
#ifndef QUAXCOPTER_SDLWRAPPERS_H
#define QUAXCOPTER_SDLWRAPPERS_H
// C++ syntax für C-Bibliothek SDL
#include <string>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_system.h>
#include <SDL_messagebox.h>
#include <SDL_timer.h>
#include <png++/rgb_pixel.hpp>
#include <png++/image.hpp>
namespace sdl {
typedef SDL_Rect Rect;
// Replacement for rect nullptr
const Rect NoRect {-1};
inline bool ValidRect(const Rect &r) {
return r.x != -1;
}
class Context {
public:
Context();
~Context();
Context(const Context &) = delete;
Context(Context &&) = default;
inline void showSimpleMessageBox(Uint32 flags, const char *title, const char *message) {
SDL_ShowSimpleMessageBox(flags, title, message, nullptr);
}
inline void delay(Uint32 ms) {
SDL_Delay(ms);
}
};
class DisplayMode {
public:
DisplayMode(const Context &, int index); // GetCurrentDisplayMode
SDL_DisplayMode obj;
};
class Surface {
public:
explicit Surface(SDL_Surface *);
explicit Surface(const png::image<png::rgb_pixel> &);
Surface(Uint32 flags, int w, int h, int depth, Uint32 format);
Surface(const Surface &, const SDL_PixelFormat *fmt, Uint32 flags = 0);
Surface(const Surface &) = delete;
Surface(Surface &&) noexcept;
~Surface();
SDL_Surface * const obj;
private:
bool moved = false;
};
class Window {
public:
Window(const Context &, const char *title, int x, int y, int w, int h, Uint32 flags);
~Window();
Window(const Window &) = delete;
Window(Window &&) noexcept;
inline void show() {
SDL_ShowWindow(obj);
}
SDL_Window * const obj;
private:
bool moved;
};
int blitSurface(const Surface &src, const SDL_Rect &srcRect, const Surface &dst, const SDL_Rect &dstRect);
class Renderer;
class Texture {
public:
Texture(const Renderer &, const Surface &);
~Texture();
Texture(const Texture &) = delete;
SDL_Texture * const obj;
};
class Renderer {
public:
explicit Renderer(const Window &, Uint32 flags = 0);
Renderer(const Renderer &) = delete;
Renderer(Renderer &&) noexcept;
~Renderer();
inline int setRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a = 0xFF) {
return SDL_SetRenderDrawColor(obj, r, g, b, a);
}
inline int clear() {
return SDL_RenderClear(obj);
}
inline void present() {
return SDL_RenderPresent(obj);
}
inline int setTarget(const Texture &t) {
return SDL_SetRenderTarget(obj, t.obj);
}
inline int drawRect(const Rect &r) {
return SDL_RenderDrawRect(obj, &r);
}
inline int fillRect(const Rect &r) {
return SDL_RenderFillRect(obj, &r);
}
inline int drawPoint(int x, int y) {
return SDL_RenderDrawPoint(obj, x, y);
}
inline int copy(const Texture &t, const Rect &src = NoRect, const Rect &dst = NoRect) {
auto srcPtr = ValidRect(src) ? &src : nullptr;
auto dstPtr = ValidRect(dst) ? &dst : nullptr;
return SDL_RenderCopy(obj, t.obj, srcPtr, dstPtr);
}#ifndef QUAXCOPTER_SDLWRAPPERS_H
#define QUAXCOPTER_SDLWRAPPERS_H
// C++ syntax für C-Bibliothek SDL
#include <string>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_system.h>
#include <SDL_messagebox.h>
#include <SDL_timer.h>
#include <png++/rgb_pixel.hpp>
#include <png++/image.hpp>
namespace sdl {
typedef SDL_Rect Rect;
// Replacement for rect nullptr
const Rect NoRect {-1};
inline bool ValidRect(const Rect &r) {
return r.x != -1;
}
class Context {
public:
Context();
~Context();
Context(const Context &) = delete;
Context(Context &&) = default;
inline void showSimpleMessageBox(Uint32 flags, const char *title, const char *message) {
SDL_ShowSimpleMessageBox(flags, title, message, nullptr);
}
inline void delay(Uint32 ms) {
SDL_Delay(ms);
}
};
class DisplayMode {
public:
DisplayMode(const Context &, int index); // GetCurrentDisplayMode
SDL_DisplayMode obj;
};
class Surface {
public:
explicit Surface(SDL_Surface *);
explicit Surface(const png::image<png::rgb_pixel> &);
Surface(Uint32 flags, int w, int h, int depth, Uint32 format);
Surface(const Surface &, const SDL_PixelFormat *fmt, Uint32 flags = 0);
Surface(const Surface &) = delete;
Surface(Surface &&) noexcept;
~Surface();
SDL_Surface * const obj;
private:
bool moved = false;
};
class Window {
public:
Window(const Context &, const char *title, int x, int y, int w, int h, Uint32 flags);
~Window();
Window(const Window &) = delete;
Window(Window &&) noexcept;
inline void show() {
SDL_ShowWindow(obj);
}
SDL_Window * const obj;
private:
bool moved;
};
int blitSurface(const Surface &src, const SDL_Rect &srcRect, const Surface &dst, const SDL_Rect &dstRect);
class Renderer;
class Texture {
public:
Texture(const Renderer &, const Surface &);
~Texture();
Texture(const Texture &) = delete;
SDL_Texture * const obj;
};
class Renderer {
public:
explicit Renderer(const Window &, Uint32 flags = 0);
Renderer(const Renderer &) = delete;
Renderer(Renderer &&) noexcept;
~Renderer();
inline int setRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a = 0xFF) {
return SDL_SetRenderDrawColor(obj, r, g, b, a);
}
inline int clear() {
return SDL_RenderClear(obj);
}
inline void present() {
return SDL_RenderPresent(obj);
}
inline int setTarget(const Texture &t) {
return SDL_SetRenderTarget(obj, t.obj);
}
inline int drawRect(const Rect &r) {
return SDL_RenderDrawRect(obj, &r);
}
inline int fillRect(const Rect &r) {
return SDL_RenderFillRect(obj, &r);
}
inline int drawPoint(int x, int y) {
return SDL_RenderDrawPoint(obj, x, y);
}
inline int copy(const Texture &t, const Rect &src = NoRect, const Rect &dst = NoRect) {
auto srcPtr = ValidRect(src) ? &src : nullptr;
auto dstPtr = ValidRect(dst) ? &dst : nullptr;
return SDL_RenderCopy(obj, t.obj, srcPtr, dstPtr);
}
SDL_Renderer * const obj;
private:
bool moved = false;
};
}
#endif //QUAXCOPTER_SDLWRAPPERS_H
SDL_Renderer * const obj;
private:
bool moved = false;
};
}
#endif //QUAXCOPTER_SDLWRAPPERS_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment