113 lines
3.1 KiB
D
113 lines
3.1 KiB
D
module hibis_rsdl;
|
|
|
|
import bindbc.sdl;
|
|
import bindbc.freetype.bind;
|
|
import bindbc.freetype.bind.ftbitmap;
|
|
import hibis.renderer.renderer;
|
|
import hibis.resources.font;
|
|
import hibis.math.types;
|
|
import std.exception;
|
|
import std.format;
|
|
|
|
import std.stdio;
|
|
import std.string : toStringz;
|
|
import core.stdcpp.vector;
|
|
|
|
/** \class RSDL
|
|
* Renderer implementation using SDL2 Renderer for the Hibis 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_UpdateWindowSurface(window);
|
|
SDL_RenderPresent(renderer);
|
|
}
|
|
|
|
override void drawText(Font font, string text, int size, IntVec2 pos) {
|
|
IntVec2 dupPos = { x: pos.x, y: pos.y };
|
|
dupPos.y += size-4;
|
|
FT_Set_Pixel_Sizes(font.face, size, size);
|
|
for (int character = 0; character < text.length; character++) {
|
|
if (text[character] == '\n') {
|
|
dupPos.x = pos.x;
|
|
dupPos.y += size;
|
|
} else {
|
|
FT_Load_Char(font.face, text[character], FT_LOAD_RENDER);
|
|
|
|
drawBitmap(font.face.glyph.bitmap, size, dupPos.x + font.face.glyph.bitmap_left, dupPos.y - font.face.glyph.bitmap_top);
|
|
|
|
dupPos.x += size;
|
|
}
|
|
}
|
|
}
|
|
|
|
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 void drawBitmap(FT_Bitmap bitmap, int size, int x, int y) {
|
|
SDL_Surface* glyph = SDL_CreateRGBSurfaceFrom(bitmap.buffer, bitmap.width, bitmap.rows, 8, bitmap.pitch, 0, 0, 0, 0xFF);
|
|
|
|
SDL_SetSurfaceBlendMode(glyph, 0);
|
|
|
|
SDL_Color[256] colors;
|
|
for (int i = 0; i < 256; i++) {
|
|
ubyte newC = cast(ubyte)i;
|
|
colors[i].r = colors[i].g = colors[i].b = newC;
|
|
}
|
|
SDL_SetPaletteColors(glyph.format.palette, cast(const(SDL_Color)*)colors, 0, 256);
|
|
|
|
SDL_Rect dest = { x, y, size, size};
|
|
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, glyph);
|
|
SDL_RenderCopy(renderer, texture, null, &dest);
|
|
SDL_DestroyTexture(texture);
|
|
SDL_FreeSurface(glyph);
|
|
}
|
|
|
|
private SDL_Window* window;
|
|
private SDL_Renderer* renderer;
|
|
} |