Extract Config

This commit is contained in:
Oshgnacknak 2021-12-29 14:22:40 +01:00
parent 1f5892a1b7
commit f67add91fe
Signed by: Oshgnacknak
GPG key ID: 8CB7375654585956
2 changed files with 27 additions and 62 deletions

17
src/Config.h Normal file
View file

@ -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 */

View file

@ -1,69 +1,17 @@
#define OSH_H_IMPLEMENTATION #define OSH_H_IMPLEMENTATION
#include "osh.h" #include "osh.h"
#include "Config.h"
#include "Vector.h"
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#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 onError(const int error, const char* description) {
(void) error; (void) error;
osh::panic("GLFW error:", description); 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) { void onResize(GLFWwindow* window, int width, int height) {
glfwGetFramebufferSize(window, &width, &height); glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
@ -104,22 +52,22 @@ struct Player {
} }
void jump() { void jump() {
vel.y = JUMP_FORCE; vel.y = PLAYER_JUMP_FORCE;
jumpTimeout = JUMP_TIMEOUT; jumpTimeout = PLAYER_JUMP_TIMEOUT;
} }
void update(double dt) { void update(double dt) {
if (jumpTimeout > 0) { if (jumpTimeout > 0) {
push({0, JUMP_FORCE}); push({0, PLAYER_JUMP_FORCE});
jumpTimeout -= dt; jumpTimeout -= dt;
} }
vel += acc; vel += acc;
vel *= DRAG; vel *= PLAYER_DRAG;
acc = {0, 0}; acc = {0, 0};
pos += vel * dt; 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() { void draw() {
@ -173,13 +121,13 @@ int main(void) {
glClearColor(BACKGROUND_COLOR); glClearColor(BACKGROUND_COLOR);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
player.push({0, -GRAVITY}); player.push({0, -WORLD_GRAVITY});
player.update(dt); player.update(dt);
player.draw(); player.draw();
glColor3d(FLOOR_COLOR); glColor3d(WORLD_FLOOR_COLOR);
fillRect({ -1, FLOOR_HEIGHT }, {2, 2}); fillRect({ -1, WORLD_FLOOR_HEIGHT}, {2, 2});
glfwSwapBuffers(window); glfwSwapBuffers(window);
last = now; last = now;