Skip to content

Commit 488c36b

Browse files
committed
Fix collision debug mode
1 parent 6ad7d1b commit 488c36b

File tree

1 file changed

+67
-35
lines changed

1 file changed

+67
-35
lines changed

include/Game/Game.hpp

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
#include "Engine/SpriteSheet.hpp"
1313
#include "Engine/Texture.hpp"
1414
#include "Engine/TimeManager.hpp"
15+
#include "Engine/Updater.hpp"
1516
#include "Engine/Utilities.hpp"
1617
#include "Engine/Vector2.hpp"
1718
#include "Engine/Window.hpp"
18-
#include "Engine/Updater.hpp"
1919

2020
#include <GLFW/glfw3.h>
2121
#include <glad/glad.h>
@@ -58,10 +58,10 @@ class Game
5858

5959
glfwSetMonitorCallback(setMonitorCallback);
6060
datas.monitors.init();
61-
Vec2i monitorSize = datas.monitors.getMonitorsSize();
61+
Vec2i monitorSize = datas.monitors.getMonitorsSize();
6262
Vec2i monitorsSizeMM = datas.monitors.getMonitorPhysicalSize();
6363

64-
datas.windowSize = {1, 1};
64+
datas.windowSize = {1, 1};
6565

6666
// Evaluate pixel distance based on dpi and monitor size
6767
datas.pixelPerMeter = {(float)monitorSize.x / (monitorsSizeMM.x * 0.001f),
@@ -121,18 +121,8 @@ class Game
121121

122122
createResources();
123123

124-
Vec2i mainMonitorPosition;
125-
Vec2i mainMonitorSize;
126-
datas.monitors.getMainMonitorWorkingArea(mainMonitorPosition, mainMonitorSize);
127-
128-
datas.windowPos = mainMonitorPosition + mainMonitorSize / 2;
129-
datas.petPos = datas.windowPos;
130-
glfwSetWindowPos(datas.window, datas.windowPos.x, datas.windowPos.y);
131-
132124
glfwShowWindow(datas.window);
133-
134125
glfwSetWindowUserPointer(datas.window, &datas);
135-
136126
glfwSetMouseButtonCallback(datas.window, mousButtonCallBack);
137127
glfwSetCursorPosCallback(datas.window, cursorPositionCallback);
138128

@@ -159,8 +149,64 @@ class Game
159149
glfwTerminate();
160150
}
161151

152+
void runCollisionDetectionMode()
153+
{
154+
const std::function<void(double)> unlimitedUpdate{[&](double deltaTime) {
155+
// poll for and process events
156+
glfwPollEvents();
157+
}};
158+
159+
int frameCount = 0;
160+
const std::function<void(double)> limitedUpdateDebugCollision{[&](double deltaTime) {
161+
++frameCount;
162+
163+
// render
164+
initDrawContext();
165+
166+
if (!(frameCount & 1) && datas.pImageGreyScale && datas.pEdgeDetectionTexture && datas.pFullScreenQuad)
167+
{
168+
datas.pImageGreyScale->use();
169+
datas.pImageGreyScale->setInt("uTexture", 0);
170+
datas.pFullScreenQuad->use();
171+
datas.pEdgeDetectionTexture->use();
172+
datas.pFullScreenQuad->draw();
173+
}
174+
175+
// swap front and back buffers
176+
glfwSwapBuffers(datas.window);
177+
178+
if (frameCount & 1)
179+
{
180+
Vec2 newPos;
181+
physicSystem.CatpureScreenCollision(datas.windowSize, newPos);
182+
}
183+
}};
184+
185+
// fullscreen
186+
Vec2i monitorSize;
187+
datas.monitors.getMonitorSize(0, monitorSize);
188+
datas.windowSize = monitorSize;
189+
glfwSetWindowSize(datas.window, datas.windowSize.x, datas.windowSize.y);
190+
glfwSetWindowPos(datas.window, 0, 0);
191+
glfwSetWindowAttrib(datas.window, GLFW_MOUSE_PASSTHROUGH, true);
192+
glfwSetWindowAttrib(datas.window, GLFW_TRANSPARENT_FRAMEBUFFER, true);
193+
mainLoop.setFrameRate(1);
194+
195+
mainLoop.start();
196+
while (!glfwWindowShouldClose(datas.window))
197+
{
198+
mainLoop.update(unlimitedUpdate, limitedUpdateDebugCollision);
199+
}
200+
}
201+
162202
void run()
163203
{
204+
if (datas.debugEdgeDetection)
205+
{
206+
runCollisionDetectionMode();
207+
return;
208+
}
209+
164210
Pet pet(datas);
165211

166212
const std::function<void(double)> unlimitedUpdate{[&](double deltaTime) {
@@ -172,6 +218,7 @@ class Game
172218
// poll for and process events
173219
glfwPollEvents();
174220
}};
221+
175222
const std::function<void(double)> limitedUpdate{[&](double deltaTime) {
176223
pet.update(deltaTime);
177224

@@ -188,35 +235,20 @@ class Game
188235
}
189236
}};
190237

191-
const std::function<void(double)> limitedUpdateDebugCollision{[&](double deltaTime) {
192-
// fullscreen
193-
Vec2i monitorSize = datas.monitors.getMonitorsSize();
194-
datas.windowSize = monitorSize;
195-
glfwSetWindowSize(datas.window, datas.windowSize.x, datas.windowSize.y);
196-
197-
// render
198-
initDrawContext();
199-
200-
if (datas.pImageGreyScale && datas.pEdgeDetectionTexture && datas.pFullScreenQuad)
201-
{
202-
datas.pImageGreyScale->use();
203-
datas.pImageGreyScale->setInt("uTexture", 0);
204-
datas.pFullScreenQuad->use();
205-
datas.pEdgeDetectionTexture->use();
206-
datas.pFullScreenQuad->draw();
207-
}
208-
209-
// swap front and back buffers
210-
glfwSwapBuffers(datas.window);
211-
}};
238+
Vec2i mainMonitorPosition;
239+
Vec2i mainMonitorSize;
240+
datas.monitors.getMainMonitorWorkingArea(mainMonitorPosition, mainMonitorSize);
241+
datas.windowPos = mainMonitorPosition + mainMonitorSize / 2;
242+
datas.petPos = datas.windowPos;
243+
glfwSetWindowPos(datas.window, datas.windowPos.x, datas.windowPos.y);
212244

213245
mainLoop.emplaceTimer([&]() { physicSystem.update(1.f / datas.physicFrameRate); }, 1.f / datas.physicFrameRate,
214246
true);
215247

216248
mainLoop.start();
217249
while (!glfwWindowShouldClose(datas.window))
218250
{
219-
mainLoop.update(unlimitedUpdate, datas.debugEdgeDetection ? limitedUpdateDebugCollision : limitedUpdate);
251+
mainLoop.update(unlimitedUpdate, limitedUpdate);
220252
}
221253
}
222254
};

0 commit comments

Comments
 (0)