diff options
author | nathansmithsmith <thenathansmithsmith@gmail.com> | 2023-07-07 00:57:19 -0600 |
---|---|---|
committer | nathansmithsmith <thenathansmithsmith@gmail.com> | 2023-07-07 00:57:19 -0600 |
commit | 028cf5d33d99274deea9567159a4eb07c13ef85c (patch) | |
tree | b2d9f0ae8fb640fdbe1a41114c7c8314f9223103 /src | |
parent | 416a5cbab21c480ae9e85b07fd9424452cbcb611 (diff) |
This fucker is flying
Diffstat (limited to 'src')
-rw-r--r-- | src/entities/antifaShip.c | 27 | ||||
-rw-r--r-- | src/entities/antifaShip.h | 12 | ||||
-rw-r--r-- | src/entity.c | 87 | ||||
-rw-r--r-- | src/entity.h | 70 | ||||
-rw-r--r-- | src/game.c | 29 | ||||
-rw-r--r-- | src/game.h | 15 | ||||
-rw-r--r-- | src/gameCommon.h | 29 | ||||
-rw-r--r-- | src/gameScreen.c | 17 | ||||
-rw-r--r-- | src/gameScreen.h | 12 | ||||
-rw-r--r-- | src/mainMenu.c | 14 | ||||
-rw-r--r-- | src/mainMenu.h | 11 | ||||
-rw-r--r-- | src/playerCamera.c | 34 | ||||
-rw-r--r-- | src/playerCamera.h | 11 | ||||
-rw-r--r-- | src/world.c | 1 | ||||
-rw-r--r-- | src/world.h | 7 |
15 files changed, 375 insertions, 1 deletions
diff --git a/src/entities/antifaShip.c b/src/entities/antifaShip.c new file mode 100644 index 0000000..2552d24 --- /dev/null +++ b/src/entities/antifaShip.c @@ -0,0 +1,27 @@ +#include "antifaShip.h" +#include <raylib.h> + +void initAntifaShip(Entity * entity) { + entity->model = LoadModel("/home/nathan/Documents/KillaFacsista/assets/antifaShip.obj"); +} + +void closeAntifaShip(Entity * entity) { + UnloadModel(entity->model); +} + +void updateAntifaShip(Game * game, Entity * entity, EntityId id) { +} + +void drawAntifaShip(Game * game, Entity * entity, EntityId id) { + entityDraw(entity); + + Vector3 stick = (Vector3){ + GetGamepadAxisMovement(0, 1), + -GetGamepadAxisMovement(0, 0), + GetGamepadAxisMovement(0, 2) + }; + + stick = Vector3Scale(stick, 0.5); + + entityJoystickControl(entity, stick, fabs(GetGamepadAxisMovement(0, 3) * 50.0 + 5.0)); +} diff --git a/src/entities/antifaShip.h b/src/entities/antifaShip.h new file mode 100644 index 0000000..15e9009 --- /dev/null +++ b/src/entities/antifaShip.h @@ -0,0 +1,12 @@ +#include "gameCommon.h" +#include "entity.h" + +#ifndef ANTIFA_SHIP_H +#define ANTIFA_SHIP_H + +void initAntifaShip(Entity * entity); +void closeAntifaShip(Entity * entity); +void updateAntifaShip(Game * game, Entity * entity, EntityId id); +void drawAntifaShip(Game * game, Entity * entity, EntityId id); + +#endif diff --git a/src/entity.c b/src/entity.c new file mode 100644 index 0000000..0b6be9d --- /dev/null +++ b/src/entity.c @@ -0,0 +1,87 @@ +#include "entity.h" +#include "entities/antifaShip.h" + +// This fucker is used for creating entities. +const EntityTypeInfo entityTypeInfo[ENTITY_TYPE_COUNT] = { + (EntityTypeInfo){initAntifaShip, closeAntifaShip, updateAntifaShip, drawAntifaShip} +}; + +Entity createEntity(EntityType type) { + EntityTypeInfo info = entityTypeInfo[type]; + + // Set defaults. + Entity entity = (Entity){ + .type = type, + .position = Vector3Zero(), + .angularVelocity = 0, + .rotationAxis = Vector3Zero(), + .velocity = Vector3Zero(), + .rotation = QuaternionIdentity(), + .updateCb = info.updateCb, + .drawCb = info.drawCb, + .data = NULL + }; + + // Init. + info.initCb(&entity); + + return entity; +} + +void closeEntity(Entity * entity) { + entityTypeInfo[entity->type].closeCb(entity); +} + +// Basic wireframe drawing. +void entityDraw(Entity * entity) { + entity->model.transform = QuaternionToMatrix(entity->rotation); + + DrawModelWires( + entity->model, + entity->position, + 1, + GREEN + ); +} + +void entityUpdatePosition(Entity * entity) { + float t = GetFrameTime(); + + Vector3 velocity = (Vector3){ + entity->velocity.x * t, + entity->velocity.y * t, + entity->velocity.z * t + }; + + entity->position = Vector3Add(entity->position, velocity); +} + +void entityUpdateRotation(Entity * entity) { + float t = GetFrameTime(); + + Quaternion angularRotation = QuaternionFromAxisAngle( + entity->rotationAxis, + entity->angularVelocity * t + ); + + entity->rotation = QuaternionMultiply(entity->rotation, angularRotation); +} + +void entityJoystickControl(Entity * entity, Vector3 stick, float speed) { + // Set angular velocity. + Vector3 angularVelocity = Vector3Scale(stick, PI); + entity->angularVelocity = Vector3Length(angularVelocity); + entity->rotationAxis = stick; + entityUpdateRotation(entity); + + // Set position. + Matrix m = QuaternionToMatrix(QuaternionInvert(entity->rotation)); + + entity->velocity = (Vector3){ + m.m2 * speed, + m.m6 * speed, + m.m10 * speed, + }; + + entityUpdatePosition(entity); +} diff --git a/src/entity.h b/src/entity.h new file mode 100644 index 0000000..0f9ec3d --- /dev/null +++ b/src/entity.h @@ -0,0 +1,70 @@ +#include "gameCommon.h" + +#ifndef ENTITY_H +#define ENTITY_H + +enum { + ENTITY_NONE = -1, + ENTITY_ANTIFA, + ENTITY_SOLDATO, + ENTITY_CAPORALE, + ENTITY_SERGENTE, + ENTITY_MARESCIALLO, + ENTITY_GENERALE, + ENTITY_MUSSOLINI +}; + +#define ENTITY_TYPE_COUNT 7 + +typedef int8_t EntityType; +typedef int16_t EntityId; // Id in world. + +// Callbacks. +typedef void (*EntityUpdateCb)(Game * game, Entity * entity, EntityId id); +typedef void (*EntityDrawCb)(Game * game, Entity * entity, EntityId id); + +// This fucker is a entity. +typedef struct Entity { + EntityType type; + Model model; + + Vector3 position; + Vector3 velocity; + + float angularVelocity; + Vector3 rotationAxis; + + Quaternion rotation; + + EntityUpdateCb updateCb; + EntityDrawCb drawCb; + + // Used for whatever. + void * data; +} Entity; + +typedef void (*EntityInitCb)(Entity * entity); +typedef void (*EntityCloseCb)(Entity * entity); + +// Info for each entity type. +typedef struct EntityTypeInfo { + EntityInitCb initCb; + EntityCloseCb closeCb; + EntityUpdateCb updateCb; + EntityDrawCb drawCb; +} EntityTypeInfo; + +const extern EntityTypeInfo entityTypeInfo[ENTITY_TYPE_COUNT]; + +// Do I need a fucking comment? +Entity createEntity(EntityType type); +void closeEntity(Entity * entity); + +// Helper functions for updating and drawing. +void entityDraw(Entity * entity); +void entityUpdatePosition(Entity * entity); +void entityUpdateRotation(Entity * entity); + +void entityJoystickControl(Entity * entity, Vector3 stick, float speed); + +#endif @@ -1,15 +1,44 @@ #include "game.h" void initGame(Game * game) { + // Window. InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Killa Facsista"); + + // Screen id. + game->screenId = SCREEN_GAME; + + // Camera. + initPlayerCamera(&game->playerCamera); + + // Ship test. + game->ship = createEntity(ENTITY_ANTIFA); + + SetTargetFPS(60); + //DisableCursor(); } void closeGame(Game * game) { + // Close ship test. + closeEntity(&game->ship); + CloseWindow(); } void updateGame(Game * game) { BeginDrawing(); + switch (game->screenId) { + case SCREEN_MAIN_MENU: + updateMainMenu(game); + break; + case SCREEN_GAME: + updateGameScreen(game); + break; + default: + break; + } + + DrawFPS(5, 5); + EndDrawing(); } @@ -1,9 +1,24 @@ #include "gameCommon.h" +#include "mainMenu.h" +#include "gameScreen.h" +#include "playerCamera.h" +#include "entity.h" #ifndef GAME_H #define GAME_H +typedef enum ScreenId { + SCREEN_MAIN_MENU, + SCREEN_GAME +} ScreenId; + typedef struct Game { + ScreenId screenId; + MainMenu mainMenu; + GameScreen gameScreen; + Camera3D playerCamera; + + Entity ship; } Game; void initGame(Game * game); diff --git a/src/gameCommon.h b/src/gameCommon.h index b53b955..0d18d32 100644 --- a/src/gameCommon.h +++ b/src/gameCommon.h @@ -9,12 +9,39 @@ #include <raylib.h> #include <raymath.h> -#include "raylib.h" +#include <rlgl.h> +#include "raygui.h" #ifndef GAME_COMMON_H #define GAME_COMMON_H +// Types be like. +typedef struct Game Game; +typedef struct Entity Entity; + #define WINDOW_WIDTH 960 #define WINDOW_HEIGHT 540 +// 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) + +// Memory management. +#define KF_MALLOC(size) malloc(size) +#define KF_CALLOC(nmemb, size) calloc(nmemb, size) +#define KF_REALLOC(ptr, size) realloc(ptr, size) +#define KF_REALLOCARRAY(ptr, nmemb, size) reallocarray(ptr, nmemb, size) +#define KF_FREE(ptr) free(ptr) + +#define ALLOCATION_ERROR TraceLog(LOG_ERROR, "Allocation error in %s:%d", __FILE__, __LINE__) + +// Errors. +typedef enum KfError { + KFERROR = -1, + KFSUCCESS = 0 +} KfError; + #endif diff --git a/src/gameScreen.c b/src/gameScreen.c new file mode 100644 index 0000000..c62584a --- /dev/null +++ b/src/gameScreen.c @@ -0,0 +1,17 @@ +#include "gameScreen.h" +#include "game.h" + +void updateGameScreen(Game * game) { + ClearBackground(BLACK); + + // Camera. + updatePlayerCamera(&game->playerCamera, game); + + BeginMode3D(game->playerCamera); + + DrawGrid(100, 5.0); + + game->ship.drawCb(game, &game->ship, 0); + + EndMode3D(); +} diff --git a/src/gameScreen.h b/src/gameScreen.h new file mode 100644 index 0000000..8c3de89 --- /dev/null +++ b/src/gameScreen.h @@ -0,0 +1,12 @@ +#include "gameCommon.h" + +#ifndef GAME_SCREEN_H +#define GAME_SCREEN_H + +// Gui stuff and shit. +typedef struct GameScreen { +} GameScreen; + +void updateGameScreen(Game * game); + +#endif diff --git a/src/mainMenu.c b/src/mainMenu.c new file mode 100644 index 0000000..10f293a --- /dev/null +++ b/src/mainMenu.c @@ -0,0 +1,14 @@ +#include "mainMenu.h" +#include "game.h" + +void updateMainMenu(Game * game) { + ClearBackground(RAYWHITE); + + bool start = GuiButton( + (Rectangle){10, 10, 100, 50}, + "Start" + ); + + if (start) + game->screenId = SCREEN_GAME; +} diff --git a/src/mainMenu.h b/src/mainMenu.h new file mode 100644 index 0000000..7dd5036 --- /dev/null +++ b/src/mainMenu.h @@ -0,0 +1,11 @@ +#include "gameCommon.h" + +#ifndef MAIN_MENU_H +#define MAIN_MENU_H + +typedef struct MainMenu { +} MainMenu; + +void updateMainMenu(Game * game); + +#endif diff --git a/src/playerCamera.c b/src/playerCamera.c new file mode 100644 index 0000000..4b9530f --- /dev/null +++ b/src/playerCamera.c @@ -0,0 +1,34 @@ +#include "playerCamera.h" +#include "game.h" + +void initPlayerCamera(Camera3D * camera) { + *camera = (Camera3D){ + //.position = (Vector3){15.0, 15.0, 15.0}, + .position = (Vector3){0.0, 15.0, -15.0}, + .target = (Vector3){0.0, 0.0, 0.0}, + .up = (Vector3){0.0, 1.0, 0.0}, + .fovy = 45.0, + .projection = CAMERA_PERSPECTIVE + }; +} + +void updatePlayerCamera(Camera3D * camera, Game * game) { + //UpdateCamera(camera, CAMERA_FIRST_PERSON); + camera->target = game->ship.position; + + Matrix m = QuaternionToMatrix(QuaternionInvert(game->ship.rotation)); + Vector3 pos = (Vector3){0.0, CAMERA_DIS/2, -CAMERA_DIS}; + + camera->position = (Vector3){ + m.m0 * pos.x + m.m1 * pos.y + m.m2 * pos.z, + m.m4 * pos.x + m.m5 * pos.y + m.m6 * pos.z, + m.m8 * pos.x + m.m9 * pos.y + m.m10 * pos.z + }; + + camera->position = Vector3Add(camera->position, game->ship.position); + camera->up = (Vector3){ + m.m1 + m.m2, + m.m5 + m.m6, + m.m9 + m.m10 + }; +} diff --git a/src/playerCamera.h b/src/playerCamera.h new file mode 100644 index 0000000..960c30d --- /dev/null +++ b/src/playerCamera.h @@ -0,0 +1,11 @@ +#include "gameCommon.h" + +#ifndef PLAYER_CAMERA_H +#define PLAYER_CAMERA_H + +#define CAMERA_DIS 10.0 + +void initPlayerCamera(Camera3D * camera); +void updatePlayerCamera(Camera3D * camera, Game * game); + +#endif diff --git a/src/world.c b/src/world.c new file mode 100644 index 0000000..ea50eca --- /dev/null +++ b/src/world.c @@ -0,0 +1 @@ +#include "world.h" diff --git a/src/world.h b/src/world.h new file mode 100644 index 0000000..379b920 --- /dev/null +++ b/src/world.h @@ -0,0 +1,7 @@ +#include "gameCommon.h" +#include "entity.h" + +#ifndef WORLD_H +#define WORLD_H + +#endif |