PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
PEBLLauncher.cpp File Reference
#include "imgui.h"
#include "backends/imgui_impl_sdl2.h"
#include "backends/imgui_impl_sdlrenderer2.h"
#include <SDL2/SDL.h>
#include <stdio.h>
#include <string>
#include <filesystem>
#include "LauncherUI.h"
#include "LauncherConfig.h"

Go to the source code of this file.

Functions

int main (int, char **)
 

Function Documentation

◆ main()

int main ( int  ,
char **   
)

Definition at line 85 of file PEBLLauncher.cpp.

86{
87 // Initialize SDL
88 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)
89 {
90 printf("Error: %s\n", SDL_GetError());
91 return -1;
92 }
93
94 // Load configuration first (need window size)
95 LauncherConfig config;
96 config.LoadConfig();
97
98 // Setup window with HiDPI support
99 SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
100 SDL_Window* window = SDL_CreateWindow(
101 "PEBL Experiment Launcher",
102 SDL_WINDOWPOS_CENTERED,
103 SDL_WINDOWPOS_CENTERED,
104 config.GetWindowWidth(),
105 config.GetWindowHeight(),
106 window_flags
107 );
108
109 if (window == nullptr)
110 {
111 printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
112 return -1;
113 }
114
115 // Create renderer with VSync
116 SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
117 if (renderer == nullptr)
118 {
119 printf("Error creating SDL_Renderer: %s\n", SDL_GetError());
120 return -1;
121 }
122
123 // Setup Dear ImGui context
124 IMGUI_CHECKVERSION();
125 ImGui::CreateContext();
126 ImGuiIO& io = ImGui::GetIO(); (void)io;
127 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
128
129 // Set imgui.ini path to settings directory (must be static to persist)
130 static std::string imguiIniPath;
131 {
132 fs::path settingsDir = fs::path(config.GetWorkspacePath()) / "settings";
133 try {
134 fs::create_directories(settingsDir);
135 } catch (...) {}
136 imguiIniPath = (settingsDir / "imgui.ini").string();
137 io.IniFilename = imguiIniPath.c_str();
138 }
139
140 // Set font size from configuration
141 ImFontConfig fontConfig;
142 fontConfig.SizePixels = config.GetFontSize();
143 io.Fonts->AddFontDefault(&fontConfig);
144
145 // Setup Dear ImGui style
146 ImGui::StyleColorsDark();
147 // ImGui::StyleColorsLight();
148
149 // Setup Platform/Renderer backends
150 ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
151 ImGui_ImplSDLRenderer2_Init(renderer);
152
153 // Initialize launcher UI
154 LauncherUI launcherUI(&config, renderer);
155
156 // Main loop
157 bool done = false;
158 while (!done)
159 {
160 // Poll and handle events
161 SDL_Event event;
162 while (SDL_PollEvent(&event))
163 {
164 ImGui_ImplSDL2_ProcessEvent(&event);
165 if (event.type == SDL_QUIT)
166 done = true;
167 if (event.type == SDL_WINDOWEVENT &&
168 event.window.event == SDL_WINDOWEVENT_CLOSE &&
169 event.window.windowID == SDL_GetWindowID(window))
170 done = true;
171 }
172
173 // Start the Dear ImGui frame
174 ImGui_ImplSDLRenderer2_NewFrame();
175 ImGui_ImplSDL2_NewFrame();
176 ImGui::NewFrame();
177
178 // Render launcher UI
179 launcherUI.Render(&done);
180
181 // Rendering
182 ImGui::Render();
183 SDL_RenderSetScale(renderer, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
184 SDL_SetRenderDrawColor(renderer, 45, 45, 48, 255);
185 SDL_RenderClear(renderer);
186 ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), renderer);
187 SDL_RenderPresent(renderer);
188 }
189
190 // Save window size to configuration before exit
191 int windowWidth, windowHeight;
192 SDL_GetWindowSize(window, &windowWidth, &windowHeight);
193 config.SetWindowWidth(windowWidth);
194 config.SetWindowHeight(windowHeight);
195 config.SaveConfig();
196
197 // Cleanup
198 ImGui_ImplSDLRenderer2_Shutdown();
199 ImGui_ImplSDL2_Shutdown();
200 ImGui::DestroyContext();
201
202 SDL_DestroyRenderer(renderer);
203 SDL_DestroyWindow(window);
204 SDL_Quit();
205
206 return 0;
207}
void SetWindowWidth(int width)
int GetFontSize() const
int GetWindowWidth() const
void SetWindowHeight(int height)
std::string GetWorkspacePath() const
int GetWindowHeight() const

References LauncherConfig::GetFontSize(), LauncherConfig::GetWindowHeight(), LauncherConfig::GetWindowWidth(), LauncherConfig::GetWorkspacePath(), LauncherConfig::LoadConfig(), LauncherUI::Render(), LauncherConfig::SaveConfig(), LauncherConfig::SetWindowHeight(), and LauncherConfig::SetWindowWidth().