-
Notifications
You must be signed in to change notification settings - Fork 2
/
glfw_opengl3.nim
232 lines (198 loc) · 7.4 KB
/
glfw_opengl3.nim
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
import std/[strutils, math]
import nimgl/[opengl,glfw]
import imguin/[glfw_opengl]
import imguin/lang/imgui_ja_gryph_ranges
import ../utils/loadImage
include ../utils/setupFonts
include imguin/simple
when defined(windows):
when not defined(vcc): # imguinVcc.res TODO WIP
include ./res/resource
import tinydialogs
const MainWinWidth = 1024
const MainWinHeight = 800
#--------------
# Configration
#--------------
# .--------------------------------------------..---------.-----------------------.------------
# | Combination of flags || | Viewport |
# |--------------------------------------------||---------|-----------------------|------------
# | fViewport | fDocking | TransparentViewport || Docking | Transparent | Outside | Description
# |:---------:|:--------:|:-------------------:||:-------:|:-----------:|:-------:| -----------
# | false | false | false || - | - | - |
# | false | true | false || v | - | - | (Default): Only docking
# | true | - | false || v | - | v | Docking and outside of viewport
# | - | - | true || v | v | - | Transparent Viewport and docking
# `-----------'----------'---------------------'`---------'-------------'---------'-------------
var
fDocking = true
fViewport = false
TransparentViewport = false
#
block:
if TransparentViewport:
fViewport = true
if fViewport:
fDocking = true
#---------------------
# Forward definitions
#---------------------
proc winMain(hWin: glfw.GLFWWindow)
#------
# main
#------
proc main() =
doAssert glfwInit()
defer: glfwTerminate()
if TransparentViewport:
glfwWindowHint(GLFWVisible, GLFW_FALSE)
glfwWindowHint(GLFWContextVersionMajor, 3)
glfwWindowHint(GLFWContextVersionMinor, 3)
glfwWindowHint(GLFWOpenglForwardCompat, GLFW_TRUE)
glfwWindowHint(GLFWOpenglProfile, GLFW_OPENGL_CORE_PROFILE)
glfwWindowHint(GLFWResizable, GLFW_TRUE)
#
glfwWindowHint(GLFWVisible, GLFW_FALSE)
var glfwWin = glfwCreateWindow(MainWinWidth, MainWinHeight)
if glfwWin.isNil:
quit(-1)
glfwWin.makeContextCurrent()
defer: glfwWin.destroyWindow()
glfwSwapInterval(1) # Enable vsync
#---------------------
# Load title bar icon
#---------------------
var IconName = os.joinPath(os.getAppDir(),"res/img/n.png")
LoadTileBarIcon(glfwWin, IconName)
#
doAssert glInit() # OpenGL init
# Setup ImGui
let context = igCreateContext(nil)
defer: context.igDestroyContext()
if fDocking:
var pio = igGetIO()
pio.ConfigFlags = pio.ConfigFlags or ImGui_ConfigFlags_DockingEnable.cint
if fViewport:
pio.ConfigFlags = pio.ConfigFlags or ImGui_ConfigFlags_ViewportsEnable.cint
pio.ConfigViewports_NoAutomerge = true
# GLFW + OpenGL
const glsl_version = "#version 130" # GL 3.0 + GLSL 130
doAssert ImGui_ImplGlfw_InitForOpenGL(cast[ptr GLFWwindow](glfwwin), true)
defer: ImGui_ImplGlfw_Shutdown()
doAssert ImGui_ImplOpenGL3_Init(glsl_version)
defer: ImGui_ImplOpenGL3_Shutdown()
# Set ini filename
#pio.IniFileName = "myIniname.ini"
glfwWin.winMain()
#---------
# winMain
#---------
proc winMain(hWin: glfw.GLFWWindow) =
var
showDemoWindow = true
showAnotherWindow = false
showFirstWindow = true
fval = 0.5f
counter = 0
sBuf = newString(200)
sFnameSelected{.global.}:string
clearColor:ccolor
showWindowDelay = 1 # TODO
if TransparentViewport:
clearColor = ccolor(elm:(x:0f, y:0f, z:0f, w:0.0f)) # Transparent
else:
clearColor = ccolor(elm:(x:0.25f, y:0.65f, z:0.85f, w:1.0f))
igStyleColorsClassic(nil)
# Add multibytes font
discard setupFonts()
var pio = igGetIO()
# main loop
while not hWin.windowShouldClose:
glfwPollEvents()
# start imgui frame
ImGui_ImplOpenGL3_NewFrame()
ImGui_ImplGlfw_NewFrame()
igNewFrame()
if showDemoWindow:
igShowDemoWindow(addr showDemoWindow)
# show a simple window that we created ourselves.
if showFirstWindow:
igBegin("Nim: Dear ImGui test with Futhark", addr showFirstWindow, 0)
defer: igEnd()
var s = "GLFW v" & $glfwGetVersionString()
s = ICON_FA_COMMENT & " " & s
igText(s.cstring)
s = "OpenGL v" & ($cast[cstring](glGetString(GL_VERSION))).split[0]
s = ICON_FA_COMMENT_SMS & " " & s
igText(s.cstring)
igText(ICON_FA_COMMENT_DOTS & " Dear ImGui"); igSameLine(0, -1.0)
igText(igGetVersion())
igText(ICON_FA_COMMENT_MEDICAL & " Nim-"); igSameLine(0, 0)
igText(NimVersion);
igInputTextWithHint("InputText" ,"Input text here" ,sBuf)
s = "Input result:" & sBuf
igText(s.cstring)
igCheckbox("Demo window", addr showDemoWindow)
igCheckbox("Another window", addr showAnotherWindow)
igSliderFloat("Float", addr fval, 0.0f, 1.0f, "%.3f", 0)
igColorEdit3("Background color", clearColor.array3, 0.ImGuiColorEditFlags)
# Show file open dialog
when defined(windows):
if igButton("Open file", ImVec2(x: 0, y: 0)):
sFnameSelected = openFileDialog("File open dialog", getCurrentDir() / "\0", ["*.nim", "*.nims"], "Text file")
igSameLine(0.0f, -1.0f)
# Show hint
if igIsItemHovered(Imgui_HoveredFlagsDelayShort.cint) and igBeginTooltip():
igText("[Open file]")
const ary = [0.6f, 0.1f, 1.0f, 0.5f, 0.92f, 0.1f, 0.2f]
igPlotLines("Curve", ary, overlayText = "Overlay string")
igText("Sin(time) = %.2f", sin(igGetTime()));
igEndTooltip();
let (_,fname,ext) = sFnameSelected.splitFile()
igText("Selected file = %s", (fname & ext).cstring)
# Counter up
if igButton("Button", ImVec2(x: 0.0f, y: 0.0f)):
inc counter
igSameLine(0.0f, -1.0f)
igText("counter = %d", counter)
igText("Application average %.3f ms/frame (%.1f FPS)".cstring, (1000.0f / igGetIO().Framerate).cfloat, igGetIO().Framerate.cfloat)
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)
# show further samll window
if showAnotherWindow:
igBegin("imgui Another Window", addr showAnotherWindow, 0)
igText("Hello from imgui")
if igButton("Close me", ImVec2(x: 0.0f, y: 0.0f)):
showAnotherWindow = false
igEnd()
# render
igRender()
glClearColor(clearColor.elm.x, clearColor.elm.y, clearColor.elm.z, clearColor.elm.w)
glClear(GL_COLOR_BUFFER_BIT)
ImGui_ImplOpenGL3_RenderDrawData(igGetDrawData())
if 0 != (pio.ConfigFlags and ImGui_ConfigFlags_ViewportsEnable.cint):
var backup_current_window = glfwGetCurrentContext()
igUpdatePlatformWindows()
igRenderPlatformWindowsDefault(nil, nil)
backup_current_window.makeContextCurrent()
hWin.swapBuffers()
if not showFirstWindow and not showDemoWindow and not showAnotherWindow:
hwin.setWindowShouldClose(true) # End program
if showWindowDelay > 0:
dec showWindowDelay
else:
once: # Avoid flickering screen at startup.
hWin.showWindow()
#### end while
#------
# main
#------
main()