Created
February 10, 2025 16:50
-
-
Save kzerot/3ce261850ad676f47bf86bb56d844f5e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package main | |
import "base:runtime" | |
import "core:os" | |
import "core:mem" | |
import "core:log" | |
import sdl "vendor:sdl3" | |
g_ctx: runtime.Context | |
FAILURE :: sdl.AppResult.FAILURE | |
CONT :: sdl.AppResult.CONTINUE | |
SUCCESS :: sdl.AppResult.SUCCESS | |
AppState::struct { | |
window: ^sdl.Window, | |
renderer: ^sdl.Renderer, | |
test_counter: i64 | |
} | |
reset_tracking_allocator :: proc() -> bool { | |
a := cast(^mem.Tracking_Allocator)context.allocator.data | |
err := false | |
if len(a.allocation_map) > 0 { | |
log.warnf("Leaked allocation count: %v", len(a.allocation_map)) | |
} | |
for _, v in a.allocation_map { | |
log.warnf("%v: Leaked %v bytes", v.location, v.size) | |
err = true | |
} | |
if !err { | |
log.debug("No leaks detected") | |
} | |
mem.tracking_allocator_clear(a) | |
return err | |
} | |
main :: proc() { | |
// Debug things | |
cl := log.create_console_logger() | |
context.logger = cl | |
log.debug("Hello debugging") | |
tracking_allocator: mem.Tracking_Allocator | |
mem.tracking_allocator_init(&tracking_allocator, context.allocator) | |
context.allocator = mem.tracking_allocator(&tracking_allocator) | |
g_ctx = context | |
log.debug("Starting our sdl thingy") | |
exit_code := sdl.EnterAppMainCallbacks(cast(i32)len(runtime.args__), | |
raw_data(runtime.args__), | |
AppInit, AppIterate, AppEvent, AppQuit) | |
reset_tracking_allocator() | |
os.exit(int(exit_code)) | |
} | |
AppInit: sdl.AppInit_func : proc "c" (appstate: ^rawptr, argc: i32, argv: [^]cstring) -> sdl.AppResult { | |
if !sdl.SetAppMetadata("Hello", "1.0", "there") { | |
return FAILURE | |
} | |
if (!sdl.Init({sdl.InitFlag.VIDEO})) { | |
return FAILURE; | |
} | |
context = g_ctx | |
state_ptr : ^AppState = (^AppState)(sdl.calloc(1, size_of(AppState))) | |
// state.test_counter = 0.0 | |
appstate^ = state_ptr | |
state_ptr.test_counter = 0 | |
if (!sdl.CreateWindowAndRenderer("Example", 800, 600, {sdl.WindowFlag.RESIZABLE}, &state_ptr.window, &state_ptr.renderer)) { | |
return FAILURE | |
} | |
return CONT | |
} | |
AppIterate :sdl.AppIterate_func : proc "c" (appstate: rawptr) -> sdl.AppResult { | |
context = g_ctx | |
as : ^AppState = (^AppState)(appstate) | |
as.test_counter += 1 | |
//log.debugf("Counter: {}", as.test_counter) | |
sdl.RenderClear(as.renderer); | |
sdl.RenderPresent(as.renderer); | |
return CONT | |
} | |
AppEvent : sdl.AppEvent_func : proc "c" (appstate: rawptr, event: ^sdl.Event) -> sdl.AppResult { | |
context = g_ctx | |
as : ^AppState = (^AppState)(appstate) | |
#partial switch (event.type) { | |
case sdl.EventType.QUIT: | |
log.debug("Exiting") | |
log.debugf("Counter at exit: {}!", as.test_counter) | |
return SUCCESS | |
} | |
return CONT | |
} | |
AppQuit : sdl.AppQuit_func : proc "c" (appstate: rawptr, result: sdl.AppResult) { | |
os.exit(0) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment