diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..0314b4d --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +*.obj filter=lfs diff=lfs merge=lfs -text +*.png filter=lfs diff=lfs merge=lfs -text +*.glb filter=lfs diff=lfs merge=lfs -text diff --git a/assets/turret.obj b/assets/turret.obj new file mode 100644 index 0000000..0b192d3 --- /dev/null +++ b/assets/turret.obj @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3be290f2a7bbe7dae5cdcfc914d811994a1a770927d6ada03c6e965f84d2f643 +size 55241 diff --git a/assets/turret_diffuse.png b/assets/turret_diffuse.png new file mode 100644 index 0000000..bf8af4e --- /dev/null +++ b/assets/turret_diffuse.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8805681c61113481d03b55ea7259f4ad52b16172d503ba056358a0f141464be9 +size 380120 diff --git a/assets/zombie.glb b/assets/zombie.glb new file mode 100644 index 0000000..b60ba2b --- /dev/null +++ b/assets/zombie.glb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:987d8988a73c4bebd33da4ad8e4cbfa832de88860cd5d58c0a929872a0d2c7ff +size 18760 diff --git a/assets/zombie.png b/assets/zombie.png new file mode 100644 index 0000000..18db4f2 --- /dev/null +++ b/assets/zombie.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b172ff1b50c3ce0587cf82a2865bb05b9a22fc54a78d446b7bfcab13ffcea0ab +size 994 diff --git a/include/td/Maths.h b/include/td/Maths.h index 9d4f795..ee569b5 100644 --- a/include/td/Maths.h +++ b/include/td/Maths.h @@ -4,7 +4,7 @@ namespace td { -static constexpr float PI = 3.141592653f; +// static constexpr float PI = 3.141592653f; template struct Vec2 { diff --git a/include/td/common/Event.h b/include/td/common/Event.h new file mode 100644 index 0000000..c977dab --- /dev/null +++ b/include/td/common/Event.h @@ -0,0 +1,37 @@ +#pragma once + +namespace td { + +class EventDispatcher; + +class Event { + +}; + +struct WindowResizeEvent { + int m_Width; + int m_Height; +}; + +class EventDispatcher { +public: + EventDispatcher(const Event& event); + + template + void Dispatch(); +}; + +// template +// class ConcreteEvent : public Event { +// public: +// void Accept(EventDispatcher& a_Dispatcher) { +// a_Dispatcher.Handle(*this); +// } +// }; + +void OnEvent(const Event& event) { + EventDispatcher dispatcher(event); + dispatcher.Dispatch(); +} + +} \ No newline at end of file diff --git a/include/td/display/Display.h b/include/td/display/Display.h index 8bc90aa..8740a1f 100644 --- a/include/td/display/Display.h +++ b/include/td/display/Display.h @@ -2,8 +2,6 @@ #include -#include -#include #include #include @@ -11,17 +9,15 @@ namespace td { class Display : public StateMachine { private: - SDL_Window* m_Window; - SDL_GLContext m_GLContext; - - int m_LastWidth, m_LastHeight; + int m_LastWidth; + int m_LastHeight; float m_AspectRatio; bool m_ShouldClose; public: utils::Signal OnAspectRatioChange; - utils::Signal OnKeyDown; + utils::Signal OnKeyDown; Display(int a_Width, int a_Height, const std::string& a_Title); ~Display(); diff --git a/include/td/display/DisplayState.h b/include/td/display/DisplayState.h index 5190945..8cf7a78 100644 --- a/include/td/display/DisplayState.h +++ b/include/td/display/DisplayState.h @@ -13,7 +13,7 @@ class DisplayState : public Display::State, private utils::SlotGuard { protected: virtual void OnAspectRatioChange(float a_Ratio) {} - virtual void OnKeyDown(SDL_Keycode a_Key) {} + virtual void OnKeyDown(int a_Key) {} }; } // namespace td diff --git a/include/td/display/state/DebugWorldState.h b/include/td/display/state/DebugWorldState.h index cce2c4d..d698796 100644 --- a/include/td/display/state/DebugWorldState.h +++ b/include/td/display/state/DebugWorldState.h @@ -13,7 +13,7 @@ namespace td { class DebugWorldState : public DisplayState { private: render::RenderPipeline m_Renderer; - render::Camera m_Camera; + Camera m_Camera; std::unique_ptr m_Server; std::unique_ptr m_Client; client::GameState* m_ClientState; @@ -31,7 +31,7 @@ class DebugWorldState : public DisplayState { protected: virtual void OnAspectRatioChange(float a_Ratio) override; - virtual void OnKeyDown(SDL_Keycode a_Key) override; + virtual void OnKeyDown(int a_Key) override; }; } // namespace td diff --git a/include/td/display/state/MainMenuState.h b/include/td/display/state/MainMenuState.h index 7449b6f..d808932 100644 --- a/include/td/display/state/MainMenuState.h +++ b/include/td/display/state/MainMenuState.h @@ -21,7 +21,7 @@ class MainMenuState : public DisplayState, public MainMenuStateStack { void RenderBackButton(); protected: - virtual void OnKeyDown(SDL_Keycode a_Key) override; + virtual void OnKeyDown(int a_Key) override; }; } // namespace td diff --git a/include/td/game/Mobs.h b/include/td/game/Mobs.h index ace8d0d..5d219e6 100644 --- a/include/td/game/Mobs.h +++ b/include/td/game/Mobs.h @@ -79,9 +79,10 @@ class Mob : public sp::MessageBase { Vec2fp m_Position; Direction m_Direction; std::vector m_Effects; - const Tower* m_LastDamage; // the last tower that damaged the mob + std::shared_ptr m_LastDamage; // the last tower that damaged the mob float m_HitCooldown; - TeamCastle* m_CastleTarget; + std::shared_ptr m_CastleTarget; + bool m_HasReachedCastle = false; // utils::CooldownTimer m_AttackTimer; MobPtr m_Next; diff --git a/include/td/game/WorldTypes.h b/include/td/game/WorldTypes.h index 60023a6..7170d7f 100644 --- a/include/td/game/WorldTypes.h +++ b/include/td/game/WorldTypes.h @@ -25,12 +25,12 @@ enum class TileType : std::uint8_t { Ice,*/ }; -static constexpr Color BLACK{0, 0, 0}; -static constexpr Color WHITE{255, 255, 255}; +// static constexpr Color BLACK(0, 0, 0); +// static constexpr Color WHITE(255, 255, 255); -static constexpr Color RED{255, 0, 0}; -static constexpr Color GREEN{0, 255, 0}; -static constexpr Color BLUE{0, 0, 255}; +// static constexpr Color RED(255, 0, 0); +// static constexpr Color GREEN(0, 255, 0); +// static constexpr Color BLUE(0, 0, 255); class TileHandler; diff --git a/include/td/render/Camera.h b/include/td/render/Camera.h deleted file mode 100644 index a2689d7..0000000 --- a/include/td/render/Camera.h +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once - -#include -#include - -namespace td { -namespace render { - -class Camera { - private: - Mat4f m_ViewMatrix; - Mat4f m_ProjectionMatrix; - Mat4f m_InvViewMatrix; - Mat4f m_InvProjectionMatrix; - - float m_CamDistance = 25.0f; - Vec3f m_CamPos{0, m_CamDistance, 0}; - Vec2f m_CamLook{}; - - float m_Yaw = -PI / 2.0f; - float m_Pitch = -PI / 2.0f + 0.0000001f; - - public: - utils::Signal<> OnPerspectiveChange; - utils::Signal<> OnViewChange; - - const Mat4f& GetViewMatrix() const { - return m_ViewMatrix; - } - - const Mat4f& GetProjectionMatrix() const { - return m_ProjectionMatrix; - } - - void UpdatePerspective(float a_AspectRatio); - - void SetCamPos(const Vec3f& a_NewPos); - const Vec3f& GetCamPos() const; -}; - -} // namespace render -} // namespace td diff --git a/include/td/render/OpenGL.h b/include/td/render/OpenGL.h deleted file mode 100644 index 75fd640..0000000 --- a/include/td/render/OpenGL.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -#ifdef TD_GL_LOADER_GLEW -#include -#else -#include -using namespace gl; -#endif diff --git a/include/td/render/Renderer.h b/include/td/render/Renderer.h index 3bb5e64..186ea1f 100644 --- a/include/td/render/Renderer.h +++ b/include/td/render/Renderer.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include #include @@ -66,15 +66,15 @@ class RenderPipeline { template Renderer::Renderer(Camera& a_Camera) : m_Shader(std::make_unique()), m_Camera(a_Camera) { - Connect(a_Camera.OnPerspectiveChange, [this](){ - m_Shader->Start(); - m_Shader->SetProjectionMatrix(m_Camera.GetProjectionMatrix()); - }); + // Connect(a_Camera.OnPerspectiveChange, [this](){ + // // m_Shader->Start(); + // m_Shader->SetProjectionMatrix(m_Camera.GetProjectionMatrix()); + // }); - Connect(a_Camera.OnViewChange, [this]() { - m_Shader->Start(); - m_Shader->SetViewMatrix(m_Camera.GetViewMatrix()); - }); + // Connect(a_Camera.OnViewChange, [this]() { + // // m_Shader->Start(); + // m_Shader->SetViewMatrix(m_Camera.GetViewMatrix()); + // }); } } // namespace render diff --git a/include/td/render/loader/WorldLoader.h b/include/td/render/loader/WorldLoader.h index 0a5dc79..b65a1f4 100644 --- a/include/td/render/loader/WorldLoader.h +++ b/include/td/render/loader/WorldLoader.h @@ -1,22 +1,14 @@ #pragma once #include -#include +#include namespace td { namespace render { namespace WorldLoader { -struct RenderData { - std::vector positions; - std::vector colors; -}; - -GL::VertexArray LoadMobModel(); -GL::VertexArray LoadWorldModel(const td::game::World* world); -GL::VertexArray LoadTileSelectModel(); -RenderData LoadTowerModel(const game::TowerPtr& tower); +Mesh LoadWorldModel(const td::game::World* world); } // namespace WorldLoader diff --git a/include/td/render/renderer/EntityRenderer.h b/include/td/render/renderer/EntityRenderer.h index f0e7249..3b529a3 100644 --- a/include/td/render/renderer/EntityRenderer.h +++ b/include/td/render/renderer/EntityRenderer.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -10,7 +11,8 @@ namespace render { class EntityRenderer : public Renderer { private: game::WorldPtr m_World; - std::unique_ptr m_EntityVao; + Model m_ZombieModel; + Texture2D m_ZombieTexture; public: EntityRenderer(Camera& a_Camera, const game::WorldPtr& a_World); diff --git a/include/td/render/renderer/TowerRenderer.h b/include/td/render/renderer/TowerRenderer.h index 946fc01..18361ce 100644 --- a/include/td/render/renderer/TowerRenderer.h +++ b/include/td/render/renderer/TowerRenderer.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -10,7 +11,8 @@ namespace render { class TowerRenderer : public Renderer { private: game::WorldPtr m_World; - std::unique_ptr m_EntityVao; + Model m_TowerModel; + Texture2D m_TowerTexture; public: TowerRenderer(Camera& a_Camera, const game::WorldPtr& a_World); diff --git a/include/td/render/renderer/WorldRenderer.h b/include/td/render/renderer/WorldRenderer.h index 8848eb2..7849eee 100644 --- a/include/td/render/renderer/WorldRenderer.h +++ b/include/td/render/renderer/WorldRenderer.h @@ -10,7 +10,7 @@ namespace render { class WorldRenderer : public Renderer { private: - std::unique_ptr m_WorldVao; + Model m_WorldModel; public: WorldRenderer(Camera& a_Camera, const game::WorldPtr& a_World); diff --git a/include/td/render/shader/CameraShaderProgram.h b/include/td/render/shader/CameraShaderProgram.h index 66960ce..663f34a 100644 --- a/include/td/render/shader/CameraShaderProgram.h +++ b/include/td/render/shader/CameraShaderProgram.h @@ -1,16 +1,18 @@ #pragma once -#include +#include +#include namespace td { namespace shader { -class CameraShaderProgram : public ShaderProgram { +class CameraShaderProgram { private: unsigned int m_LocationProjection = 0, m_LocationView = 0; public: - CameraShaderProgram() {} + CameraShaderProgram() { + } void SetProjectionMatrix(const Mat4f& proj) const; void SetViewMatrix(const Mat4f& view) const; diff --git a/include/td/render/shader/ShaderProgram.h b/include/td/render/shader/ShaderProgram.h deleted file mode 100755 index 93d1f13..0000000 --- a/include/td/render/shader/ShaderProgram.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace td { -namespace shader { - -class ShaderProgram { - public: - ShaderProgram(); - virtual ~ShaderProgram(); - - void Start() const; - void Stop() const; - - protected: - void LoadProgramFile(const std::string& vertexFile, const std::string& fragmentFile); - void LoadProgram(const std::string& vertexSource, const std::string& fragmentSource); - - virtual void GetAllUniformLocation() = 0; - int GetUniformLocation(const std::string& uniformName) const; - - void LoadFloat(unsigned int location, float value) const; - void LoadInt(unsigned int location, int value) const; - void LoadVector(unsigned int location, const Vec2f& vector) const; - void LoadVector(unsigned int location, const Vec3f& vector) const; - void LoadBoolean(unsigned int location, bool value) const; - void LoadMat4(unsigned int location, const Mat4f& mat) const; - void CleanUp() const; - - private: - unsigned int m_ProgramID; - unsigned int m_VertexShaderID; - unsigned int m_FragmentShaderID; - - unsigned int LoadShaderFromFile(const std::string& file, GLenum type); - unsigned int LoadShader(const std::string& source, GLenum type); -}; - -} // namespace shader -} // namespace td \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 4bd56a2..ae10de3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,3 @@ -#include #include #include diff --git a/src/td/display/Display.cpp b/src/td/display/Display.cpp index 2ecda38..1c5ea10 100644 --- a/src/td/display/Display.cpp +++ b/src/td/display/Display.cpp @@ -1,133 +1,35 @@ #include -#include -#include -#include -#include -#include - +#include #include #include -#include + +#include +#include +#include namespace td { Display::Display(int a_Width, int a_Height, const std::string& a_Title) : m_LastWidth(0), m_LastHeight(0), m_AspectRatio(1), m_ShouldClose(false) { - if (!SDL_Init(SDL_INIT_VIDEO)) { - utils::LOGE(utils::Format("Could not initialize SDL! SDL error: %s", SDL_GetError())); - } - m_Window = SDL_CreateWindow(a_Title.c_str(), a_Width, a_Height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); + InitWindow(a_Width, a_Height, a_Title.c_str()); m_LastWidth = a_Width; m_LastHeight = a_Height; m_AspectRatio = (float)m_LastWidth / m_LastHeight; - // Prepare and create context -#ifdef __ANDROID__ - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); -#else - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); -#endif - SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); - SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6); - SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); - SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0); - - SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); - SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); - - SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); - SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8); - - m_GLContext = SDL_GL_CreateContext(m_Window); - - if (!m_GLContext) { - utils::LOGE(utils::Format("Could not create context! SDL error: %s", SDL_GetError())); - } - - int major, minor, mask; - int r, g, b, a, depth; - int mBuffers, mSamples; - - SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &mask); - SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major); - SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor); - - SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &r); - SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &g); - SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &b); - SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &a); - - SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &depth); - - SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &mBuffers); - SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &mSamples); - - const char* mask_desc; - - if (mask & SDL_GL_CONTEXT_PROFILE_CORE) { - mask_desc = "core"; - } else if (mask & SDL_GL_CONTEXT_PROFILE_COMPATIBILITY) { - mask_desc = "compatibility"; - } else if (mask & SDL_GL_CONTEXT_PROFILE_ES) { - mask_desc = "es"; - } else { - mask_desc = "?"; - } - - utils::LOG(utils::Format( - "GL Context : %i.%i %s, Color : R:%i G:%i B:%i A:%i, Depth bits : %i", major, minor, mask_desc, r, g, b, a, depth)); - - utils::LOG(utils::Format("MultiSamples : Buffers : %i, Samples : %i", mBuffers, mSamples)); - - SDL_GL_MakeCurrent(m_Window, m_GLContext); - - GLenum error = glewInit(); - if (error) { - utils::LOGE(utils::Format("Error initializing glew : %s", glewGetErrorString(error))); - } - // WindowResizeEvent(WindowWidth, WindowHeight); // vsync - SDL_GL_SetSwapInterval(1); + SetTargetFPS(60); - // 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 + rlImGuiSetup(true); - // Setup Dear ImGui style - ImGui::StyleColorsDark(); - // ImGui::StyleColorsLight(); + float main_scale = 1.5f; - // Setup scaling - float main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay()); - // ImGuiStyle& style = ImGui::GetStyle(); - // style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing - // this - // // requires resetting Style + calling this again) - // style.FontSizeBase = 13 * main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We - // leave - // // both here for documentation purpose) + ImGui::GetStyle().FontScaleMain = main_scale; - - ImFontConfig cfg; - cfg.SizePixels = 13 * main_scale * 2; - io.Fonts->AddFontDefault(&cfg); - - // Setup Platform/Renderer backends - ImGui_ImplSDL3_InitForOpenGL(m_Window, m_GLContext); - ImGui_ImplOpenGL3_Init("#version 330"); LoadTheme(); } @@ -136,62 +38,66 @@ void Display::Close() { } void Display::PollEvents() { - SDL_Event event; - while (SDL_PollEvent(&event)) { - switch (event.type) { - case SDL_EVENT_QUIT: - case SDL_EVENT_WINDOW_CLOSE_REQUESTED: { - m_ShouldClose = true; - break; - } + // PollInputEvents(); + // SDL_Event event; + // while (SDL_PollEvent(&event)) { + // switch (event.type) { + // case SDL_EVENT_QUIT: + // case SDL_EVENT_WINDOW_CLOSE_REQUESTED: { + // m_ShouldClose = true; + // break; + // } - case SDL_EVENT_WINDOW_RESIZED: { - m_LastWidth = event.window.data1; - m_LastHeight = event.window.data2; - m_AspectRatio = (float)m_LastWidth / m_LastHeight; - OnAspectRatioChange(m_AspectRatio); - break; - } + // case SDL_EVENT_WINDOW_RESIZED: { + // m_LastWidth = event.window.data1; + // m_LastHeight = event.window.data2; + // m_AspectRatio = (float)m_LastWidth / m_LastHeight; + // OnAspectRatioChange(m_AspectRatio); + // break; + // } - case SDL_EVENT_KEY_DOWN: { - if (!event.key.repeat) - OnKeyDown(event.key.key); - break; - } + // case SDL_EVENT_KEY_DOWN: { + // if (!event.key.repeat) + // OnKeyDown(event.key.key); + // break; + // } - default: - break; - } - ImGui_ImplSDL3_ProcessEvent(&event); - } - // Start the Dear ImGui frame - ImGui_ImplOpenGL3_NewFrame(); - ImGui_ImplSDL3_NewFrame(); - ImGui::NewFrame(); + // default: + // break; + // } + // ImGui_ImplSDL3_ProcessEvent(&event); + // } + // // Start the Dear ImGui frame + // ImGui_ImplOpenGL3_NewFrame(); + // ImGui_ImplSDL3_NewFrame(); + // ImGui::NewFrame(); + if (WindowShouldClose()) + m_ShouldClose = true; + BeginDrawing(); + rlImGuiBegin(); } void Display::Update(float a_Delta) { StateMachine::Update(a_Delta); -#ifndef NDEBUG - ImGui::ShowDemoWindow(); -#endif + #ifndef NDEBUG + ImGui::ShowDemoWindow(); + #endif ImGui::Render(); - ImGuiIO& io = ImGui::GetIO(); - glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y); - // glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w); - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); - SDL_GL_SwapWindow(m_Window); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + rlImGuiEnd(); + EndDrawing(); + ClearBackground(DARKGRAY); + // ImGuiIO& io = ImGui::GetIO(); + // glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y); + // // glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w); + // ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + // SDL_GL_SwapWindow(m_Window); + // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } Display::~Display() { - ImGui_ImplOpenGL3_Shutdown(); - ImGui_ImplSDL3_Shutdown(); - ImGui::DestroyContext(); - SDL_GL_DestroyContext(m_GLContext); - SDL_DestroyWindow(m_Window); - SDL_Quit(); + rlImGuiShutdown(); + CloseWindow(); } } // namespace td diff --git a/src/td/display/state/DebugWorldState.cpp b/src/td/display/state/DebugWorldState.cpp index 6e29c8f..80a7d9d 100644 --- a/src/td/display/state/DebugWorldState.cpp +++ b/src/td/display/state/DebugWorldState.cpp @@ -1,8 +1,7 @@ #include -#include -#include - +#include +#include #include #include #include @@ -20,8 +19,7 @@ #include #include -#include -#include +#include namespace td { @@ -70,45 +68,51 @@ DebugWorldState::DebugWorldState(Display& a_Display) : DisplayState(a_Display) { m_Client->ChangeState("Player0"); // camera - m_Camera.SetCamPos({77, 7, 13}); - m_Camera.UpdatePerspective(m_StateMachine.GetAspectRatio()); + // m_Camera = Camera{{0}}; + m_Camera.position = (Vector3){77.0f, 7.0f, 13.0f}; // Camera position + m_Camera.target = (Vector3){0.0f, 1.0f, -1.0f}; // Camera looking at point + m_Camera.up = (Vector3){0.0f, 1.0f, 0.0f}; // Camera up vector (rotation towards target) + m_Camera.fovy = 45.0f; // Camera field-of-view Y + m_Camera.projection = CAMERA_PERSPECTIVE; // Camera projection type + + // m_Camera.SetCamPos({77, 7, 13}); + // m_Camera.UpdatePerspective(m_StateMachine.GetAspectRatio()); } void DebugWorldState::Update(float a_Delta) { m_Server->Update(a_Delta * m_PlaySpeed); m_Client->Update(a_Delta * m_PlaySpeed); - if (m_ClientState) + UpdateCamera(&m_Camera, CAMERA_FREE); + + if (m_ClientState) { + BeginMode3D(m_Camera); m_Renderer.Render(m_ClientState->GetCurrentLerp()); + EndMode3D(); + } + + constexpr int SECONDS = 10; + if (IsKeyPressed(KEY_Q)) { + m_Client->SendPacket(td::protocol::packets::SpawnTroopPacket(td::EntityType::Zombie, 1)); + } + + if (IsKeyPressed(KEY_Z)) + m_Client->SendPacket(td::protocol::packets::PlaceTowerPacket(td::TowerType::Archer, td::TowerCoords(77, 13))); + + if (IsKeyPressed(KEY_F)) { + m_Server->Update(SECONDS); + m_Client->Update(SECONDS); + } + + if (IsKeyPressed(KEY_P)) + m_PlaySpeed = 1 - m_PlaySpeed; } void DebugWorldState::OnAspectRatioChange(float a_Ratio) { - m_Camera.UpdatePerspective(a_Ratio); + // m_Camera.UpdatePerspective(a_Ratio); } -void DebugWorldState::OnKeyDown(SDL_Keycode a_Key) { +void DebugWorldState::OnKeyDown(int a_Key) { // temporary tests - constexpr int SECONDS = 10; - switch (a_Key) { - case SDLK_A: - m_Client->SendPacket(td::protocol::packets::SpawnTroopPacket(td::EntityType::Zombie, 1)); - break; - - case SDLK_Z: - m_Client->SendPacket(td::protocol::packets::PlaceTowerPacket(td::TowerType::Archer, td::TowerCoords(77, 13))); - break; - - case SDLK_F: - m_Server->Update(SECONDS); - m_Client->Update(SECONDS); - break; - - case SDLK_P: - m_PlaySpeed = 1 - m_PlaySpeed; - break; - - default: - break; - } } DebugWorldState::~DebugWorldState() {} diff --git a/src/td/display/state/MainMenuState.cpp b/src/td/display/state/MainMenuState.cpp index 09aa7d3..e96400a 100644 --- a/src/td/display/state/MainMenuState.cpp +++ b/src/td/display/state/MainMenuState.cpp @@ -34,8 +34,8 @@ void MainMenuState::RenderBackButton() { PopState(); } -void MainMenuState::OnKeyDown(SDL_Keycode a_Key) { - if (a_Key == SDLK_ESCAPE) +void MainMenuState::OnKeyDown(int a_Key) { + if (a_Key == KEY_ESCAPE) PopState(); } diff --git a/src/td/render/Camera.cpp b/src/td/render/Camera.cpp deleted file mode 100644 index 49b2a42..0000000 --- a/src/td/render/Camera.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "td/Maths.h" -#include - -#include - -namespace td { -namespace render { - -void Camera::UpdatePerspective(float a_AspectRatio) { - m_ProjectionMatrix = maths::Perspective(80.0f / 180.0f * PI, a_AspectRatio, 0.1f, 160.0f); - m_InvProjectionMatrix = maths::Inverse(m_ProjectionMatrix); - OnPerspectiveChange(); -} - -void Camera::SetCamPos(const Vec3f& a_NewPos) { - Vec3f front = { - std::cos(m_Yaw) * std::cos(m_Pitch), - std::sin(m_Pitch), - std::sin(m_Yaw) * std::cos(m_Pitch) - }; - - m_CamPos = a_NewPos; - m_ViewMatrix = maths::Look(m_CamPos, front, { 0, 1, 0 }); - m_InvViewMatrix = maths::Transpose(maths::Inverse(m_ViewMatrix)); // why transpose ? I don't know - OnViewChange(); -} - -const Vec3f& Camera::GetCamPos() const { - return m_CamPos; -} - -} // namespace render -} // namespace td diff --git a/src/td/render/Renderer.cpp b/src/td/render/Renderer.cpp index cc5a032..78622fb 100644 --- a/src/td/render/Renderer.cpp +++ b/src/td/render/Renderer.cpp @@ -1,25 +1,23 @@ #include -#include - namespace td { namespace render { void BasicRenderer::Render(const GL::VertexArray& a_Vao) { a_Vao.Bind(); - glDrawArrays(GL_TRIANGLES, 0, a_Vao.GetVertexCount()); - // glDrawElements(GL_TRIANGLES, a_Vao.GetVertexCount(), GL_UNSIGNED_INT, nullptr); + // rlDrawArrays(RL_TRIANGLES, 0, a_Vao.GetVertexCount()); + // rlDrawElements(RL_TRIANrlES, a_Vao.GetVertexCount(), RL_UNSIGNED_INT, nullptr); a_Vao.Unbind(); } RenderPipeline::RenderPipeline() { - glEnable(GL_TEXTURE_2D); - glEnable(GL_BLEND); - glEnable(GL_DEPTH_TEST); - glEnable(GL_CULL_FACE); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glDepthFunc(GL_LESS); - glFrontFace(GL_CCW); + // rlEnable(RL_TEXTURE_2D); + // rlEnable(RL_BLEND); + // rlEnable(RL_DEPTH_TEST); + // rlEnable(RL_CULL_FACE); + // rlBlendFunc(RL_SRC_ALPHA, RL_ONE_MINUS_SRC_ALPHA); + // rlDepthFunc(RL_LESS); + // rlFrontFace(RL_CCW); } } // namespace render diff --git a/src/td/render/loader/GLLoader.cpp b/src/td/render/loader/GLLoader.cpp index 48b388d..b22f26f 100644 --- a/src/td/render/loader/GLLoader.cpp +++ b/src/td/render/loader/GLLoader.cpp @@ -1,28 +1,26 @@ #include -#include - namespace td { namespace GL { VertexArray::~VertexArray() { - if (m_ID != 0) - glDeleteVertexArrays(1, &m_ID); + // if (m_ID != 0) + // glDeleteVertexArrays(1, &m_ID); } VertexArray::VertexArray(ElementBuffer&& indicies) : m_ElementBuffer(std::move(indicies)) { - glGenVertexArrays(1, &m_ID); + // glGenVertexArrays(1, &m_ID); Bind(); BindElementArrayBuffer(); // Unbind(); } void VertexArray::Bind() const { - glBindVertexArray(m_ID); + // glBindVertexArray(m_ID); } void VertexArray::Unbind() const { - glBindVertexArray(0); + // glBindVertexArray(0); } void VertexArray::BindVertexBuffer(VertexBuffer&& VertexBuffer) { @@ -36,24 +34,24 @@ void VertexArray::BindElementArrayBuffer() { } VertexBuffer::~VertexBuffer() { - if (m_ID != 0) - glDeleteBuffers(1, &m_ID); + // if (m_ID != 0) + // glDeleteBuffers(1, &m_ID); } VertexBuffer::VertexBuffer(const std::vector& data, unsigned int stride) : m_DataStride(stride) { - glGenBuffers(1, &m_ID); + // glGenBuffers(1, &m_ID); Bind(); - glBufferData(GL_ARRAY_BUFFER, static_cast(data.size() * sizeof(float)), nullptr, GL_STATIC_DRAW); - glBufferSubData(GL_ARRAY_BUFFER, 0, static_cast(data.size() * sizeof(float)), data.data()); + // glBufferData(GL_ARRAY_BUFFER, static_cast(data.size() * sizeof(float)), nullptr, GL_STATIC_DRAW); + // glBufferSubData(GL_ARRAY_BUFFER, 0, static_cast(data.size() * sizeof(float)), data.data()); Unbind(); } void VertexBuffer::Bind() const { - glBindBuffer(GL_ARRAY_BUFFER, m_ID); + // glBindBuffer(GL_ARRAY_BUFFER, m_ID); } void VertexBuffer::Unbind() const { - glBindBuffer(GL_ARRAY_BUFFER, 0); + // glBindBuffer(GL_ARRAY_BUFFER, 0); } void VertexBuffer::AddVertexAttribPointer(unsigned int index, unsigned int coordinateSize, unsigned int offset) { @@ -67,32 +65,32 @@ void VertexBuffer::AddVertexAttribPointer(unsigned int index, unsigned int coord void VertexBuffer::BindVertexAttribs() const { for (const VertexAttribPointer& pointer : m_VertexAttribs) { - glVertexAttribPointer(pointer.m_Index, static_cast(pointer.m_Size), GL_FLOAT, false, m_DataStride * sizeof(float), - reinterpret_cast(static_cast(pointer.m_Offset))); - glEnableVertexAttribArray(pointer.m_Index); + // glVertexAttribPointer(pointer.m_Index, static_cast(pointer.m_Size), GL_FLOAT, false, m_DataStride * sizeof(float), + // reinterpret_cast(static_cast(pointer.m_Offset))); + // glEnableVertexAttribArray(pointer.m_Index); } } ElementBuffer::ElementBuffer(const std::vector& indicies) { m_TriangleCount = indicies.size(); - glGenBuffers(1, &m_ID); + // glGenBuffers(1, &m_ID); Bind(); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, static_cast(indicies.size() * sizeof(unsigned int)), nullptr, GL_STATIC_DRAW); - glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, static_cast(indicies.size() * sizeof(unsigned int)), indicies.data()); + // glBufferData(GL_ELEMENT_ARRAY_BUFFER, static_cast(indicies.size() * sizeof(unsigned int)), nullptr, GL_STATIC_DRAW); + // glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, static_cast(indicies.size() * sizeof(unsigned int)), indicies.data()); Unbind(); } ElementBuffer::~ElementBuffer() { - if (m_ID != 0) - glDeleteBuffers(1, &m_ID); + // if (m_ID != 0) + // glDeleteBuffers(1, &m_ID); } void ElementBuffer::Bind() const { - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ID); + // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ID); } void ElementBuffer::Unbind() const { - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } } // namespace GL diff --git a/src/td/render/loader/WorldLoader.cpp b/src/td/render/loader/WorldLoader.cpp index fa478ce..8fac49f 100644 --- a/src/td/render/loader/WorldLoader.cpp +++ b/src/td/render/loader/WorldLoader.cpp @@ -1,8 +1,6 @@ #include -#include -#include - +#include #include namespace td { @@ -11,11 +9,12 @@ namespace render { namespace WorldLoader { const static int POSITION_VERTEX_SIZE = 3; -// const static int TEXTURE_VERTEX_SIZE = 2; +const static int COLOR_VERTEX_SIZE = 4; -GL::VertexArray LoadWorldModel(const td::game::World* world) { +Mesh LoadWorldModel(const td::game::World* world) { + Mesh mesh = {0}; std::vector positions; - std::vector colors; + std::vector colors; for (const auto& [coords, chunk] : world->GetChunks()) { std::int32_t chunkX = coords.x * td::game::Chunk::ChunkWidth; @@ -42,15 +41,10 @@ GL::VertexArray LoadWorldModel(const td::game::World* world) { const td::Color* tileColor = world->GetTileColor(tile); for (int i = 0; i < 6; i++) { - int color = 255; - color |= tileColor->r << 24; - color |= tileColor->g << 16; - color |= tileColor->b << 8; - - int newColorIndex = colors.size(); - colors.push_back(0); - - memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int)); + colors.push_back(tileColor->r); + colors.push_back(tileColor->g); + colors.push_back(tileColor->b); + colors.push_back(255); } } } @@ -64,15 +58,10 @@ GL::VertexArray LoadWorldModel(const td::game::World* world) { positions.insert(positions.end(), {fromX, 0, fromY, fromX, 0, toY, toX, 0, fromY, fromX, 0, toY, toX, 0, toY, toX, 0, fromY}); for (int i = 0; i < 6; i++) { - int color = 255; - color |= world->GetSpawnColor(TeamColor(spawnColor)).r << 24; - color |= world->GetSpawnColor(TeamColor(spawnColor)).g << 16; - color |= world->GetSpawnColor(TeamColor(spawnColor)).b << 8; - - int newColorIndex = colors.size(); - colors.push_back(0); - - memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int)); + colors.push_back(world->GetSpawnColor(TeamColor(spawnColor)).r); + colors.push_back(world->GetSpawnColor(TeamColor(spawnColor)).g); + colors.push_back(world->GetSpawnColor(TeamColor(spawnColor)).b); + colors.push_back(255); } } @@ -84,162 +73,31 @@ GL::VertexArray LoadWorldModel(const td::game::World* world) { positions.insert(positions.end(), {fromX, 0, fromY, fromX, 0, toY, toX, 0, fromY, fromX, 0, toY, toX, 0, toY, toX, 0, fromY}); for (int i = 0; i < 6; i++) { - int color = 255; - color |= world->GetSpawnColor(TeamColor(castleColor)).r << 24; - color |= world->GetSpawnColor(TeamColor(castleColor)).g << 16; - color |= world->GetSpawnColor(TeamColor(castleColor)).b << 8; - - int newColorIndex = colors.size(); - colors.push_back(0); - - memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int)); + colors.push_back(world->GetSpawnColor(TeamColor(castleColor)).r); + colors.push_back(world->GetSpawnColor(TeamColor(castleColor)).g); + colors.push_back(world->GetSpawnColor(TeamColor(castleColor)).b); + colors.push_back(255); } } - GL::VertexBuffer positionVBO(positions, POSITION_VERTEX_SIZE); - positionVBO.AddVertexAttribPointer(0, POSITION_VERTEX_SIZE, 0); - GL::VertexBuffer colorVBO(colors, 1); - colorVBO.AddVertexAttribPointer(1, 1, 0); + mesh.vertexCount = positions.size() / 3; + mesh.triangleCount = mesh.vertexCount / 3; - std::vector indexes(positions.size() / 3, 0); - for (size_t i = 0; i < indexes.size(); i++) { - indexes[i] = i + 1; - } + const std::size_t verteciesSize = mesh.vertexCount * POSITION_VERTEX_SIZE * sizeof(float); + const std::size_t colorsSize = mesh.vertexCount * COLOR_VERTEX_SIZE * sizeof(unsigned char); - GL::ElementBuffer indexVBO(indexes); + mesh.vertices = (float*)MemAlloc(verteciesSize); + mesh.colors = (unsigned char*)MemAlloc(colorsSize); - GL::VertexArray worldVao(std::move(indexVBO)); // each pos = 3 vertecies - worldVao.Bind(); - worldVao.BindVertexBuffer(std::move(positionVBO)); - worldVao.BindVertexBuffer(std::move(colorVBO)); - worldVao.Unbind(); - return worldVao; + std::memcpy(reinterpret_cast(mesh.vertices), reinterpret_cast(positions.data()), verteciesSize); + std::memcpy(reinterpret_cast(mesh.colors), reinterpret_cast(colors.data()), colorsSize); + + UploadMesh(&mesh, false); + + return mesh; } -GL::VertexArray LoadTileSelectModel() { - std::vector positions = { - -0.5f, - -0.5f, - -1.0f, - - 0.5f, - -0.5f, - -1.0f, - - 0.0f, - 0.5f, - -1.0f, - - 1, - .01, - 1, - - 0, - .01, - 1, - - 0, - 1, - 1, - }; - - int color = 255 << 24 | 255 << 16 | 255 << 8 | 150; - float colorFloat; - - memcpy(reinterpret_cast(&colorFloat), &color, sizeof(float)); - - std::vector colors(6, colorFloat); - - GL::VertexBuffer positionVBO(positions, POSITION_VERTEX_SIZE); - positionVBO.AddVertexAttribPointer(0, POSITION_VERTEX_SIZE, 0); - GL::VertexBuffer colorVBO(colors, 1); - colorVBO.AddVertexAttribPointer(1, 1, 0); - - std::vector indexes(positions.size() / 3, 0); - // for (size_t i = 0; i < indexes.size(); i++) { - // indexes[i] = i + 1; - // } - GL::ElementBuffer indexVBO(indexes); - - GL::VertexArray tileSelectVao(std::move(indexVBO)); - tileSelectVao.Bind(); - tileSelectVao.BindVertexBuffer(std::move(positionVBO)); - tileSelectVao.BindVertexBuffer(std::move(colorVBO)); - tileSelectVao.Unbind(); - - return tileSelectVao; -} - -RenderData LoadTowerModel(const game::TowerPtr& tower) { - RenderData renderData; - - float towerX, towerDX; - float towerY, towerDY; - - if (tower->GetSize() == game::TowerSize::Little) { - towerX = tower->GetCenterX() - 1.5f; - towerDX = tower->GetCenterX() + 1.5f; - - towerY = tower->GetCenterY() - 1.5f; - towerDY = tower->GetCenterY() + 1.5f; - } else { - towerX = tower->GetCenterX() - 2.5f; - towerDX = tower->GetCenterX() + 2.5f; - - towerY = tower->GetCenterY() - 2.5f; - towerDY = tower->GetCenterY() + 2.5f; - } - std::vector positions = {towerDX, 0.001, towerY, towerX, 0.001, towerY, towerX, 0.001, towerDY, towerDX, 0.001, towerY, - towerX, 0.001, towerDY, towerDX, 0.001, towerDY}; - - renderData.positions = positions; - - std::uint8_t towerType = static_cast(tower->GetType()); - std::uint8_t r = 10 * towerType + 40, g = 5 * towerType + 30, b = 10 * towerType + 20; - - float colorFloat; - int color = r << 24 | g << 16 | b << 8 | 255; - memcpy(&colorFloat, &color, sizeof(int)); - - std::vector colors(6, colorFloat); - renderData.colors = colors; - - return renderData; -} - - - -GL::VertexArray LoadMobModel() { - std::vector positions = { - -0.5, 0, -0.5, - -0.5, 0, 0.5, - 0.5, 0, -0.5, - - 0.5, 0, -0.5, - -0.5, 0, 0.5, - 0.5, 0, 0.5 - }; - - std::vector indexes(positions.size() / 3, 0); - // for (size_t i = 0; i < indexes.size(); i++) { - // indexes[i] = i + 1; - // } - GL::ElementBuffer indexVBO(indexes); - - GL::VertexBuffer positionVBO(positions, POSITION_VERTEX_SIZE); - positionVBO.AddVertexAttribPointer(0, POSITION_VERTEX_SIZE, 0); - - GL::VertexArray mobVao(std::move(indexVBO)); // each pos = 1 color - mobVao.Bind(); - mobVao.BindVertexBuffer(std::move(positionVBO)); - mobVao.Unbind(); - return mobVao; -} - - - } // namespace WorldLoader - } // namespace render } // namespace td diff --git a/src/td/render/renderer/EntityRenderer.cpp b/src/td/render/renderer/EntityRenderer.cpp index 6e373f3..9f733c9 100644 --- a/src/td/render/renderer/EntityRenderer.cpp +++ b/src/td/render/renderer/EntityRenderer.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include @@ -6,24 +8,22 @@ namespace td { namespace render { EntityRenderer::EntityRenderer(Camera& a_Camera, const game::WorldPtr& a_World) : Renderer(a_Camera), m_World(a_World) { - m_EntityVao = std::make_unique(WorldLoader::LoadMobModel()); - m_Shader->Start(); - m_Shader->SetColorEffect({1, 0, 1}); + m_ZombieModel = LoadModel("assets/zombie.glb"); } -EntityRenderer::~EntityRenderer() {} +EntityRenderer::~EntityRenderer() { + UnloadModel(m_ZombieModel); +} void EntityRenderer::Render(float a_Lerp) { - m_Shader->Start(); for (const auto& mob : m_World->GetMobList()) { float x = Lerp(*mob, a_Lerp, [](const game::Mob& a_Mob) { return static_cast(a_Mob.m_Position.x); }); float z = Lerp(*mob, a_Lerp, [](const game::Mob& a_Mob) { return static_cast(a_Mob.m_Position.y); }); - m_Shader->SetModelPos({x, .001, z}); - Renderer::Render(*m_EntityVao); + DrawModel(m_ZombieModel, {x, .001, z}, 1.0f, {255, 255, 255, 255}); } } diff --git a/src/td/render/renderer/TowerRenderer.cpp b/src/td/render/renderer/TowerRenderer.cpp index f0ebbfa..3eefbf6 100644 --- a/src/td/render/renderer/TowerRenderer.cpp +++ b/src/td/render/renderer/TowerRenderer.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -6,20 +7,21 @@ namespace td { namespace render { TowerRenderer::TowerRenderer(Camera& a_Camera, const game::WorldPtr& a_World) : Renderer(a_Camera), m_World(a_World) { - m_EntityVao = std::make_unique(WorldLoader::LoadMobModel()); - m_Shader->Start(); - m_Shader->SetColorEffect({0, 0, 1}); + m_TowerModel = LoadModel("assets/turret.obj"); + m_TowerTexture = LoadTexture("assets/turret_diffuse.png"); + m_TowerModel.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = m_TowerTexture; } -TowerRenderer::~TowerRenderer() {} +TowerRenderer::~TowerRenderer() { + UnloadModel(m_TowerModel); + UnloadTexture(m_TowerTexture); +} void TowerRenderer::Render(float a_Lerp) { - m_Shader->Start(); for (const auto& tower : m_World->GetTowers()) { - m_Shader->SetModelPos({tower->GetCenterX(), .001, tower->GetCenterY()}); - Renderer::Render(*m_EntityVao); + DrawModel(m_TowerModel, {tower->GetCenterX(), .001, tower->GetCenterY()}, 0.3f, {255, 255, 255, 255}); } } diff --git a/src/td/render/renderer/WorldRenderer.cpp b/src/td/render/renderer/WorldRenderer.cpp index df57f0c..f378e04 100644 --- a/src/td/render/renderer/WorldRenderer.cpp +++ b/src/td/render/renderer/WorldRenderer.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -9,24 +10,26 @@ namespace td { namespace render { WorldRenderer::WorldRenderer(Camera& a_Camera, const game::WorldPtr& a_World) : Renderer(a_Camera) { - m_WorldVao = std::make_unique(WorldLoader::LoadWorldModel(a_World.get())); + Mesh mesh = WorldLoader::LoadWorldModel(a_World.get()); + m_WorldModel = LoadModelFromMesh(mesh); } -WorldRenderer::~WorldRenderer() {} +WorldRenderer::~WorldRenderer() { + UnloadModel(m_WorldModel); +} void WorldRenderer::UpdateControls() { if (ImGui::IsMouseDown(ImGuiMouseButton_Left)) { constexpr float sensitivity = 1.0f; float delta = ImGui::GetIO().DeltaTime; auto mouseDelta = ImGui::GetIO().MouseDelta; - m_Camera.SetCamPos(m_Camera.GetCamPos() + Vec3f{-mouseDelta.x * delta * sensitivity, 0, -mouseDelta.y * delta * sensitivity}); + m_Camera.position.x -= mouseDelta.x * delta * sensitivity; + m_Camera.position.z -= mouseDelta.y * delta * sensitivity; } } void WorldRenderer::Render(float a_Lerp) { - UpdateControls(); - m_Shader->Start(); - Renderer::Render(*m_WorldVao); + DrawModel(m_WorldModel, {}, 1.0f, {255, 255, 255, 255}); } } // namespace render diff --git a/src/td/render/shader/CameraShaderProgram.cpp b/src/td/render/shader/CameraShaderProgram.cpp index a54aeed..ee5a37c 100644 --- a/src/td/render/shader/CameraShaderProgram.cpp +++ b/src/td/render/shader/CameraShaderProgram.cpp @@ -4,16 +4,16 @@ namespace td { namespace shader { void CameraShaderProgram::SetProjectionMatrix(const Mat4f& proj) const { - LoadMat4(m_LocationProjection, proj); + // LoadMat4(m_LocationProjection, proj); } void CameraShaderProgram::SetViewMatrix(const Mat4f& view) const { - LoadMat4(m_LocationView, view); + // LoadMat4(m_LocationView, view); } void CameraShaderProgram::GetAllUniformLocation() { - m_LocationProjection = static_cast(GetUniformLocation("projectionMatrix")); - m_LocationView = static_cast(GetUniformLocation("viewMatrix")); + // m_LocationProjection = static_cast(GetUniformLocation("projectionMatrix")); + // m_LocationView = static_cast(GetUniformLocation("viewMatrix")); } diff --git a/src/td/render/shader/EntityShader.cpp b/src/td/render/shader/EntityShader.cpp index 3080487..faefe19 100644 --- a/src/td/render/shader/EntityShader.cpp +++ b/src/td/render/shader/EntityShader.cpp @@ -84,22 +84,22 @@ void main(void){ #endif EntityShader::EntityShader() : CameraShaderProgram() { - ShaderProgram::LoadProgram(vertexSource, fragmentSource); + // ShaderProgram::LoadProgram(vertexSource, fragmentSource); } void EntityShader::GetAllUniformLocation() { - CameraShaderProgram::GetAllUniformLocation(); - m_LocationColorEffect = static_cast(GetUniformLocation("ColorEffect")); - m_LocationPosition = static_cast(GetUniformLocation("modelPosition")); + // CameraShaderProgram::GetAllUniformLocation(); + // m_LocationColorEffect = static_cast(GetUniformLocation("ColorEffect")); + // m_LocationPosition = static_cast(GetUniformLocation("modelPosition")); } void EntityShader::SetColorEffect(const Vec3f& color) { - LoadVector(m_LocationColorEffect, color); + // LoadVector(m_LocationColorEffect, color); } void EntityShader::SetModelPos(const Vec3f& pos) const { - LoadVector(m_LocationPosition, pos); + // LoadVector(m_LocationPosition, pos); } } // namespace shader diff --git a/src/td/render/shader/ShaderProgram.cpp b/src/td/render/shader/ShaderProgram.cpp deleted file mode 100755 index 9406bb5..0000000 --- a/src/td/render/shader/ShaderProgram.cpp +++ /dev/null @@ -1,145 +0,0 @@ -/* - * ShaderProgram.cpp - * - * Created on: 31 janv. 2020 - * Author: simon - */ - -#include -#include -#include - -#include -#include -#include -#include - -namespace td { -namespace shader { - -ShaderProgram::ShaderProgram() : - m_ProgramID(0), m_VertexShaderID(0), m_FragmentShaderID(0) { -} - -ShaderProgram::~ShaderProgram() { - CleanUp(); -} - -void ShaderProgram::Start() const { - glUseProgram(m_ProgramID); -} - -void ShaderProgram::Stop() const { - glUseProgram(0); -} - -int ShaderProgram::GetUniformLocation(const std::string& uniformName) const { - const int location = glGetUniformLocation(m_ProgramID, uniformName.c_str()); - if (location == -1) { - utils::LOGD(utils::Format("Warning ! Uniform variable %s not found !", uniformName.c_str())); - } - return location; -} - -void ShaderProgram::LoadFloat(unsigned int location, float value) const { - glUniform1f(static_cast(location), value); -} - -void ShaderProgram::LoadInt(unsigned int location, int value) const { - glUniform1i(static_cast(location), value); -} - -void ShaderProgram::LoadVector(unsigned int location, - const Vec2f& vector) const { - glUniform2f(static_cast(location), vector.x, vector.y); -} - -void ShaderProgram::LoadVector(unsigned int location, - const Vec3f& vector) const { - glUniform3f(static_cast(location), vector.x, vector.y, vector.z); -} - -void ShaderProgram::LoadBoolean(unsigned int location, bool value) const { - glUniform1i(static_cast(location), value); -} - -void ShaderProgram::LoadMat4(unsigned int location, const Mat4f& mat) const { - glUniformMatrix4fv(static_cast(location), 1, false, reinterpret_cast(&mat)); -} - -void ShaderProgram::CleanUp() const { - Stop(); - glDetachShader(m_ProgramID, m_VertexShaderID); - glDetachShader(m_ProgramID, m_FragmentShaderID); - glDeleteShader(m_VertexShaderID); - glDeleteShader(m_FragmentShaderID); - glDeleteProgram(m_ProgramID); -} - -void ShaderProgram::LoadProgramFile(const std::string& vertexFile, - const std::string& fragmentFile) { - m_VertexShaderID = static_cast(LoadShaderFromFile(vertexFile, GL_VERTEX_SHADER)); - m_FragmentShaderID = static_cast(LoadShaderFromFile(fragmentFile, GL_FRAGMENT_SHADER)); - m_ProgramID = glCreateProgram(); - glAttachShader(m_ProgramID, m_VertexShaderID); - glAttachShader(m_ProgramID, m_FragmentShaderID); - glLinkProgram(m_ProgramID); - glValidateProgram(m_ProgramID); - GetAllUniformLocation(); -} - -void ShaderProgram::LoadProgram(const std::string& vertexSource, - const std::string& fragmentSource) { - m_VertexShaderID = static_cast(LoadShader(vertexSource, GL_VERTEX_SHADER)); - m_FragmentShaderID = static_cast(LoadShader(fragmentSource, GL_FRAGMENT_SHADER)); - m_ProgramID = glCreateProgram(); - glAttachShader(m_ProgramID, m_VertexShaderID); - glAttachShader(m_ProgramID, m_FragmentShaderID); - glLinkProgram(m_ProgramID); - glValidateProgram(m_ProgramID); - GetAllUniformLocation(); -} - -unsigned int ShaderProgram::LoadShader(const std::string& source, GLenum type) { - unsigned int shaderID = glCreateShader(type); - - const char* c_str = source.c_str(); - int* null = 0; - glShaderSource(shaderID, 1, &c_str, null); // @suppress("Function cannot be resolved") - glCompileShader(shaderID); - GLint compilesuccessful; - glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compilesuccessful); - if (!compilesuccessful) { - GLsizei size; - glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &size); - std::vector shaderError(static_cast(size)); - glGetShaderInfoLog(shaderID, size, &size, shaderError.data()); - - utils::LOGE("Could not compile shader !"); - - utils::LOGE(shaderError.data()); - - utils::LOGD(utils::Format("\nShader source : \n" - "------------------------------------------------------------------------------------------------------------------------------------\n" - "%s\n" - "------------------------------------------------------------------------------------------------------------------------------------\n" - , source.c_str())); - } - return shaderID; -} - -unsigned int ShaderProgram::LoadShaderFromFile(const std::string& file, GLenum type) { - std::stringstream stream; - std::ifstream fileStream(file); - - if (fileStream) { - stream << fileStream.rdbuf(); - } else { - return 0; - } - - return LoadShader(stream.str(), type); -} - -} // namespace shader -} // namespace td \ No newline at end of file diff --git a/src/td/render/shader/WorldShader.cpp b/src/td/render/shader/WorldShader.cpp index 01d9e64..7637c4e 100644 --- a/src/td/render/shader/WorldShader.cpp +++ b/src/td/render/shader/WorldShader.cpp @@ -82,7 +82,7 @@ void main(void){ #endif WorldShader::WorldShader() : CameraShaderProgram() { - ShaderProgram::LoadProgram(vertexSource, fragmentSource); + // ShaderProgram::LoadProgram(vertexSource, fragmentSource); } } // namespace shader diff --git a/src/td/simulation/system/EntityMove.cpp b/src/td/simulation/system/EntityMove.cpp index b597831..a825c2b 100644 --- a/src/td/simulation/system/EntityMove.cpp +++ b/src/td/simulation/system/EntityMove.cpp @@ -46,6 +46,9 @@ class DirectionTileVisitor : public game::TileHandler { void EntityMove::Tick(const game::World& a_World, WorldSnapshot& a_State, FpFloat a_Delta) { for (auto& mob : a_State.m_Mobs) { + if (mob->m_HasReachedCastle) + continue; + auto tile = a_World.GetTile(static_cast(mob->m_Position.x), static_cast(mob->m_Position.y)); Direction direction = mob->m_Direction; if (tile) { @@ -56,6 +59,8 @@ void EntityMove::Tick(const game::World& a_World, WorldSnapshot& a_State, FpFloa auto directVector = GetUnitDirection(direction); mob->m_Position.x += directVector.x * a_Delta; mob->m_Position.y += directVector.y * a_Delta; + + } } diff --git a/xmake.lua b/xmake.lua index 0b93abe..196352e 100644 --- a/xmake.lua +++ b/xmake.lua @@ -2,8 +2,8 @@ add_rules("mode.debug", "mode.release") add_repositories("persson-repo https://git.ale-pri.com/Persson-dev/xmake-repo.git") -add_requires("imgui 1.92.0", {configs = {sdl3 = true, opengl3 = true}}) -add_requires("libsdl3 3.2.16", "splib 2.3.2", "zlib", "glew", "fpm", "enet6") +add_requires("rlimgui") +add_requires("splib 2.3.2", "zlib", "fpm", "enet6") set_languages("c++20") @@ -21,9 +21,8 @@ target("Tower-Defense2") add_includedirs("include", {public = true}) set_kind("binary") add_files("src/**.cpp") - add_packages("libsdl3", "imgui", "glew", "splib", "zlib", "fpm", "enet6", {public = true}) + add_packages("raylib", "rlimgui", "splib", "zlib", "fpm", "enet6", {public = true}) set_rundir(".") - add_defines("TD_GL_LOADER_GLEW") -- Tests