diff options
-rw-r--r-- | CMakeLists.txt | 3 | ||||
-rw-r--r-- | assets/antifaShip.mtl | 12 | ||||
-rw-r--r-- | assets/antifaShip.obj | 101 | ||||
-rw-r--r-- | assets/antifaShip.ply | bin | 0 -> 1315 bytes | |||
-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 |
19 files changed, 490 insertions, 2 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index d0f185b..6f9e256 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,10 +4,11 @@ cmake_minimum_required(VERSION 3.11) project(KillaFacsista VERSION 1.0) set(C_STANDARD 99) +set(C_STANDARD_REQUIRED ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Add source files. -file(GLOB SRC_FILES src/*.c) +file(GLOB SRC_FILES src/*.c src/entities/*.c) add_executable(${PROJECT_NAME} ${SRC_FILES}) diff --git a/assets/antifaShip.mtl b/assets/antifaShip.mtl new file mode 100644 index 0000000..dc779b0 --- /dev/null +++ b/assets/antifaShip.mtl @@ -0,0 +1,12 @@ +# Blender 3.5.1 MTL File: 'antifaShip2.blend' +# www.blender.org + +newmtl Material +Ns 250.000000 +Ka 1.000000 1.000000 1.000000 +Kd 0.800000 0.800000 0.800000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 diff --git a/assets/antifaShip.obj b/assets/antifaShip.obj new file mode 100644 index 0000000..27320a0 --- /dev/null +++ b/assets/antifaShip.obj @@ -0,0 +1,101 @@ +# Blender 3.5.1 +# www.blender.org +mtllib antifaShip.mtl +o Cube +v -1.000000 0.482858 1.000000 +v -3.000000 0.482858 -0.999999 +v -3.000000 -1.000000 -0.999999 +v 1.000000 0.482858 1.000000 +v 3.000000 0.482858 -1.000001 +v 3.000000 -1.000000 -1.000001 +v 2.321289 -0.773763 -1.300001 +v 2.321289 0.256621 -1.300001 +v -2.321290 0.256621 -1.299999 +v -2.321290 -0.773763 -1.299999 +v 2.000001 -1.000000 3.499999 +v -1.999999 -1.000000 3.500001 +v 0.000003 -1.000000 8.600000 +v 0.000000 -1.000000 1.000000 +vn -0.0000 1.0000 -0.0000 +vn -0.4043 -0.0000 -0.9146 +vn -0.0000 -0.0000 -1.0000 +vn -0.0000 -0.7984 -0.6021 +vn -0.0000 0.7984 -0.6021 +vn 0.4043 -0.0000 -0.9146 +vn -0.6783 0.6742 0.2923 +vn -0.6782 0.6742 0.2924 +vn -0.0000 0.9815 0.1915 +vn 0.1420 -0.9880 0.0610 +vn -0.0330 -0.9880 -0.1510 +vn -0.0000 -1.0000 -0.0000 +vn -0.6782 0.6742 0.2923 +vn 0.6782 0.6742 0.2923 +vn 0.6782 0.6742 0.2924 +vn 0.6783 0.6742 0.2923 +vn -0.4042 -0.0000 0.9147 +vn -0.4043 -0.0000 0.9146 +vn 0.1400 -0.9892 -0.0428 +vn -0.0396 -0.9892 0.1409 +vn 0.6820 0.7155 0.1516 +vn 0.0265 -0.9892 -0.1439 +vn 0.0166 -0.9892 -0.1454 +vt 0.000000 0.000000 +vt 0.625000 0.500000 +vt 0.000000 0.000000 +vt 0.625000 0.750000 +vt 0.375000 0.750000 +vt 0.000000 0.000000 +vt 0.875000 0.500000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.875000 0.750000 +vt 0.125000 0.750000 +vt 0.000000 0.000000 +vt 0.125000 0.750000 +vt 0.375000 1.000000 +vt 0.875000 0.750000 +vt 0.625000 1.000000 +vt 0.625000 0.750000 +vt 0.375000 0.750000 +vt 0.125000 0.625000 +vt 0.000000 0.000000 +vt 0.375000 0.625000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.062500 0.375000 +vt 0.250000 0.687500 +vt 0.000000 0.000000 +s 0 +usemtl Material +f 2/4/1 1/2/1 4/7/1 +f 2/4/2 9/17/2 10/18/2 +f 7/14/3 10/18/3 9/17/3 +f 7/13/4 6/11/4 3/5/4 +f 9/17/5 2/4/5 5/10/5 +f 7/13/6 8/15/6 5/10/6 +f 3/6/7 1/1/8 2/3/8 +f 1/2/9 13/23/9 4/7/9 +f 2/4/1 4/7/1 5/10/1 +f 2/4/2 10/18/2 3/5/2 +f 7/14/3 9/17/3 8/16/3 +f 7/13/4 3/5/4 10/18/4 +f 9/17/5 5/10/5 8/15/5 +f 13/23/10 12/22/11 11/19/12 +f 11/19/12 12/21/12 14/25/12 6/11/12 +f 7/13/6 5/10/6 6/11/6 +f 1/1/13 12/22/13 13/23/13 +f 12/22/13 1/1/13 3/6/13 +f 11/20/14 4/8/14 13/23/14 +f 4/8/15 6/12/16 5/9/15 +f 1/2/1 4/7/1 5/10/1 +f 10/18/3 8/16/3 7/14/3 +f 7/14/17 8/16/18 6/11/18 +f 9/17/2 10/18/2 3/5/2 +f 8/16/5 9/17/5 2/4/5 +f 6/11/4 10/18/4 7/14/4 +f 3/6/19 6/11/12 14/24/12 12/22/20 +f 4/8/21 11/20/21 6/12/21 +f 14/25/12 6/11/12 11/19/12 +f 14/25/12 12/22/22 3/6/23 +f 14/26/12 11/20/12 12/22/12 +f 6/12/12 14/26/12 3/6/12 diff --git a/assets/antifaShip.ply b/assets/antifaShip.ply Binary files differnew file mode 100644 index 0000000..c15ec88 --- /dev/null +++ b/assets/antifaShip.ply 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 |