2024-01-21 20:38:02 -08:00
|
|
|
package sectr
|
|
|
|
|
2024-02-08 07:50:36 -08:00
|
|
|
import "base:runtime"
|
2024-02-22 18:19:29 -08:00
|
|
|
import c "core:c/libc"
|
2024-01-21 20:38:02 -08:00
|
|
|
import "core:dynlib"
|
|
|
|
import "core:mem"
|
2024-01-29 22:29:48 -08:00
|
|
|
import "core:mem/virtual"
|
2024-01-21 20:38:02 -08:00
|
|
|
import "core:os"
|
2024-01-29 22:29:48 -08:00
|
|
|
import "core:slice"
|
2024-01-21 20:38:02 -08:00
|
|
|
import "core:strings"
|
|
|
|
import rl "vendor:raylib"
|
|
|
|
|
2024-02-08 07:50:36 -08:00
|
|
|
Path_Assets :: "../assets/"
|
|
|
|
Path_Input_Replay :: "scratch.sectr_replay"
|
2024-01-21 20:38:02 -08:00
|
|
|
|
|
|
|
ModuleAPI :: struct {
|
|
|
|
lib : dynlib.Library,
|
2024-02-27 04:50:57 -08:00
|
|
|
write_time : FileTime,
|
2024-01-21 20:38:02 -08:00
|
|
|
lib_version : i32,
|
|
|
|
|
2024-03-07 12:57:05 -08:00
|
|
|
startup : type_of( startup ),
|
|
|
|
shutdown : type_of( sectr_shutdown ),
|
|
|
|
reload : type_of( reload ),
|
|
|
|
tick : type_of( tick ),
|
|
|
|
clean_frame : type_of( clean_frame ),
|
2024-01-21 20:38:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@export
|
2024-03-07 12:57:05 -08:00
|
|
|
startup :: proc( persistent_mem, frame_mem, transient_mem, files_buffer_mem : ^VArena, host_logger : ^ Logger )
|
2024-01-21 20:38:02 -08:00
|
|
|
{
|
2024-02-27 04:50:57 -08:00
|
|
|
logger_init( & Memory_App.logger, "Sectr", host_logger.file_path, host_logger.file )
|
|
|
|
context.logger = to_odin_logger( & Memory_App.logger )
|
2024-02-08 19:33:53 -08:00
|
|
|
|
2024-01-29 22:29:48 -08:00
|
|
|
// Setup memory for the first time
|
|
|
|
{
|
2024-02-27 04:50:57 -08:00
|
|
|
using Memory_App;
|
2024-03-07 12:57:05 -08:00
|
|
|
persistent = persistent_mem
|
|
|
|
frame = frame_mem
|
|
|
|
transient = transient_mem
|
|
|
|
files_buffer = files_buffer_mem
|
2024-02-13 14:16:39 -08:00
|
|
|
|
2024-03-07 12:57:05 -08:00
|
|
|
context.allocator = persistent_allocator()
|
|
|
|
context.temp_allocator = transient_allocator()
|
2024-01-29 22:29:48 -08:00
|
|
|
}
|
2024-02-08 19:33:53 -08:00
|
|
|
|
2024-02-13 14:16:39 -08:00
|
|
|
state := new( State, persistent_allocator() )
|
2024-01-29 22:29:48 -08:00
|
|
|
using state
|
2024-01-21 20:38:02 -08:00
|
|
|
|
2024-03-07 12:57:05 -08:00
|
|
|
// Setup General Slab
|
|
|
|
{
|
|
|
|
alignment := uint(mem.DEFAULT_ALIGNMENT)
|
|
|
|
|
|
|
|
policy : SlabPolicy
|
|
|
|
policy_ptr := & policy
|
|
|
|
push( policy_ptr, SlabSizeClass { 16 * Megabyte, 4 * Kilobyte, alignment })
|
|
|
|
push( policy_ptr, SlabSizeClass { 32 * Megabyte, 16 * Kilobyte, alignment })
|
|
|
|
push( policy_ptr, SlabSizeClass { 64 * Megabyte, 32 * Kilobyte, alignment })
|
|
|
|
push( policy_ptr, SlabSizeClass { 64 * Megabyte, 64 * Kilobyte, alignment })
|
|
|
|
push( policy_ptr, SlabSizeClass { 64 * Megabyte, 128 * Kilobyte, alignment })
|
|
|
|
push( policy_ptr, SlabSizeClass { 64 * Megabyte, 256 * Kilobyte, alignment })
|
|
|
|
push( policy_ptr, SlabSizeClass { 64 * Megabyte, 512 * Kilobyte, alignment })
|
|
|
|
push( policy_ptr, SlabSizeClass { 64 * Megabyte, 1 * Megabyte, alignment })
|
|
|
|
push( policy_ptr, SlabSizeClass { 64 * Megabyte, 2 * Megabyte, alignment })
|
|
|
|
push( policy_ptr, SlabSizeClass { 64 * Megabyte, 4 * Megabyte, alignment })
|
|
|
|
push( policy_ptr, SlabSizeClass { 64 * Megabyte, 8 * Megabyte, alignment })
|
|
|
|
push( policy_ptr, SlabSizeClass { 64 * Megabyte, 16 * Megabyte, alignment })
|
|
|
|
push( policy_ptr, SlabSizeClass { 64 * Megabyte, 32 * Megabyte, alignment })
|
|
|
|
push( policy_ptr, SlabSizeClass { 256 * Megabyte, 64 * Megabyte, alignment })
|
|
|
|
push( policy_ptr, SlabSizeClass { 256 * Megabyte, 128 * Megabyte, alignment })
|
|
|
|
push( policy_ptr, SlabSizeClass { 512 * Megabyte, 256 * Megabyte, alignment })
|
|
|
|
push( policy_ptr, SlabSizeClass { 512 * Megabyte, 512 * Megabyte, alignment })
|
|
|
|
|
|
|
|
alloc_error : AllocatorError
|
|
|
|
general_slab, alloc_error = slab_init( policy_ptr, allocator = persistent_allocator() )
|
|
|
|
verify( alloc_error == .None, "Failed to allocate the general slab allocator" )
|
|
|
|
}
|
|
|
|
|
2024-02-09 10:09:58 -08:00
|
|
|
context.user_ptr = state
|
|
|
|
|
2024-02-08 07:50:36 -08:00
|
|
|
input = & input_data[1]
|
|
|
|
input_prev = & input_data[0]
|
|
|
|
|
2024-02-22 18:19:29 -08:00
|
|
|
// rl.Odin_SetMalloc( RL_MALLOC )
|
|
|
|
|
2024-03-02 07:24:09 -08:00
|
|
|
rl.SetConfigFlags( {
|
|
|
|
rl.ConfigFlag.WINDOW_RESIZABLE,
|
2024-03-02 17:22:28 -08:00
|
|
|
// rl.ConfigFlag.WINDOW_TOPMOST,
|
2024-03-02 07:24:09 -08:00
|
|
|
})
|
2024-02-11 20:00:06 -08:00
|
|
|
|
2024-01-21 20:38:02 -08:00
|
|
|
// Rough setup of window with rl stuff
|
2024-02-11 20:00:06 -08:00
|
|
|
window_width : i32 = 1000
|
|
|
|
window_height : i32 = 600
|
2024-01-21 20:38:02 -08:00
|
|
|
win_title : cstring = "Sectr Prototype"
|
2024-02-11 20:00:06 -08:00
|
|
|
rl.InitWindow( window_width, window_height, win_title )
|
2024-02-08 19:33:53 -08:00
|
|
|
log( "Raylib initialized and window opened" )
|
2024-01-21 20:38:02 -08:00
|
|
|
|
2024-02-11 20:00:06 -08:00
|
|
|
window := & state.app_window
|
|
|
|
window.extent.x = f32(window_width) * 0.5
|
|
|
|
window.extent.y = f32(window_height) * 0.5
|
|
|
|
|
2024-02-10 00:40:53 -08:00
|
|
|
// We do not support non-uniform DPI.
|
2024-02-11 20:00:06 -08:00
|
|
|
window.dpi_scale = rl.GetWindowScaleDPI().x
|
2024-02-13 14:16:39 -08:00
|
|
|
window.ppcm = os_default_ppcm * window.dpi_scale
|
2024-02-10 00:40:53 -08:00
|
|
|
|
2024-01-21 20:38:02 -08:00
|
|
|
// Determining current monitor and setting the target frametime based on it..
|
2024-02-10 00:40:53 -08:00
|
|
|
monitor_id = rl.GetCurrentMonitor()
|
2024-01-22 00:47:53 -08:00
|
|
|
monitor_refresh_hz = rl.GetMonitorRefreshRate( monitor_id )
|
2024-01-21 20:38:02 -08:00
|
|
|
rl.SetTargetFPS( monitor_refresh_hz )
|
2024-02-27 04:50:57 -08:00
|
|
|
log( str_fmt_tmp( "Set target FPS to: %v", monitor_refresh_hz ) )
|
2024-01-21 20:38:02 -08:00
|
|
|
|
|
|
|
// Basic Font Setup
|
|
|
|
{
|
2024-02-13 14:16:39 -08:00
|
|
|
font_provider_startup()
|
2024-02-13 15:50:22 -08:00
|
|
|
// path_rec_mono_semicasual_reg := strings.concatenate( { Path_Assets, "RecMonoSemicasual-Regular-1.084.ttf" })
|
|
|
|
// font_rec_mono_semicasual_reg = font_load( path_rec_mono_semicasual_reg, 24.0, "RecMonoSemiCasual_Regular" )
|
2024-02-13 14:16:39 -08:00
|
|
|
|
2024-02-13 15:50:22 -08:00
|
|
|
// path_squidgy_slimes := strings.concatenate( { Path_Assets, "Squidgy Slimes.ttf" } )
|
|
|
|
// font_squidgy_slimes = font_load( path_squidgy_slimes, 24.0, "Squidgy_Slime" )
|
2024-02-12 17:52:56 -08:00
|
|
|
|
2024-03-07 12:57:05 -08:00
|
|
|
path_firacode := strings.concatenate( { Path_Assets, "FiraCode-Regular.ttf" }, frame_allocator() )
|
2024-02-13 14:16:39 -08:00
|
|
|
font_firacode = font_load( path_firacode, 24.0, "FiraCode" )
|
2024-02-12 17:52:56 -08:00
|
|
|
|
2024-02-13 14:16:39 -08:00
|
|
|
// font_data, read_succeded : = os.read_entire_file( path_rec_mono_semicasual_reg )
|
2024-02-23 06:36:23 -08:00
|
|
|
// verify( read_succeded, fmt.tprintf("Failed to read font file for: %v", path_rec_mono_semicasual_reg) )
|
2024-01-21 20:38:02 -08:00
|
|
|
|
2024-02-13 14:16:39 -08:00
|
|
|
// cstr := strings.clone_to_cstring( path_rec_mono_semicasual_reg )
|
|
|
|
// font_rec_mono_semicasual_reg = rl.LoadFontEx( cstr, cast(i32) points_to_pixels(24.0), nil, 0 )
|
|
|
|
// delete( cstr)
|
|
|
|
|
|
|
|
// rl.GuiSetFont( font_rec_mono_semicasual_reg ) // TODO(Ed) : Does this do anything?
|
|
|
|
default_font = font_firacode
|
2024-02-08 19:33:53 -08:00
|
|
|
log( "Default font loaded" )
|
2024-01-21 20:38:02 -08:00
|
|
|
}
|
2024-02-09 10:09:58 -08:00
|
|
|
|
2024-02-12 17:52:56 -08:00
|
|
|
// Demo project setup
|
2024-02-10 00:40:53 -08:00
|
|
|
{
|
|
|
|
using project
|
|
|
|
path = "./"
|
|
|
|
name = "First Project"
|
|
|
|
workspace.name = "First Workspace"
|
|
|
|
{
|
|
|
|
using project.workspace
|
|
|
|
cam = {
|
2024-02-13 14:16:39 -08:00
|
|
|
target = { 0, 0 },
|
|
|
|
offset = transmute(Vec2) window.extent,
|
2024-02-10 00:40:53 -08:00
|
|
|
rotation = 0,
|
2024-02-13 14:16:39 -08:00
|
|
|
zoom = 1.0,
|
2024-02-10 00:40:53 -08:00
|
|
|
}
|
|
|
|
// cam = {
|
|
|
|
// position = { 0, 0, -100 },
|
|
|
|
// target = { 0, 0, 0 },
|
|
|
|
// up = { 0, 1, 0 },
|
|
|
|
// fovy = 90,
|
|
|
|
// projection = rl.CameraProjection.ORTHOGRAPHIC,
|
|
|
|
// }
|
|
|
|
|
2024-02-22 20:15:29 -08:00
|
|
|
// Setup workspace UI state
|
|
|
|
ui_startup( & workspace.ui, persistent_allocator() )
|
2024-02-10 00:40:53 -08:00
|
|
|
}
|
|
|
|
}
|
2024-01-21 20:38:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// For some reason odin's symbols conflict with native foreign symbols...
|
|
|
|
@export
|
|
|
|
sectr_shutdown :: proc()
|
|
|
|
{
|
2024-02-27 04:50:57 -08:00
|
|
|
if Memory_App.persistent == nil {
|
2024-01-22 00:47:53 -08:00
|
|
|
return
|
|
|
|
}
|
2024-01-29 22:29:48 -08:00
|
|
|
state := get_state()
|
2024-02-08 07:50:36 -08:00
|
|
|
|
|
|
|
// Replay
|
|
|
|
{
|
2024-03-07 12:57:05 -08:00
|
|
|
file_close( Memory_App.replay.active_file )
|
2024-02-08 07:50:36 -08:00
|
|
|
}
|
|
|
|
|
2024-02-13 14:16:39 -08:00
|
|
|
font_provider_shutdown()
|
|
|
|
|
2024-02-08 19:33:53 -08:00
|
|
|
log("Module shutdown complete")
|
2024-01-21 20:38:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@export
|
2024-03-07 12:57:05 -08:00
|
|
|
reload :: proc( persistent_mem, frame_mem, transient_mem, files_buffer_mem : ^VArena, host_logger : ^ Logger )
|
2024-01-21 20:38:02 -08:00
|
|
|
{
|
2024-02-27 04:50:57 -08:00
|
|
|
using Memory_App;
|
2024-01-29 22:29:48 -08:00
|
|
|
|
2024-03-07 12:57:05 -08:00
|
|
|
persistent = persistent_mem
|
|
|
|
frame = frame_mem
|
|
|
|
transient = transient_mem
|
|
|
|
files_buffer = files_buffer_mem
|
2024-02-08 13:05:15 -08:00
|
|
|
|
2024-03-07 12:57:05 -08:00
|
|
|
context.allocator = persistent_allocator()
|
|
|
|
context.temp_allocator = transient_allocator()
|
2024-02-13 15:50:22 -08:00
|
|
|
|
2024-02-13 23:29:08 -08:00
|
|
|
// Procedure Addresses are not preserved on hot-reload. They must be restored for persistent data.
|
|
|
|
// The only way to alleviate this is to either do custom handles to allocators
|
|
|
|
// Or as done below, correct containers using allocators on reload.
|
|
|
|
// Thankfully persistent dynamic allocations are rare, and thus we know exactly which ones they are.
|
|
|
|
|
2024-03-07 15:29:49 -08:00
|
|
|
font_provider_data := & get_state().font_provider_data
|
|
|
|
// font_provide_data.font_cache.hashes.allocator = slab_allocator()
|
|
|
|
// font_provide_data.font_cache.entries.allocator = slab_allocator()
|
2024-02-13 15:50:22 -08:00
|
|
|
|
2024-02-22 20:15:29 -08:00
|
|
|
ui_reload( & get_state().project.workspace.ui, persistent_allocator() )
|
|
|
|
|
2024-02-08 19:33:53 -08:00
|
|
|
log("Module reloaded")
|
2024-02-08 07:50:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(Ed) : This lang really not have a fucking swap?
|
|
|
|
swap :: proc( a, b : ^ $Type ) -> ( ^ Type, ^ Type ) {
|
|
|
|
return b, a
|
2024-01-21 20:38:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@export
|
2024-03-02 07:24:09 -08:00
|
|
|
tick :: proc( delta_time : f64, delta_ns : Duration ) -> b32
|
2024-01-21 20:38:02 -08:00
|
|
|
{
|
2024-03-07 12:57:05 -08:00
|
|
|
context.allocator = frame_allocator()
|
|
|
|
context.temp_allocator = transient_allocator()
|
2024-02-13 15:50:22 -08:00
|
|
|
|
2024-03-02 07:24:09 -08:00
|
|
|
get_state().frametime_delta_ns = delta_ns
|
|
|
|
|
2024-02-10 00:40:53 -08:00
|
|
|
result := update( delta_time )
|
|
|
|
render()
|
|
|
|
return result
|
2024-01-21 20:38:02 -08:00
|
|
|
}
|
2024-01-29 22:29:48 -08:00
|
|
|
|
|
|
|
@export
|
2024-03-07 12:57:05 -08:00
|
|
|
clean_frame :: proc() {
|
|
|
|
free_all( frame_allocator() )
|
2024-01-29 22:29:48 -08:00
|
|
|
}
|