Skip to content

Commit

Permalink
Add sector zfloor / zceling edition
Browse files Browse the repository at this point in the history
  • Loading branch information
P0ulpy committed Jul 6, 2024
1 parent e251a4a commit 66eec96
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 90 deletions.
14 changes: 12 additions & 2 deletions src/ColorHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ constexpr Color MY_DARK_BLUE { 40, 17, 64, 255 };
constexpr Color MY_BEIGE { 242, 211, 172, 255 };
constexpr Color MY_BLACK { 50, 50, 50, 255 };

constexpr Color ColorAlpha255(Color color, uint8_t alpha)
/// @brief
/// @param color base color to be darken
/// @param darkness 0 to 1 value
/// @return Darken color
inline Color ColorDarken(Color color, float darkness)
{
return { color.r, color.g, color.b, alpha };
float clampedDarkness = Clamp(darkness, 0, 1);

uint8_t red = roundf(Lerp(color.r, 0, clampedDarkness));
uint8_t green = roundf(Lerp(color.g, 0, clampedDarkness));
uint8_t blue = roundf(Lerp(color.b, 0, clampedDarkness));

return { red, green, blue, 255 };
}
213 changes: 152 additions & 61 deletions src/Editor/WorldEditor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <imgui.h>
#include <imgui_internal.h>
#include <rlimgui.h>
#include <rlImGui.h>
#include <raylib.h>

#include "../World.hpp"
Expand All @@ -29,90 +29,181 @@ class WorldEditor

void DrawGUI()
{
ImGui::Begin("World Editor");
ImGuiID worldEditorDockerSpace = ImGui::GetID("worldEditorDockerSpace");
ImGui::DockSpace(worldEditorDockerSpace);
ImGui::End();

ImGui::SetNextWindowDockID(worldEditorDockerSpace, ImGuiCond_FirstUseEver);
RenderSectorsGui();

ImGui::SetNextWindowDockID(worldEditorDockerSpace, ImGuiCond_FirstUseEver);
RenderViewportGui();
}

void Update(float dt)
{

}

void Render(RaycastingCamera& cam)
{
Camera2D minimapCamera = { 0 };
minimapCamera.target = cam.position;
minimapCamera.offset = { (float)renderTexture.texture.width / 2, (float)renderTexture.texture.height / 2 };
minimapCamera.rotation = 0.0f;
minimapCamera.zoom = 1.f;//zoom;

BeginTextureMode(renderTexture);

BeginMode2D(minimapCamera);

ClearBackground(LIGHTGRAY);

Vector2 camHeadingDirectionPoint = Vector2Add(cam.position, Vector2Scale(cam.Forward(), 50));
DrawLineV(cam.position, camHeadingDirectionPoint, RED);
DrawCircleV(cam.position, 10, GREEN);

for(const auto& [ sectorId, sector ] : world.Sectors)
{
bool sectorSelected = sectorId == currentSelectedSector;

Vector2 selectionMin = { (float)renderTexture.texture.width, (float)renderTexture.texture.height };
Vector2 selectionMax = { 0, 0 };

for(const auto& wall : sector.walls)
{
Color color = WHITE;
if(sectorSelected)
{
color = RED;
if(wall.toSector != NULL_SECTOR)
{
color = BLUE;
}
}
else if(wall.toSector != NULL_SECTOR) color = PURPLE;

DrawLineV(wall.segment.a, wall.segment.b, color);

if(sectorSelected)
{
if(wall.segment.a.x < selectionMin.x)
selectionMin.x = wall.segment.a.x;
if(wall.segment.b.x < selectionMin.x)
selectionMin.x = wall.segment.b.x;

if(wall.segment.a.y < selectionMin.y)
selectionMin.y = wall.segment.a.y;
if(wall.segment.b.y < selectionMin.y)
selectionMin.y = wall.segment.b.y;

if(wall.segment.a.x > selectionMax.x)
selectionMax.x = wall.segment.a.x;
if(wall.segment.b.x > selectionMax.x)
selectionMax.x = wall.segment.b.x;

if(wall.segment.a.y > selectionMax.y)
selectionMax.y = wall.segment.a.y;
if(wall.segment.b.y > selectionMax.y)
selectionMax.y = wall.segment.b.y;
}
}

if(sectorSelected)
{
static const float padding = 10;

selectionMin.x -= padding;
selectionMin.y -= padding;
selectionMax.x += padding;
selectionMax.y += padding;

int width = selectionMax.x - selectionMin.x;
int height = selectionMax.y - selectionMin.y;

DrawRectangleLines(selectionMin.x, selectionMin.y, width, height, ColorAlpha(RED, .5f));
}
}

EndMode2D();

EndTextureMode();
}

