From 64be3e5d5788e5e866d7f5fb7b1c6f754735f2f4 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Thu, 13 Apr 2023 15:13:46 -0500 Subject: add message-passing --- main.lua | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'main.lua') diff --git a/main.lua b/main.lua index f5512b0..5ef09dd 100644 --- a/main.lua +++ b/main.lua @@ -43,9 +43,18 @@ package.loaded['baseRotationScript'] = function(db, id, dt) transform.matrix:rotateZ(math.pi * dt) end package.loaded['cameraRotationScript'] = function(db, id, dt) - local transform = db:getComponent(id, "transform") - transform.matrix:identity():translate(Vec3{0, 0, -6 + math.sin(math.pi * glfw.GetTime())}) + local transform = db:getComponent(id, "transform") + transform.matrix:identity():translate(Vec3{0, 0, -6 + math.sin(math.pi * glfw.GetTime())}) +end + + +function transmitMessage(msg, data) + local query = db:queryComponent(msg) + for id, handler in pairs(query) do + local f = require(handler.scriptName) + f(db, id, data) end +end local id = db:createEntity() @@ -87,11 +96,16 @@ end) -- resize window correctly window:setFramebufferSizeCallback(function(_, width, height) gl.Viewport(0, 0, width, height) - local cameraParams = db:getComponent(camera, "camera") - cameraParams.projection:perspectiveResize(width/height) vw, vh = width, height + transmitMessage("onWindowResize", { width=width, height=height }) end) +package.loaded["cameraHandleResize"] = function(db, id, data) + local camera = db:getComponent(id, "camera") + camera.projection:perspectiveResize(data.width/data.height) +end +db:addComponent(camera, "onWindowResize", { scriptName = "cameraHandleResize" }) + -- averager (for fps) function averager(memory) local buf = {} -- cgit v1.2.1