Skip to content

Commit 2cede81

Browse files
author
Jay
committed
Allow set_image and set_background_image at runtime on elements
1 parent 1b2efd7 commit 2cede81

File tree

3 files changed

+122
-17
lines changed

3 files changed

+122
-17
lines changed

build.zig.zon

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.{
22
.name = .engine,
3-
.version = "0.6.3",
3+
.version = "0.7.0",
44
.fingerprint = 0xe8a81a8d0aa558d5,
55
.minimum_zig_version = "0.15.2",
66
.dependencies = .{
@@ -17,8 +17,8 @@
1717
.hash = "zigimg-0.1.0-8_eo2vBrFQBhsrLpexdcDQY-zrlzkyFZfKuYX-Nry6KN",
1818
},
1919
.resources = .{
20-
.url = "git+https://github.com/loftafi/resources.git#813d0deff77ff0b6c72fbd0da29412e7cee6837e",
21-
.hash = "resources-0.5.0-J0GwI5SaAQD7uRJxMaI5j6mykCnTjTh9kiuMIzgkTUF1",
20+
.url = "git+https://github.com/loftafi/resources.git#2d842bc2e48e897123bacdc1d11c1b7f47f75416",
21+
.hash = "resources-0.5.1-J0GwI9WaAQCMBhfbQky1jRbG6JTSLhMHusRix33fgLy1",
2222
},
2323
},
2424
.paths = .{

src/engine.zig

Lines changed: 117 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,10 @@ pub const Element = struct {
607607
tint_texture(texture, display.theme.failed_button_colour);
608608
return;
609609
},
610+
.custom => {
611+
tint_texture(texture, self.background.colour);
612+
return;
613+
},
610614
else => {
611615
// Otherwise apply toggle colurs if needed
612616
},
@@ -766,6 +770,52 @@ pub const Element = struct {
766770
}
767771
}
768772

