From f67add91fe165fb01e53e2702aad8c0b0c7216bd Mon Sep 17 00:00:00 2001 From: Oshgnacknak Date: Wed, 29 Dec 2021 14:22:40 +0100 Subject: [PATCH] Extract Config --- src/Config.h | 17 ++++++++++++ src/hophop.cpp | 72 +++++++------------------------------------------- 2 files changed, 27 insertions(+), 62 deletions(-) create mode 100644 src/Config.h diff --git a/src/Config.h b/src/Config.h new file mode 100644 index 0000000..0660f3a --- /dev/null +++ b/src/Config.h @@ -0,0 +1,17 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#define WORLD_GRAVITY 0.05 + +#define WORLD_FLOOR_HEIGHT -0.9 +#define WORLD_FLOOR_COLOR 0.15, 0.05, 0.05 + +#define PLAYER_JUMP_TIMEOUT 0.14 +#define PLAYER_JUMP_FORCE 0.3 +#define PLAYER_DRAG 0.98 +#define PLAYER_RADIUS 0.02 +#define PLAYER_COLOR 1.0, 1.0, 1.0 + +#define BACKGROUND_COLOR 0.05, 0.05, 0.05, 1.0 + +#endif /* CONFIG_H */ diff --git a/src/hophop.cpp b/src/hophop.cpp index 8f74892..91804ec 100644 --- a/src/hophop.cpp +++ b/src/hophop.cpp @@ -1,69 +1,17 @@ #define OSH_H_IMPLEMENTATION #include "osh.h" +#include "Config.h" +#include "Vector.h" #include #include -#define GRAVITY 0.05 -#define FLOOR_HEIGHT -0.9 -#define JUMP_TIMEOUT 0.14 -#define JUMP_FORCE 0.3 -#define DRAG 0.98 -#define PLAYER_RADIUS 0.02 - -#define PLAYER_COLOR 1.0, 1.0, 1.0 -#define FLOOR_COLOR 0.15, 0.05, 0.05 -#define BACKGROUND_COLOR 0.05, 0.05, 0.05, 1.0 void onError(const int error, const char* description) { (void) error; osh::panic("GLFW error:", description); } -struct Vector2 { - double x, y; - - Vector2(double x = 0, double y = 0) : x(x), y(y) {} - - Vector2 conjugate() { - return {x, -y}; - } - - Vector2 operator*(double d) { - return {x*d, y*d}; - } - - Vector2 operator*=(double d) { - x *= d; - y *= d; - return *this; - } - - Vector2 operator+(Vector2 v) { - return {x+v.x, y+v.y}; - } - - Vector2 operator+=(Vector2 v) { - x += v.x; - y += v.y; - return *this; - } - - Vector2 operator-(Vector2 v) { - return {x-v.x, y-v.y}; - } - - Vector2 operator-=(Vector2 v) { - x -= v.x; - y -= v.y; - return *this; - } -}; - -void print1(osh::Formatter auto& fmt, const Vector2& v) { - osh::printp(fmt, '(', v.x, ", ", v.y, ')'); -} - void onResize(GLFWwindow* window, int width, int height) { glfwGetFramebufferSize(window, &width, &height); glViewport(0, 0, width, height); @@ -104,22 +52,22 @@ struct Player { } void jump() { - vel.y = JUMP_FORCE; - jumpTimeout = JUMP_TIMEOUT; + vel.y = PLAYER_JUMP_FORCE; + jumpTimeout = PLAYER_JUMP_TIMEOUT; } void update(double dt) { if (jumpTimeout > 0) { - push({0, JUMP_FORCE}); + push({0, PLAYER_JUMP_FORCE}); jumpTimeout -= dt; } vel += acc; - vel *= DRAG; + vel *= PLAYER_DRAG; acc = {0, 0}; pos += vel * dt; - pos.y = osh::max(pos.y, FLOOR_HEIGHT + size.y); + pos.y = osh::max(pos.y, WORLD_FLOOR_HEIGHT + size.y); } void draw() { @@ -173,13 +121,13 @@ int main(void) { glClearColor(BACKGROUND_COLOR); glClear(GL_COLOR_BUFFER_BIT); - player.push({0, -GRAVITY}); + player.push({0, -WORLD_GRAVITY}); player.update(dt); player.draw(); - glColor3d(FLOOR_COLOR); - fillRect({ -1, FLOOR_HEIGHT }, {2, 2}); + glColor3d(WORLD_FLOOR_COLOR); + fillRect({ -1, WORLD_FLOOR_HEIGHT}, {2, 2}); glfwSwapBuffers(window); last = now;