-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.cpp
289 lines (263 loc) · 10.3 KB
/
app.cpp
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
278
279
280
281
282
283
284
285
286
287
288
289
#include "app.h"
#include <SDL2/SDL.h>
#include <SDL_mixer.h>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/opencv.hpp>
#include <imgui.h>
#include <imfilebrowser.h>
#include "imgui_impl_sdl2.h"
#include "imgui_impl_sdlrenderer2.h"
#include "camera.h"
#include "race.h"
using namespace std;
App::App()
{
}
int App::Initialize_Subsystems()
{
// Setup SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER | SDL_INIT_AUDIO) != 0)
{
printf("Error: %s\n", SDL_GetError());
return -1;
}
// From 2.0.18: Enable native IME.
#ifdef SDL_HINT_IME_SHOW_UI
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
#endif
int width = 1920;
int height = 1080;
SDL_DisplayMode dm;
if (SDL_GetDesktopDisplayMode(0, &dm)!=0)
{
printf("Error: SDL_GetDesktopDisplayMode(): %s\n", SDL_GetError());
}
else
{
width = dm.w;
height = dm.h;
}
// Create window with SDL_Renderer graphics context
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
mWindow = SDL_CreateWindow("VisionRC", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, window_flags);
if (mWindow == nullptr)
{
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
return -1;
}
mRenderer = SDL_CreateRenderer(mWindow, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
if (mRenderer == nullptr)
{
SDL_Log("Error creating SDL_Renderer!");
return -1;
}
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
// Setup Dear ImGui style
//ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
ImGui::StyleColorsClassic();
// Setup Platform/Renderer backends
ImGui_ImplSDL2_InitForSDLRenderer(mWindow, mRenderer);
ImGui_ImplSDLRenderer2_Init(mRenderer);
return 1;
}
int App::Run()
{
// Our state
bool show_demo_window = false;
//ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
ImVec4 clear_color = ImVec4(0.0f, 0.0f, 0.0f, 1.00f);
std::unique_ptr<Camera> race_camera = nullptr;
// Main loop
bool hasCheckedForSources = false;
bool done = false;
unsigned int a = 0;
unsigned int b = 0;
double delta = 0;
double desiredFPS = 30;
static int currentCamId = -1;
ImGui::FileBrowser fileDialog;
std::string errorMessage;
std::vector<std::unique_ptr<Camera>> availableSources;
fileDialog.SetTitle("Offline Race Source");
fileDialog.SetTypeFilters({ ".mp4"});
while (!done)
{
a = SDL_GetTicks();
delta = a - b;
if (delta > 1000 / desiredFPS )
{
b = a;
// Poll and handle events (inputs, window resize, etc.)
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
SDL_Event event;
while (SDL_PollEvent(&event))
{
ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT)
done = true;
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(mWindow))
done = true;
}
// Start the Dear ImGui frame
ImGui_ImplSDLRenderer2_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
if (race_camera != nullptr)
{
desiredFPS = race_camera->GetCameraFPS();
race_camera->NextFrame();
CURRENT_RACE.Update(*race_camera);
race_camera->Draw();
if(CURRENT_RACE.Draw(*race_camera))
{
if(currentCamId != -1)
{
availableSources.clear();
currentCamId = -1;
hasCheckedForSources = false;
}
race_camera = nullptr;
}
ImGui::Begin("Timing Fidelity");
std::string label="Camera Polling Every " + std::to_string(1000.0f / ImGui::GetIO().Framerate) + " ms or " + std::to_string((int)ImGui::GetIO().Framerate) + " times per second";
if(ImGui::GetIO().Framerate < 25)
{
ImGui::TextColored(ImVec4(1,0,0,1), "%s", label.c_str());
}
else
{
ImGui::TextColored(ImVec4(0,1,0,1), "%s", label.c_str());
}
ImGui::End();
}
else
{
ImGui::Begin("Select Race Camera Source");
if (!availableSources.empty())
{
errorMessage.clear();
std::string sourcesFromVec;
for(int i = 0; i < availableSources.size(); i ++)
{
sourcesFromVec += std::to_string(i) +'\0';
}
if (ImGui::Combo("Live From USB Source", ¤tCamId, sourcesFromVec.c_str()))
{
race_camera = std::move(availableSources[currentCamId]);
}
}
else
{
if(!hasCheckedForSources)
{
for (int i = 0; i < 15; i++)
{
try
{
std::unique_ptr<Camera> cam = std::make_unique<Camera>(mRenderer, i);
availableSources.push_back(std::move(cam));
errorMessage.clear();
}
catch(std::exception ex)
{
errorMessage = "No usb camera found, is one plugged in and not already in use?";
}
}
hasCheckedForSources = true;
}
}
if(ImGui::Button("Live From IP Camera"))
{
ImGui::OpenPopup("IpCam");
}
if (ImGui::BeginPopupModal("IpCam"))
{
ImGui::Text("Please put in a rtsp url.");
ImGui::Separator();
static char rtspUrl[255] = "";
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::InputText("URL", rtspUrl, IM_ARRAYSIZE(rtspUrl));
ImGui::PopStyleVar();
if (ImGui::Button("OK", ImVec2(120, 0)))
{
try
{
race_camera = std::make_unique<Camera>(mRenderer, rtspUrl, false);
memset(rtspUrl, 0, sizeof rtspUrl);
errorMessage.clear();
ImGui::CloseCurrentPopup();
}
catch(std::exception ex)
{
errorMessage = "No ip camera found, are you sure url is correct?";
}
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if (ImGui::Button("Cancel", ImVec2(120, 0)))
{
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
ImGui::SameLine();
if(ImGui::Button("Offline From Video"))
{
fileDialog.Open();
}
if(!errorMessage.empty())
{
ImGui::TextColored(ImVec4(1,0,0,1), "%s", errorMessage.c_str());
}
ImGui::End();
// select cam type
}
fileDialog.Display();
if(fileDialog.HasSelected())
{
try
{
race_camera = std::make_unique<Camera>(mRenderer, fileDialog.GetSelected().string(), true);
errorMessage.clear();
}
catch(std::exception ex)
{
errorMessage = "Could not open file. Are you sure its valid mp4?";
}
fileDialog.ClearSelected();
}
// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
if (show_demo_window)
ImGui::ShowDemoWindow(&show_demo_window);
// Rendering
ImGui::Render();
SDL_RenderSetScale(mRenderer, ImGui::GetIO().DisplayFramebufferScale.x, ImGui::GetIO().DisplayFramebufferScale.y);
SDL_SetRenderDrawColor(mRenderer, (Uint8)(clear_color.x * 255), (Uint8)(clear_color.y * 255), (Uint8)(clear_color.z * 255), (Uint8)(clear_color.w * 255));
SDL_RenderClear(mRenderer);
ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), mRenderer);
SDL_RenderPresent(mRenderer);
}
}
// Cleanup
ImGui_ImplSDLRenderer2_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
SDL_DestroyRenderer(mRenderer);
SDL_DestroyWindow(mWindow);
SDL_Quit();
return 0;
}