-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdl2_opengl3.nelua
More file actions
277 lines (240 loc) · 9.2 KB
/
sdl2_opengl3.nelua
File metadata and controls
277 lines (240 loc) · 9.2 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
require'math'
require'os'
--
require'cimgui'
require'simple'
require'imgui_backend_opengl3'
require'imgui_backend_sdl2'
require'loadImage'
require'saveImage'
require'utils'
require'IconsFontAwesome6'
--- External functions
local function getFramerate(): cfloat <cimport> end -- from utils/tentativeCode.c
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()
if 0 ~= SDL_Init(SDL_INIT_VIDEO + SDL_INIT_TIMER + SDL_INIT_GAMECONTROLLER) then
print("SDL2 initialization failed")
os.exit(1)
end
if SDL_TRUE ~= SDL_SetHint("SDL_RENDER_SCALE_QUALITY", "2") then
print("Linear texture filtering could not be enabled")
os.exit(1)
end
local window: *SDL_Window = nilptr
local glsl_version:cstring -- = "#version 330"
for _, ver in ipairs(versions) do
SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_CONTEXT_FLAGS, 0)
SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_CORE)
SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION, ver[0])
SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_CONTEXT_MINOR_VERSION, ver[1])
SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_STENCIL_SIZE, 8);
local flags = SDL_WINDOW_HIDDEN + SDL_WINDOW_OPENGL + SDL_WINDOW_RESIZABLE + SDL_WINDOW_ALLOW_HIGHDPI
window = SDL_CreateWindow("ImGui with Nelua",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, -- x,y
MainWinWidth, MainWinHeight -- w,h
,flags) -- flags
if nilptr ~= window then
glsl_version = "#version " .. tostring( ver[0] * 100 + ver[1] * 10)
break
end
end
if nilptr == window then
print("Window could not be created")
os.exit(false)
end
defer SDL_DestroyWindow(window) end
local gl_context = SDL_GL_CreateContext(window)
defer SDL_GL_DeleteContext(gl_context) end
SDL_GL_MakeCurrent(window, gl_context)
SDL_GL_SetSwapInterval(1)
-- # Setup imgui
igCreateContext()
defer igDestroyContext() end
if gladLoadGL((@function(cstring): function(): void)(SDL_GL_GetProcAddress)) == 0 then
error 'Could not initialize GLAD'
end
ImGui_ImplSDL2_InitForOpenGL(window , gl_context)
defer ImGui_ImplSDL2_Shutdown() end
ImGui_ImplOpenGL3_Init(glsl_version)
defer ImGui_ImplOpenGL3_Shutdown() end
igStyleColorsDark()
-- igStyleColorsClassic()
setupFonts()
---------------
--- Load image
---------------
local textureId: GLuint
local textureWidth: int32 = 0
local textureHeight: int32 = 0
local ImageName = "airplane-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
--------------------
local xQuit = false
while not xQuit do
local event: SDL_Event
while SDL_PollEvent(&event) ~= 0 do
ImGui_ImplSDL2_ProcessEvent(&event)
if event.type== SDL_QUIT then
xQuit = true
end
if event.type == SDL_WINDOWEVENT and event.window.event == SDL_WINDOWEVENT_CLOSE and event.window.windowID == SDL_GetWindowID(window) then
xQuit = true
end
end
ImGui_ImplOpenGL3_NewFrame() -- # Start ImGui frame
ImGui_ImplSDL2_NewFrame()
igNewFrame()
--- ImGui GUI promgram start
if showDemoWindow then
igShowDemoWindow(&showDemoWindow)
end
do
igBegin("Nelua: Dear ImGui test", &showFirstWindow, 0)
defer igEnd() end
local ver: SDL_version
SDL_GetVersion(&ver)
local s = "SDL v" .. string.format("%d.%d.%d",ver.major, ver.minor, ver.patch)
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("##", 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 tint_col = ImVec4{1, 1, 1, 1}
local border_col = ImVec4{0, 0, 0, 0}
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
--- ImGui GUI program end
igRender()
glViewport(0, 0, MainWinWidth, MainWinHeight)
glClearColor(clearColor[0], clearColor[1], clearColor[2], 1.0)
glClear(GL_COLOR_BUFFER_BIT)
ImGui_ImplOpenGL3_RenderDrawData(igGetDrawData())
SDL_GL_SwapWindow(window)
if showWindowDelay > 0 then
showWindowDelay = showWindowDelay - 1
elseif showWindowDelay == 0 then
showWindowDelay = -1
SDL_ShowWindow(window)
end
end -- main while loop end
end -- main() end
main()