From 224c1a57d46e6ecfc4ce28869d1408ee2e4b557a Mon Sep 17 00:00:00 2001 From: itqop Date: Wed, 15 May 2024 12:39:51 +0300 Subject: [PATCH] Add Game classes --- CMakeLists.txt | 2 +- include/game.h | 164 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 include/game.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 323ffe9..b186e9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,7 +18,7 @@ add_executable("${CMAKE_PROJECT_NAME}") set_property(TARGET "${CMAKE_PROJECT_NAME}" PROPERTY CXX_STANDARD 17) target_compile_definitions("${CMAKE_PROJECT_NAME}" PUBLIC _CRT_SECURE_NO_WARNINGS) target_compile_definitions("${CMAKE_PROJECT_NAME}" PUBLIC RESOURCES_PATH="${CMAKE_CURRENT_SOURCE_DIR}/resources/") -target_sources("${CMAKE_PROJECT_NAME}" PRIVATE ${MY_SOURCES} "include/log.h" "include/tools.h") +target_sources("${CMAKE_PROJECT_NAME}" PRIVATE ${MY_SOURCES} "include/log.h" "include/tools.h" "include/game.h") if(MSVC) set_target_properties("${CMAKE_PROJECT_NAME}" PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup") set_target_properties("${CMAKE_PROJECT_NAME}" PROPERTIES LINK_FLAGS_RELEASE "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup") diff --git a/include/game.h b/include/game.h new file mode 100644 index 0000000..b0da9ef --- /dev/null +++ b/include/game.h @@ -0,0 +1,164 @@ +#pragma once + +#include "gl2d/gl2d.h" +#include +#include "glm/vec2.hpp" + + +class GameInput +{ +public: + + class Button + { + public: + + bool isPressed() { return pressed; }; + bool isHeld() { return held; } + bool isReleased() { return released; } + + + private: + + friend GameInput; + + bool held = 0; + bool pressed = 0; + bool released = 0; + + void setPress() + { + pressed = true; + held = true; + } + + void setRelease() + { + released = true; + held = false; + } + + + void resetInput() + { + released = 0; + pressed = 0; + } + }; + + enum keys + { + FRONT, + BACK, + LEFT, + RIGHT, + LEFT_CLICK, + RIGHT_CLICK, + DOWN, + UP, + C, + P, + BUTTONS_COUNT + }; + + Button getKey(int k) const + { + if (k < 0 || k >= BUTTONS_COUNT) + { + return Button{}; + } + + return buttons[k]; + } + + void setKeyPress(int k) + { + if (k < 0 || k >= BUTTONS_COUNT) + { + return; + } + + buttons[k].setPress(); + } + + void setKeyRelease(int k) + { + if (k < 0 || k >= BUTTONS_COUNT) + { + return; + } + + buttons[k].setRelease(); + } + + + void resetInputsThisFrame() + { + for (int i = 0; i < BUTTONS_COUNT; i++) + { + buttons[i].resetInput(); + } + } + + void resetInputsToZero() + { + for (int i = 0; i < BUTTONS_COUNT; i++) + { + buttons[i].setRelease(); + buttons[i].resetInput(); + } + } + + int getMousePosX() const + { + return mousePosX; + } + + int getMousePosY() const + { + return mousePosY; + } + + glm::ivec2 getMousePos() const + { + return glm::ivec2(mousePosX, mousePosY); + } + + void setMousePosition(int x, int y) + { + mousePosX = x; + mousePosY = y; + } + +private: + + Button buttons[BUTTONS_COUNT]; + int mousePosX; + int mousePosY; + +}; + +class Game +{ +public: + + Game(int screenW, int screenH) { onCreate(screenW, screenH); } + ~Game() { onDestroy(); } + + void onUpdate(float deltaTime, const GameInput& input); + + + void updateWindowMetrics(int screenW, int screenH); + + +private: + void onCreate(int screenW, int screenH); + + void onDestroy() {}; + + gl2d::Renderer2D renderer2d; + gl2d::Font font; + int screenW, screenH; + + +}; \ No newline at end of file