Skip to content

Commit

Permalink
Merge pull request #12 from jakubtomsu/helpers
Browse files Browse the repository at this point in the history
Add Odin helpers
  • Loading branch information
floooh authored Apr 20, 2024
2 parents 4ca585e + 228e90e commit ae38c0e
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 2 deletions.
4 changes: 2 additions & 2 deletions sokol/build_clibs_windows.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set sources=log app gfx glue time audio debugtext shape gl

REM D3D11 Debug
for %%s in (%sources%) do (
cl /c /D_DEBUG /DIMPL /DSOKOL_D3D11 c\sokol_%%s.c
cl /c /D_DEBUG /DIMPL /DSOKOL_D3D11 c\sokol_%%s.c /Z7
lib /OUT:%%s\sokol_%%s_windows_x64_d3d11_debug.lib sokol_%%s.obj
del sokol_%%s.obj
)
Expand All @@ -18,7 +18,7 @@ for %%s in (%sources%) do (

REM GL Debug
for %%s in (%sources%) do (
cl /c /D_DEBUG /DIMPL /DSOKOL_GLCORE33 c\sokol_%%s.c
cl /c /D_DEBUG /DIMPL /DSOKOL_GLCORE33 c\sokol_%%s.c /Z7
lib /OUT:%%s\sokol_%%s_windows_x64_gl_debug.lib sokol_%%s.obj
del sokol_%%s.obj
)
Expand Down
37 changes: 37 additions & 0 deletions sokol/helpers/allocator.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package sokol_helpers

// Use native odin allocators in sokol allocator interface

import sapp "../app"
import sg "../gfx"
import "base:runtime"

Allocator :: struct {
alloc_fn: proc "c" (size: u64, user_data: rawptr) -> rawptr,
free_fn: proc "c" (ptr: rawptr, user_data: rawptr),
user_data: rawptr,
}

// context_ptr: a pointer to a context which persists during the lifetime of the program.
// Note: you can transmute() this into a logger for any specific sokol library.
allocator :: proc(context_ptr: ^runtime.Context) -> Allocator {
return {
alloc_fn = allocator_alloc_proc,
free_fn = allocator_free_proc,
user_data = cast(rawptr)context_ptr,
}
}

allocator_alloc_proc :: proc "c" (size: u64, user_data: rawptr) -> rawptr {
context = (cast(^runtime.Context)user_data)^
bytes, err := runtime.mem_alloc(size = int(size))
if err != nil {
return nil
}
return raw_data(bytes)
}

allocator_free_proc :: proc "c" (ptr: rawptr, user_data: rawptr) {
context = (cast(^runtime.Context)user_data)^
runtime.mem_free(ptr)
}
37 changes: 37 additions & 0 deletions sokol/helpers/glue.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package sokol_helpers

// Alternative native odin implementation of sokol_glue.h, in case you want to minimize C dependencies
// (since sokol_glue is only a few lines of code)

import sapp "../app"
import sg "../gfx"

glue_environment :: proc() -> (env: sg.Environment) {
env.defaults.color_format = cast(sg.Pixel_Format)sapp.color_format()
env.defaults.depth_format = cast(sg.Pixel_Format)sapp.depth_format()
env.defaults.sample_count = sapp.sample_count()
env.metal.device = sapp.metal_get_device()
env.d3d11.device = sapp.d3d11_get_device()
env.d3d11.device_context = sapp.d3d11_get_device_context()
env.wgpu.device = sapp.wgpu_get_device()
return env
}

glue_swapchain :: proc() -> (swapchain: sg.Swapchain) {
swapchain.width = sapp.width()
swapchain.height = sapp.height()
swapchain.sample_count = sapp.sample_count()
swapchain.color_format = cast(sg.Pixel_Format)sapp.color_format()
swapchain.depth_format = cast(sg.Pixel_Format)sapp.depth_format()
swapchain.metal.current_drawable = sapp.metal_get_current_drawable()
swapchain.metal.depth_stencil_texture = sapp.metal_get_depth_stencil_texture()
swapchain.metal.msaa_color_texture = sapp.metal_get_msaa_color_texture()
swapchain.d3d11.render_view = sapp.d3d11_get_render_view()
swapchain.d3d11.resolve_view = sapp.d3d11_get_resolve_view()
swapchain.d3d11.depth_stencil_view = sapp.d3d11_get_depth_stencil_view()
swapchain.wgpu.render_view = sapp.wgpu_get_render_view()
swapchain.wgpu.resolve_view = sapp.wgpu_get_resolve_view()
swapchain.wgpu.depth_stencil_view = sapp.wgpu_get_depth_stencil_view()
swapchain.gl.framebuffer = sapp.gl_get_framebuffer()
return swapchain
}
59 changes: 59 additions & 0 deletions sokol/helpers/logger.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package sokol_helpers

// Pass sokol logs into native odin logging system

import sapp "../app"
import sg "../gfx"
import "base:runtime"
import "core:log"

Logger :: struct {
func: proc "c" (
tag: cstring,
log_level: u32,
log_item: u32,
message: cstring,
line_nr: u32,
filename: cstring,
user_data: rawptr,
),
user_data: rawptr,
}

// context_ptr: a pointer to a context which persists during the lifetime of the program.
// Note: you can transmute() this into a logger for any specific sokol library.
logger :: proc "contextless" (context_ptr: ^runtime.Context) -> Logger {
return {func = logger_proc, user_data = cast(rawptr)context_ptr}
}

logger_proc :: proc "c" (
tag: cstring,
log_level: u32,
log_item: u32,
message: cstring,
line_nr: u32,
filename: cstring,
user_data: rawptr,
) {
context = (cast(^runtime.Context)user_data)^

loc := runtime.Source_Code_Location {
file_path = string(filename),
line = i32(line_nr),
}

level: log.Level
switch log_level {
case 0:
log.panicf("Sokol Panic: (%i) %s: %s", log_item, tag, message, location = loc)

case 1:
level = .Error
case 2:
level = .Warning
case:
level = .Info
}

log.logf(level, "(%i) %s: %s", log_item, tag, message, location = loc)
}

0 comments on commit ae38c0e

Please sign in to comment.