From 1094987452da0bb5022b53b52114857d72a053ec Mon Sep 17 00:00:00 2001 From: Igor Khramtsov Date: Sat, 1 Aug 2020 19:21:39 +0700 Subject: [PATCH] Input control --- README.md | 2 +- src/Application.cpp | 2 +- src/GameObject.cpp | 5 +++++ src/GameObject.h | 1 + src/MyApp.cpp | 37 ++++++++++++++++++++++++++++++++++--- src/MyApp.h | 12 ++++++++++++ src/Shader.cpp | 15 +++------------ src/Shader.h | 2 +- src/utils.h | 9 +++++++++ 9 files changed, 67 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 264166c..8884c3e 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,6 @@ make # your build command dependent on platform 1. [X] Project initialization (cmake) 2. [X] Creating a window (glfw) 3. [X] Drawing (glew, glm) - 4. [ ] Input control + 4. [X] Input control 5. [ ] Collisions 6. [ ] Game logic \ No newline at end of file diff --git a/src/Application.cpp b/src/Application.cpp index 30e630c..d06bda9 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -52,7 +52,7 @@ void Application::Run() { auto lastTime = glfwGetTime(); double curTime; double deltaTime = 0.; - double updateRate = 1. / 50.; + double updateRate = 1. / 40.; while(!glfwWindowShouldClose(m_Window)) { m_Renderer.Clear(); diff --git a/src/GameObject.cpp b/src/GameObject.cpp index 3fd1890..7b35e87 100644 --- a/src/GameObject.cpp +++ b/src/GameObject.cpp @@ -18,6 +18,11 @@ void GameObject::setPosition(float x, float y) { transform.pos[1] = y; } +void GameObject::move(float x, float y) { + transform.pos[0] += x; + transform.pos[1] += y; +} + void GameObject::setScale(float x, float y) { transform.scale[0] = x; transform.scale[1] = y; diff --git a/src/GameObject.h b/src/GameObject.h index 1f74ebf..68e68e5 100644 --- a/src/GameObject.h +++ b/src/GameObject.h @@ -14,5 +14,6 @@ class GameObject { glm::vec2 getCenter() const; void setColor(float v0, float v1, float v2, float v3); void setPosition(float x, float y); + void move(float x, float y); void setScale(float x, float y); }; \ No newline at end of file diff --git a/src/MyApp.cpp b/src/MyApp.cpp index 49c08e5..16ac38e 100644 --- a/src/MyApp.cpp +++ b/src/MyApp.cpp @@ -1,4 +1,5 @@ #include "MyApp.h" +#include "utils.h" MyApp::MyApp(const char *title, int width, int height) : Application(title, width, height) { m_Renderer.setClearColor(0.1f, 0.1f, 0.1f, 1.f); @@ -32,11 +33,37 @@ void MyApp::Start() { m_Ball.renderebale = m_Renderer.CreateRect(25, 25); m_Ball.setColor(0.7f, 0.3f, 0.3f, 1.f); m_Ball.setPosition(m_Platform.transform.pos.x + 120.f / 2.f - 25.f / 2.f, m_Platform.transform.pos.y + 30.f); + + m_State = STATE_START; } void MyApp::onUpdate() { - if (isKeyPressed(GLFW_KEY_LEFT)) - m_Platform.setPosition(m_Platform.transform.pos.x - 1.f, m_Platform.transform.pos.y); + // TODO: Movement should be rewrited + auto ballSpeed = m_Platform.transform.velocity.x; + if (isKeyPressed(GLFW_KEY_LEFT)) { + curAccelerationSpeed -= accelerationSpeed * (MAX(abs(curAccelerationSpeed), 0.25f) / topAccelerationSpeed); + } else if (isKeyPressed(GLFW_KEY_RIGHT)) { + curAccelerationSpeed += accelerationSpeed * (MAX(abs(curAccelerationSpeed), 0.25f) / topAccelerationSpeed); + } else { + curAccelerationSpeed -= MIN(friction / 2., abs(curAccelerationSpeed)) * sign(curAccelerationSpeed); + if (abs(ballSpeed) > 0) + ballSpeed += MIN(friction * (MAX(abs(ballSpeed), 0.25f) / topSpeed), abs(ballSpeed)) * -sign(ballSpeed); + } + ballSpeed += curAccelerationSpeed; + curAccelerationSpeed = MIN(abs(curAccelerationSpeed), topAccelerationSpeed) * sign(curAccelerationSpeed); + ballSpeed = MIN(abs(ballSpeed), topSpeed) * sign(ballSpeed); + + m_Platform.move(ballSpeed, 0.f); + m_Platform.transform.velocity.x = ballSpeed; + + if (m_State == STATE_START) { + auto center = m_Platform.getCenter(); + m_Ball.setPosition(center.x - (25.f / 2.f), center.y + 30.f); + } else { + m_Ball.move(m_Ball.transform.velocity.x, m_Ball.transform.velocity.y); + } + + std::cout << "curAcc: " << curAccelerationSpeed << " curSpeed: " << ballSpeed << '\n'; } void MyApp::onDraw() { @@ -53,5 +80,9 @@ void MyApp::onDraw() { } void MyApp::onKeyCallback(int key, int action) { - + if (m_State == STATE_START && key == GLFW_KEY_SPACE && action == GLFW_PRESS) { + m_Ball.transform.velocity.x = m_Platform.transform.velocity.x; + m_Ball.transform.velocity.y = 6.f; + m_State = STATE_GAME; + } } \ No newline at end of file diff --git a/src/MyApp.h b/src/MyApp.h index 8968e21..ea00c50 100644 --- a/src/MyApp.h +++ b/src/MyApp.h @@ -3,6 +3,9 @@ #include "Application.h" #include "GameObject.h" +#define STATE_START 0 +#define STATE_GAME 1 + class MyApp : public Application { private: GameObject *m_Bricks = new GameObject[30]; @@ -14,6 +17,15 @@ class MyApp : public Application { int padding_x = 50; int padding_y = 200; int spacing = 10; + + int m_State = STATE_START; + + const float topSpeed = 14.f; + // float curSpeed = 0.f; + const float friction = 3.4f; + const float accelerationSpeed = 1.1f; + float curAccelerationSpeed = .0f; + const float topAccelerationSpeed = 2.5f; public: MyApp(const char *title, int width, int height); diff --git a/src/Shader.cpp b/src/Shader.cpp index 66748b7..1272885 100644 --- a/src/Shader.cpp +++ b/src/Shader.cpp @@ -1,22 +1,13 @@ #include "Shader.h" #include "utils.h" -#include "FileWatcher.h" #include -Shader::Shader(const char* vert_file, const char* frag_file, bool hot) { +Shader::Shader(const char* vert_file, const char* frag_file) { char* vert; char* frag; - if (hot) { - FileWatcher watcher_vert = FileWatcher(vert_file, 350); - watcher_vert.start([&vert_file, &vert]() -> void { readFile(vert_file, &vert); }); - - FileWatcher watcher_frag(frag_file, 350); - watcher_frag.start([&frag_file, &frag]() -> void { readFile(frag_file, &frag); }); - } else { - readFile(vert_file, &vert); - readFile(frag_file, &frag); - } + readFile(vert_file, &vert); + readFile(frag_file, &frag); this->m_ProgramID = Shader::createShaderProgram(vert, frag); } diff --git a/src/Shader.h b/src/Shader.h index 8e576c2..be32ac4 100644 --- a/src/Shader.h +++ b/src/Shader.h @@ -11,7 +11,7 @@ class Shader { public: - Shader(const char*, const char*, bool = false); + Shader(const char*, const char*); ~Shader(); void setUniform4f(const char* name, float v0, float v1, float v2, float v3) const; diff --git a/src/utils.h b/src/utils.h index eb0a026..607df90 100644 --- a/src/utils.h +++ b/src/utils.h @@ -19,6 +19,15 @@ static bool GLErrorsPrint(const char* fname) { return true; } +static int sign(float f) { + if (f > 0.) return 1.f; + else if (f < 0.) return -1.f; + else return 0.f; +} +#define MAX(x, y) (x >= y ? x : y) +#define MIN(x, y) (x <= y ? x : y) + + static void readFile(const char *filename, char** source) { std::cout << "Scanning file " << filename << '\n';