hibiscus/renderer/rsdl/source/gameengine_rsdl/package.d
2023-05-21 21:55:02 +01:00

62 lines
1.5 KiB
D

module gameengine_rsdl;
import bindbc.sdl;
import gameengine.renderer.renderer;
import gameengine.math.types;
import std.exception;
import std.format;
import std.string : toStringz;
/** \class RSDL
* Renderer implementation using SDL2 Renderer for the game engine
*/
class RSDL : Renderer {
this(string title, IntVec2 size) {
const SDLSupport ret = loadSDL();
if(ret != sdlSupport) {
throw new Exception("Couldn't load SDL library!");
}
SDL_Init(SDL_INIT_VIDEO);
// Create window. `title` is cast to a char* here as toStringz returns an immutable char* (causing an error)
window = SDL_CreateWindow(cast(char*)toStringz(title), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
size.x, size.y, SDL_WINDOW_RESIZABLE);
if (window is null) {
string error = format("Couldn't create window! what: %d", SDL_GetError());
throw new Exception(error);
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer is null) {
string error = format("Couldn't create renderer! what: %d", SDL_GetError());
throw new Exception(error);
}
}
~this() {
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
override void clearScreen(Color col = Color(0, 0, 0, 1)) {
SDL_SetRenderDrawColor(renderer, col.r, col.g, col.b, col.a);
SDL_RenderClear(renderer);
}
override void renderCurrent() {
SDL_RenderPresent(renderer);
}
override void preDraw() {}
override void postDraw() {}
override void setWindowTitle(string title) {}
private SDL_Window* window;
private SDL_Renderer* renderer;
}