1
0
Fork 0

Compare commits

..

4 commits

Author SHA1 Message Date
940c1d706b main/debug: Make the frame timing graph a sliding window
Use memmove to shift the whole array by one measure to the left,
dropping the oldest one and adding a new one at the head.
This creates a better visual effect of a graph being produced on the right
and scrolling to the left, rather than a looping writing head that overwrites
previous data.
2024-01-29 00:21:14 +00:00
41437cd259 Clean-up: Change indentation to tabs 2024-01-28 22:08:35 +00:00
66ec111f5d World/Debug: Properly check the MapEdit status
If a Dear ImGui window is collapsed or closed, ImGui::Begin returns false.
Check it properly to bypass rendering if is indeed not needed.
2024-01-28 22:05:20 +00:00
43970df494 World: Move the scale factor to a constant
Clear up the computation and split the result from the ray cast to the scale
factor.

Update the castRay explanations to add the issue of unmatched axes.
2024-01-28 21:58:36 +00:00
6 changed files with 374 additions and 363 deletions

View file

@ -107,6 +107,9 @@ float World::castRay(float originX, float originY, float orientation) const
* the grid for the other one * the grid for the other one
* - Depending on the orientation, signs must be taken into account * - Depending on the orientation, signs must be taken into account
* to work 360° * to work 360°
* - Those formulas consider regular axes (x,y), however the world is
* built around left-handed axes (x,y), so the rendered world is
* mirrored. This also explains some weird signs for rotations.
*/ */
/* Offsets to get back on the grid from the ray's origin. */ /* Offsets to get back on the grid from the ray's origin. */
float hOffsetX; float hOffsetX;
@ -148,7 +151,7 @@ float World::castRay(float originX, float originY, float orientation) const
if (orientation < 180) { if (orientation < 180) {
vOffsetX = ceilf(originX); vOffsetX = ceilf(originX);
vOffsetY = ceilf(originX) - originX; vOffsetY = ceilf(originX) - originX;
vDir = +1; vDir = 1;
vRound = 0; vRound = 0;
} else { } else {
vOffsetX = floorf(originX); vOffsetX = floorf(originX);
@ -239,6 +242,8 @@ void World::render(sf::RenderWindow& window) const
window.draw(ground); window.draw(ground);
window.draw(ceiling); window.draw(ceiling);
const float worldToCamera = (player.focalLength*2)/player.sensorSize;
/* /*
* Throw rays and draw walls over the ceiling and ground. * Throw rays and draw walls over the ceiling and ground.
* Only throws in the plane, which doesn't work for levels/3D. * Only throws in the plane, which doesn't work for levels/3D.
@ -252,7 +257,7 @@ void World::render(sf::RenderWindow& window) const
} else if (rayAngle > 360) { } else if (rayAngle > 360) {
rayAngle -= 360; rayAngle -= 360;
} }
float obstacleScale = player.focalLength*2/(castRay(player.x, player.y, rayAngle)*player.sensorSize); float obstacleScale = worldToCamera / castRay(player.x, player.y, rayAngle);
/* 2 Is wall height in meters. */ /* 2 Is wall height in meters. */
fillColumn(window, i, obstacleScale); fillColumn(window, i, obstacleScale);
} }
@ -269,7 +274,7 @@ void World::step(const float& stepTime) {
player.rotate(player.currentRotationSpeed*stepTime); player.rotate(player.currentRotationSpeed*stepTime);
#ifdef IMGUI #ifdef IMGUI
ImGui::Begin("MapEdit"); if (ImGui::Begin("MapEdit")) {
static int blockToPlace = 1; static int blockToPlace = 1;
/* /*
@ -320,7 +325,8 @@ void World::step(const float& stepTime) {
ImGui::PushStyleColor(ImGuiCol_Header, blockColor); ImGui::PushStyleColor(ImGuiCol_Header, blockColor);
if (ImGui::Selectable("", currentBlock != BlockType::AIR, 0, ImVec2(10, 10))) { if (ImGui::Selectable("", currentBlock != BlockType::AIR, 0, ImVec2(10, 10))) {
map[x + w*y] = currentBlock == (BlockType)blockToPlace ? BlockType::AIR : (BlockType)blockToPlace; map[x + w * y] =
currentBlock == (BlockType) blockToPlace ? BlockType::AIR : (BlockType) blockToPlace;
} }
ImGui::PopStyleColor(2); ImGui::PopStyleColor(2);
ImGui::PopID(); ImGui::PopID();
@ -337,7 +343,7 @@ void World::step(const float& stepTime) {
ImGui::RadioButton("Window", &blockToPlace, (int) BlockType::WINDOW); ImGui::RadioButton("Window", &blockToPlace, (int) BlockType::WINDOW);
ImGui::Unindent(); ImGui::Unindent();
ImGui::EndGroup(); ImGui::EndGroup();
}
ImGui::End(); ImGui::End();
#endif #endif
} }

View file

@ -115,7 +115,12 @@ int main()
ImGui::SetNextWindowBgAlpha(0.2f); ImGui::SetNextWindowBgAlpha(0.2f);
ImGui::Begin("FPS", nullptr ,window_flags); ImGui::Begin("FPS", nullptr ,window_flags);
if (fpsDataClock.getElapsedTime().asMilliseconds() > 20) { if (fpsDataClock.getElapsedTime().asMilliseconds() > 20) {
frameTimings[frameCount%100] = static_cast<float>(deltaT.asMicroseconds()); if (frameCount >= 100) {
memmove(frameTimings, frameTimings + 1, 99 * sizeof(float));
frameTimings[99] = static_cast<float>(deltaT.asMicroseconds());
} else [[unlikely]] {
frameTimings[frameCount] = static_cast<float>(deltaT.asMicroseconds());
}
frameCount++; frameCount++;
fpsDataClock.restart(); fpsDataClock.restart();
} }