117 lines
2.9 KiB
D
117 lines
2.9 KiB
D
module hibis_rsdl.rsdl;
|
|
|
|
import hibis.resources.resource;
|
|
import hibis.callback;
|
|
import hibis.logging.types;
|
|
import hibis_rsdl.resources.font;
|
|
|
|
import bindbc.sdl;
|
|
import sdl_ttf;
|
|
import hibis.renderer.renderer;
|
|
import hibis.math.types;
|
|
import std.exception;
|
|
import std.format;
|
|
|
|
import std.stdio;
|
|
import std.string : toStringz;
|
|
|
|
/** \class RSDL
|
|
* Renderer implementation using SDL2 Renderer for the Hibis game engine
|
|
*/
|
|
class RSDL : Renderer {
|
|
this(string title, IntVec2 size, LoggerCallback callback) {
|
|
this.logger = callback;
|
|
const SDLSupport ret = loadSDL();
|
|
if(ret != sdlSupport) {
|
|
throw new Exception("Couldn't load SDL library!");
|
|
}
|
|
|
|
const SDLTTFSupport ttf = loadSDLTTF();
|
|
|
|
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());
|
|
SDL_Quit();
|
|
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());
|
|
SDL_DestroyWindow(window);
|
|
SDL_Quit();
|
|
throw new Exception(error);
|
|
}
|
|
|
|
if (TTF_Init() != 0) {
|
|
logger(LoggingSeverity.Fatal, format("Couldn't load SDL_TTF! what: %d", TTF_GetError()));
|
|
SDL_DestroyRenderer(renderer);
|
|
SDL_DestroyWindow(window);
|
|
SDL_Quit();
|
|
TTF_Quit();
|
|
throw new Exception("");
|
|
}
|
|
}
|
|
|
|
~this() {
|
|
TTF_Quit();
|
|
SDL_DestroyRenderer(renderer);
|
|
SDL_DestroyWindow(window);
|
|
SDL_Quit();
|
|
|
|
unloadSDLTTF();
|
|
unloadSDL();
|
|
}
|
|
|
|
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_UpdateWindowSurface(window);
|
|
SDL_RenderPresent(renderer);
|
|
}
|
|
|
|
override void drawText(Resource resource, string text, IntRect rect, Color color) {
|
|
if (Font font = cast(Font)resource) {
|
|
SDL_Surface* textSurface = TTF_RenderText_Solid(font.loadedFont, cast(const(char)*)toStringz(text), SDL_Color(color.r, color.g, color.b, color.a));
|
|
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
|
|
|
|
SDL_Rect textRect;
|
|
textRect.x = rect.x;
|
|
textRect.y = rect.y;
|
|
textRect.w = rect.w;
|
|
textRect.h = rect.h;
|
|
|
|
SDL_RenderCopy(renderer, textTexture, null, &textRect);
|
|
|
|
SDL_DestroyTexture(textTexture);
|
|
SDL_FreeSurface(textSurface);
|
|
}
|
|
}
|
|
|
|
override void preDraw() {}
|
|
override void postDraw() {}
|
|
|
|
override void update() {
|
|
SDL_Event event;
|
|
|
|
while(SDL_PollEvent(&event)) {
|
|
if (event.type == SDL_QUIT) keepOpen = false;
|
|
}
|
|
}
|
|
|
|
override void setWindowTitle(string title) {}
|
|
|
|
private SDL_Window* window;
|
|
private SDL_Renderer* renderer;
|
|
|
|
private LoggerCallback logger;
|
|
} |