Initial rendering

This commit is contained in:
Tulpen 2023-05-21 14:02:53 +01:00
parent cf60b9ace5
commit cbe861693e
5 changed files with 71 additions and 7 deletions

5
.gitignore vendored
View file

@ -1,6 +1,9 @@
# Directories # Directories
.dub .dub
*/docs/ */docs/
.vscode
# Files
*.so *.so
*.dylib *.dylib
*.dll *.dll
@ -13,8 +16,6 @@
*.o *.o
*.obj *.obj
*.lst *.lst
# Files
docs.json docs.json
__dummy.html __dummy.html
dub.selections.json dub.selections.json

View file

@ -3,9 +3,9 @@ module gameengine.math.types;
// Structs // Structs
/// Image /// Image
// RGBA value struct // RGBA value struct. Value is between 0 and 255
struct Color { struct Color {
float r, g, b, a; ubyte r, g, b, a;
} }
// 2D // 2D

View file

@ -1,5 +1,16 @@
module gameengine.renderer.renderer; module gameengine.renderer.renderer;
import gameengine.math.types;
abstract class Renderer { 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);
} }

View file

@ -1,7 +1,55 @@
module gameengine_rsdl; module gameengine_rsdl;
import bindbc.sdl;
import gameengine.renderer.renderer; import gameengine.renderer.renderer;
import gameengine.math.types;
import std.exception;
import std.format;
class RSDL : Renderer { 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) {} override void setWindowTitle(string title) {}
private SDL_Window* window;
private SDL_Renderer* renderer;
} }

View file

@ -1,17 +1,21 @@
import std.stdio; import std.stdio;
import gameengine.core.engine; import gameengine.core.engine;
import gameengine.math.types;
import gameengine.renderer.renderer; import gameengine.renderer.renderer;
import gameengine_rsdl; import gameengine_rsdl;
import core.thread; import core.thread;
import std.string : toStringz;
void main() { void main() {
Renderer renderer = new RSDL(); Renderer renderer = new RSDL(cast(char*)toStringz("test".dup), IntVec2(800, 600));
Engine engine = new Engine(renderer); Engine engine = new Engine(renderer);
while (true) { while (true) {
engine.runNodeProcesses(); engine.runNodeProcesses();
Thread.sleep(1.msecs); renderer.clearScreen(Color(255, 0, 255, 255));
renderer.renderCurrent();
Thread.sleep(16.msecs);
} }
} }