diff options
author | nathan <nathansmith@disroot.org> | 2025-07-04 13:00:08 +0000 |
---|---|---|
committer | nathan <nathansmith@disroot.org> | 2025-07-04 13:00:08 +0000 |
commit | 2b2b69ee31f00aa46ab6baa967e12437ce7334d1 (patch) | |
tree | 1ba7fd19c30bb33ef20f55afc84ccfff30460969 | |
parent | de3a92fb2bde8cbec688f788c53f7ba52d6a723a (diff) | |
download | FindThings-2b2b69ee31f00aa46ab6baa967e12437ce7334d1.tar.gz FindThings-2b2b69ee31f00aa46ab6baa967e12437ce7334d1.tar.bz2 FindThings-2b2b69ee31f00aa46ab6baa967e12437ce7334d1.zip |
Heightmap, player, and more concepts
-rw-r--r-- | assets/heightmap.png | bin | 0 -> 179712 bytes | |||
-rw-r--r-- | concepts.org | 10 | ||||
-rw-r--r-- | src/Makefile | 2 | ||||
-rw-r--r-- | src/assets.c | 41 | ||||
-rw-r--r-- | src/assets.h | 32 | ||||
-rw-r--r-- | src/game.c | 54 | ||||
-rw-r--r-- | src/game.h | 11 | ||||
-rw-r--r-- | src/player.c | 75 | ||||
-rw-r--r-- | src/player.h | 18 | ||||
-rw-r--r-- | src/settings.c | 7 | ||||
-rw-r--r-- | src/settings.h | 7 | ||||
-rw-r--r-- | src/utils.h | 15 |
12 files changed, 265 insertions, 7 deletions
diff --git a/assets/heightmap.png b/assets/heightmap.png Binary files differnew file mode 100644 index 0000000..b3aa460 --- /dev/null +++ b/assets/heightmap.png diff --git a/concepts.org b/concepts.org index 0d7cc94..7a3d0aa 100644 --- a/concepts.org +++ b/concepts.org @@ -77,7 +77,11 @@ said you will have your neck snapped no matter what you do. + It will have a open world making heavy use of height maps. + No real physics, just dummy physics. + Nearly everything will be a intractable object. -+ Very lite and careful use of memory allocation. ++ Very lite and careful use of memory allocation. Use the stack a lot more. + The world data will be stored in mostly arrays in a header file. -+ BVH like structure for collision. - + +** Intractable objects +All objects will have a simple object id. When you interact with it the +procedure for that object will be called. The ids can also be used for mesh +instancing. + diff --git a/src/Makefile b/src/Makefile index afa926d..711cb4a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -9,9 +9,11 @@ OBJECTS = $(SOURCES:.c=.o) $(CC) -c $(CFLAGS) -o $@ $< $(TARGET): $(OBJECTS) $(CC) $(CFLAGS) -o $(TARGET) $(OBJECTS) $(LDFLAGS) + cp -r ../assets ./ .PHONY: clean clean: rm *.o rm $(TARGET) + rm -rf assets diff --git a/src/assets.c b/src/assets.c new file mode 100644 index 0000000..79c7ef9 --- /dev/null +++ b/src/assets.c @@ -0,0 +1,41 @@ +#include "assets.h" + +const char imageAssetPaths[IMAGE_ASSET_COUNT][FT_NAMEMAX] = { + "heightmap.png" +}; + +const char textureAssetPaths[TEXTURE_ASSET_COUNT][FT_NAMEMAX] = { + "heightmap.png" +}; + +void initAssets(Assets* assets) +{ + // Images. + for (int index = 0; index < IMAGE_ASSET_COUNT; ++index) + { + assets->images[index] = LoadImage( + TextFormat("assets/%s", imageAssetPaths[index])); + } + + // Textures. + for (int index = 0; index < TEXTURE_ASSET_COUNT; ++index) + { + assets->textures[index] = LoadTexture( + TextFormat("assets/%s", textureAssetPaths[index])); + } +} + +void closeAssets(Assets* assets) +{ + // Images. + for (int index = 0; index < IMAGE_ASSET_COUNT; ++index) + { + UnloadImage(assets->images[index]); + } + + // Textures. + for (int index = 0; index < TEXTURE_ASSET_COUNT; ++index) + { + UnloadTexture(assets->textures[index]); + } +} diff --git a/src/assets.h b/src/assets.h new file mode 100644 index 0000000..c5fb7ac --- /dev/null +++ b/src/assets.h @@ -0,0 +1,32 @@ +#include "utils.h" + +#ifndef ASSETS_H +#define ASSETS_H + +#define IMAGE_ASSET_COUNT 1 +#define TEXTURE_ASSET_COUNT 1 + +extern const char imageAssetPaths[IMAGE_ASSET_COUNT][FT_NAMEMAX]; +extern const char textureAssetPaths[TEXTURE_ASSET_COUNT][FT_NAMEMAX]; + +typedef int8_t Assetid; + +// Image asset ids. +enum { + HEIGHT_MAP_IMAGE +}; + +// Texture asset ids. +enum { + HEIGHT_MAP_TEXTURE +}; + +typedef struct Assets { + Image images[IMAGE_ASSET_COUNT]; + Texture textures[TEXTURE_ASSET_COUNT]; +} Assets; + +void initAssets(Assets* assets); +void closeAssets(Assets* assets); + +#endif @@ -3,23 +3,75 @@ void initGame(Game* game) { + game->sceneId = GAME_SCENE; + // Settings. game->settings = defaultSettings(); // Window. InitWindow(game->settings.windowWidth, game->settings.windowHeight, "Find Things"); + SetWindowState(FLAG_WINDOW_RESIZABLE); + + // Assets. + initAssets(&game->assets); + + // Player. + game->player = createPlayer(); + game->player.position = (Vector3){50.0, 30.0, 50.0}; + + // Heightmap. + Mesh heightmapMesh = GenMeshHeightmap(game->assets.images[HEIGHT_MAP_IMAGE], + (Vector3){100.0, 30.0, 100.0}); + game->heightmap = LoadModelFromMesh(heightmapMesh); + game->heightmap.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = + game->assets.textures[HEIGHT_MAP_TEXTURE]; + + DisableCursor(); +} + +void updateMainMenuScene(Game* game) +{ + ClearBackground(BLACK); +} + +void updateGameScene(Game* game) +{ + ClearBackground(BLACK); + + BeginMode3D(game->player.camera); + + updatePlayer(&game->player, game); + + DrawModel(game->heightmap, Vector3Zero(), 1.0, WHITE); + + EndMode3D(); } void updateGame(Game* game) { BeginDrawing(); - ClearBackground(RAYWHITE); + + switch (game->sceneId) + { + case MAIN_MENU_SCENE: + updateMainMenuScene(game); + break; + case GAME_SCENE: + updateGameScene(game); + break; + default: + break; + } + + DrawFPS(0, 0); EndDrawing(); } void closeGame(Game* game) { + closeAssets(&game->assets); + UnloadModel(game->heightmap); CloseWindow(); } @@ -1,11 +1,22 @@ #include "utils.h" #include "settings.h" +#include "assets.h" +#include "player.h" #ifndef GAME_H #define GAME_H +typedef enum SceneID { + MAIN_MENU_SCENE, + GAME_SCENE +} SceneID; + typedef struct Game { + SceneID sceneId; Settings settings; + Assets assets; + Player player; + Model heightmap; } Game; void initGame(Game* game); diff --git a/src/player.c b/src/player.c new file mode 100644 index 0000000..ec6eaf2 --- /dev/null +++ b/src/player.c @@ -0,0 +1,75 @@ +#include "player.h" +#include "game.h" + +Player createPlayer() +{ + return (Player){ + .position = Vector3Zero(), + .direction = (Vector3){0.0, 0.0, 0.0}, + .velocity = Vector3Zero(), + .camera = (Camera){ + .position = (Vector3){0.0, 0.0, 0.0}, + .target = Vector3Zero(), + .up = (Vector3){0.0, 1.0, 0.0}, + .fovy = 100, + .projection = CAMERA_PERSPECTIVE + }, + .cameraAngle = Vector2Zero() + }; +} + +// TODO: move magic numbers to settings +void updatePlayer(Player* player, Game* game) +{ + Camera* camera = &player->camera; + Vector2* cameraAngle = &player->cameraAngle; + Settings* settings = &game->settings; + + // Get mouse speed. + Vector2 mouseSpeed = Vector2Scale(GetMouseDelta(), settings->mouseSpeed); + mouseSpeed = Vector2Scale(mouseSpeed, 1.0 / (PI * 2.0)); + + // Update camera angle. + *cameraAngle = Vector2Add(*cameraAngle, mouseSpeed); + cameraAngle->x = Wrap(cameraAngle->x, -PI, PI); + cameraAngle->y = Clamp(cameraAngle->y, -PI / 2.5, PI / 2.5); + + // Set player direction + Matrix matrix = MatrixRotateXYZ( + (Vector3){-cameraAngle->y, cameraAngle->x, 0.0}); + player->direction = (Vector3){matrix.m2, matrix.m6, matrix.m10}; + + // Player movement. + player->velocity = Vector3Zero(); + + if (IsKeyDown(settings->forwardKey)) + { + player->velocity.z += cosf(cameraAngle->x); + player->velocity.x += -sinf(cameraAngle->x); + } + if (IsKeyDown(settings->leftKey)) + { + player->velocity.z += cosf(cameraAngle->x - (PI / 2.0)); + player->velocity.x += -sinf(cameraAngle->x - (PI / 2.0)); + } + if (IsKeyDown(settings->backwardKey)) + { + player->velocity.z += -cosf(cameraAngle->x); + player->velocity.x += sinf(cameraAngle->x); + } + if (IsKeyDown(settings->rightKey)) + { + player->velocity.z += cosf(cameraAngle->x + (PI / 2.0)); + player->velocity.x += -sinf(cameraAngle->x + (PI / 2.0)); + } + + player->velocity = Vector3Scale(player->velocity, 5.0); + + // Apply velocity + player->position = Vector3Add( + player->position, Vector3Scale(player->velocity, GetFrameTime())); + + // Apply camera. + camera->position = player->position; + camera->target = Vector3Add(player->position, player->direction); +} diff --git a/src/player.h b/src/player.h new file mode 100644 index 0000000..4983752 --- /dev/null +++ b/src/player.h @@ -0,0 +1,18 @@ +#include "utils.h" + +#ifndef PLAYER_H +#define PLAYER_H + +typedef struct Player { + Vector3 position; + Vector3 direction; + Vector3 velocity; + + Camera camera; + Vector2 cameraAngle; +} Player; + +Player createPlayer(); +void updatePlayer(Player* player, Game* game); + +#endif diff --git a/src/settings.c b/src/settings.c index 26aad2f..ea54fbe 100644 --- a/src/settings.c +++ b/src/settings.c @@ -4,6 +4,11 @@ Settings defaultSettings() { return (Settings){ .windowWidth = 960, - .windowHeight = 720 + .windowHeight = 720, + .mouseSpeed = 0.1, + .forwardKey = KEY_W, + .backwardKey = KEY_S, + .rightKey = KEY_D, + .leftKey = KEY_A }; } diff --git a/src/settings.h b/src/settings.h index 2d2c4f4..3576955 100644 --- a/src/settings.h +++ b/src/settings.h @@ -7,6 +7,13 @@ typedef struct Settings { // Window. int windowWidth; int windowHeight; + + // Controls. + float mouseSpeed; + KeyboardKey forwardKey; + KeyboardKey backwardKey; + KeyboardKey rightKey; + KeyboardKey leftKey; } Settings; Settings defaultSettings(); diff --git a/src/utils.h b/src/utils.h index e3b52ed..e289189 100644 --- a/src/utils.h +++ b/src/utils.h @@ -1,13 +1,17 @@ #include <stdio.h> #include <stdlib.h> -#include <stdlib.h> +#include <stdint.h> +#include <math.h> #include <raylib.h> -#include "raylib.h" +#include <raymath.h> +#include "raygui.h" #ifndef UTIL_H #define UTIL_H +typedef struct Game Game; + #define FT_NAMEMAX 256 // Memory management. @@ -27,4 +31,11 @@ typedef enum FTError { FTSUCCESS = 0 } FTError; +// Bit shit. +#define SET_BIT(b, n) (b | (0x1 << n)) +#define CLEAR_BIT(b, n) (b & ~(0x1 << n)) +#define IS_BIT_SET(b, n) ((b >> n) & 0x1) +#define TOGGLE_BIT(b, n) (b ^ (0x1 << n)) +#define HAS_FLAG(v, f) ((v & f) == f) + #endif |