MainMenuState.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "MainMenuState.h"
  2. #include "GamePlayState.h"
  3. #include "AssetEditorState.h"
  4. #include "MapEditorState.h"
  5. #include "../Engine.h"
  6. #include <imgui.h>
  7. #include <SDL2/SDL_log.h>
  8. void MainMenuState::Enter(Engine& engine)
  9. {
  10. fadeAlpha = 1.0f;
  11. }
  12. void MainMenuState::Exit(Engine& engine)
  13. {
  14. }
  15. void MainMenuState::HandleEvents(Engine& engine, SDL_Event& e)
  16. {
  17. }
  18. void MainMenuState::Update(Engine& engine, float dt)
  19. {
  20. if (fadeAlpha < 1.0f)
  21. {
  22. fadeAlpha += dt * 2.0f;
  23. if (fadeAlpha > 1.0f)
  24. {
  25. fadeAlpha = 1.0f;
  26. }
  27. }
  28. }
  29. void MainMenuState::Render(Engine& engine)
  30. {
  31. SDL_Renderer* renderer = engine.GetRenderer();
  32. SDL_SetRenderDrawColor(renderer, 10, 20, 40, 255);
  33. SDL_RenderClear(renderer);
  34. }
  35. void MainMenuState::RenderImGui(Engine& engine)
  36. {
  37. ImGuiIO& io = ImGui::GetIO();
  38. ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x * 0.5f, io.DisplaySize.y * 0.5f),
  39. ImGuiCond_Always,
  40. ImVec2(0.5f, 0.5f));
  41. ImGui::SetNextWindowSize(ImVec2(400, 500), ImGuiCond_FirstUseEver);
  42. ImGui::Begin("Main Menu", nullptr, ImGuiWindowFlags_NoTitleBar |
  43. ImGuiWindowFlags_NoResize |
  44. ImGuiWindowFlags_NoMove |
  45. ImGuiWindowFlags_NoCollapse);
  46. ImGui::PushFont(ImGui::GetIO().Fonts->Fonts[0]);
  47. ImGui::Dummy(ImVec2(0, 40));
  48. ImGui::SetCursorPosX((ImGui::GetWindowSize().x - ImGui::CalcTextSize("kariokaEngine").x) * 0.5f);
  49. ImGui::TextColored(ImVec4(0.9f, 0.9f, 1.0f, fadeAlpha), "kariokaEngine");
  50. // Main Menu Buttons
  51. ImGui::Dummy(ImVec2(0.0f, 100.0f));
  52. float buttonWidth = 200.f;
  53. float windowWidth = ImGui::GetWindowSize().x;
  54. ImGui::SetCursorPosX((windowWidth - buttonWidth) * 0.5f);
  55. if (ImGui::Button("Enter World", ImVec2(buttonWidth, 30)))
  56. {
  57. // engine.stateManager.ChangeState(std::make_unique<GamePlayState>(), engine);
  58. engine.stateManager.PushState(std::make_unique<GamePlayState>(), engine);
  59. }
  60. ImGui::SetCursorPosX((windowWidth - buttonWidth) * 0.5f);
  61. if (ImGui::Button("Map Editor", ImVec2(buttonWidth, 30)))
  62. {
  63. engine.stateManager.PushState(std::make_unique<MapEditorState>(), engine);
  64. }
  65. ImGui::SetCursorPosX((windowWidth - buttonWidth) * 0.5f);
  66. if (ImGui::Button("Asset Editor", ImVec2(buttonWidth, 30)))
  67. {
  68. engine.stateManager.PushState(std::make_unique<AssetEditorState>(), engine);
  69. }
  70. ImGui::SetCursorPosX((windowWidth - buttonWidth) * 0.5f);
  71. if (ImGui::Button("Exit", ImVec2(buttonWidth, 30)))
  72. {
  73. engine.StopRunning();
  74. }
  75. ImGui::PopFont();
  76. ImGui::End();
  77. }