private:
void RenderViewportGui()
{
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
ImGui::SetNextWindowSizeConstraints(ImVec2(renderTexture.texture.width, renderTexture.texture.height), ImVec2((float)GetScreenWidth(), (float)GetScreenHeight()));





if (ImGui::Begin("Viewport", nullptr, ImGuiWindowFlags_NoScrollbar))
{
ImGui::Begin("World Editor");
ImGuiID worldEditorDockerSpace = ImGui::GetID("worldEditorDockerSpace");
ImGui::DockSpace(worldEditorDockerSpace);
ImGui::End();

ImGui::SetNextWindowDockID(worldEditorDockerSpace, ImGuiCond_FirstUseEver);
ImGui::Begin("Sectors");

ImGui::Text("salut");

ImGui::End();

ImGui::SetNextWindowDockID(worldEditorDockerSpace, ImGuiCond_FirstUseEver);
// ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
// ImGui::SetNextWindowSizeConstraints(ImVec2(renderTexture.texture.width, renderTexture.texture.height), ImVec2((float)GetScreenWidth(), (float)GetScreenHeight()));

if (ImGui::Begin("Viewport", nullptr, ImGuiWindowFlags_NoScrollbar))
ImVec2 windowSize = ImGui::GetWindowSize();
if(windowSize.x != renderTexture.texture.width || windowSize.y != renderTexture.texture.height)
{
// ImVec2 windowSize = ImGui::GetWindowSize();
// if(windowSize.x != renderTexture.texture.width || windowSize.y != renderTexture.texture.height)
// {
// ResizeRenderTextureSize(windowSize.x, windowSize.y);
// }

// rlImGuiImageRenderTextureFit(&renderTexture, true);
ResizeRenderTextureSize(windowSize.x, windowSize.y);
}
ImGui::End();

// ImGui::PopStyleVar();
rlImGuiImageRenderTextureFit(&renderTexture, true);
}
ImGui::End();





// ImGui::Begin("World Editor", nullptr, ImGuiWindowFlags_DockNodeHost);

// ImGui::Begin("Sectors");
// ImGui::Text("salut");
// ImGui::End();

// ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
// ImGui::SetNextWindowSizeConstraints(ImVec2(renderTexture.texture.width, renderTexture.texture.height), ImVec2((float)GetScreenWidth(), (float)GetScreenHeight()));

// if (ImGui::Begin("Viewport", nullptr, ImGuiWindowFlags_NoScrollbar))
// {
// ImVec2 windowSize = ImGui::GetWindowSize();
// if(windowSize.x != renderTexture.texture.width || windowSize.y != renderTexture.texture.height)
// {
// ResizeRenderTextureSize(windowSize.x, windowSize.y);
// }

// rlImGuiImageRenderTextureFit(&renderTexture, true);
// }
// ImGui::End();

// ImGui::PopStyleVar();

// ImGui::End();
ImGui::PopStyleVar();
}

