Handle rendering of Rigids

This commit is contained in:
Ludovic 'Archivist' Lagouardette 2023-10-02 15:03:07 +02:00
parent 737afc8179
commit fc534fd8e1
7 changed files with 74 additions and 1 deletions

View file

@ -9,6 +9,7 @@ import std.format;
import std.conv : to;
import core.time;
import toki.symbols;
import world.screen;
extern(C) __gshared string[] rt_options = [ "gcopt=initReserve:128 minPoolSize:256 parallel:2 profile:1" ];
@ -17,6 +18,9 @@ int main(string[] args) {
try {
InitWindow(800,600,"Unnamed project");
log(LogLevel.INFOHIGH, "Window Initialized...");
Screen.x = Screen.y = 0;
Screen.width = Screen.px_width = 800;
Screen.height = Screen.px_height = 600;
log(LogLevel.INFOHIGH, "Readying the engine...");
GrEngine engine = new GrEngine();
auto grstd = grLoadStdLibrary();
@ -37,7 +41,6 @@ int main(string[] args) {
engine.load(bytecode);
log(LogLevel.INFOHIGH, "Go Hot Dog, Go...");
engine.process();
Toki[] p = convert("toki pona");
auto reference = MonoTime.currTime();
auto deltaTime = 1f/60f;

10
source/utils/shapes.d Normal file
View file

@ -0,0 +1,10 @@
module utils.shapes;
public struct Vec2f {
float x,y;
}
public struct Rectf {
float x, y;
float width, height;
}

7
source/world/entity.d Normal file
View file

@ -0,0 +1,7 @@
module world.entity;
import world.world_object;
class Entity : WorldObject {
}

7
source/world/package.d Normal file
View file

@ -0,0 +1,7 @@
module world;
public {
import world.world_object;
import world.entity;
import world.rigid;
}

25
source/world/rigid.d Normal file
View file

@ -0,0 +1,25 @@
module world.rigid;
import world.world_object;
import world.screen;
import raylib;
import std.conv;
class Rigid : WorldObject {
Texture2D sprite;
override void render(float) {
if(IsTextureReady(sprite)){
// Get position
int drawX = to!int(Screen.px_width*(position.x + hitbox.x - Screen.x)/Screen.width);
int drawY = to!int(Screen.px_height*(position.y + hitbox.y - Screen.y)/Screen.height);
float scale = Screen.px_height/Screen.height;
// Culling
if(drawX > Screen.width || drawX + sprite.width*scale < 0) return;
if(drawY > Screen.height || drawY + sprite.height*scale < 0) return;
DrawTextureEx(sprite, Vector2(drawX, drawY), 0, scale, Colors.WHITE);
}
}
}

9
source/world/screen.d Normal file
View file

@ -0,0 +1,9 @@
module world.screen;
public ScreenT Screen;
struct ScreenT {
float x,y;
float width, height;
int px_width, px_height;
}

View file

@ -0,0 +1,12 @@
module world.world_object;
import utils.shapes;
class WorldObject {
public Vec2f position;
public Rectf hitbox;
void update(float deltaTime);
void render(float deltaTime);
}