From 92803015adf19848c3f3a30caea889006ad05999 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Wed, 22 Mar 2023 12:46:23 -0500 Subject: tidy up main.lua --- honey/init.lua | 22 ++++++++++++++++++---- honey/shader.lua | 15 +++++++++++++++ honey/std.lua | 1 + 3 files changed, 34 insertions(+), 4 deletions(-) (limited to 'honey') diff --git a/honey/init.lua b/honey/init.lua index c3dda37..6afa324 100644 --- a/honey/init.lua +++ b/honey/init.lua @@ -2,9 +2,9 @@ local glfw = honey.glfw local gl = honey.gl local window = require 'honey.window' -local hinit = {} -setmetatable(hinit, {__index=_G}) -setfenv(1, hinit) +local module = {} +setmetatable(module, {__index=_G}) +setfenv(1, module) function init(width, height, title) @@ -16,14 +16,28 @@ function init(width, height, title) local window = honey.Window(width, height, title) glfw.MakeContextCurrent(window.win) gl.InitGlad() + gl.Enable(gl.DEPTH_TEST) return window end +function loop(window, update) + local prevTime = 0 + while not window:shouldClose() do + local time = glfw.GetTime() + local dt = time - prevTime + prevTime = time + update(dt) + window:swapBuffers() + glfw.PollEvents() + end +end + + function terminate() glfw.Terminate() end -return hinit +return module diff --git a/honey/shader.lua b/honey/shader.lua index 1fb9f5b..083b260 100644 --- a/honey/shader.lua +++ b/honey/shader.lua @@ -15,11 +15,26 @@ local function compileShader(source, type) return shader end +local function readFile(filename) + local f, err = io.open(filename) + if not f then error(err) end + local str = f:read("*a") + f:close() + return str +end + function Shader.new(_, sources) local self = {} self.locations = {} self.links = {} + if sources.vertexFile then + sources.vertex = readFile(sources.vertexFile) + end + if sources.fragmentFile then + sources.fragment = readFile(sources.fragmentFile) + end + local shaders = {} if sources.vertex then table.insert(shaders, compileShader(sources.vertex, gl.VERTEX_SHADER)) diff --git a/honey/std.lua b/honey/std.lua index 028a2a8..7cb7695 100644 --- a/honey/std.lua +++ b/honey/std.lua @@ -1,6 +1,7 @@ local init = require 'honey.init' honey.init = init.init +honey.loop = init.loop honey.terminate = init.terminate honey.ecs = require 'honey.ecs' -- cgit v1.2.1