comments, clang-format and screensize

This commit is contained in:
speckitor
2026-07-06 13:12:17 +07:00
parent c426dd2d9c
commit d70dd97f1d
8 changed files with 122 additions and 93 deletions
+4
View File
@@ -0,0 +1,4 @@
BasedOnStyle: LLVM
IndentWidth: 4
ColumnLimit: 120
BreakBeforeBraces: Allman
+5
View File
@@ -1,6 +1,11 @@
all: build/game
build/game: src/*
mkdir -p build
gcc -o build/game src/* \
-I./raylib-6.0_linux_amd64/include \
-L./raylib-6.0_linux_amd64/lib \
-l:libraylib.a -lm -lX11
format:
find src/ -type f | xargs clang-format -i
+10 -7
View File
@@ -2,18 +2,21 @@
void MyDrawCube(Cube *cube)
{
if (!cube->grabbed && cube->position.y > cube->edge/2.0f)
// Gravity
if (!cube->grabbed && cube->position.y > cube->edge / 2.0f)
{
float delta = GetFrameTime();
cube->yvel -= delta*32.0f;
cube->position.y += cube->yvel*delta;
if (cube->position.y <= cube->edge/2.0f)
cube->yvel -= delta * 32.0f;
cube->position.y += cube->yvel * delta;
if (cube->position.y <= cube->edge / 2.0f)
{
cube->position.y = cube->edge/2.0f;
cube->position.y = cube->edge / 2.0f;
cube->yvel = 0.0f;
}
}
DrawModelEx(cube->model, cube->position, (Vector3){ 0.0f, 1.0f, 0.0f }, cube->hRotation, (Vector3){ 1.0f, 1.0f, 1.0f }, cube->color);
DrawModelWiresEx(cube->model, cube->position, (Vector3){ 0.0f, 1.0f, 0.0f }, cube->hRotation, (Vector3){ 1.0f, 1.0f, 1.0f }, cube->wiresColor);
DrawModelEx(cube->model, cube->position, (Vector3){0.0f, 1.0f, 0.0f}, cube->hRotation, (Vector3){1.0f, 1.0f, 1.0f},
cube->color);
DrawModelWiresEx(cube->model, cube->position, (Vector3){0.0f, 1.0f, 0.0f}, cube->hRotation,
(Vector3){1.0f, 1.0f, 1.0f}, cube->wiresColor);
}
+2 -1
View File
@@ -3,7 +3,8 @@
#include "raylib.h"
typedef struct {
typedef struct
{
bool initialized;
Model model;
Vector3 position;
+8 -8
View File
@@ -1,13 +1,13 @@
#include <stddef.h>
#include <stdio.h>
#include "raylib.h"
#include "cube.h"
#include "player.h"
#include "raylib.h"
#include "screen.h"
#include <stddef.h>
#include <stdio.h>
Cube cube = {
.initialized = false,
.position = (Vector3){ 0.0f, 0.5f, 0.0f },
.position = (Vector3){0.0f, 0.5f, 0.0f},
.edge = 1.0f,
.color = RED,
.wiresColor = BLACK,
@@ -32,14 +32,14 @@ void DrawLevel00(Player *player)
{
if ((y & 1) ^ (x & 1))
{
DrawPlane((Vector3){ x*tileSize, 0.0f, y*tileSize}, (Vector2){ tileSize, tileSize }, LIGHTGRAY);
DrawPlane((Vector3){x * tileSize, 0.0f, y * tileSize}, (Vector2){tileSize, tileSize}, LIGHTGRAY);
}
}
}
DrawCube((Vector3){ -16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, BLUE); // Draw a blue wall
DrawCube((Vector3){ 16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, GREEN); // Draw a green wall
DrawCube((Vector3){ 0.0f, 2.5f, 16.0f }, 32.0f, 5.0f, 1.0f, YELLOW); // Draw a yellow wall
DrawCube((Vector3){-16.0f, 2.5f, 0.0f}, 1.0f, 5.0f, 32.0f, BLUE); // Draw a blue wall
DrawCube((Vector3){16.0f, 2.5f, 0.0f}, 1.0f, 5.0f, 32.0f, GREEN); // Draw a green wall
DrawCube((Vector3){0.0f, 2.5f, 16.0f}, 32.0f, 5.0f, 1.0f, YELLOW); // Draw a yellow wall
UpdateGrab(player, &cube); // Check if we grabbed a cube
+21 -18
View File
@@ -1,8 +1,8 @@
#include <stddef.h>
#include "player.h"
#include "raylib.h"
#include "raymath.h"
#include "screen.h"
#include "player.h"
#include <stddef.h>
extern Vector2 lookRotation;
extern Vector2 lean;
@@ -15,20 +15,20 @@ static Color colors[MAX_COLUMNS] = {0};
int main()
{
InitWindow(800, 600, "Super cool game");
InitWindow(1600, 900, "Super cool game");
Player player = {0};
Camera *camera = &player.camera;
Body *body = &player.body;
player.grab = NULL;
Vector2 sensitivity = { 0.001f, 0.001f };
Vector2 sensitivity = {0.001f, 0.001f};
float headLerp = STAND_HEIGHT;
camera->fovy = 75.0f;
camera->projection = CAMERA_PERSPECTIVE;
camera->position = (Vector3) {
camera->position = (Vector3){
body->position.x,
body->position.y + (BOTTOM_HEIGHT + headLerp),
body->position.z,
@@ -39,10 +39,11 @@ int main()
DisableCursor();
SetTargetFPS(GAME_FPS);
while (!WindowShouldClose()) {
while (!WindowShouldClose())
{
Vector2 mouseDelta = GetMouseDelta();
lookRotation.x -= mouseDelta.x*sensitivity.x;
lookRotation.y += mouseDelta.y*sensitivity.y;
lookRotation.x -= mouseDelta.x * sensitivity.x;
lookRotation.y += mouseDelta.y * sensitivity.y;
char sideway = (IsKeyDown(KEY_D) - IsKeyDown(KEY_A));
char forward = (IsKeyDown(KEY_W) - IsKeyDown(KEY_S));
@@ -51,25 +52,27 @@ int main()
UpdateBody(&player, lookRotation.x, sideway, forward, IsKeyPressed(KEY_SPACE), crouching);
float delta = GetFrameTime();
headLerp = Lerp(headLerp, (crouching ? CROUCH_HEIGHT : STAND_HEIGHT), 20.0f*delta);
headLerp = Lerp(headLerp, (crouching ? CROUCH_HEIGHT : STAND_HEIGHT), 20.0f * delta);
camera->position = (Vector3){
body->position.x,
body->position.y + (BOTTOM_HEIGHT + headLerp),
body->position.z,
};
if (body->grounded && ((forward != 0) || (sideway != 0))) {
headTimer += delta*3.0f;
walkLerp = Lerp(walkLerp, 1.0f, 10.0f*delta);
camera->fovy = Lerp(camera->fovy, 55.0f, 5.0f*delta);
if (body->grounded && ((forward != 0) || (sideway != 0)))
{
headTimer += delta * 3.0f;
walkLerp = Lerp(walkLerp, 1.0f, 10.0f * delta);
camera->fovy = Lerp(camera->fovy, 55.0f, 5.0f * delta);
}
else {
walkLerp = Lerp(walkLerp, 0.0f, 10.0f*delta);
camera->fovy = Lerp(camera->fovy, 60.0f, 5.0f*delta);
else
{
walkLerp = Lerp(walkLerp, 0.0f, 10.0f * delta);
camera->fovy = Lerp(camera->fovy, 60.0f, 5.0f * delta);
}
lean.x = Lerp(lean.x, sideway*0.02f, 10.0f*delta);
lean.y = Lerp(lean.y, forward*0.015f, 10.0f*delta);
lean.x = Lerp(lean.x, sideway * 0.02f, 10.0f * delta);
lean.y = Lerp(lean.y, forward * 0.015f, 10.0f * delta);
UpdateFpsCamera(&player);
+50 -39
View File
@@ -1,75 +1,81 @@
#include <stddef.h>
#include "player.h"
#include "raylib.h"
#include "raymath.h"
#include "player.h"
#include <stddef.h>
Vector2 lookRotation = {0};
Vector2 lean = {0};
float headTimer = 0.0f;
float walkLerp = 0.0f;
void UpdateFpsCamera(Player *player)
{
Camera *camera = &player->camera;
const Vector3 up = (Vector3){ 0.0f, 1.0f, 0.0f };
const Vector3 targetOffset = (Vector3){ 0.0f, 0.0f, -1.0f };
const Vector3 up = (Vector3){0.0f, 1.0f, 0.0f};
const Vector3 targetOffset = (Vector3){0.0f, 0.0f, -1.0f};
Vector3 yaw = Vector3RotateByAxisAngle(targetOffset, up, lookRotation.x);
float maxAngleUp = Vector3Angle(up, yaw);
maxAngleUp -= 0.001f;
if ( -(lookRotation.y) > maxAngleUp) { lookRotation.y = -maxAngleUp; }
if (-(lookRotation.y) > maxAngleUp)
{
lookRotation.y = -maxAngleUp;
}
Vector3 right = Vector3Normalize(Vector3CrossProduct(yaw, up));
float pitchAngle = -lookRotation.y - lean.y;
pitchAngle = Clamp(pitchAngle, -PI/2 + 0.0001f, PI/2 - 0.0001f);
pitchAngle = Clamp(pitchAngle, -PI / 2 + 0.0001f, PI / 2 - 0.0001f);
Vector3 pitch = Vector3RotateByAxisAngle(yaw, right, pitchAngle);
float headSin = sinf(headTimer*PI);
float headCos = cosf(headTimer*PI);
float headSin = sinf(headTimer * PI);
float headCos = cosf(headTimer * PI);
const float stepRotation = 0.01f;
camera->up = Vector3RotateByAxisAngle(up, pitch, headSin*stepRotation + lean.x);
camera->up = Vector3RotateByAxisAngle(up, pitch, headSin * stepRotation + lean.x);
const float bobSide = 0.1f;
const float bobUp = 0.15f;
Vector3 bobbing = Vector3Scale(right, headSin*bobSide);
bobbing.y = fabsf(headCos*bobUp);
Vector3 bobbing = Vector3Scale(right, headSin * bobSide);
bobbing.y = fabsf(headCos * bobUp);
camera->position = Vector3Add(camera->position, Vector3Scale(bobbing, walkLerp));
camera->target = Vector3Add(camera->position, pitch);
if (player->grab)
{
// Move cube where your camera looks
Vector3 forward = Vector3Subtract(camera->target, camera->position);
forward = Vector3Normalize(forward);
Vector3 cubePosition = Vector3Add(camera->target, forward);
player->grab->position = cubePosition;
if (player->grab->position.y <= player->grab->edge/2.0f)
{
player->grab->position.y = player->grab->edge/2.0f;
}
player->grab->hRotation = lookRotation.x * RAD2DEG;
}
// Don't let putting grab under the floor
if (player->grab->position.y <= player->grab->edge / 2.0f)
{
player->grab->position.y = player->grab->edge / 2.0f;
}
player->grab->hRotation = lookRotation.x * RAD2DEG; // Rottate cube to be perpendicular to camera
}
}
void UpdateBody(Player *player, float rot, char side, char forward, bool jumpPressed, bool crouchHold)
{
Body *body = &player->body;
Vector2 input = (Vector2){ (float)side, (float)-forward };
Vector2 input = (Vector2){(float)side, (float)-forward};
// Slow down diagonal movement
#if defined(NORMALIZE_INPUT)
if ((side != 0) && (forward != 0)) input = Vector2Normalize(input);
if ((side != 0) && (forward != 0))
input = Vector2Normalize(input);
#endif
float delta = GetFrameTime();
if (!body->grounded) body->velocity.y -= GRAVITY*delta;
if (!body->grounded)
body->velocity.y -= GRAVITY * delta;
if (body->grounded && jumpPressed)
{
@@ -81,37 +87,43 @@ void UpdateBody(Player *player, float rot, char side, char forward, bool jumpPre
// PlaySound(fxJump);
}
Vector3 front = (Vector3){ sinf(rot), 0.f, cosf(rot) };
Vector3 right = (Vector3){ cosf(-rot), 0.f, sinf(-rot) };
Vector3 front = (Vector3){sinf(rot), 0.f, cosf(rot)};
Vector3 right = (Vector3){cosf(-rot), 0.f, sinf(-rot)};
Vector3 desiredDir = (Vector3){ input.x*right.x + input.y*front.x, 0.0f, input.x*right.z + input.y*front.z, };
body->dir = Vector3Lerp(body->dir, desiredDir, CONTROL*delta);
Vector3 desiredDir = (Vector3){
input.x * right.x + input.y * front.x,
0.0f,
input.x * right.z + input.y * front.z,
};
body->dir = Vector3Lerp(body->dir, desiredDir, CONTROL * delta);
float decel = (body->grounded ? FRICTION : AIR_DRAG);
Vector3 hvel = (Vector3){ body->velocity.x*decel, 0.0f, body->velocity.z*decel };
Vector3 hvel = (Vector3){body->velocity.x * decel, 0.0f, body->velocity.z * decel};
float hvelLength = Vector3Length(hvel); // Magnitude
if (hvelLength < (MAX_SPEED*0.01f)) hvel = (Vector3){ 0 };
if (hvelLength < (MAX_SPEED * 0.01f))
hvel = (Vector3){0};
// This is what creates strafing
float speed = Vector3DotProduct(hvel, body->dir);
float maxSpeed = (crouchHold? CROUCH_SPEED : MAX_SPEED);
float accel = Clamp(maxSpeed - speed, 0.f, MAX_ACCEL*delta);
hvel.x += body->dir.x*accel;
hvel.z += body->dir.z*accel;
float maxSpeed = (crouchHold ? CROUCH_SPEED : MAX_SPEED);
float accel = Clamp(maxSpeed - speed, 0.f, MAX_ACCEL * delta);
hvel.x += body->dir.x * accel;
hvel.z += body->dir.z * accel;
body->velocity.x = hvel.x;
body->velocity.z = hvel.z;
float diffx = body->velocity.x*delta;
float diffy = body->velocity.y*delta;
float diffz = body->velocity.z*delta;
float diffx = body->velocity.x * delta;
float diffy = body->velocity.y * delta;
float diffz = body->velocity.z * delta;
body->position.x += diffx;
body->position.y += diffy;
body->position.z += diffz;
// Move grab with the player
if (player->grab)
{
Cube *cube = player->grab;
@@ -147,13 +159,12 @@ void UpdateGrab(Player *player, Cube *cube)
return;
}
float halfEdge = cube->edge/2;
float halfEdge = cube->edge / 2;
BoundingBox cubeBox = {
(Vector3){ cube->position.x - halfEdge, cube->position.y - halfEdge, cube->position.z - halfEdge },
(Vector3){ cube->position.x + halfEdge, cube->position.y + halfEdge, cube->position.z + halfEdge }
};
(Vector3){cube->position.x - halfEdge, cube->position.y - halfEdge, cube->position.z - halfEdge},
(Vector3){cube->position.x + halfEdge, cube->position.y + halfEdge, cube->position.z + halfEdge}};
Vector2 screenCenter = { (float)GetScreenWidth()/2.0f, (float)GetScreenHeight()/2.0f};
Vector2 screenCenter = {(float)GetScreenWidth() / 2.0f, (float)GetScreenHeight() / 2.0f};
Ray lookRay = GetScreenToWorldRay(screenCenter, player->camera);
RayCollision collision = GetRayCollisionBox(lookRay, cubeBox);
+6 -4
View File
@@ -1,9 +1,9 @@
#ifndef PLAYER_H
#define PLAYER_H
#include <stdbool.h>
#include "raylib.h"
#include "cube.h"
#include "raylib.h"
#include <stdbool.h>
#define GRAVITY 32.0f
#define MAX_SPEED 20.0f
@@ -17,14 +17,16 @@
#define CROUCH_HEIGHT 0.4f
#define STAND_HEIGHT 1.0f
typedef struct {
typedef struct
{
Vector3 position;
Vector3 velocity;
Vector3 dir;
bool grounded;
} Body;
typedef struct {
typedef struct
{
Body body;
Camera camera;
Cube *grab;