diff options
author | talha <sarcxd@gmail.com> | 2025-03-06 10:37:36 +0500 |
---|---|---|
committer | talha <sarcxd@gmail.com> | 2025-03-06 10:37:36 +0500 |
commit | 23cc1a7d8c5ae6a397071763d0326990868c37af (patch) | |
tree | 0eb8fd47d678c57ec43c22ed104997c10c900260 | |
parent | 1d0bff7b975a63ef9047f16acdb2ef03123772d9 (diff) |
Making project work with clangd:
- Moved a lot of items into separate header files
- Still compiling everything as a single translation unit (but maybe a
heavier translation unit [if that is possible])
- setup compile_flags, a way to use clangd without using a build system
- stuff like clangd and cmake don't work well with unity builds, so I
avoid jumping through hoops
-rw-r--r-- | compile_flags.txt | 8 | ||||
-rw-r--r-- | source/array/array.h | 3 | ||||
-rw-r--r-- | source/core.h | 24 | ||||
-rwxr-xr-x | source/main.cpp | 111 | ||||
-rwxr-xr-x | source/math.h | 4 | ||||
-rw-r--r-- | source/memory/arena.h | 2 | ||||
-rwxr-xr-x | source/memory/memory.c | 246 | ||||
-rwxr-xr-x | source/memory/memory.h | 98 | ||||
-rw-r--r-- | source/renderer/renderer.cpp | 3 | ||||
-rw-r--r-- | source/renderer/renderer.h | 87 |
10 files changed, 138 insertions, 448 deletions
diff --git a/compile_flags.txt b/compile_flags.txt new file mode 100644 index 0000000..474e164 --- /dev/null +++ b/compile_flags.txt @@ -0,0 +1,8 @@ +-xc++ +-std=c++11 +-Wall +-I./include +-I/usr/include +-I/usr/local/include +-DDEBUG +-O2 diff --git a/source/array/array.h b/source/array/array.h index 6ddf5f0..624c90c 100644 --- a/source/array/array.h +++ b/source/array/array.h @@ -1,5 +1,8 @@ #pragma once +#include "../core.h" +#include "../memory/arena.h" + struct r32_array { size_t size; size_t capacity; diff --git a/source/core.h b/source/core.h new file mode 100644 index 0000000..e65a273 --- /dev/null +++ b/source/core.h @@ -0,0 +1,24 @@ +#pragma once + +#include <stddef.h> +#include <stdint.h> + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; + +typedef int8_t s8; +typedef int16_t s16; +typedef int32_t s32; +typedef int64_t s64; + +typedef float r32; +typedef double r64; + +typedef u8 b8; + +#define KB(x) (1024 * (x)) +#define MB(x) (1024 * KB((x))) +#define GB(x) (1024 * MB((x))) + diff --git a/source/main.cpp b/source/main.cpp index fa9f6e4..d6d371e 100755 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,31 +1,22 @@ -#include <SDL2/SDL.h> -#include <glad/glad.h> - +//----------------------------- #include <stdio.h> -#include <memory.h> - -#define STB_TRUETYPE_IMPLEMENTATION -#include "stb_truetype.h" - -#include <stdint.h> - -typedef uint8_t u8; -typedef uint16_t u16; -typedef uint32_t u32; -typedef uint64_t u64; +//----------------------------- -typedef int8_t s8; -typedef int16_t s16; -typedef int32_t s32; -typedef int64_t s64; -typedef float r32; -typedef double r64; - -typedef u8 b8; +//----------------------------- +#include <SDL2/SDL.h> +#include <glad/glad.h> +//----------------------------- +//----------------------------- +#include "core.h" #include "memory/arena.h" #include "math.h" +#include "array/array.cpp" +#include "renderer/renderer.h" +#include "renderer/renderer.cpp" +//----------------------------- + struct Str256 { char buffer[256]; @@ -82,41 +73,6 @@ enum PlatformKey { PK_D = 4, }; -struct TextChar { - s64 lsb; - s64 advance; - Vec2 bbox0; - Vec2 bbox1; - Vec2 size; -}; - -struct TextState { - r32 scale; - u32 pixel_size; - s32 ascent; - s32 descent; - s32 linegap; - u32 texture_atlas_id; - u32 sp; - u32 vao; - u32 vbo; - u32 chunk_size; - IVec2 bbox0; - IVec2 bbox1; - stbtt_fontinfo font; - s32* char_indexes; - Mat4* transforms; - TextChar* char_map; -}; - -#define BATCH_SIZE 2000 - -#define KB(x) (1024 * (x)) -#define MB(x) (1024 * KB((x))) -#define GB(x) (1024 * MB((x))) - -#include "array/array.cpp" - struct Rect { Vec2 lb; Vec2 rt; @@ -193,46 +149,6 @@ struct Level0x1 { typedef struct Level0x1 Level; -struct GLRenderer { - // colored quad - b8 cq_init; - u32 cq_sp; - u32 cq_vao; - // camera - Vec3 preset_up_dir; - Mat4 cam_proj; - // ui camera - b8 ui_cam_update; - Vec3 ui_cam_pos; - Vec3 ui_cam_look; - Mat4 ui_cam_view; - // game camera - b8 cam_update; - Vec3 cam_pos; - Vec3 cam_look; - Mat4 cam_view; - // Batched cq - // batching buffer - u32 cq_batch_sp; - u32 cq_batch_vao; - u32 cq_batch_vbo; - u32 cq_batch_count; - r32_array cq_pos_batch; - r32_array cq_color_batch; - // Batched line - u32 line_sp; - u32 line_vao; - u32 line_vbo; - u32 line_batch_count; - r32_array line_pos_batch; - r32_array line_color_batch; - - // ui text - TextState ui_text; -}; - -#include "renderer/renderer.h" -#include "renderer/renderer.cpp" struct Controller { b8 move_up; @@ -517,7 +433,6 @@ void setup_level(GameState *state, GLRenderer *renderer, Arena *arena) load_level(state, arena, level_path); - Entity player = state->game_level.entities[state->player.index]; Entity goal = state->game_level.entities[state->goal.index]; Vec2 scr_dims; renderer->cam_pos.x = goal.position.x - (state->screen_size.x/2.0f * state->render_scale.x); diff --git a/source/math.h b/source/math.h index cc56a8f..d335bee 100755 --- a/source/math.h +++ b/source/math.h @@ -1,8 +1,10 @@ #ifndef MATH_H #define MATH_H -#include "SDL2/SDL_assert.h" #include <math.h> +#include <string.h> +#include "SDL2/SDL_assert.h" +#include "core.h" #define PI 3.14159265358979323846264338327950288f #define SQUARE(x) ((x)*(x)) diff --git a/source/memory/arena.h b/source/memory/arena.h index ce167f9..ada70b2 100644 --- a/source/memory/arena.h +++ b/source/memory/arena.h @@ -1,6 +1,8 @@ #pragma once #include <assert.h> +#include <string.h> +#include "../core.h" #ifndef ALIGNMENT #define ALIGNMENT (2*sizeof(void*)) diff --git a/source/memory/memory.c b/source/memory/memory.c deleted file mode 100755 index 1d521e4..0000000 --- a/source/memory/memory.c +++ /dev/null @@ -1,246 +0,0 @@ -#include "memory.h" - -b8 is_power_of_two(uintptr_t x) { return (x & (x - 1)) == 0; } - -uintptr_t fast_modulo(uintptr_t p, uintptr_t a) { return (p & (a - 1)); } - -uintptr_t align_forward(uintptr_t ptr, size_t alignment) { - uintptr_t p, a, modulo; - - assert(is_power_of_two(alignment)); - - p = ptr; - a = (uintptr_t)alignment; - modulo = fast_modulo(p, a); - - if (modulo != 0) { - p += (a - modulo); - } - - return p; -} - -//=========================================================================================== -// ---------------------------------- Arena -// ------------------------------------------------- -//=========================================================================================== - -/* - A cases where arena allocation WILL fail: - | size = size_t + ${some_number_that_comes_up_higher_than_offset} - - This is because there is no check being made -*/ -void arena_init(struct Arena *a, unsigned char *backing_store, - size_t capacity) { - a->buffer = backing_store; - a->curr_offset = 0; - a->prev_offset = 0; - a->capacity = capacity; -} - -void *arena_alloc_aligned(struct Arena *a, size_t size, size_t alignment) { - void *ptr = NULL; - - assert(is_power_of_two(alignment)); - - uintptr_t curr_ptr = (uintptr_t)a->buffer + a->curr_offset; - uintptr_t offset = align_forward(curr_ptr, alignment); - offset = offset - (uintptr_t)a->buffer; - - if (size <= a->capacity - offset) { - ptr = &a->buffer[offset]; - a->prev_offset = a->curr_offset; - a->curr_offset = offset + size; - memset(ptr, 0, size); - } - - return ptr; -} - -void *arena_alloc(struct Arena *a, size_t size) { - return arena_alloc_aligned(a, size, DEFAULT_ALIGNMENT); -} - -void *arena_resize_aligned(struct Arena *a, void *old_memory, size_t old_size, - size_t new_size, size_t alignment) { - unsigned char *old = (unsigned char *)old_memory; - void *ptr = NULL; - - assert(is_power_of_two(alignment)); - - if (old >= a->buffer && old < a->buffer + a->capacity) { - if (a->buffer + a->prev_offset == old) { - // extend_last_element - if (new_size > old_size) { - size_t size_increase = new_size - old_size; - if (size_increase > (a->capacity - a->curr_offset)) { - new_size = old_size; - size_increase = 0; - } - memset(&a->buffer[a->curr_offset], 0, size_increase); - } - a->curr_offset = a->prev_offset + new_size; - ptr = old_memory; - } else { - ptr = arena_alloc_aligned(a, new_size, alignment); - if (ptr != NULL) { - size_t copy_size = old_size < new_size ? old_size : new_size; - memmove(ptr, old_memory, copy_size); - } - } - } - - return ptr; -} - -void *arena_resize(struct Arena *a, void *old_mem, size_t old_size, - size_t new_size) { - return arena_resize_aligned(a, old_mem, old_size, new_size, - DEFAULT_ALIGNMENT); -} - -void arena_clear(struct Arena *a) { - a->curr_offset = 0; - a->prev_offset = 0; -} - -//=========================================================================================== -// ---------------------------------- STACK -// ------------------------------------------------- -//=========================================================================================== - -void stack_init(struct stack *s, void *backing_store, size_t capacity) { - s->buffer = (unsigned char *)backing_store; - s->prev_offset = 0; - s->curr_offset = 0; - s->capacity = capacity; -} - -size_t calc_padding_with_header(uintptr_t ptr, uintptr_t alignment, - size_t hdr_sz) { - uintptr_t p, a, modulo, padding, space_needed; - - assert(is_power_of_two(alignment)); - - padding = space_needed = 0; - - p = ptr; - a = alignment; - modulo = fast_modulo(p, a); - - if (modulo != 0) { - padding = a - modulo; - } - - space_needed = (uintptr_t)hdr_sz; - if (padding < space_needed) { - space_needed -= padding; - if (fast_modulo(space_needed, a) != 0) { - padding = padding + space_needed + a; - } else { - padding = padding + space_needed; - } - } - - return (size_t)padding; -} - -struct ResVoid stack_alloc_aligned(struct stack *s, size_t size, - size_t alignment) { - uintptr_t curr_addr, next_addr; - size_t padding; - struct stack_hdr *header; - - assert(is_power_of_two(alignment)); - if (alignment > 128) { - alignment = 128; - } - - struct ResVoid result = {.status = MEM_OK, .bytes_count = 0, .memory = 0}; - - curr_addr = (uintptr_t)s->buffer + (uintptr_t)s->curr_offset; - padding = calc_padding_with_header(curr_addr, (uintptr_t)alignment, - sizeof(struct stack_hdr)); - - if (size > s->capacity - (s->curr_offset + padding)) { - result.status = MEM_FULL; - return result; - } - - next_addr = curr_addr + (uintptr_t)padding; - header = (struct stack_hdr *)(next_addr - sizeof(struct stack_hdr)); - header->prev_offset = s->prev_offset; - header->padding = padding; - - s->prev_offset = s->curr_offset + padding; - s->curr_offset = s->prev_offset + size; - - result.memory = memset((void *)next_addr, 0, size); - result.bytes_count = size; - - return result; -} - -struct ResVoid stack_alloc(struct stack *s, size_t size) { - return stack_alloc_aligned(s, size, DEFAULT_ALIGNMENT); -} - -enum MemStatus stack_free(struct stack *s) { - uintptr_t last_ele = (uintptr_t)s->buffer + (uintptr_t)s->prev_offset; - struct stack_hdr *header = - (struct stack_hdr *)(last_ele - sizeof(struct stack_hdr)); - - uintptr_t prev_ele = (uintptr_t)s->buffer + (uintptr_t)header->prev_offset; - s->curr_offset = - (size_t)((last_ele - (uintptr_t)header->padding) - (uintptr_t)s->buffer); - s->prev_offset = (size_t)(prev_ele - (uintptr_t)s->buffer); - - return MEM_OK; -} - -struct ResVoid stack_resize_aligned(struct stack *s, void *old_memory, - size_t old_size, size_t new_size, - size_t alignment) { - struct ResVoid result = {.status = MEM_OK, .bytes_count = 0, .memory = 0}; - - if (old_memory < s->buffer || old_memory > s->buffer + s->capacity) { - result.status = MEM_OUT_OF_BOUNDS; - return result; - } - - // is_last_element() - if (s->buffer + s->prev_offset == old_memory) { - if (new_size > old_size) { - size_t size_difference = new_size - old_size; - if (size_difference > s->capacity - s->curr_offset) { - result.status = MEM_FULL; - return result; - } - - memset(&s->buffer[s->curr_offset], 0, size_difference); - } - s->curr_offset = s->prev_offset + new_size; - - result.memory = old_memory; - return result; - } - - result = stack_alloc_aligned(s, new_size, alignment); - size_t min_size = - old_size < result.bytes_count ? old_size : result.bytes_count; - memmove(result.memory, old_memory, min_size); - - return result; -} - -struct ResVoid stack_resize(struct stack *s, void *old_memory, size_t old_size, - size_t new_size) { - return stack_resize_aligned(s, old_memory, old_size, new_size, - DEFAULT_ALIGNMENT); -} - -void stack_clear(struct stack *s) { - s->prev_offset = 0; - s->curr_offset = 0; -} diff --git a/source/memory/memory.h b/source/memory/memory.h deleted file mode 100755 index e402c0b..0000000 --- a/source/memory/memory.h +++ /dev/null @@ -1,98 +0,0 @@ -#ifndef AMR_MEMORY_H -#define AMR_MEMORY_H - -#include <stddef.h> -#include <stdint.h> -#include <assert.h> -#include <string.h> - -#ifndef AMR_TYPES_H -#define AMR_TYPES_H - -typedef int8_t s8; -typedef int16_t s16; -typedef int32_t s32; -typedef int64_t s64; - -typedef uint8_t u8; -typedef uint16_t u16; -typedef uint32_t u32; -typedef uint64_t u64; - -typedef u8 b8; - -#endif // AMR_TYPES_H - - -#ifndef DEFAULT_ALIGNMENT -#define DEFAULT_ALIGNMENT (2*sizeof(void *)) -#endif - -// @todo: build a logging mechanism for handling errors -// maybe read about that - -enum MemStatus { MEM_OK=0, MEM_OUT_OF_BOUNDS, MEM_FULL }; - -struct ResVoid { - enum MemStatus status; - size_t bytes_count; - void* memory; -}; - -b8 is_power_of_two(uintptr_t x); -uintptr_t fast_modulo(uintptr_t p, uintptr_t a); -uintptr_t align_forward(uintptr_t ptr, size_t align); - -//=========================================================================================== -// ---------------------------------- ARENA ------------------------------------------------- -//=========================================================================================== - -struct Arena { - unsigned char* buffer; - size_t prev_offset; - size_t curr_offset; - size_t capacity; -}; - -void arena_init(struct Arena *a, unsigned char *backing_store, size_t capacity); -void* arena_alloc_aligned(struct Arena* a, size_t size, size_t align); -void* arena_alloc(struct Arena* a, size_t size); -void* arena_resize_aligned(struct Arena* a, void* old_memory, size_t old_size, - size_t new_size, size_t align); -void* arena_resize(struct Arena* a, void* old_mem, size_t old_size, - size_t new_size); -void arena_clear(struct Arena *a); - -//=========================================================================================== -// ---------------------------------- STACK ------------------------------------------------- -//=========================================================================================== - -/* -* @todo: stack needs to be updated, it's really just a work in progress right now. -* The main thing is minimizing the use of compound types, since that is pretty annoying to deal with. -* I would rather write code that makes sure to collapse all possible cases and lets me just not worry about code. -* Would rather stick to worrying about data being data -*/ - -struct stack { - unsigned char* buffer; - size_t prev_offset; - size_t curr_offset; - size_t capacity; -}; - -struct stack_hdr { - size_t prev_offset; - size_t padding; -}; - -void stack_init(struct stack* s, void *backing_store, size_t capacity); -struct ResVoid stack_alloc_aligned(struct stack* s, size_t size, size_t alignment); -struct ResVoid stack_alloc(struct stack* s, size_t size); -enum MemStatus stack_free(struct stack* s); -struct ResVoid stack_resize_aligned(struct stack* s, void* old_memory, size_t old_size, - size_t new_size, size_t alignment); -struct ResVoid stack_resize(struct stack* s, void* old_memory, size_t old_size, size_t new_size); -void stack_clear(struct stack* s); - -#endif diff --git a/source/renderer/renderer.cpp b/source/renderer/renderer.cpp index 2ba8887..22eb333 100644 --- a/source/renderer/renderer.cpp +++ b/source/renderer/renderer.cpp @@ -1,3 +1,6 @@ +#include <stdio.h> +#include "glad/glad.h" +#include "SDL2/SDL_rwops.h" #include "renderer.h" u32 gl_shader_program(char* vs, char* fs) diff --git a/source/renderer/renderer.h b/source/renderer/renderer.h index 806d832..6d9b7f6 100644 --- a/source/renderer/renderer.h +++ b/source/renderer/renderer.h @@ -1,5 +1,79 @@ #pragma once +#define STB_TRUETYPE_IMPLEMENTATION +#include "stb_truetype.h" + +#include "../core.h" +#include "../math.h" +#include "../array/array.h" + +#define BATCH_SIZE 2000 + +struct TextChar { + s64 lsb; + s64 advance; + Vec2 bbox0; + Vec2 bbox1; + Vec2 size; +}; + +struct TextState { + r32 scale; + u32 pixel_size; + s32 ascent; + s32 descent; + s32 linegap; + u32 texture_atlas_id; + u32 sp; + u32 vao; + u32 vbo; + u32 chunk_size; + IVec2 bbox0; + IVec2 bbox1; + stbtt_fontinfo font; + s32* char_indexes; + Mat4* transforms; + TextChar* char_map; +}; + +struct GLRenderer { + // colored quad + b8 cq_init; + u32 cq_sp; + u32 cq_vao; + // camera + Vec3 preset_up_dir; + Mat4 cam_proj; + // ui camera + b8 ui_cam_update; + Vec3 ui_cam_pos; + Vec3 ui_cam_look; + Mat4 ui_cam_view; + // game camera + b8 cam_update; + Vec3 cam_pos; + Vec3 cam_look; + Mat4 cam_view; + // Batched cq + // batching buffer + u32 cq_batch_sp; + u32 cq_batch_vao; + u32 cq_batch_vbo; + u32 cq_batch_count; + r32_array cq_pos_batch; + r32_array cq_color_batch; + // Batched line + u32 line_sp; + u32 line_vao; + u32 line_vbo; + u32 line_batch_count; + r32_array line_pos_batch; + r32_array line_color_batch; + + // ui text + TextState ui_text; +}; + u32 gl_shader_program(char *vs, char *fs); u32 gl_shader_program_from_path(const char *vspath, const char *fspath); @@ -17,7 +91,7 @@ void gl_setup_colored_quad_optimized( GLRenderer* renderer, u32 sp ); -void gl_draw_colored_quad_optimised( +void gl_draw_colored_quad_optimized( GLRenderer* renderer, Vec3 position, Vec2 size, @@ -26,18 +100,21 @@ void gl_draw_colored_quad_optimised( void gl_cq_flush(GLRenderer *renderer); // ==================== LINE ==================== -void gl_setup_colored_line(GLRenderer *renderer, u32 sp); -void gl_draw_colored_line( +void gl_setup_line(GLRenderer *renderer, u32 sp); +void gl_draw_line( GLRenderer *renderer, Vec3 start, Vec3 end, - Vec2 size, Vec3 color ); void gl_line_flush(GLRenderer *renderer); // ==================== FONT RENDERING ==================== void gl_setup_text(TextState *uistate); -void gl_render_text(GLRenderer *renderer, Vec2 position, Vec2 color, r32 size); +void gl_render_text(GLRenderer *renderer, + char *text, + Vec2 position, + Vec3 color, + r32 font_size); void gl_text_flush(GLRenderer *renderer, u32 render_count); |