Skip to content

Commit

Permalink
Reimplement most of the immediate mode operations in Draw, move it to
Browse files Browse the repository at this point in the history
luajit_ffi_gen.
  • Loading branch information
dgavedissian committed Nov 26, 2023
1 parent 8ebc864 commit 337dd10
Show file tree
Hide file tree
Showing 12 changed files with 793 additions and 532 deletions.
2 changes: 1 addition & 1 deletion engine/lib/phx/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{path::Path, env};
use std::{env, path::Path};

fn link_lib_from_cmake(lib: &str, root: &Path, path_segments: &[&str]) {
let mut path = root.to_path_buf();
Expand Down
71 changes: 0 additions & 71 deletions engine/lib/phx/script/ffi_common/Draw.lua

This file was deleted.

77 changes: 77 additions & 0 deletions engine/lib/phx/script/ffi_gen/Draw.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
-- Draw ------------------------------------------------------------------------
local Loader = {}

function Loader.declareType()
return 0, 'Draw'
end

function Loader.defineType()
local ffi = require('ffi')
local libphx = require('libphx').lib
local Draw

do -- C Definitions
ffi.cdef [[
void Draw_PushAlpha (float a);
void Draw_PopAlpha ();
void Draw_Axes (Vec3f const* pos, Vec3f const* x, Vec3f const* y, Vec3f const* z, float scale, float alpha);
void Draw_Border (float s, float x, float y, float w, float h);
void Draw_Box3 (Box3f const* b);
void Draw_Clear (float r, float g, float b, float a);
void Draw_ClearDepth (float d);
void Draw_Color (float r, float g, float b, float a);
void Draw_Flush ();
void Draw_Line (float x1, float y1, float x2, float y2);
void Draw_Line3 (Vec3f const* p1, Vec3f const* p2);
void Draw_LineWidth (float width);
void Draw_Plane (Vec3f const* p, Vec3f const* n, float scale);
void Draw_Point (float x, float y);
void Draw_Point3 (float x, float y, float z);
void Draw_PointSize (float size);
void Draw_Quad (Vec2f const* p1, Vec2f const* p2, Vec2f const* p3, Vec2f const* p4);
void Draw_Quad3 (Vec3f const* p1, Vec3f const* p2, Vec3f const* p3, Vec3f const* p4);
void Draw_Rect (float x1, float y1, float xs, float ys);
void Draw_SmoothLines (bool enabled);
void Draw_SmoothPoints (bool enabled);
void Draw_Sphere (Vec3f const* p, float r);
void Draw_Tri (Vec2f const* v1, Vec2f const* v2, Vec2f const* v3);
void Draw_Tri3 (Vec3f const* v1, Vec3f const* v2, Vec3f const* v3);
]]
end

do -- Global Symbol Table
Draw = {
PushAlpha = libphx.Draw_PushAlpha,
PopAlpha = libphx.Draw_PopAlpha,
Axes = libphx.Draw_Axes,
Border = libphx.Draw_Border,
Box3 = libphx.Draw_Box3,
Clear = libphx.Draw_Clear,
ClearDepth = libphx.Draw_ClearDepth,
Color = libphx.Draw_Color,
Flush = libphx.Draw_Flush,
Line = libphx.Draw_Line,
Line3 = libphx.Draw_Line3,
LineWidth = libphx.Draw_LineWidth,
Plane = libphx.Draw_Plane,
Point = libphx.Draw_Point,
Point3 = libphx.Draw_Point3,
PointSize = libphx.Draw_PointSize,
Quad = libphx.Draw_Quad,
Quad3 = libphx.Draw_Quad3,
Rect = libphx.Draw_Rect,
SmoothLines = libphx.Draw_SmoothLines,
SmoothPoints = libphx.Draw_SmoothPoints,
Sphere = libphx.Draw_Sphere,
Tri = libphx.Draw_Tri,
Tri3 = libphx.Draw_Tri3,
}

if onDef_Draw then onDef_Draw(Draw, mt) end
Draw = setmetatable(Draw, mt)
end

return Draw
end

return Loader
33 changes: 17 additions & 16 deletions engine/lib/phx/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct Engine {
window: Window,
cache: CachedWindow,
winit_window: WinitWindow,
draw: Draw,
hmgui: HmGui,
input: Input,
frame_state: FrameState,
Expand Down Expand Up @@ -59,14 +60,17 @@ impl Engine {

// Create window.
let window = Window::default();
let cache = CachedWindow { window: window.clone() };
let cache = CachedWindow {
window: window.clone(),
};
let winit_window = WinitWindow::new(&event_loop, &window);

Self {
init_time: TimeStamp::now(),
window,
cache,
winit_window,
draw: Draw::new(),
hmgui: HmGui::new(Font::load("Rajdhani", 14)),
input: Default::default(),
frame_state: Default::default(),
Expand Down Expand Up @@ -96,7 +100,9 @@ impl Engine {
}

if self.window.title != self.cache.window.title {
self.winit_window.window().set_title(self.window.title.as_str());
self.winit_window
.window()
.set_title(self.window.title.as_str());
}

if self.window.mode != self.cache.window.mode {
Expand Down Expand Up @@ -278,6 +284,8 @@ impl Engine {
let event_loop = EventLoop::new();
let mut engine = Engine::new(&event_loop);

Draw::set_instance(&mut engine.draw as *mut Draw);

let event_handler = move |event: Event<()>,
_: &EventLoopWindowTarget<()>,
control_flow: &mut ControlFlow| {
Expand Down Expand Up @@ -324,11 +332,7 @@ impl Engine {
engine.frame_state.low_power_event = false;
engine.frame_state.timeout_reached = false; //auto_timeout_reached || manual_timeout_reached;
}
event::Event::WindowEvent {
event,
window_id: _winit_window_id,
..
} => {
event::Event::WindowEvent { event, .. } => {
engine.frame_state.low_power_event = true;

match event {
Expand All @@ -338,14 +342,11 @@ impl Engine {
.resolution
.set_physical_resolution(size.width, size.height);
}
WindowEvent::ScaleFactorChanged {
new_inner_size,
..
} => {
engine
.window
.resolution
.set_physical_resolution(new_inner_size.width, new_inner_size.height);
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
engine.window.resolution.set_physical_resolution(
new_inner_size.width,
new_inner_size.height,
);
}
WindowEvent::CloseRequested => {
call_lua_func(&engine, "AppClose");
Expand Down Expand Up @@ -525,7 +526,7 @@ impl Engine {
let width = engine.window.resolution.physical_width();
let height = engine.window.resolution.physical_height();
engine.winit_window.resize(width, height)
},
}
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
// All other errors (Outdated, Timeout) should be resolved by the next frame
Expand Down
1 change: 1 addition & 0 deletions engine/lib/phx/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![allow(unused_imports)]
#![allow(unused_variables)]
#![feature(extern_types)]

pub mod audio;
Expand Down
Loading

0 comments on commit 337dd10

Please sign in to comment.