Skip to content

Commit 4b7009a

Browse files
committed
Implement overlay plugin and marching ants
1 parent af92fe7 commit 4b7009a

File tree

5 files changed

+346
-13
lines changed

5 files changed

+346
-13
lines changed

src/bindings/gl.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ local nonCoreFnDefs = {
8080
glDispatchCompute = "void(*)(GLuint, GLuint, GLuint)",
8181
glMemoryBarrier = "void(*)(unsigned int)",
8282
glBindImageTexture = "void(*)(GLuint, GLuint, GLint, unsigned char, GLint, GLenum, GLenum)",
83+
84+
glCreateFramebuffers = "void(*)(GLsizei, GLuint*)",
85+
glBindFramebuffer = "void(*)(GLenum, GLuint)",
86+
glNamedFramebufferTexture = "void(*)(GLuint, GLenum, GLuint, GLint)",
87+
glNamedFramebufferTextureLayer = "void(*)(GLuint, GLenum, GLuint, GLint, GLint)",
88+
glCheckNamedFramebufferStatus = "GLenum(*)(GLuint, GLenum)",
89+
glDeleteFramebuffers = "void(*)(GLsizei, const GLuint*)",
8390
}
8491

8592
---@type fun(name: string): function
@@ -186,11 +193,16 @@ return {
186193
SYNC_GPU_COMMANDS_COMPLETE = 0x9117,
187194
SYNC_FLUSH_COMMANDS_BIT = 0x00000001,
188195

196+
LESS = 0x0201,
189197
LESS = 0x0201,
190198
LESS_EQUAL = 0x0203,
191199
GREATER = 0x0204,
192200
GREATER_EQUAL = 0x0206,
193201

202+
FRAMEBUFFER = 0x8D40,
203+
COLOR_ATTACHMENT0 = 0x8CE0,
204+
FRAMEBUFFER_COMPLETE = 0x8CD5,
205+
194206
--- @param type ShaderType
195207
--- @param src string
196208
--- @return number
@@ -356,4 +368,29 @@ return {
356368

357369
---@type fun(func: number)
358370
depthFunc = C.glDepthFunc,
371+
372+
---@return number
373+
createFramebuffer = function()
374+
local fboId = ffi.new("GLuint[1]")
375+
C.glCreateFramebuffers(1, fboId)
376+
return fboId[0]
377+
end,
378+
379+
---@type fun(n: number, framebuffers: userdata)
380+
createFramebuffers = C.glCreateFramebuffers,
381+
382+
---@type fun(framebuffer: number, attachment: number, texture: number, level: number)
383+
namedFramebufferTexture = C.glNamedFramebufferTexture,
384+
385+
---@type fun(framebuffer: number, attachment: number, texture: number, level: number, layer: number)
386+
namedFramebufferTextureLayer = C.glNamedFramebufferTextureLayer,
387+
388+
---@type fun(framebuffer: number, target: number): number
389+
checkNamedFramebufferStatus = C.glCheckNamedFramebufferStatus,
390+
391+
---@type fun(target: number, framebuffer: number)
392+
bindFramebuffer = C.glBindFramebuffer,
393+
394+
---@type fun(n: number, framebuffers: userdata)
395+
deleteFramebuffers = C.glDeleteFramebuffers,
359396
}

src/main.lua

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ local RenderPlugin = require("plugin.render")
1010
local LayoutPlugin = require("plugin.layout")
1111
local TextPlugin = require("plugin.text")
1212
local UIPlugin = require("plugin.ui")
13+
local OverlayPlugin = require("plugin.overlay")
1314

1415
---@alias Message
1516
--- | { type: "onWindowCreate", window: Window }
@@ -60,6 +61,7 @@ local UIPlugin = require("plugin.ui")
6061
---@field text plugin.Text
6162
---@field ui plugin.UI
6263
---@field layout plugin.Layout
64+
---@field overlay plugin.Overlay
6365

6466
---@alias App.Tool "brush" | "eraser" | "fill" | "pencil" | "text" | "select" | "square" | "circle" | "line" | "curve"
6567

@@ -74,6 +76,7 @@ local UIPlugin = require("plugin.ui")
7476
---@field isDrawing boolean
7577
---@field currentColor { r: number, g: number, b: number, a: number }
7678
---@field currentAction App.Action
79+
---@field startTime number
7780
local App = {}
7881
App.__index = App
7982

@@ -84,10 +87,12 @@ function App.new()
8487
self.plugins.text = TextPlugin.new(self.plugins.render)
8588
self.plugins.layout = LayoutPlugin.new(function(w) return self:view(w) end, self.plugins.text)
8689
self.plugins.ui = UIPlugin.new(self.plugins.layout, self.plugins.render)
90+
self.plugins.overlay = OverlayPlugin.new(self.plugins.render)
8791

8892
self.isDrawing = false
8993
self.currentColor = { r = 0, g = 0, b = 0, a = 1 }
9094
self.currentAction = { tool = "brush" }
95+
self.startTime = os.clock()
9196

9297
return self
9398
end
@@ -650,6 +655,14 @@ function App:view(window)
650655
elementHeight = elementHeight,
651656
}
652657
end),
658+
Element.new("div")
659+
:withStyle({
660+
bgImage = self.plugins.overlay:getTexture(window),
661+
width = { rel = 1 },
662+
height = { rel = 1 },
663+
margin = { right = 20, left = 20, top = 20, bottom = 20 },
664+
position = "relative",
665+
}),
653666
}),
654667

