From cbe861693e33999c11e46fb679d544a23ddb1da8 Mon Sep 17 00:00:00 2001 From: tulpenkiste Date: Sun, 21 May 2023 14:02:53 +0100 Subject: [PATCH] Initial rendering --- .gitignore | 5 +- engine/source/math/types.d | 4 +- engine/source/renderer/renderer.d | 13 ++++- .../rsdl/source/gameengine_rsdl/package.d | 48 +++++++++++++++++++ test/source/app.d | 8 +++- 5 files changed, 71 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index a638545..c1a633e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ # Directories .dub */docs/ +.vscode + +# Files *.so *.dylib *.dll @@ -13,8 +16,6 @@ *.o *.obj *.lst - -# Files docs.json __dummy.html dub.selections.json \ No newline at end of file diff --git a/engine/source/math/types.d b/engine/source/math/types.d index 0fd7241..dc7fb02 100644 --- a/engine/source/math/types.d +++ b/engine/source/math/types.d @@ -3,9 +3,9 @@ module gameengine.math.types; // Structs /// Image -// RGBA value struct +// RGBA value struct. Value is between 0 and 255 struct Color { - float r, g, b, a; + ubyte r, g, b, a; } // 2D diff --git a/engine/source/renderer/renderer.d b/engine/source/renderer/renderer.d index d0efe06..1036213 100644 --- a/engine/source/renderer/renderer.d +++ b/engine/source/renderer/renderer.d @@ -1,5 +1,16 @@ module gameengine.renderer.renderer; +import gameengine.math.types; + abstract class Renderer { - void setWindowTitle(string str); + // Draw + void clearScreen(Color); + void renderCurrent(); + + // Pre and Post draw + void preDraw(); + void postDraw(); + + // Util + void setWindowTitle(string); } \ No newline at end of file diff --git a/renderer/rsdl/source/gameengine_rsdl/package.d b/renderer/rsdl/source/gameengine_rsdl/package.d index 5dd1e9a..f12b438 100644 --- a/renderer/rsdl/source/gameengine_rsdl/package.d +++ b/renderer/rsdl/source/gameengine_rsdl/package.d @@ -1,7 +1,55 @@ module gameengine_rsdl; +import bindbc.sdl; import gameengine.renderer.renderer; +import gameengine.math.types; +import std.exception; +import std.format; class RSDL : Renderer { + this(char* title, IntVec2 size) { + const SDLSupport ret = loadSDL(); + if(ret != sdlSupport) { + throw new Exception("Couldn't load SDL"); + } + + SDL_Init(SDL_INIT_VIDEO); + + window = SDL_CreateWindow(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; } \ No newline at end of file diff --git a/test/source/app.d b/test/source/app.d index c2713a8..67033f9 100644 --- a/test/source/app.d +++ b/test/source/app.d @@ -1,17 +1,21 @@ import std.stdio; import gameengine.core.engine; +import gameengine.math.types; import gameengine.renderer.renderer; import gameengine_rsdl; import core.thread; +import std.string : toStringz; void main() { - Renderer renderer = new RSDL(); + Renderer renderer = new RSDL(cast(char*)toStringz("test".dup), IntVec2(800, 600)); Engine engine = new Engine(renderer); while (true) { engine.runNodeProcesses(); - Thread.sleep(1.msecs); + renderer.clearScreen(Color(255, 0, 255, 255)); + renderer.renderCurrent(); + Thread.sleep(16.msecs); } }