@@ -83,9 +83,7 @@ pub fn init() void {
8383 worldFrameBuffer .updateSize (Window .width , Window .height , c .GL_RGB16F );
8484 Bloom .init ();
8585 MeshSelection .init ();
86- MenuBackGround .init () catch | err | {
87- std .log .err ("Failed to initialize the Menu Background: {s}" , .{@errorName (err )});
88- };
86+ MenuBackGround .init ();
8987 Skybox .init ();
9088 chunk_meshing .init ();
9189 mesh_storage .init ();
@@ -482,7 +480,7 @@ pub const MenuBackGround = struct {
482480 var angle : f32 = 0 ;
483481 var lastTime : i128 = undefined ;
484482
485- fn init () ! void {
483+ fn init () void {
486484 lastTime = std .time .nanoTimestamp ();
487485 pipeline = graphics .Pipeline .init (
488486 "assets/cubyz/shaders/background/vertex.vert" ,
@@ -530,9 +528,27 @@ pub const MenuBackGround = struct {
530528 c .glBindBuffer (c .GL_ELEMENT_ARRAY_BUFFER , vbos [1 ]);
531529 c .glBufferData (c .GL_ELEMENT_ARRAY_BUFFER , @intCast (indices .len * @sizeOf (c_int )), & indices , c .GL_STATIC_DRAW );
532530
533- // Load a random texture from the backgrounds folder. The player may make their own pictures which can be chosen as well.
534- texture = .{.textureID = 0 };
535- var dir = try main .files .cwd ().openIterableDir ("assets/backgrounds" );
531+ const backgroundPath = chooseBackgroundImagePath (main .stackAllocator ) catch | err | {
532+ std .log .err ("Couldn't open background path: {s}" , .{@errorName (err )});
533+ texture = .{.textureID = 0 };
534+ return ;
535+ };
536+ defer main .stackAllocator .free (backgroundPath );
537+ texture = graphics .Texture .initFromFile (backgroundPath );
538+ }
539+
540+ fn chooseBackgroundImagePath (allocator : main.heap.NeverFailingAllocator ) ! []const u8 {
541+ // Whenever the version changes copy over the new background image and display it.
542+ if (! std .mem .eql (u8 , settings .lastVersionString , settings .version .version )) {
543+ const defaultImageData = try main .files .cwd ().read (main .stackAllocator , "assets/cubyz/default_background.png" );
544+ defer main .stackAllocator .free (defaultImageData );
545+ try main .files .cubyzDir ().write ("backgrounds/default_background.png" , defaultImageData );
546+
547+ return std .fmt .allocPrint (allocator .allocator , "{s}/backgrounds/default_background.png" , .{main .files .cubyzDirStr ()}) catch unreachable ;
548+ }
549+
550+ // Otherwise load a random texture from the backgrounds folder. The player may make their own pictures which can be chosen as well.
551+ var dir = try main .files .cubyzDir ().openIterableDir ("backgrounds" );
536552 defer dir .close ();
537553
538554 var walker = dir .walk (main .stackAllocator );
@@ -551,13 +567,10 @@ pub const MenuBackGround = struct {
551567 }
552568 }
553569 if (fileList .items .len == 0 ) {
554- std .log .warn ("Couldn't find any background scene images in \" assets/backgrounds\" ." , .{});
555- return ;
570+ return error .NoBackgroundImagesFound ;
556571 }
557572 const theChosenOne = main .random .nextIntBounded (u32 , & main .seed , @as (u32 , @intCast (fileList .items .len )));
558- const theChosenPath = std .fmt .allocPrint (main .stackAllocator .allocator , "assets/backgrounds/{s}" , .{fileList .items [theChosenOne ]}) catch unreachable ;
559- defer main .stackAllocator .free (theChosenPath );
560- texture = graphics .Texture .initFromFile (theChosenPath );
573+ return std .fmt .allocPrint (allocator .allocator , "{s}/backgrounds/{s}" , .{main .files .cubyzDirStr (), fileList .items [theChosenOne ]}) catch unreachable ;
561574 }
562575
563576 pub fn deinit () void {
@@ -643,7 +656,7 @@ pub const MenuBackGround = struct {
643656 }
644657 c .glBindFramebuffer (c .GL_FRAMEBUFFER , 0 );
645658
646- const fileName = std .fmt .allocPrint (main .stackAllocator .allocator , "assets /backgrounds/{s}_{}.png" , .{game .world .? .name , game .world .? .gameTime .load (.monotonic )}) catch unreachable ;
659+ const fileName = std .fmt .allocPrint (main .stackAllocator .allocator , "{s} /backgrounds/{s}_{}.png" , .{main . files . cubyzDirStr (), game .world .? .name , game .world .? .gameTime .load (.monotonic )}) catch unreachable ;
647660 defer main .stackAllocator .free (fileName );
648661 image .exportToFile (fileName ) catch | err | {
649662 std .log .err ("Cannot write file {s} due to {s}" , .{fileName , @errorName (err )});
0 commit comments