773+
pub inline fn set_image(
774+
self: *Element,
775+
gpa: Allocator,
776+
display: *Display,
777+
repository: *Resources,
778+
name: []const u8,
779+
) (Allocator.Error || Resources.Error || engine.Error)!?*TextureInfo {
780+
const start = std.time.milliTimestamp();
781+
const texture = try display.load_texture_resource_from_repo(gpa, repository, name);
782+
if (texture == null) {
783+
info("set_image failed to find image resource named \"{s}\"", .{name});
784+
return null;
785+
}
786+
const end = std.time.milliTimestamp();
787+
debug("set_image loaded image named \"{s}\" in {d}ms", .{ name, end - start });
788+
789+
if (self.texture != null) {
790+
display.release_texture_resource(gpa, self.texture.?);
791+
self.texture = null;
792+
}
793+
self.texture = texture.?;
794+
return texture;
795+
}
796+
797+
pub inline fn set_background_image(
798+
self: *Element,
799+
gpa: Allocator,
800+
display: *Display,
801+
repository: *Resources,
802+
name: []const u8,
803+
) (Allocator.Error || Resources.Error || engine.Error)!?*TextureInfo {
804+
const texture = try display.load_texture_resource_from_repo(gpa, repository, name);
805+
if (texture == null) {
806+
info("set_image failed to find image resource named \"{s}\"", .{name});
807+
return null;
808+
}
809+
debug("set_background_image loaded image named \"{s}\"", .{name});
810+
811+
if (self.background.image != null) {
812+
display.release_texture_resource(gpa, self.background.image.?);
813+
self.background.image = null;
814+
}
815+
self.background.image = texture.?;
816+
return texture;
817+
}
818+
769819
/// set_text updates the `text` and `translation` fields of labels,
770820
/// checkboxes and buttons, and regenerates the grahpics/image
771821
/// textures for each word if the text was changed or `forced`
@@ -1484,6 +1534,7 @@ pub const Element = struct {
14841534
_: ?Clip,
14851535
scroll_offset: Vector,
14861536
) void {
1537+
//debug("ds {s} {d}x{d}", .{ element.name, element.rect.width, element.rect.height });
14871538
if (element.texture) |texture| {
14881539
var dest: Rect = .{
14891540
.x = element.rect.x + element.pad.left,
@@ -1622,12 +1673,12 @@ pub const Element = struct {
16221673
dest.y += dest.height;
16231674
dest.height = 0 - dest.height;
16241675
}
1676+
element.apply_background_tint(display, background_image);
16251677
if (element.background.image_corner_radius == 0) {
16261678
_ = sdl.SDL_RenderTexture(display.renderer, background_image, null, @ptrCast(&dest));
16271679
} else {
16281680
var corner: f32 = element.background.corner_radius;
16291681
if (corner * 2 > dest.height) corner = dest.height / 2;
1630-
element.apply_background_tint(display, background_image);
16311682
_ = sdl.SDL_RenderTexture9Grid(
16321683
display.renderer,
16331684
background_image,
@@ -1683,7 +1734,19 @@ pub const Element = struct {
16831734
dest.height = 0 - dest.height;
16841735
}
16851736
if (element.type.button.style != .custom) {
1686-
_ = sdl.SDL_SetTextureColorMod(icon_image, tint.r, tint.g, tint.b);
1737+
_ = sdl.SDL_SetTextureColorMod(
1738+
icon_image,
1739+
tint.r,
1740+
tint.g,
1741+
tint.b,
1742+
);
1743+
} else {
1744+
_ = sdl.SDL_SetTextureColorMod(
1745+
icon_image,
1746+
element.colour.r,
1747+
element.colour.g,
1748+
element.colour.b,
1749+
);
16871750
}
16881751
_ = sdl.SDL_RenderTexture(display.renderer, icon_image, null, @ptrCast(&dest));
16891752
}
@@ -2576,7 +2639,7 @@ pub const Display = struct {
25762639
debug("Checking for desktop icon", .{});
25772640
if (try display.resources.lookupOne("desktop icon", .image, gpa)) |resource| {
25782641
var surface: SurfaceInfo = undefined;
2579-
try display.make_surface_from_resource(resource, gpa, &surface);
2642+
try display.make_surface_from_resource(display.resources, resource, gpa, &surface);
25802643
defer surface.deinit(gpa);
25812644
if (!sdl.SDL_SetWindowIcon(window, surface.surface)) {
25822645
info("Did not set desktop icon", .{});
@@ -3498,10 +3561,56 @@ pub const Display = struct {
34983561
return null;
34993562
}
35003563
var si: SurfaceInfo = undefined;
3501-
try self.make_surface_from_resource(resource.?, allocator, &si);
3564+
try self.make_surface_from_resource(self.resources, resource.?, allocator, &si);
35023565
defer si.deinit(allocator);
35033566

3567+
const start = std.time.milliTimestamp();
35043568
const texture = sdl.SDL_CreateTextureFromSurface(self.renderer, si.surface);
3569+
const end = std.time.milliTimestamp();
3570+
debug("sdl create texture in {d}ms", .{end - start});
3571+
3572+
const ti = try TextureInfo.create(allocator, name, texture);
3573+
ti.references += 1;
3574+
try self.textures.put(allocator, ti.name, ti);
3575+
return ti;
3576+
}
3577+
3578+
/// Load an image from the resource bundle or resource directory.
3579+
pub fn load_texture_resource_from_repo(
3580+
self: *Display,
3581+
allocator: Allocator,
3582+
bucket: *Resources,
3583+
name: []const u8,
3584+
) (Error || Allocator.Error || Resources.Error)!?*TextureInfo {
3585+
if (name.len == 0) {
3586+
return null;
3587+
}
3588+
3589+
if (self.textures.get(name)) |texture| {
3590+
texture.references += 1;
3591+
return texture;
3592+
}
3593+
3594+
var start = std.time.milliTimestamp();
3595+
const resource = try bucket.lookupOne(name, .image, allocator);
3596+
if (resource == null) {
3597+
return null;
3598+
}
3599+
var end = std.time.milliTimestamp();
3600+
debug("search image named \"{s}\" in {d}ms", .{ name, end - start });
3601+
start = end;
3602+
3603+
var si: SurfaceInfo = undefined;
3604+
try self.make_surface_from_resource(bucket, resource.?, allocator, &si);
3605+
defer si.deinit(allocator);
3606+
end = std.time.milliTimestamp();
3607+
debug("made surface named \"{s}\" in {d}ms", .{ name, end - start });
3608+
3609+
start = std.time.milliTimestamp();
3610+
const texture = sdl.SDL_CreateTextureFromSurface(self.renderer, si.surface);
3611+
end = std.time.milliTimestamp();
3612+
debug("sdl create texture in {d}ms", .{end - start});
3613+
35053614
const ti = try TextureInfo.create(allocator, name, texture);
35063615
ti.references += 1;
35073616
try self.textures.put(allocator, ti.name, ti);
@@ -3520,13 +3629,14 @@ pub const Display = struct {
35203629
}
35213630
};
35223631

3523-
pub fn make_surface_from_resource(
3524-
self: *Display,
3632+
fn make_surface_from_resource(
3633+
_: *Display,
3634+
bucket: *Resources,
35253635
resource: *Resource,
35263636
allocator: Allocator,
35273637
si: *SurfaceInfo,
35283638
) (Error || Allocator.Error)!void {
3529-
si.buffer = try sdl_load_resource(self.resources, resource, allocator);
3639+
si.buffer = try sdl_load_resource(bucket, resource, allocator);
35303640
errdefer allocator.free(si.buffer);
35313641
si.img = zigimg.Image.fromMemory(allocator, si.buffer[0..]) catch |e| {
35323642
if (e == error.OutOfMemory) return error.OutOfMemory;

src/read_write.zig

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,15 @@ pub fn init_resource_loader(
99
allocator: Allocator,
1010
bundle_filename: []const u8,
1111
dev_repo: []const u8,
12-
) error{
13-
OutOfMemory,
12+
) (Allocator.Error || Resources.Error || engine.Error || error{
1413
ResourceReadError,
1514
NoResources,
16-
ReadMetadataFailed,
17-
ReadRepoFileFailed,
18-
InvalidResourceUID,
19-
MetadataMissing,
2015
Utf8ExpectedContinuation,
2116
Utf8OverlongEncoding,
2217
Utf8EncodesSurrogateHalf,
2318
Utf8CodepointTooLarge,
2419
Utf8InvalidStartByte,
25-
}!*Resources {
20+
})!*Resources {
2621
const start = std.time.milliTimestamp();
2722
var resources = try Resources.create(allocator);
2823
errdefer resources.destroy();

0 commit comments

Comments
 (0)