-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglfw_opengl3_jp.nelua
241 lines (204 loc) · 7.84 KB
/
glfw_opengl3_jp.nelua
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
require'math'
--
require'imgui'
require'imgui_backend_opengl3'
require'imgui_backend_glfw'
require'loadImage'
require'saveImage'
require'utils'
require'IconsFontAwesome6'
--- External functions
local function getFramerate(): cfloat <cimport> end -- from utils/tentativeCode.c
local function setupFonts() <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
---------
--- main
---------
local main = function()
glfwInit()
defer glfwTerminate() end
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3)
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3)
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE)
local glfwWin = glfwCreateWindow(MainWinWidth, MainWinHeight, "ImGui with Nelua ", nilptr, nilptr)
if not glfwWin then
error "Could not create window"
end
defer glfwDestroyWindow(glfwWin) end
glfwMakeContextCurrent(glfwWin)
glfwSwapInterval(1) -- # Enable vsync
------------------------
--- Load title bar icon
------------------------
local IconName = "res/img/icon_jp.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
local glsl_version:cstring <comptime> = "#version 330"
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
--------------------
--- 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 test タイトル漢字", &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("日本語入力テスト" ,"ここに日本語を入力" ,sBuf, #sBuf, 0, nilptr, nilptr)
s = "入力結果: " .. sBuf
igText(s)
if igButton("ファイルを開く", ImVec2{0, 0}) then
end
igSameLine(0.0, -1.0)
if igIsItemHovered(ImGuiHoveredFlags_DelayShort) and igBeginTooltip() then
defer igEndTooltip() end
igText("[ファイルオープン]")
local ary: [7]cfloat = {0.6, 0.1, 1.0, 0.5, 0.92, 0.1, 0.2}
igPlotLines_FloatPtr("曲線", &ary, 7, 0, "オーバーレイ文字",igGET_FLT_MIN(), igGET_FLT_MAX(), ImVec2{0,0}, #cfloat)
igText("正弦波(time) = %.2f", math.sin(igGetTime()));
end
igCheckbox("デモ ウインドウ", &showDemoWindow)
igSliderFloat("浮動小数", &fval, 0.0, 1.0, "%.3f", 0)
igColorEdit3("背景色変更", &clearColor, 0)
-- Save button for capturing window image
igPushID_Int(0)
igPushStyleColor_Vec4(ImGuiCol_enum.ImGuiCol_Button, ImVec4{0.7, 0.7, 0.0, 1.0})
igPushStyleColor_Vec4(ImGuiCol_enum.ImGuiCol_ButtonHovered, ImVec4{0.8, 0.8, 0.0, 1.0})
igPushStyleColor_Vec4(ImGuiCol_enum.ImGuiCol_ButtonActive, ImVec4{0.9, 0.9, 0.0, 1.0})
igPushStyleColor_Vec4(ImGuiCol_enum.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("画面の画像を保存", 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("保存名: %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("保存形式の選択")
igText("計数 = %d", counter)
igText("フレームレート %.3f ms/frame (%.1f FPS)", (1000.0 / getFramerate()), getFramerate())
igSeparatorText(ICON_FA_WRENCH .. " Icon font test ")
igText(ICON_FA_TRASH_CAN .. " ゴミ箱")
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("画像ロードテスト", 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:ImVec2
local imageBoxPosEnd:ImVec2
igGetCursorScreenPos(&imageBoxPosTop) -- # Get absolute pos.
igImage(textureId, size, uv0, uv1, tint_col, border_col)
igGetCursorScreenPos(&imageBoxPosEnd) -- # 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()