diff options
author | talha <sarcxd@gmail.com> | 2025-01-15 14:26:29 +0500 |
---|---|---|
committer | talha <sarcxd@gmail.com> | 2025-01-15 14:26:29 +0500 |
commit | d718dc1cfc04f1eabce48159f258a7f2dc281fc3 (patch) | |
tree | bd611715982f37bc22811cfde4ad508d69dbec6f | |
parent | 7d26a3cc6cfd9c207693ef977b0f0922d52e1d5c (diff) |
Added IVec, setting up level editing
-rw-r--r-- | source/array/array.cpp | 50 | ||||
-rw-r--r-- | source/array/array.h | 23 | ||||
-rwxr-xr-x | source/main.cpp | 101 | ||||
-rwxr-xr-x | source/math.h | 65 | ||||
-rw-r--r-- | source/todo.txt | 32 |
5 files changed, 225 insertions, 46 deletions
diff --git a/source/array/array.cpp b/source/array/array.cpp new file mode 100644 index 0000000..ac7fd9a --- /dev/null +++ b/source/array/array.cpp @@ -0,0 +1,50 @@ +#include "array.h" + +// :r32_array_functions +void array_init(Arena* a, r32_array* arr, size_t capacity) { + arr->buffer = (r32*)arena_alloc(a, capacity*sizeof(r32)); + + assert(arr->buffer != NULL); + + arr->size = 0; + arr->capacity = capacity; +} + +void array_insert(r32_array* arr, r32* ele, size_t ele_size) { + b8 assert_cond = arr->size + ele_size <= arr->capacity; + assert(assert_cond); + + void* ptr = &arr->buffer[arr->size]; + memcpy(ptr, ele, sizeof(r32)*ele_size); + arr->size += ele_size; +} + +void array_clear(r32_array* arr) { + memset(arr->buffer, 0, sizeof(r32)*arr->capacity); + arr->size = 0; +} + +// :u32_array_functions +void array_init(Arena* a, u32_array* arr, size_t capacity) { + arr->buffer = (u32*)arena_alloc(a, capacity*sizeof(r32)); + + assert(arr->buffer != NULL); + + arr->size = 0; + arr->capacity = capacity; +} + +void array_insert(u32_array* arr, u32* ele, size_t ele_size) { + b8 assert_cond = arr->size + ele_size <= arr->capacity; + assert(assert_cond); + + void* ptr = &arr->buffer[arr->size]; + memcpy(ptr, ele, sizeof(u32)*ele_size); + arr->size += ele_size; +} + +void array_clear(u32_array* arr) { + memset(arr->buffer, 0, sizeof(u32)*arr->capacity); + arr->size = 0; +} + diff --git a/source/array/array.h b/source/array/array.h new file mode 100644 index 0000000..cbe710a --- /dev/null +++ b/source/array/array.h @@ -0,0 +1,23 @@ +#pragma once + +struct r32_array { + r32 *buffer; + size_t size; + size_t capacity; +}; + +struct u32_array { + u32 *buffer; + size_t size; + size_t capacity; +}; + +// @r32_array +void array_init(Arena* a, r32_array* arr, size_t capacity); +void array_insert(r32_array* arr, r32* ele, size_t ele_size); +void array_clear(r32_array* arr); + +// @u32_array +void array_init(Arena* a, u32_array* arr, size_t capacity); +void array_insert(u32_array* arr, u32* ele, size_t ele_size); +void array_clear(u32_array* arr); diff --git a/source/main.cpp b/source/main.cpp index f86f404..f84b158 100755 --- a/source/main.cpp +++ b/source/main.cpp @@ -38,6 +38,8 @@ * - Audio */ +#include <stdint.h> + typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; @@ -90,37 +92,31 @@ struct TextState { #define BATCH_SIZE 2000 -struct r32_array { - r32* buffer; - u32 size; - u32 capacity; -}; - -void array_init(Arena* a, r32_array* arr, u32 capacity) { - arr->buffer = (r32*)arena_alloc(a, capacity*sizeof(r32)); - - assert(arr->buffer != NULL); +#define KB(x) (1024 * (x)) +#define MB(x) (1024 * KB((x))) +#define GB(x) (1024 * MB((x))) - arr->size = 0; - arr->capacity = capacity; -} - -void array_insert(r32_array* arr, r32* ele, u32 ele_size) { - b8 assert_cond = arr->size + ele_size <= arr->capacity; - if (!assert_cond) { - SDL_Log("arr->size: %d, arr->capacity: %d", arr->size, arr->capacity); - } - assert(assert_cond); +#include "array/array.cpp" - void* ptr = &arr->buffer[arr->size]; - memcpy(ptr, ele, sizeof(r32)*ele_size); - arr->size += ele_size; -} +enum ENTITY_TYPE { + PLAYER = 0, + OBSTACLE = 1, + GOAL = 2 +}; -void array_clear(r32_array* arr) { - memset(arr->buffer, 0, sizeof(r32)*arr->capacity); - arr->size = 0; -} +struct Level { + /* + * Level will be a 2d array with positions defining the elements + * the dimensions of the array are: size.width * size.height + * + * here size is an IVec2 and it's parameters are multiples of atom_size + * atom_size is a parameter the game defines as the smallest chunk of pixels + * the game will consider + */ + const char *version = "0.0.1"; + u32_array map; + IVec2 size; +}; struct GLRenderer { // colored quad @@ -734,8 +730,8 @@ Vec3 get_screen_position_from_percent(GameState state, Vec3 v) { int main(int argc, char* argv[]) { - u32 scr_width = 1024; - u32 scr_height = 768; + u32 scr_width = 1280; + u32 scr_height = 720; if (SDL_Init(SDL_INIT_VIDEO) != 0) { @@ -773,7 +769,7 @@ int main(int argc, char* argv[]) u32 pos_ele_count = BATCH_SIZE * 4*6; u32 color_ele_count = BATCH_SIZE * 3*6; // 1GB <= (((1b*1024)kb*1024)mb*1024)mb - u32 mem_size = (1024*1024*1024); + u32 mem_size = GB(1); void* batch_memory = calloc(mem_size, sizeof(r32)); Arena batch_arena; arena_init(&batch_arena, (unsigned char*)batch_memory, mem_size*sizeof(r32)); @@ -801,7 +797,7 @@ int main(int argc, char* argv[]) gl_setup_colored_quad_optimized(renderer, cq_batch_sp); - r32 render_scale = 2.0f; + r32 render_scale = 1.0f; // ========== // setup text // 1. setup free type library stuff @@ -874,30 +870,32 @@ int main(int argc, char* argv[]) // direction in which player is effectively travelling Vec2 p_motion_dir = Vec2{0.0f, 0.0f}; + // @thinking: level object handling + // there should be a most smallest supported unit + // smallest_size: 16x16 + // object placement should be in pixels + // in order to scale to different resolutions it should be multiplied by + // scaling factor + Vec2 atom_size = Vec2{16.0f, 16.0f} * render_scale; + GameState state = {0}; state.world_size = Vec2{(r32)scr_width, (r32)scr_height}; state.screen_size = Vec2{(r32)scr_width, (r32)scr_height}; state.render_scale = vec2(render_scale); Vec3 player_position = Vec3{0.0f, 70.0f, -1.0f}; - Vec2 player_size = vec2(40.0f); + Vec2 player_size = atom_size * render_scale; state.player = rect(player_position, player_size); - // @thinking: level object handling - // there should be a most smallest supported unit - // smallest_size: 16x16 - // object placement should be in pixels - // in order to scale to different resolutions it should be multiplied by - // scaling factor - Vec2 atom_size = {16.0f, 16.0f}; - Vec3 floor_position = Vec3{640.0f*render_scale, 400.0f*render_scale, -2.0f}; - Vec2 floor_size = atom_size*Vec2{40.0f, 1.5f}; + r32 obstacle_z = -2.0f; + Vec3 floor_position = Vec3{640.0f*render_scale, 400.0f*render_scale, obstacle_z}; + Vec2 floor_size = atom_size * Vec2{5.0f, 1.0f}; state.floor = rect(floor_position, floor_size); Vec3 wall_position = get_world_position_from_percent( - state, Vec3{20.0f, 10.0f, -2.0f} + state, Vec3{20.0f, 10.0f, obstacle_z} ); - Vec2 wall_size = atom_size*Vec2{1.5f, 8.0f}; + Vec2 wall_size = atom_size * render_scale * Vec2{1.0f, 8.0f}; state.wall = rect(wall_position, wall_size); // gameplay camera movement stuff @@ -923,6 +921,17 @@ int main(int argc, char* argv[]) FrameTimer timer = frametimer(); + // @level_editor + void* level_memory = calloc(mem_size, sizeof(u32)); + size_t level_mem_size = GB(1); + Arena level_arena; + arena_init(&level_arena, (unsigned char*)level_memory, level_mem_size*sizeof(u32)); + + Level test_level; + test_level.size = IVec2{32, 32}; + const char *level_name = "test_level"; + array_init(&level_arena, &(level_arena.map), test_level.size.x * test_level.size.y); + while (game_running) { update_frame_timer(&timer); @@ -1301,8 +1310,8 @@ int main(int argc, char* argv[]) gl_cq_flush(renderer); - array_clear(r32, renderer->cq_pos_batch); - array_clear(r32, renderer->cq_color_batch); + array_clear(&renderer->cq_pos_batch); + array_clear(&renderer->cq_color_batch); renderer->cq_batch_count = 0; // render ui text diff --git a/source/math.h b/source/math.h index 3f20e43..1d0ebc2 100755 --- a/source/math.h +++ b/source/math.h @@ -33,6 +33,71 @@ r32 clampf(r32 x, r32 bottom, r32 top) // ==== Vector Math ==== +union IVec2 { + struct { + u32 x; + u32 y; + }; + u32 data[2]; + + IVec2 operator+(u32 scaler) { + IVec2 res; + res.x = this->x + scaler; + res.y = this->y + scaler; + + return res; + } + + IVec2 operator+(IVec2 v) { + IVec2 res; + res.x = this->x + v.x; + res.y = this->y + v.y; + + return res; + } + + IVec2 operator-(u32 scaler) { + IVec2 res; + res.x = this->x - scaler; + res.y = this->y - scaler; + + return res; + } + + IVec2 operator-(IVec2 v) { + IVec2 res; + res.x = this->x - v.x; + res.y = this->y - v.y; + + return res; + } + + IVec2 operator*(u32 scaler) { + IVec2 res; + res.x = this->x * scaler; + res.y = this->y * scaler; + + return res; + } + + IVec2 operator*(IVec2 v) { + IVec2 res; + res.x = this->x * v.x; + res.y = this->y * v.y; + + return res; + } + + IVec2 operator/(u32 scaler) { + SDL_assert(scaler != 0); + IVec2 res; + res.x = this->x / scaler; + res.y = this->y / scaler; + + return res; + } +}; + union Vec2 { struct { r32 x; diff --git a/source/todo.txt b/source/todo.txt new file mode 100644 index 0000000..0ff0dc1 --- /dev/null +++ b/source/todo.txt @@ -0,0 +1,32 @@ +Project Estimation/Target ~ 1 - 2 months (was so off on this) +well, to be fair, if I account for actual time worked, then I was not +so off on my estimate, I think I am still in the estimation range +Tasks: + +DONE: +- gravity - very barebones version done +- horizontal motion on ground + - accelerate + - constant speed + - decelerate to give impression of sliding +- horizontal motion when falling + - inertia-like movement when accelerating prior to falling + - loss of inertia when player was at high speed before + sliding off and other small movement when in free fall + - movement and deceleration when not moving in free fall +- Jumping +- Fixed framerate to prevent weird quirks with movement +- Basic camera follower +- move from row-major to column major setup for math library +- Efficient Quad Renderer + +TODO: +- Some way to make and define levels, maybe just make this grid based? +- Level Creation +- Update camera follower for centering player in view (with limits) after + a few seconds (maybe like 2 seconds) +- Level completion Object +- Audio +- make movement grid based +- Implement Broad Phase Collision for efficient collision handling +- vectorize math library with simd |