Skip to content

Commit f3cf190

Browse files
Move assets/backgrounds to a global location and add a migration path.
The version-specific background is copied over whenever a version change is detected. This also made it trivial to fix #1841 as well. progress towards #1791
1 parent f8e56f1 commit f3cf190

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed

src/main.zig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,17 @@ pub fn main() void { // MARK: main()
581581
files.init();
582582
defer files.deinit();
583583

584+
// Background image migration, should be removed after version 0 (#480)
585+
if(files.cwd().hasDir("assets/backgrounds")) moveBlueprints: {
586+
std.fs.rename(std.fs.cwd(), "assets/backgrounds", files.cubyzDir().dir, "backgrounds") catch |err| {
587+
const notification = std.fmt.allocPrint(stackAllocator.allocator, "Encountered error while moving backgrounds: {s}\nYou may have to move your assets/backgrounds manually to {s}/backgrounds", .{@errorName(err), files.cubyzDirStr()}) catch unreachable;
588+
defer stackAllocator.free(notification);
589+
gui.windowlist.notification.raiseNotification(notification);
590+
break :moveBlueprints;
591+
};
592+
std.log.info("Moved backgrounds to {s}/backgrounds", .{files.cubyzDirStr()});
593+
}
594+
584595
settings.init();
585596
defer settings.deinit();
586597

src/renderer.zig

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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)});

src/settings.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub const entityLookback: i16 = 100;
1313

1414
pub const highestSupportedLod: u3 = 5;
1515

16+
pub var lastVersionString: []const u8 = "";
17+
1618
pub var simulationDistance: u16 = 4;
1719

1820
pub var cpuThreads: ?u64 = null;
@@ -130,6 +132,10 @@ pub fn save() void {
130132
defer zonObject.deinit(main.stackAllocator);
131133

132134
inline for(@typeInfo(@This()).@"struct".decls) |decl| {
135+
if(comptime std.mem.eql(u8, decl.name, "lastVersionString")) {
136+
zonObject.put(decl.name, version.version);
137+
continue;
138+
}
133139
const is_const = @typeInfo(@TypeOf(&@field(@This(), decl.name))).pointer.is_const; // Sadly there is no direct way to check if a declaration is const.
134140
if(!is_const) {
135141
const declType = @TypeOf(@field(@This(), decl.name));

0 commit comments

Comments
 (0)