@@ -1517,6 +1517,8 @@ pub const Element = struct {
15171517 dest .x += scroll_offset .x ;
15181518 dest .y += scroll_offset .y ;
15191519
1520+ if (dest .height <= 0 or dest .width <= 0 ) return ;
1521+
15201522 // TODO: Sprites might have frames
15211523 const source : sdl.SDL_FRect = .{
15221524 .x = 0 ,
@@ -2456,10 +2458,10 @@ pub const Display = struct {
24562458 on_resized : ? * const fn (display : * Display , element : * Element ) bool = null ,
24572459 event_hook : ? * const fn (display : * Display , e : u32 ) error {OutOfMemory }! void = null ,
24582460
2459- pub fn create (allocator : Allocator , app_name : [:0 ]const u8 , app_version : [:0 ]const u8 , app_id : [:0 ]const u8 , dev_resource_folder : []const u8 , translation_filename : []const u8 , gui_flags : usize ) ! * Display {
2460- var display = try allocator .create (Display );
2461- errdefer allocator .destroy (display );
2462- display .allocator = allocator ;
2461+ pub fn create (gpa : Allocator , app_name : [:0 ]const u8 , app_version : [:0 ]const u8 , app_id : [:0 ]const u8 , dev_resource_folder : []const u8 , translation_filename : []const u8 , gui_flags : usize ) ! * Display {
2462+ var display = try gpa .create (Display );
2463+ errdefer gpa .destroy (display );
2464+ display .allocator = gpa ;
24632465 display .hovered = null ;
24642466 display .selected = null ;
24652467 display .keyboard_selected = false ;
@@ -2513,11 +2515,11 @@ pub const Display = struct {
25132515 }
25142516
25152517 debug ("Initialising resource loader" , .{});
2516- display .resources = try init_resource_loader (allocator , engine .RESOURCE_BUNDLE_FILENAME , dev_resource_folder );
2517- if (try display .resources .lookupOne (translation_filename , .csv )) | resource | {
2518- const data = try sdl_load_resource (display .resources , resource , allocator );
2519- defer allocator .free (data );
2520- try display .translation .load_translation_data (allocator , data );
2518+ display .resources = try init_resource_loader (gpa , engine .RESOURCE_BUNDLE_FILENAME , dev_resource_folder );
2519+ if (try display .resources .lookupOne (translation_filename , .csv , gpa )) | resource | {
2520+ const data = try sdl_load_resource (display .resources , resource , gpa );
2521+ defer gpa .free (data );
2522+ try display .translation .load_translation_data (gpa , data );
25212523 debug ("Translation file loaded" , .{});
25222524 } else {
25232525 err ("No translation file found." , .{});
@@ -2538,13 +2540,23 @@ pub const Display = struct {
25382540 return error .graphics_renderer_failed ;
25392541 };
25402542
2541- debug ("Renderers: {}" , .{driver_formatter (renderer )});
2543+ const current_driver = sdl .SDL_GetRendererName (renderer ).? ;
2544+ const count = sdl .SDL_GetNumRenderDrivers ();
2545+ var i : c_int = 0 ;
2546+ while (i < count ) : (i += 1 ) {
2547+ const driver = sdl .SDL_GetRenderDriver (i ).? ;
2548+ if (std .mem .orderZ (u8 , current_driver , driver ) == .eq ) {
2549+ debug ("Renderer: {s} (selected)" , .{std .mem .span (driver )});
2550+ } else {
2551+ debug ("Renderers: {s}" , .{std .mem .span (driver )});
2552+ }
2553+ }
25422554
25432555 debug ("Checking for desktop icon" , .{});
2544- if (try display .resources .lookupOne ("desktop icon" , .image )) | resource | {
2556+ if (try display .resources .lookupOne ("desktop icon" , .image , gpa )) | resource | {
25452557 var surface : SurfaceInfo = undefined ;
2546- try display .make_surface_from_resource (resource , allocator , & surface );
2547- defer surface .deinit (allocator );
2558+ try display .make_surface_from_resource (resource , gpa , & surface );
2559+ defer surface .deinit (gpa );
25482560 if (! sdl .SDL_SetWindowIcon (window , surface .surface )) {
25492561 info ("Did not set desktop icon" , .{});
25502562 } else {
@@ -2580,21 +2592,21 @@ pub const Display = struct {
25802592 display .scale = display .pixel_scale / display .user_scale ;
25812593
25822594 // App can accept these keybindings or replace them
2583- try display .keybindings .put (allocator , sdl .SDLK_D , toggle_dev_mode );
2584- try display .keybindings .put (allocator , sdl .SDLK_K , use_next_theme );
2585- try display .keybindings .put (allocator , sdl .SDLK_X , increase_content_size );
2586- try display .keybindings .put (allocator , sdl .SDLK_PLUS , increase_content_size );
2587- try display .keybindings .put (allocator , sdl .SDLK_EQUALS , increase_content_size );
2588- try display .keybindings .put (allocator , sdl .SDLK_MINUS , decrease_content_size );
2589- try display .keybindings .put (allocator , sdl .SDLK_KP_PLUS , increase_content_size );
2590- try display .keybindings .put (allocator , sdl .SDLK_KP_MINUS , decrease_content_size );
2595+ try display .keybindings .put (gpa , sdl .SDLK_D , toggle_dev_mode );
2596+ try display .keybindings .put (gpa , sdl .SDLK_K , use_next_theme );
2597+ try display .keybindings .put (gpa , sdl .SDLK_X , increase_content_size );
2598+ try display .keybindings .put (gpa , sdl .SDLK_PLUS , increase_content_size );
2599+ try display .keybindings .put (gpa , sdl .SDLK_EQUALS , increase_content_size );
2600+ try display .keybindings .put (gpa , sdl .SDLK_MINUS , decrease_content_size );
2601+ try display .keybindings .put (gpa , sdl .SDLK_KP_PLUS , increase_content_size );
2602+ try display .keybindings .put (gpa , sdl .SDLK_KP_MINUS , decrease_content_size );
25912603 if (engine .dev_build ) {
2592- try display .keybindings .put (allocator , sdl .SDLK_B , make_bundle );
2604+ try display .keybindings .put (gpa , sdl .SDLK_B , make_bundle );
25932605 }
25942606
25952607 display .themes = .empty ;
25962608 for (& default_themes ) | * theme | {
2597- try display .themes .append (allocator , theme .* );
2609+ try display .themes .append (gpa , theme .* );
25982610 }
25992611 display .update_system_theme ();
26002612
@@ -2629,16 +2641,16 @@ pub const Display = struct {
26292641 }
26302642
26312643 /// Cleanup memory assocaited with this display.
2632- pub fn destroy (self : * Display , allocator : Allocator ) void {
2644+ pub fn destroy (self : * Display , gpa : Allocator ) void {
26332645 trace ("Engine cleanup" , .{});
26342646
2635- self .root .deinit (self , allocator );
2636- self .themes .deinit (allocator );
2647+ self .root .deinit (self , gpa );
2648+ self .themes .deinit (gpa );
26372649
26382650 for (self .fonts .items ) | item | {
2639- item .destroy (allocator );
2651+ item .destroy (gpa );
26402652 }
2641- self .fonts .deinit (allocator );
2653+ self .fonts .deinit (gpa );
26422654
26432655 var i = self .textures .iterator ();
26442656 while (i .next ()) | x | {
@@ -2648,23 +2660,23 @@ pub const Display = struct {
26482660 x .value_ptr .* .references ,
26492661 });
26502662 }
2651- x .value_ptr .* .destroy (allocator );
2663+ x .value_ptr .* .destroy (gpa );
26522664 }
2653- self .textures .deinit (allocator );
2665+ self .textures .deinit (gpa );
26542666 self .resources .destroy ();
26552667 for (self .animators .items ) | animator | {
2656- allocator .destroy (animator );
2668+ gpa .destroy (animator );
26572669 }
2658- self .animators .deinit (allocator );
2670+ self .animators .deinit (gpa );
26592671
26602672 sdl .SDL_DestroyRenderer (self .renderer );
26612673 sdl .SDL_DestroyWindow (self .window );
26622674 sdl .TTF_Quit ();
26632675 sdl .SDL_Quit ();
26642676
2665- self .keybindings .deinit (allocator );
2666- self .translation .deinit (allocator );
2667- allocator .destroy (self );
2677+ self .keybindings .deinit (gpa );
2678+ self .translation .deinit (gpa );
2679+ gpa .destroy (self );
26682680 }
26692681
26702682 /// Check that a theme name is a valid theme name. Return a stack
@@ -2885,7 +2897,7 @@ pub const Display = struct {
28852897 std .debug .assert (parent .type == .panel );
28862898 //debug("relayout: {d}x{d}", .{ parent.width, parent.rect.height });
28872899
2888- var expanders = std . BoundedArray (* Element , 10 ){};
2900+ var expanders = BoundedArray (* Element , 10 ){};
28892901 var expander_weights : f32 = 0 ;
28902902
28912903 // Make sure this element never exceeds its maximum.
@@ -3028,7 +3040,7 @@ pub const Display = struct {
30283040 inline fn relayout_top_to_bottom (
30293041 _ : * Display ,
30303042 parent : * Element ,
3031- expanders : * std . BoundedArray (* Element , 10 ),
3043+ expanders : * BoundedArray (* Element , 10 ),
30323044 expander_weights : f32 ,
30333045 ) void {
30343046 // Layout each item from top to bottom, initially ignoring
@@ -3151,7 +3163,7 @@ pub const Display = struct {
31513163 inline fn relayout_left_to_right (
31523164 _ : * Display ,
31533165 parent : * Element ,
3154- _ : * std . BoundedArray (* Element , 10 ),
3166+ _ : * BoundedArray (* Element , 10 ),
31553167 _ : f32 ,
31563168 ) void {
31573169 // Draw panel children from top left corner of the panel
@@ -3318,7 +3330,7 @@ pub const Display = struct {
33183330 allocator : Allocator ,
33193331 name : []const u8 ,
33203332 ) (error { OutOfMemory , UnknownImageFormat , ResourceNotFound , ResourceReadError } || ResourcesError )! * FontInfo {
3321- const resource = try self .resources .lookupOne (name , .font );
3333+ const resource = try self .resources .lookupOne (name , .font , allocator );
33223334 if (resource == null ) {
33233335 err ("load_font({s}) Font not in resource folder" , .{name });
33243336 return error .ResourceNotFound ;
@@ -3451,7 +3463,7 @@ pub const Display = struct {
34513463 return texture ;
34523464 }
34533465
3454- const resource = try self .resources .lookupOne (name , .image );
3466+ const resource = try self .resources .lookupOne (name , .image , allocator );
34553467 if (resource == null ) {
34563468 return null ;
34573469 }
@@ -3473,7 +3485,7 @@ pub const Display = struct {
34733485
34743486 pub fn deinit (si : * @This (), gpa : Allocator ) void {
34753487 gpa .free (si .buffer );
3476- si .img .deinit ();
3488+ si .img .deinit (gpa );
34773489 sdl .SDL_DestroySurface (si .surface );
34783490 }
34793491 };
@@ -3490,7 +3502,7 @@ pub const Display = struct {
34903502 if (e == error .OutOfMemory ) return error .OutOfMemory ;
34913503 return error .UnknownImageFormat ;
34923504 };
3493- errdefer si .img .deinit ();
3505+ errdefer si .img .deinit (allocator );
34943506
34953507 var row_size : c_int = 0 ;
34963508 var sdl_format : sdl.SDL_PixelFormat = sdl .SDL_PIXELFORMAT_UNKNOWN ;
@@ -4336,7 +4348,7 @@ pub const Display = struct {
43364348 //};
43374349
43384350 const base_folder = "/tmp/" ;
4339- var buffer = std . BoundedArray (u8 , 1000 ){};
4351+ var buffer = BoundedArray (u8 , 1000 ){};
43404352 buffer .appendSlice (base_folder ) catch {
43414353 return std .mem .Allocator .Error .OutOfMemory ;
43424354 };
@@ -5100,31 +5112,6 @@ inline fn c_unicode_to_u21(text: [*c]const u8) u21 {
51005112 } catch text [0 ];
51015113}
51025114
5103- /// Return a formatter that outputs the list of available renderers, and
5104- /// marking the current renderer that is in use.
5105- fn driver_formatter (renderer : * sdl.SDL_Renderer ) std.fmt.Formatter (list_drivers ) {
5106- return .{ .data = .{ .renderer = renderer } };
5107- }
5108-
5109- fn list_drivers (
5110- context : struct { renderer : * sdl .SDL_Renderer },
5111- comptime _ : []const u8 ,
5112- _ : std .fmt .FormatOptions ,
5113- writer : anytype ,
5114- ) ! void {
5115- const current_driver = sdl .SDL_GetRendererName (context .renderer ).? ;
5116- const count = sdl .SDL_GetNumRenderDrivers ();
5117- var i : c_int = 0 ;
5118- while (i < count ) : (i += 1 ) {
5119- const driver = sdl .SDL_GetRenderDriver (i ).? ;
5120- if (i != 0 ) try writer .writeAll (", " );
5121- try writer .writeAll (std .mem .span (driver ));
5122- if (std .mem .orderZ (u8 , current_driver , driver ) == .eq ) {
5123- try writer .writeAll (" (selected)" );
5124- }
5125- }
5126- }
5127-
51285115/// SDL provides extra information that is sometimes helpful for debugging, lets print this
51295116/// information when we are in debug mode.
51305117///
@@ -5135,7 +5122,7 @@ fn sdl_log_callback(
51355122 category : c_int ,
51365123 priority : sdl.SDL_LogPriority ,
51375124 message : [* c ]const u8 ,
5138- ) callconv (.C ) void {
5125+ ) callconv (.c ) void {
51395126 std .log .scoped (.term_scope ).debug ("SDL ({s}, {s}) {s}" , .{
51405127 @tagName (SdlLogCategory .fromInt (category )),
51415128 @tagName (SdlLogPriority .fromInt (priority )),
@@ -5215,18 +5202,20 @@ pub fn log_output(
52155202 comptime format : []const u8 ,
52165203 args : anytype ,
52175204) void {
5205+ var buffer : [1024 * 5 ]u8 = undefined ;
52185206 const prefix = "[" ++ comptime level ++ "] " ;
52195207 if (builtin .mode == .Debug ) {
52205208 // Log to terminal in debug mode
52215209 std .debug .lockStdErr ();
52225210 defer std .debug .unlockStdErr ();
5223- const stderr = std .io .getStdErr ().writer ();
5211+ var stderr_writer = std .fs .File .stderr ().writer (& buffer );
5212+ const stderr = & stderr_writer .interface ;
52245213 nosuspend stderr .print (prefix ++ format ++ "\n " , args ) catch return ;
5214+ stderr .flush () catch {};
52255215 } else {
52265216 // Log using SDL when in release mode
52275217 if (scope != .term_scope ) {
5228- var buf : [3000 ]u8 = undefined ;
5229- if (std .fmt .bufPrintZ (& buf , prefix ++ format , args )) | f | {
5218+ if (std .fmt .bufPrintZ (& buffer , prefix ++ format , args )) | f | {
52305219 sdl .SDL_LogInfo (@intFromEnum (SdlLogCategory .application ), f .ptr );
52315220 } else | _ | {
52325221 sdl .SDL_LogInfo (@intFromEnum (SdlLogCategory .application ), prefix ++ format );
@@ -5521,6 +5510,7 @@ pub const Animator = @import("animator.zig");
55215510
55225511const praxis = @import ("praxis" );
55235512const Lang = @import ("praxis" ).Lang ;
5513+ const BoundedArray = praxis .BoundedArray ;
55245514
55255515pub const Chunker = @import ("chunker.zig" ).Chunker ;
55265516pub const Translation = @import ("translation.zig" ).Translation ;
0 commit comments