-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglfw_opengl3.nelua
More file actions
251 lines (214 loc) · 7.86 KB
/
glfw_opengl3.nelua
File metadata and controls
251 lines (214 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
require 'os'
require'math'
--
require'cimgui'
require'simple'
require'imgui_backend_opengl3'
require'imgui_backend_glfw'
require'loadImage'
require'saveImage'
require'utils'
require'IconsFontAwesome6'
--- External functions
local function setupFonts(): *ImFont <cimport> end -- from utils/setupFonts.c
--- Initial Window size
local MainWinWidth <const> = 1024
local MainWinHeight <const> = 900
--- Constants
local SaveImageName = "ImageSaved"
--- Global vars
local imageExt:string
local recImageFormat = @record{kind: string, ext:string}
local imageFormatTbl : [4]recImageFormat = {{"JPEG 90%",".jpg"}, {"PNG",".png"}, {"BMP",".bmp"}, {"TGA",".tga"}}
local cbItemIndex = 0
local versions : [][2]int32 = {{4, 6} ,{4, 5} ,{4, 4} ,{4, 3} ,{4, 2} ,{4, 1} ,{4, 0} ,{3, 3} }
---------
--- main
---------
local main = function()
glfwInit()
defer glfwTerminate() end
local glfwWin: *GLFWwindow = nilptr
local glsl_version:cstring -- = "#version 330"
for _, ver in ipairs(versions) do
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, ver[0])
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, ver[1])
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE)
glfwWin = glfwCreateWindow(MainWinWidth, MainWinHeight, "ImGui with Nelua ", nilptr, nilptr)
if nilptr ~= glfwWin then
glsl_version = "#version " .. tostring( ver[0] * 100 + ver[1] * 10)
break
end
end
if nilptr == glfwWin then
print("Could not create window")
os.exit(false)
end
defer glfwDestroyWindow(glfwWin) end
glfwMakeContextCurrent(glfwWin)
glfwSwapInterval(1) -- # Enable vsync
------------------------
--- Load title bar icon
------------------------
local IconName = "res/img/nl.png"
LoadTitleBarIcon(glfwWin, IconName)
if gladLoadGL(glfwGetProcAddress) == 0 then
error 'Could not initialize GLAD'
end
print('OpenGL loaded:', (@cstring)(glGetString(GL_VERSION)))
local context = igCreateContext()
defer igDestroyContext(context) end
ImGui_ImplGlfw_InitForOpenGL(glfwWin, true)
defer ImGui_ImplGlfw_Shutdown() end
ImGui_ImplOpenGL3_Init(glsl_version)
defer ImGui_ImplOpenGL3_Shutdown() end
setupFonts()
--igStyleColorsDark()
igStyleColorsClassic()
---------------
--- Load image
---------------
local textureId: GLuint
local textureWidth: int32 = 0
local textureHeight: int32 = 0
local ImageName = "parrots-400.png"
if fileExists(ImageName) then
if not loadTextureFromFile(ImageName, &textureId, &textureWidth, &textureHeight) then
print("Error!: Image load error: ", ImageName)
end
else
print("Error!: Image file not found error: ", ImageName)
end
defer glDeleteTextures(1, &textureId) end
local showDemoWindow = true
local showFirstWindow = true
local fval:cfloat = 0.5
local showWindowDelay = 1 -- TODO : Avoid flickering window at start up.
local counter = 0
local sBuf:string = string.create(100)
sBuf[1] = 0
local clearColor: [3]cfloat = {0.25,0.65,0.85}
local zoomTextureID: GLuint = 0 -- # Must be == 0 at first
defer glDeleteTextures(1, &zoomTextureID) end
local pio = igGetIO()
--------------------
--- Main while loop
--------------------
while glfwWindowShouldClose(glfwWin) == 0 do
glfwPollEvents()
ImGui_ImplOpenGL3_NewFrame() -- # start imgui frame
ImGui_ImplGlfw_NewFrame()
igNewFrame()
if showDemoWindow then
igShowDemoWindow(&showDemoWindow)
end
do
igBegin("Nelua: Dear ImGui demo", &showFirstWindow, 0)
defer igEnd() end
local s = "GLFW v" .. tostring(glfwGetVersionString())
s = ICON_FA_COMMENT .. " " .. s
igText(s)
s = "OpenGL v" .. tostring((@cstring)(glGetString(GL_VERSION)))
s = ICON_FA_COMMENT_SMS .. " " .. s
igText(s)
igText(ICON_FA_COMMENT_DOTS .. " Dear ImGui") igSameLine(0, -1.0)
igText(igGetVersion())
igText(ICON_FA_COMMENT_MEDICAL .. " ") igSameLine(0, 0)
igText(_VERSION)
igInputTextWithHint("InputText" ,"Input text here" ,sBuf, #sBuf, 0, nilptr, nilptr)
s = "Input result:" .. sBuf
igText(s)
if igButton("Open file", ImVec2{0, 0}) then
end
igSameLine(0.0, -1.0)
if igIsItemHovered(ImGuiHoveredFlags_DelayShort) and igBeginTooltip() then
defer igEndTooltip() end
igText("[Open file]")
local ary: [7]cfloat = {0.6, 0.1, 1.0, 0.5, 0.92, 0.1, 0.2}
igPlotLines_FloatPtr("Curve", &ary, 7, 0, "Overlay string",igGET_FLT_MIN(), igGET_FLT_MAX(), ImVec2{0,0}, #cfloat)
igText("Sin(time) = %.2f", math.sin(igGetTime()));
end
igCheckbox("Demo window", &showDemoWindow)
igSliderFloat("Float", &fval, 0.0, 1.0, "%.3f", 0)
igColorEdit3("Background color", &clearColor, 0)
-- Save button for capturing window image
igPushID_Int(0)
igPushStyleColor_Vec4(ImGuiCol_Button, ImVec4{0.7, 0.7, 0.0, 1.0})
igPushStyleColor_Vec4(ImGuiCol_ButtonHovered, ImVec4{0.8, 0.8, 0.0, 1.0})
igPushStyleColor_Vec4(ImGuiCol_ButtonActive, ImVec4{0.9, 0.9, 0.0, 1.0})
igPushStyleColor_Vec4(ImGuiCol_Text, ImVec4{0.0, 0.0, 0.0, 1.0})
-- Image save button
imageExt = imageFormatTbl[cbItemIndex].ext
local svName = string.format("%s_%05d%s", SaveImageName, counter,imageExt)
if igButton("Save window image", ImVec2{0, 0}) then
local wkSize = getMainViewport_WorkSize()
saveImage(svName,0, 0, wkSize.x, wkSize.y, 3) --- Save Image !
end
igPopStyleColor(4)
igPopID()
-- Show tooltip help
setTooltip(string.format("Save to %s" , svName))
counter = counter + 1
igSameLine(0.0, -1.0)
-- ComboBox: Select save image format
igSetNextItemWidth(100)
if igBeginCombo("##combo", imageFormatTbl[cbItemIndex].kind, 0) then
defer igEndCombo() end
for n,val in ipairs(imageFormatTbl) do
local is_selected = (cbItemIndex == n)
if igSelectable_BoolPtr(val.kind, &is_selected, 0, ImVec2{0.0, 0.0}) then
if is_selected then
igSetItemDefaultFocus()
end
cbItemIndex = n
end
end
end
setTooltip("Select image format")
igText("counter = %d", counter)
igText("Application average %.3f ms/frame (%.1f FPS)", (1000.0 / pio.Framerate), pio.Framerate)
igSeparatorText(ICON_FA_WRENCH .. " Icon font test ")
igText(ICON_FA_TRASH_CAN .. " Trash")
igText(ICON_FA_MAGNIFYING_GLASS_PLUS ..
" " .. ICON_FA_POWER_OFF ..
" " .. ICON_FA_MICROPHONE ..
" " .. ICON_FA_MICROCHIP ..
" " .. ICON_FA_VOLUME_HIGH ..
" " .. ICON_FA_SCISSORS ..
" " .. ICON_FA_SCREWDRIVER_WRENCH ..
" " .. ICON_FA_BLOG)
end
-- # Show image load window
do
igBegin("Image load test", nilptr, 0)
defer igEnd() end
-- # Load image
local size = ImVec2{textureWidth, textureHeight}
local uv0 = ImVec2{0, 0}
local uv1 = ImVec2{1, 1}
local imageBoxPosTop = igGetCursorScreenPos() -- # Get absolute pos.
local texRef = ImTextureRef{nilptr, textureId}
igImage(texRef, size, uv0, uv1)
local imageBoxPosEnd = igGetCursorScreenPos() -- # Get absolute pos.
-- #
if igIsItemHovered(ImGuiHoveredFlags_DelayNone) then
zoomGlass(&zoomTextureID, textureWidth, imageBoxPosTop, imageBoxPosEnd)
end
end
igRender()
glClearColor(clearColor[0], clearColor[1], clearColor[2], 1.0)
glClear(GL_COLOR_BUFFER_BIT)
ImGui_ImplOpenGL3_RenderDrawData(igGetDrawData())
glfwSwapBuffers(glfwWin)
if showWindowDelay > 0 then
showWindowDelay = showWindowDelay - 1
elseif showWindowDelay == 0 then
showWindowDelay = -1
glfwShowWindow(glfwWin)
end
end -- main while loop end
end -- main() end
main()