void Render(RaycastingCamera& cam)
void RenderSectorsGui()
{
using namespace std::string_literals;

ImGui::Begin("Sectors");

for(auto& [ sectorId, sector ] : world.Sectors)
{
static const ImGuiTreeNodeFlags BaseSectorFlags =
ImGuiTreeNodeFlags_OpenOnArrow
| ImGuiTreeNodeFlags_OpenOnDoubleClick
| ImGuiTreeNodeFlags_SpanAvailWidth;

ImGuiTreeNodeFlags sectorFlags = BaseSectorFlags;
if (currentSelectedSector == sectorId)
{
sectorFlags |= ImGuiTreeNodeFlags_Selected;
}

std::string label = "Sector - "s + std::to_string(sectorId);
bool sectorOpen = ImGui::TreeNodeEx(label.c_str(), sectorFlags);

if (ImGui::IsItemClicked() && !ImGui::IsItemToggledOpen())
{
currentSelectedSector = sectorId;
}

if (sectorOpen)
{
RenderSectorContentGui(sector);
ImGui::TreePop();
}
}

ImGui::End();
}

void Update(float dt)
void RenderSectorContentGui(Sector& sector)
{
ImGui::SliderFloat("zCeiling", &sector.zCeiling, -10, 10);
ImGui::SliderFloat("zFloor", &sector.zFloor, -10, 10);

for(size_t i = 0; i < sector.walls.size(); ++i)
{
ImGui::Text("[%d] => %d", i, sector.walls[i].toSector);
}
}

private:
World& world;
RenderTexture2D renderTexture;

// State
SectorID currentSelectedSector = NULL_SECTOR;
};
2 changes: 1 addition & 1 deletion src/MiniMapViewport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ class MiniMapViewport
RenderTexture2D renderTexture { 0 };

bool focused = false;
float zoom = 0.3f;
float zoom = 1.f;
};
2 changes: 1 addition & 1 deletion src/RaycastingCamera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct RaycastingCamera
float fov { 60 };
float fovVectical { 120 };

float farPlaneDistance = 1000.0f;
float farPlaneDistance = 900.0f;
float nearPlaneDistance = 100.f;

size_t maxRenderItr { 25 };
Expand Down
15 changes: 14 additions & 1 deletion src/RaycastingCameraViewport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,22 @@ class RaycastingCameraViewport
}
}

focused = ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows);
bool isFocused = ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows);
if(isFocused && !focused)
{
SetMousePosition(GetScreenWidth() / 2, GetScreenHeight() / 2);
}

focused = isFocused;

// draw the view
rlImGuiImageRenderTextureFit(&renderTexture, true);

if(IsFocused() && IsKeyPressed(KEY_ESCAPE))
{
ImGui::SetFocusID(ImGui::GetID("Game Player"), ImGui::GetCurrentWindow());
focused = false;
}
}
ImGui::End();

Expand Down
8 changes: 4 additions & 4 deletions src/WorldRasterizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ RasterizeWorldContext InitRasterizeWorldContext(uint32_t RenderTargetWidth, uint
.RenderTargetWidth = RenderTargetWidth,
.RenderTargetHeight = RenderTargetHeight,
.FloorVerticalOffset = ComputeVerticalOffset(cam, RenderTargetHeight),
.CamCurrentSectorElevationOffset = ComputeElevationOffset(cam, world, RenderTargetHeight),
.CamCurrentSectorElevationOffset = 0,//ComputeElevationOffset(cam, world, RenderTargetHeight),
};

context.yBoundaries.resize(RenderTargetWidth);
Expand Down Expand Up @@ -272,13 +272,13 @@ CameraYLineData ComputeCameraYAxis(

void RenderCameraYLine(CameraYLineData renderData, Color color, bool topBorder, bool bottomBorder)
{
uint8_t brightness = Lerp(255, 0, renderData.normalizedDepth);
float darkness = Lerp(1, 0, renderData.normalizedDepth);

DrawLineV(
renderData.top,
renderData.bottom,
// ColorAlpha255(color, brightness)
color
ColorDarken(color, renderData.normalizedDepth)
// color
);

if(topBorder)
Expand Down
4 changes: 2 additions & 2 deletions src/WorldRasterizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ struct CameraYLineData
{
Vector2 top;
Vector2 bottom;
float depth;
float normalizedDepth;
float depth = 0;
float normalizedDepth = 0;
};

CameraYLineData ComputeCameraYAxis(
Expand Down
Loading

0 comments on commit 66eec96

Please sign in to comment.