summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMelik Houij <houijmelik08@protonmail.com>2022-08-31 12:41:02 +0200
committerMelik Houij <houijmelik08@protonmail.com>2022-08-31 12:41:02 +0200
commit720b156ee59bf8f4c7d862b43cd065990105242d (patch)
tree1ba80d88e797545f411b1a220f5ca8916b0c5a56
parent19d215559b0093380efd72c9363e652e0f871739 (diff)
added proper formatting for button input's allowing for press, hold,
release (and hold length)
-rw-r--r--UI.hpp10
-rw-r--r--gobject.h61
-rw-r--r--main.cpp2
-rw-r--r--scene.h62
4 files changed, 79 insertions, 56 deletions
diff --git a/UI.hpp b/UI.hpp
index 3f176ee..5b3ea7d 100644
--- a/UI.hpp
+++ b/UI.hpp
@@ -6,7 +6,7 @@
#include <string>
#include "text.h"
#include "shader.h"
-#include <bitset>
+#include "gobject.h"
enum uiMenus {
PAUSE,
EXIT
@@ -62,14 +62,14 @@ class UI
}
- void draw(std::bitset<8> ginputs)
+ void draw(std::array<inGameInput,8> ginputs)
{
// NOTE draws the black border around
glBindVertexArray(VAO);
this->uiShader->use();
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
- if (ginputs.test(0)) {
+ if (ginputs[0].bState == PRESSED) {
if (selectedUI == 0) {
selectedUI = drawnUI.size();
}
@@ -77,7 +77,7 @@ class UI
selectedUI--;
}
}
- if (ginputs.test(1)) {
+ if (ginputs[1].bState == PRESSED) {
if (selectedUI == drawnUI.size())
{
selectedUI = 0;
@@ -94,7 +94,7 @@ class UI
for (unsigned int i = 0; i < drawnUI.size(); i++) {
if(i == selectedUI)
{
- textObj.Render(drawnUI.at(i), 25.0f, 800.0f - (240.0f * i), 2.5f, glm::vec3(1.0, 0.5f, 0.5f));
+ textObj.Render(drawnUI.at(i), 25.0f, 800.0f - (120.0f * i), 2.5f, glm::vec3(1.0, 0.5f, 0.5f));
}
else {
textObj.Render(drawnUI.at(i), 25.0f, 800.0f - (120.0f * i), 2.0f, glm::vec3(1.0, 0.8f, 0.2f));
diff --git a/gobject.h b/gobject.h
index d496a81..0fa9737 100644
--- a/gobject.h
+++ b/gobject.h
@@ -7,10 +7,61 @@
#include <iostream>
#include <vector>
+#include <array>
#include <glm/glm.hpp>
#include "animation.h"
#include "shader.h"
+enum ginput{
+UNPRESSED = -1,
+RELEASED = 0,
+PRESSED,
+HELD
+};
+
+struct inGameInput{
+ginput bState = UNPRESSED; // button State
+uint16_t timeHeld = 0; // time held
+ void pressed(const bool pressed) // NOTE button pressed state function
+ {
+ if (pressed)
+ {
+ switch (bState){
+ case UNPRESSED :
+ bState = PRESSED;
+ timeHeld++;
+ break;
+
+ case PRESSED :
+ bState = HELD;
+ timeHeld++;
+ break;
+
+ case HELD :
+ timeHeld++;
+ break;
+ default:
+ break;
+
+ }
+
+ }
+ else {
+ switch (bState){
+ case PRESSED :
+ case HELD :
+ bState = RELEASED;
+ break;
+
+ default :
+ bState = UNPRESSED;
+ timeHeld = 0;
+ }
+ }
+ }
+};
+
+
class gobject
{
// NOTE game Object, this will probably be the one changed the most often
@@ -54,7 +105,7 @@ class gobject
this->xPos = xP;
this->yPos = yP;
}
- void update(GLFWwindow *window, bool paused, std::bitset<8> gameInput)
+ void update(GLFWwindow *window, bool paused, std::array<inGameInput,8> gameInput)
{
// NOTE Update function, almost all of this code is temporary and for testing
// TODO add a proper reaction to inputs
@@ -62,16 +113,16 @@ class gobject
// NOTE lord forgive me
if (!paused)
{
- if (gameInput.test(2))
+ if (gameInput[2].bState >= PRESSED)
this->move(-0.1f);
- if (gameInput.test(3))
+ if (gameInput[3].bState >= PRESSED)
this->move(0.1f);
- if (gameInput.test(4))
+ if (gameInput[4].bState == PRESSED)
{
this->currentanim = 0;
reset = true;
}
- if (gameInput.test(5))
+ if (gameInput[5].bState == PRESSED)
{
this->currentanim = 1;
reset = true;
diff --git a/main.cpp b/main.cpp
index 529e860..f909424 100644
--- a/main.cpp
+++ b/main.cpp
@@ -19,7 +19,7 @@ Scene* current;
int SCR_WIDTH = 960;
int SCR_HEIGHT = 540;
// NOTE target FPS, the end game will run at 60 frames, however i am putting it on 20 to make it easier to observe
-int targetFps = 20;
+int targetFps = 60;
// define base function
// NOTE another way of input is probably better int Input = 0;
// TODO Add Scaling, this is probably done on the shader level
diff --git a/scene.h b/scene.h
index 2b6d3c4..ffa4799 100644
--- a/scene.h
+++ b/scene.h
@@ -3,7 +3,7 @@
// this file should handle almost everything in the game
#include <vector>
-#include <bitset>
+#include <array>
#include "shader.h"
//#include <irrklang/irrKlang.h>
#include "gobject.h"
@@ -17,57 +17,29 @@
// first 4 bits are going to store directional input
//
+
class Scene
{
// NOTE the game will run one Scene at a Time
// Every 2D game object will run a universal Shader, the shader will have it's uniform updated each update cycle
private:
- std::bitset<8> gameInput;
+ std::array<inGameInput,8> buttons;
+ // TODO reform code, use structs instead of bitset;
void pollGInputs(){
- gameInput.reset();
-
- // NOTE look at this atrocity
- if (glfwGetKey(window, GLFW_KEY_W)) {
- gameInput.set(0);
- }
-
- if (glfwGetKey(window, GLFW_KEY_S))
- {
- gameInput.set(1);
- }
-
- if (glfwGetKey(window, GLFW_KEY_A)) {
- gameInput.set(2);
- }
-
- if (glfwGetKey(window,GLFW_KEY_D)) {
- gameInput.set(3);
- }
-
- if (glfwGetKey(window, GLFW_KEY_I)) {
- gameInput.set(4);
- }
-
- if (glfwGetKey(window, GLFW_KEY_O)) {
- gameInput.set(5);
- }
-
- if (glfwGetKey(window, GLFW_KEY_P))
- {
- gameInput.set(6);
- }
-
- if (glfwGetKey(window, GLFW_KEY_N))
- {
- gameInput.set(7);
- }
-
-
-
-
+ // TODO change code around to be able to work off of any input
+
+ // NOTE boilerplate dump, will make a proper loop or smthn later lmao
+ buttons[0].pressed(glfwGetKey(window, GLFW_KEY_W));
+ buttons[1].pressed(glfwGetKey(window, GLFW_KEY_S));
+ buttons[2].pressed(glfwGetKey(window, GLFW_KEY_A));
+ buttons[3].pressed(glfwGetKey(window, GLFW_KEY_D));
+ buttons[4].pressed(glfwGetKey(window, GLFW_KEY_I));
+ buttons[5].pressed(glfwGetKey(window, GLFW_KEY_O));
+ buttons[6].pressed(glfwGetKey(window, GLFW_KEY_P));
+ buttons[7].pressed(glfwGetKey(window, GLFW_KEY_N));
};
public:
std::vector<gobject> objects;
@@ -175,7 +147,7 @@ class Scene
for(gobject& x:objects)
{
//NOTE simple, runs an update to each game Object on the Vector
- x.update(window, paused, gameInput);
+ x.update(window, paused, buttons);
}
if(this->paused == false)
@@ -184,7 +156,7 @@ class Scene
sound.play();
}
else {
- uiObj->draw(gameInput);
+ uiObj->draw(buttons);
}
// NOTE to test audio playback