-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from jakubtomsu/helpers
Add Odin helpers
- Loading branch information
Showing
4 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |