Add Game classes

This commit is contained in:
itqop 2024-05-15 12:39:51 +03:00
parent e73a8b76ce
commit 224c1a57d4
2 changed files with 165 additions and 1 deletions

View File

@ -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")

164
include/game.h Normal file
View File

@ -0,0 +1,164 @@
#pragma once
#include "gl2d/gl2d.h"
#include <glad/glad.h>
#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;
};