Skip to content

Commit

Permalink
Input control
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorKhramtsov committed Aug 1, 2020
1 parent 52e2659 commit 1094987
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions src/GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/GameObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
37 changes: 34 additions & 3 deletions src/MyApp.cpp
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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() {
Expand All @@ -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;
}
}
12 changes: 12 additions & 0 deletions src/MyApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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);

Expand Down
15 changes: 3 additions & 12 deletions src/Shader.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
#include "Shader.h"
#include "utils.h"
#include "FileWatcher.h"

#include <GL/glew.h>

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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down

0 comments on commit 1094987

Please sign in to comment.