ImGui_SDL2_OpenGL3_Demo.dpr 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. {
  2. FreePascal / Delphi bindings for ImGui
  3. Copyright (C) 2023 Coldzer0 <Coldzer0 [at] protonmail.ch>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the MIT License.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. MIT License for more details.
  10. }
  11. Program ImGui_SDL2_OpenGL3_Demo;
  12. {$IFDEF FPC}
  13. {$mode objfpc}{$H+}{$J-}
  14. {$Optimization FASTMATH}
  15. {$ENDIF}
  16. {$IfDef LINUX}
  17. {$IfDef FPC}
  18. {$LinkLib m}
  19. {$EndIf}
  20. {$EndIf}
  21. {$IfOpt D-}
  22. {$IfDef MSWINDOWS}
  23. {$AppType GUI}
  24. {$EndIf}
  25. {$ELSE}
  26. {$IfDef MSWINDOWS}
  27. {$AppType console}
  28. {$EndIf}
  29. {$EndIf}
  30. Uses
  31. {$IFDEF FPC}
  32. cmem,
  33. {$ENDIF}
  34. Math,
  35. SysUtils,
  36. sdl2,
  37. glad_gl,
  38. PasImGui,
  39. PasImGui.Utils,
  40. PasImGui.ImPlot,
  41. PasImGui.Backend.SDL2,
  42. PasImGui.Renderer.OpenGL3,
  43. TestWindow, CustomNodeGraph;
  44. Var
  45. counter: Integer;
  46. ShowPascalDemoWindow: Boolean = False;
  47. ShowAnotherWindow: Boolean = False;
  48. ShowDemoWindow: Boolean = False;
  49. ShowNodeWindow: Boolean = False;
  50. ShowImPlotPascalDemo: Boolean = False;
  51. clearColor: ImVec4;
  52. Var
  53. ImGuiCtx: PImGuiContext;
  54. IO: PImGuiIO;
  55. style : PImGuiStyle;
  56. quit: Boolean;
  57. window: PSDL_Window;
  58. e: TSDL_Event;
  59. testwin: TTestWindow;
  60. backup_current_window: PSDL_Window;
  61. backup_current_context: TSDL_GLContext;
  62. current: TSDL_DisplayMode;
  63. flags: Longword;
  64. gl_context: TSDL_GLContext;
  65. w, h: Integer;
  66. glsl_version: PAnsiChar = '';
  67. // LinePlots
  68. var
  69. xs1, ys1 : Array [0..1000] of Single;
  70. xs2, ys2 : Array [0..19] of Double;
  71. procedure ImPlotPascalDemoWindow();
  72. var
  73. i: Integer;
  74. begin
  75. if not ImGui.Begin_('ImPlot Pascal Demo') then
  76. begin
  77. ImGui.End_();
  78. exit;
  79. end;
  80. for i := 0 to Pred(1001) do
  81. begin
  82. xs1[i] := i * 0.001;
  83. ys1[i] := 0.5 + 0.5 * Sin(50 * (xs1[i] + ImGui.GetTime() / 10));
  84. end;
  85. for i := 0 to Pred(20) do
  86. begin
  87. xs2[i] := i * 1/19.0;
  88. ys2[i] := xs2[i] * xs2[i];
  89. end;
  90. if (ImPlot.BeginPlot('Line Plots')) then
  91. begin
  92. ImPlot.SetupAxes('x','y');
  93. ImPlot.PlotLine('f(x)', PSingle(@xs1), PSingle(@ys1), 1001);
  94. ImPlot.SetNextMarkerStyle(ImPlotMarker_Circle);
  95. ImPlot.PlotLine('g(x)', PDouble(@xs2), PDouble(@ys2), 20, ImPlotLineFlags_Segments);
  96. ImPlot.EndPlot();
  97. end;
  98. ImGui.End_();
  99. end;
  100. Procedure ShowGreetingWindows;
  101. Var
  102. draw_list: PImDrawList;
  103. pos: ImVec2;
  104. Const
  105. HelloPascal: PAnsiChar = ' Hello From FreePascal / Delphi ';
  106. Begin
  107. Begin
  108. //ImGui.SetWindowPos(ImVec2.New(100, 100), ImGuiCond_FirstUseEver);
  109. ImGui.SetNextWindowPosCenter(ImGuiCond_FirstUseEver);
  110. If Not ImGui.Begin_('Greeting') Then
  111. Begin
  112. // Early out if the window is collapsed, as an optimization.
  113. ImGui.End_;
  114. exit;
  115. End;
  116. ImGui.Text('Hello, world %d', [counter]);
  117. If ImGui.Button('Add') Then
  118. Begin
  119. //button was pressed, do something special!
  120. Inc(counter);
  121. End;
  122. If ImGui.IsItemHovered(ImGuiHoveredFlags_RectOnly) Then
  123. Begin
  124. ImGui.SameLine();
  125. ImGui.Text('button hovered');
  126. End;
  127. ImGui.SameLine();
  128. pos := ImGui.GetCursorScreenPos();
  129. draw_list := ImGui.GetWindowDrawList();
  130. draw_list^.AddRectFilled(pos, ImVec2.New(pos.x + 50, pos.y + 20), $88000055);
  131. pos := ImVec2.New(pos.x + 50 + 20, pos.y);
  132. ImGui.SetCursorScreenPos(pos);
  133. draw_list^.AddRectFilled(pos, ImVec2.New(pos.x +
  134. ImGui.CalcTextSize(HelloPascal).x, pos.y + Trunc(ImGui.GetFont()^.EllipsisWidth * 2)), $88005500);
  135. ImGui.Text(HelloPascal);
  136. If ImGui.IsWindowHovered() Then
  137. ImGui.Text('window hovered')
  138. Else If ImGui.IsWindowHovered(ImGuiHoveredFlags_AnyWindow) Then
  139. ImGui.Text('some window hovered');
  140. ImGui.End_;
  141. End;
  142. Pos := ImGui.GetCenterViewPort(ImGui.GetMainViewport());
  143. Pos.y := Pos.y + 100;
  144. ImGui.SetNextWindowPos(Pos, ImGuiCond_FirstUseEver, ImVec2.New(0.5, 0.5));
  145. Begin
  146. ImGui.Begin_('Another greeting', @showAnotherWindow);
  147. ImGui.Text('Hello, next world %d', [counter]);
  148. If ImGui.Button('Not OK!') Then
  149. Begin
  150. Dec(counter);
  151. End;
  152. ImGui.End_;
  153. End;
  154. End;
  155. var
  156. GlobalStyleInitalized : Boolean = False;
  157. color_for_text, color_for_head, color_for_area, color_for_body, color_for_pops: ImVec3;
  158. Procedure RenderPascalCode();
  159. Var
  160. Pos: ImVec2;
  161. begin
  162. if not GlobalStyleInitalized then
  163. begin
  164. color_for_text := ImVec3.New(236.0 / 255.0, 240.0 / 255.0, 241.0 / 255.0);
  165. color_for_head := ImVec3.New(41.0 / 255.0, 128.0 / 255.0, 185.0 / 255.0);
  166. color_for_area := ImVec3.New(57.0 / 255.0, 79.0 / 255.0, 105.0 / 255.0);
  167. color_for_body := ImVec3.New(44.0 / 255.0, 62.0 / 255.0, 80.0 / 255.0);
  168. color_for_pops := ImVec3.New(33.0 / 255.0, 46.0 / 255.0, 60.0 / 255.0);
  169. GlobalStyleInitalized := True;
  170. end;
  171. //draw your scene or simple windows
  172. Pos := ImGui.GetCenterViewPort(ImGui.GetMainViewport());
  173. Pos.y := Pos.y - 160;
  174. ImGui.SetNextWindowPos(Pos, ImGuiCond_FirstUseEver, ImVec2.New(0.5, 0.5));
  175. Begin
  176. ImGui.Begin_('Hello From FreePascal / Delphi', nil, ImGuiWindowFlags_None);
  177. ImGui.Text('This is some useful text');
  178. ImGui.Checkbox('ImGui Demo', @showDemoWindow); ImGui.SameLine();
  179. ImGui.Checkbox('ImPlot Pascal Demo', @ShowImPlotPascalDemo);
  180. ImGui.Checkbox('Pascal Node Window', @showNodeWindow);
  181. ImGui.Checkbox('Pascal Demo Window', @showPascalDemoWindow);
  182. ImGui.Checkbox('Another Pascal window', @showAnotherWindow);
  183. ImGui.Spacing();
  184. ImGui.Separator();
  185. ImGui.Text('Global Styling Test');
  186. ImGui.NewLine();
  187. ImGui.ColorEdit3('color_for_text', @color_for_text);
  188. ImGui.ColorEdit3('color_for_head', @color_for_head);
  189. ImGui.ColorEdit3('color_for_area', @color_for_area);
  190. ImGui.ColorEdit3('color_for_body', @color_for_body);
  191. ImGui.ColorEdit3('color_for_pops', @color_for_pops);
  192. //If (ImGui.Button('Apply')) Then
  193. begin
  194. imgui_easy_theming(color_for_text, color_for_head, color_for_area, color_for_body, color_for_pops);
  195. end;
  196. ImGui.NewLine();
  197. ImGui.Separator();
  198. ImGui.ColorEdit3('Background color', @clearColor);
  199. If (ImGui.Button('Add')) Then
  200. begin
  201. Inc(counter);
  202. end;
  203. ImGui.SameLine(0.0, -1.0);
  204. ImGui.Text('counter = %d', [counter]);
  205. ImGui.Text('Application average %.3f ms/frame (%.f FPS)',
  206. [1000.0 / IO^.Framerate, IO^.Framerate]);
  207. ImGui.End_();
  208. End;
  209. If showAnotherWindow Then
  210. Begin
  211. ShowGreetingWindows;
  212. End;
  213. If showDemoWindow Then
  214. Begin
  215. ImGui.ShowDemoWindow(@showDemoWindow);
  216. End;
  217. End;
  218. Function PasAllocMem(sz: NativeUInt; {%H-}user_data: Pointer): Pointer; Cdecl;
  219. Begin
  220. Result := {$IFDEF FPC}cmem.Malloc(sz);{$ELSE}AllocMem(sz);{$ENDIF}
  221. End;
  222. Procedure PasFreeMem(ptr: Pointer; {%H-}user_data: Pointer); Cdecl;
  223. Begin
  224. {$IFDEF FPC}cmem.Free(ptr);{$ELSE}FreeMem(ptr);{$ENDIF}
  225. End;
  226. {$R *.res}
  227. procedure DrawGUI();
  228. begin
  229. // start imgui frame
  230. ImGui_OpenGL3_NewFrame();
  231. ImGui_ImplSDL2_NewFrame_Pas();
  232. ImGui.NewFrame();
  233. // Main UI Code
  234. Begin
  235. RenderPascalCode();
  236. if showNodeWindow then
  237. ShowExampleAppCustomNodeGraph(@showNodeWindow);
  238. If showPascalDemoWindow Then
  239. testwin.Show(showPascalDemoWindow);
  240. if ShowImPlotPascalDemo then
  241. ImPlotPascalDemoWindow();
  242. End;
  243. // render
  244. ImGui.Render();
  245. SDL_GL_MakeCurrent(window, gl_context);
  246. glViewport(0, 0, Trunc(IO^.DisplaySize.x), Trunc(IO^.DisplaySize.y));
  247. glClearColor(clearColor.x, clearColor.y, clearColor.z, clearColor.w);
  248. glClear(GL_COLOR_BUFFER_BIT);
  249. ImGui_OpenGL3_RenderDrawData(ImGui.GetDrawData());
  250. // IMGUI_DOCK
  251. If (IO^.ConfigFlags And ImGuiConfigFlags_ViewportsEnable) <> 0 Then
  252. Begin
  253. backup_current_window := SDL_GL_GetCurrentWindow();
  254. backup_current_context := SDL_GL_GetCurrentContext();
  255. ImGui.UpdatePlatformWindows();
  256. ImGui.RenderPlatformWindowsDefault(nil, nil);
  257. SDL_GL_MakeCurrent(backup_current_window, backup_current_context);
  258. End;
  259. //show frame on display
  260. SDL_GL_SwapWindow(window);
  261. end;
  262. function window_redraw_on_resize(userdata: Pointer; e : PSDL_Event): Int32; cdecl;
  263. begin
  264. if (e^.type_ = SDL_WINDOWEVENT) and (e^.window.event = SDL_WINDOWEVENT_RESIZED) then
  265. begin
  266. glViewport(0, 0, e^.window.data1, e^.window.data2);
  267. DrawGUI();
  268. end;
  269. Result := 1;
  270. end;
  271. Begin
  272. { TODO: This is here for testing - Remove this later :V }
  273. //DeleteFile('MyApp.ini');
  274. // Set the Default Alloc & Free to point to Pascal Allocators
  275. igSetAllocatorFunctions(@PasAllocMem, @PasFreeMem, nil);
  276. //prevent SDL from raising a debugger exception to name threads
  277. SDL_SetHint(SDL_HINT_WINDOWS_DISABLE_THREAD_NAMING, '1');
  278. //open new SDL window with OpenGL rendering support
  279. If SDL_Init(SDL_INIT_VIDEO or SDL_INIT_TIMER) < 0 Then
  280. Begin
  281. if IsConsole then
  282. Writeln(Format('failed to init: %s', [SDL_GetError()]));
  283. End;
  284. // Decide GL+GLSL versions
  285. {$IfDef DARWIN}
  286. // GL 3.2 Core + GLSL 150
  287. // Always required on Mac
  288. glsl_version = '#version 150';
  289. SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
  290. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  291. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  292. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
  293. {$ELSE}
  294. // GL 4.1 uses GLSL 4.10
  295. glsl_version := '#version 410';
  296. SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
  297. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  298. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  299. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
  300. {$EndIf}
  301. // From 2.0.18: Enable native IME.
  302. SDL_SetHint(SDL_HINT_IME_SHOW_UI, '1');
  303. SDL_SetHint(SDL_HINT_RENDER_DRIVER, 'opengl');
  304. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  305. SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
  306. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  307. SDL_GetCurrentDisplayMode(0, @current);
  308. flags := SDL_WINDOW_OPENGL Or SDL_WINDOW_RESIZABLE Or SDL_WINDOW_ALLOW_HIGHDPI;
  309. //flags := flags or SDL_WINDOW_FULLSCREEN_DESKTOP;
  310. h := 720;
  311. w := 1280;
  312. window := SDL_CreateWindow('Hello From FreePascal / Delphi (SDL2+OpenGL3)', SDL_WINDOWPOS_CENTERED,
  313. SDL_WINDOWPOS_CENTERED, w, h, flags);
  314. If window = nil Then
  315. Begin
  316. raise Exception.Create(Format('Failed to create window: %s', [SDL_GetError()]));
  317. halt;
  318. End;
  319. gl_context := SDL_GL_CreateContext(window);
  320. SDL_GL_SetSwapInterval(1); //enable VSync
  321. // setup imgui
  322. ImGui.CreateContext(nil);
  323. // Setup ImPlot
  324. ImPlot.Initialize();
  325. //set docking
  326. IO := ImGui.GetIO();
  327. // Ini Config File name
  328. IO^.IniFilename := 'MyApp.ini';
  329. // Enable Logging
  330. ImGuiCtx := ImGui.GetCurrentContext();
  331. ImGuiCtx^.LogType := ImGuiLogType_TTY;
  332. ImGuiCtx^.LogEnabled := True;
  333. // Enable Keyboard Controls
  334. IO^.ConfigFlags := IO^.ConfigFlags Or ImGuiConfigFlags_NavEnableKeyboard;
  335. // Enable Docking
  336. IO^.ConfigFlags := IO^.ConfigFlags Or ImGuiConfigFlags_DockingEnable;
  337. // Enable Multi-Viewport / Platform Windows
  338. IO^.ConfigFlags := IO^.ConfigFlags Or ImGuiConfigFlags_ViewportsEnable;
  339. // Init ImGui SDL2 OpenGL using Pure Pascal
  340. ImGui_ImplSDL2_InitForOpenGL_Pas(window, gl_context);
  341. ImGui_OpenGL3_Init(glsl_version);
  342. { uncomment to set a different gui theme }
  343. SetupImGuiStyle2(); // Using imgui_easy_theming
  344. //Imgui.StyleColorsDark(nil);
  345. //Imgui.StyleColorsLight(ImGui.GetStyle());
  346. // When viewports are enabled we tweak WindowRounding/WindowBg
  347. // so platform windows can look identical to regular ones.
  348. If (IO^.ConfigFlags And ImGuiConfigFlags_ViewportsEnable) <> 0 Then
  349. begin
  350. style := ImGui.GetStyle();
  351. style^.WindowRounding := 0.0;
  352. style^.Colors[ImGuiCol_WindowBg].w := 1.0;
  353. end;
  354. // Load Fonts
  355. //IO^.Fonts^.AddFontDefault();
  356. IO^.Fonts^.AddFontFromFileTTF('fonts/DroidSans.ttf', 25.0);
  357. IO^.Fonts^.AddFontFromFileTTF('fonts/JetBrainsMonoNerdFontPropo-Italic.ttf ', 28.0);
  358. // Background Color
  359. clearColor.x := 0.45;
  360. clearColor.y := 0.55;
  361. clearColor.z := 0.60;
  362. clearColor.w := 1.00;
  363. testwin := TTestWindow.Create;
  364. counter := 0;
  365. quit := False;
  366. SDL_SetEventFilter(@window_redraw_on_resize, nil);
  367. While Not quit Do
  368. Begin
  369. //handle input
  370. While SDL_PollEvent(@e) <> 0 Do
  371. Begin
  372. ImGui_ImplSDL2_ProcessEvent_Pas(@e);
  373. If e.type_ = SDL_QUITEV Then
  374. quit := True;
  375. If (e.type_ = SDL_WINDOWEVENT) And (e.window.event = SDL_WINDOWEVENT_CLOSE) And
  376. (e.window.windowID = SDL_GetWindowID(window)) Then
  377. quit := True;
  378. End;
  379. DrawGUI();
  380. End;
  381. testwin.Free;
  382. // clean up
  383. ImGui_OpenGL3_Shutdown();
  384. ImGui_ImplSDL2_Shutdown_Pas();
  385. ImGui.DestroyContext(nil);
  386. SDL_GL_DeleteContext(gl_context);
  387. If window <> nil Then
  388. Begin
  389. SDL_DestroyWindow(window);
  390. window := nil;
  391. End;
  392. SDL_Quit();
  393. End.