some reorganizing
This commit is contained in:
parent
34c6db6acd
commit
cc377c5cf3
7 changed files with 121 additions and 21 deletions
|
@ -36,3 +36,19 @@ struct User_Input {
|
|||
struct { i32 x; i32 y; } mouse_pos;
|
||||
};
|
||||
|
||||
struct Window_Data {
|
||||
// Real width and height of the window
|
||||
i32 width;
|
||||
i32 height;
|
||||
bool size_just_changed;
|
||||
|
||||
f32 desired_aspect_ratio;
|
||||
};
|
||||
|
||||
struct App_State {
|
||||
Window_Data win_data;
|
||||
User_Input user_input;
|
||||
|
||||
u8 *inspected_file;
|
||||
u64 inspected_file_size;
|
||||
};
|
|
@ -20,10 +20,8 @@ internal inline void monitor_mouse_btn(GLFWwindow *window, u16 *mouse_btn_state,
|
|||
}
|
||||
}
|
||||
|
||||
internal void run_main_loop(GLFWwindow *window, Arena *arena)
|
||||
internal void run_main_loop(GLFWwindow *window, Arena *arena, App_State &app)
|
||||
{
|
||||
App_State app {};
|
||||
|
||||
f32 delta_time;
|
||||
chr::time_point last_saved_time = chr::high_resolution_clock::now();
|
||||
|
||||
|
|
81
src/mem.cpp
81
src/mem.cpp
|
@ -1,3 +1,5 @@
|
|||
// Mostly taken from the raddebugger -- thanks, Ryan.
|
||||
|
||||
#define align_pow2(x, b) (((x) + (b) - 1) & ( ~((b) - 1)))
|
||||
#define SLLStackPush_N(f,n,next) ((n)->next=(f), (f)=(n))
|
||||
#define SLLStackPop_N(f,next) ((f)=(f)->next)
|
||||
|
@ -51,6 +53,7 @@ Arena *arena_alloc_sized(u64 init_res, u64 init_cmt)
|
|||
arena->cmt = cmt;
|
||||
arena->res = res;
|
||||
arena->align = 8;
|
||||
arena->grow = true;
|
||||
}
|
||||
|
||||
return arena;
|
||||
|
@ -82,7 +85,7 @@ void *arena_push_impl(Arena *arena, u64 size)
|
|||
u64 pos_mem = align_pow2(cur->pos, arena->align);
|
||||
u64 pos_new = pos_mem + size;
|
||||
|
||||
if (cur->res < pos_new) {
|
||||
if (cur->res < pos_new && arena->grow) {
|
||||
Arena *new_block;
|
||||
if (size < arena_huge_push_threshold()) {
|
||||
new_block = arena_alloc();
|
||||
|
@ -103,7 +106,7 @@ void *arena_push_impl(Arena *arena, u64 size)
|
|||
|
||||
if (cur->cmt < pos_new) {
|
||||
u64 cmt_new_aligned = align_pow2(pos_new, ARENA_COMMIT_SIZE);
|
||||
u64 cmt_new_clamped = std::min(cmt_new_aligned, cur->res);
|
||||
u64 cmt_new_clamped = min(cmt_new_aligned, cur->res);
|
||||
u64 cmt_new_size = cmt_new_clamped - cur->cmt;
|
||||
b32x is_cmt_ok = os_commit((u8*)cur + cur->cmt, cmt_new_size);
|
||||
|
||||
|
@ -120,3 +123,77 @@ void *arena_push_impl(Arena *arena, u64 size)
|
|||
|
||||
return mem;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T *arena_push(Arena *arena)
|
||||
{
|
||||
void *mem = arena_push_impl(arena, sizeof(T));
|
||||
return (T *)mem;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T *arena_push_array_no_zero(Arena *arena, u64 count)
|
||||
{
|
||||
void *mem = arena_push_impl(arena, sizeof(T) * count);
|
||||
return (T *)mem;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T *arena_push_array(Arena *arena, u64 count)
|
||||
{
|
||||
T *ary = arena_push_array_no_zero<T>(arena, count);
|
||||
memset(ary, 0, sizeof(T) * count);
|
||||
return ary;
|
||||
}
|
||||
|
||||
u8 *arena_push_contiguous(Arena *arena, u64 size)
|
||||
{
|
||||
b32 restore = arena->grow;
|
||||
arena->grow = 0;
|
||||
void *mem = arena_push_impl(arena, size);
|
||||
arena->grow = restore;
|
||||
return (u8 *)mem;
|
||||
}
|
||||
|
||||
u64 arena_pos(Arena *arena)
|
||||
{
|
||||
Arena *cur = arena->cur;
|
||||
u64 pos = cur->base_pos + cur->pos;
|
||||
return pos;
|
||||
}
|
||||
|
||||
void arena_pop_to(Arena *arena, u64 big_pos_unclamped)
|
||||
{
|
||||
u64 big_pos = min(ARENA_HEADER_SIZE, big_pos_unclamped);
|
||||
|
||||
// unroll the chain
|
||||
Arena *current = arena->cur;
|
||||
for (Arena *prev = 0; current->base_pos >= big_pos; current = prev) {
|
||||
prev = current->prev;
|
||||
os_release(current, current->res);
|
||||
}
|
||||
assert(current);
|
||||
arena->cur = current;
|
||||
|
||||
// compute arena-relative position
|
||||
u64 new_pos = big_pos - current->base_pos;
|
||||
assert(new_pos <= current->pos);
|
||||
|
||||
// poison popped memory block
|
||||
asan_poison_memory_region((u8*)current + new_pos, (current->pos - new_pos));
|
||||
|
||||
// update position
|
||||
current->pos = new_pos;
|
||||
}
|
||||
|
||||
Temp temp_begin(Arena *arena)
|
||||
{
|
||||
u64 pos = arena_pos(arena);
|
||||
Temp temp = {arena, pos};
|
||||
return temp;
|
||||
}
|
||||
|
||||
void temp_end(Temp temp)
|
||||
{
|
||||
arena_pop_to(temp.arena, temp.pos);
|
||||
}
|
||||
|
|
|
@ -10,4 +10,10 @@ struct Arena {
|
|||
u64 cmt;
|
||||
u64 res;
|
||||
u64 align;
|
||||
b8 grow;
|
||||
};
|
||||
|
||||
struct Temp {
|
||||
Arena *arena;
|
||||
u64 pos;
|
||||
};
|
||||
|
|
|
@ -1,22 +1,17 @@
|
|||
struct Window_Data {
|
||||
// Real width and height of the window
|
||||
i32 width;
|
||||
i32 height;
|
||||
bool size_just_changed;
|
||||
|
||||
f32 desired_aspect_ratio;
|
||||
};
|
||||
|
||||
struct App_State {
|
||||
Window_Data win_data;
|
||||
User_Input user_input;
|
||||
};
|
||||
|
||||
internal void update_and_render(Arena *arena, App_State &app, f32 delta_time)
|
||||
{
|
||||
ImGui::SetNextWindowPos({ 0, 0 });
|
||||
ImGui::SetNextWindowSize({ (f32)app.win_data.width, (f32)app.win_data.height });
|
||||
|
||||
Temp scratch = temp_begin(arena);
|
||||
defer { temp_end(scratch); };
|
||||
|
||||
u64 text_buf_size = min(app.inspected_file_size, 2048);
|
||||
char *text_buf = arena_push_array<char>(scratch.arena, text_buf_size);
|
||||
// TODO: convert file content to human readable
|
||||
|
||||
if (ImGui::Begin("test")) {
|
||||
ImGui::InputTextMultiline("##text_buf", text_buf, text_buf_size);
|
||||
ImGui::End();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
#include "types.h"
|
||||
#include "defer.hpp"
|
||||
#include "mem.h"
|
||||
#include "input.h"
|
||||
#include "app_state.h"
|
||||
|
||||
namespace chr = std::chrono;
|
||||
|
||||
|
@ -148,7 +148,11 @@ int main(int argc, char **argv)
|
|||
}
|
||||
|
||||
// Start main loop
|
||||
run_main_loop(window, arena);
|
||||
App_State app {};
|
||||
app.inspected_file = (u8*)fmem;
|
||||
app.inspected_file_size = fsize;
|
||||
|
||||
run_main_loop(window, arena, app);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -10,7 +10,11 @@ using b8 = bool;
|
|||
using b32 = int32_t;
|
||||
using b32x = int;
|
||||
|
||||
using i8 = int8_t;
|
||||
using i32 = int32_t;
|
||||
using i64 = int64_t;
|
||||
|
||||
#define internal static
|
||||
|
||||
#define max(a, b) ((a) > (b)) ? (a) : (b)
|
||||
#define min(a, b) ((a) < (b)) ? (a) : (b)
|
||||
|
|
Loading…
Reference in a new issue