655668
Element.new("div")
@@ -674,6 +687,46 @@ function App:event(event, handler)
674687
return windowUpdate
675688
end
676689

690+
if event.name == "redraw" then
691+
local ctx = self.plugins.render:getContext(event.window)
692+
self.plugins.render:draw(ctx)
693+
694+
self.plugins.overlay:clear(event.window)
695+
696+
if self.currentAction.tool == "select" and self.currentAction.start then
697+
local start = self.currentAction.start
698+
local finish = self.currentAction.finish or start
699+
700+
local x1 = start.x
701+
local y1 = start.y
702+
local x2 = finish.x
703+
local y2 = finish.y
704+
705+
local boxX = math.min(x1, x2)
706+
local boxY = math.min(y1, y2)
707+
local boxW = math.abs(x2 - x1)
708+
local boxH = math.abs(y2 - y1)
709+
710+
self.plugins.overlay:addBox(
711+
event.window,
712+
boxX, boxY, boxW, boxH,
713+
{ r = 0, g = 0, b = 0, a = 1 },
714+
2
715+
)
716+
end
717+
718+
local time = os.clock() - self.startTime
719+
self.plugins.overlay:draw(event.window, "marching_ants", time)
720+
721+
ctx.renderCtx:swapBuffers()
722+
723+
if self.currentAction.tool == "select" and (self.currentAction.start or self.isDrawing) then
724+
handler:requestRedraw(event.window)
725+
end
726+
727+
return nil
728+
end
729+
677730
local renderUpdate = self.plugins.render:event(event, handler)
678731
if renderUpdate then
679732
return renderUpdate
@@ -691,6 +744,7 @@ function App:update(message, window)
691744
if message.type == "onWindowCreate" then
692745
-- Now we can initialize assets for a specific window
693746
self.plugins.render:register(window)
747+
self.plugins.overlay:register(window)
694748

695749
if window == self.plugins.window.mainCtx.window then
696750
self.resources = self:makeResources()
@@ -719,6 +773,7 @@ function App:update(message, window)
719773
local x = (message.x / message.elementWidth) * 800
720774
local y = (message.y / message.elementHeight) * 600
721775
self.currentAction.start = { x = x, y = y }
776+
self.currentAction.finish = nil
722777
elseif self.currentAction.tool == "pencil" then
723778
self.resources.compute:stamp(
724779
(message.x / message.elementWidth) * 800,
@@ -751,9 +806,11 @@ function App:update(message, window)
751806
local startPos = { x = math.min(start.x, x), y = math.min(start.y, y) }
752807
local finishPos = { x = math.max(start.x, x), y = math.max(start.y, y) }
753808
self.resources.compute:setSelection(startPos.x, startPos.y, finishPos.x, finishPos.y)
809+
self.currentAction.finish = { x = x, y = y }
754810
end
755811
end
756812
self.isDrawing = false
813+
window.shouldRedraw = true
757814
elseif message.type == "Hovered" then
758815
if self.isDrawing then
759816
if self.currentAction.tool == "eraser" then
@@ -776,6 +833,10 @@ function App:update(message, window)
776833
1,
777834
self.currentColor
778835
)
836+
elseif self.currentAction.tool == "select" and self.currentAction.start then
837+
local x = (message.x / message.elementWidth) * 800
838+
local y = (message.y / message.elementHeight) * 600
839+
self.currentAction.finish = { x = x, y = y }
779840
end
780841
self.plugins.ui:refreshView(window)
781842
end
@@ -785,8 +846,10 @@ function App:update(message, window)
785846
elseif message.type == "ToolClicked" then
786847
if message.tool == "select" then
787848
self.resources.compute:resetSelection()
849+
self.currentAction = { tool = message.tool }
850+
else
851+
self.currentAction = { tool = message.tool }
788852
end
789-
self.currentAction = { tool = message.tool }
790853
self.plugins.ui:refreshView(window)
791854
elseif message.type == "ClearClicked" then
792855
local textureManager = self.plugins.render.sharedResources.textureManager

0 commit comments

Comments
 (0)