hibiscus/renderer/rsdl/source/hibis_rsdl.d
2023-05-22 21:22:13 +01:00

64 lines
1.6 KiB
D

module hibis_rsdl;
import bindbc.sdl;
import hibis.renderer.renderer;
import hibis.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 update() {}
override void setWindowTitle(string title) {}
private SDL_Window* window;
private SDL_Renderer* renderer;
}