Skip to content

Commit 1b7ea30

Browse files
author
Jay
committed
Allow translucent panel backgrounds and sprites
1 parent 2b9351a commit 1b7ea30

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.{
22
.name = .engine,
3-
.version = "0.2.3",
3+
.version = "0.2.4",
44
.fingerprint = 0xe8a81a8d0aa558d5,
55
.minimum_zig_version = "0.14.1",
66
.dependencies = .{

src/engine.zig

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ pub const Element = struct {
608608
if (self.type == .panel) {
609609
switch (self.type.panel.style) {
610610
.emphasised => {
611+
_ = sdl.SDL_SetTextureAlphaMod(texture, display.theme.emphasised_panel_colour.a);
611612
_ = sdl.SDL_SetTextureColorMod(
612613
texture,
613614
display.theme.emphasised_panel_colour.r,
@@ -617,6 +618,7 @@ pub const Element = struct {
617618
return;
618619
},
619620
.success => {
621+
_ = sdl.SDL_SetTextureAlphaMod(texture, display.theme.success_panel_colour.a);
620622
_ = sdl.SDL_SetTextureColorMod(
621623
texture,
622624
display.theme.success_panel_colour.r,
@@ -626,6 +628,7 @@ pub const Element = struct {
626628
return;
627629
},
628630
.failed => {
631+
_ = sdl.SDL_SetTextureAlphaMod(texture, display.theme.failed_panel_colour.a);
629632
_ = sdl.SDL_SetTextureColorMod(
630633
texture,
631634
display.theme.failed_panel_colour.r,
@@ -635,6 +638,7 @@ pub const Element = struct {
635638
return;
636639
},
637640
.faded => {
641+
_ = sdl.SDL_SetTextureAlphaMod(texture, display.theme.faded_panel_colour.a);
638642
_ = sdl.SDL_SetTextureColorMod(
639643
texture,
640644
display.theme.faded_panel_colour.r,
@@ -644,6 +648,7 @@ pub const Element = struct {
644648
return;
645649
},
646650
.background => {
651+
_ = sdl.SDL_SetTextureAlphaMod(texture, display.theme.background_colour.a);
647652
_ = sdl.SDL_SetTextureColorMod(
648653
texture,
649654
display.theme.background_colour.r,
@@ -653,6 +658,7 @@ pub const Element = struct {
653658
return;
654659
},
655660
.normal => {
661+
_ = sdl.SDL_SetTextureAlphaMod(texture, display.theme.label_background_colour.a);
656662
_ = sdl.SDL_SetTextureColorMod(
657663
texture,
658664
display.theme.label_background_colour.r,
@@ -669,6 +675,18 @@ pub const Element = struct {
669675
},
670676
}
671677
}
678+
if (self.type == .sprite) {
679+
if (self.background_colour.a != 0) {
680+
_ = sdl.SDL_SetTextureAlphaMod(texture, self.background_colour.a);
681+
_ = sdl.SDL_SetTextureColorMod(
682+
texture,
683+
self.background_colour.r,
684+
self.background_colour.g,
685+
self.background_colour.b,
686+
);
687+
return;
688+
}
689+
}
672690
_ = sdl.SDL_SetTextureColorMod(
673691
texture,
674692
display.theme.label_background_colour.r,
@@ -1177,9 +1195,9 @@ pub const Element = struct {
11771195
// Any element can have a background texture
11781196
if (element.type != .button) {
11791197
if (element.background_texture) |texture| {
1180-
if (element.type != .text_input and element.type != .panel and element.type != .button) {
1181-
info("drawing background for {s}", .{@tagName(element.type)});
1182-
}
1198+
//if (element.type != .text_input and element.type != .panel and element.type != .button) {
1199+
// info("drawing background for {s}", .{@tagName(element.type)});
1200+
//}
11831201
// Elements may optionally have a background texture
11841202
var dest = element.rect.move(&scroll_offset);
11851203
if (element.flip.x) {
@@ -1205,8 +1223,9 @@ pub const Element = struct {
12051223
}
12061224
}
12071225

1208-
// Any element can contain a basic background fill colour
1209-
if (element.type != .rectangle and element.background_colour.a > 0) {
1226+
// Non rectangles and sprites use background_colour to draw an actual
1227+
// background.
1228+
if (element.type != .rectangle and element.type != .sprite and element.background_colour.a > 0) {
12101229
_ = sdl.SDL_SetRenderDrawColor(
12111230
display.renderer,
12121231
element.background_colour.r,
@@ -4723,6 +4742,18 @@ pub fn setup_sprite(
47234742
err("Failed to load sprite texture named \"{s}\"", .{image});
47244743
}
47254744
}
4745+
4746+
if (element.background_texture_name) |image| {
4747+
if (try self.load_texture_resource(allocator, image)) |texture| {
4748+
element.background_texture = texture;
4749+
if (element.rect.width == 0)
4750+
element.rect.width = @floatFromInt(texture.texture.w);
4751+
if (element.rect.height == 0)
4752+
element.rect.height = @floatFromInt(texture.texture.h);
4753+
} else {
4754+
err("Failed to load sprite background texture named \"{s}\"", .{image});
4755+
}
4756+
}
47264757
}
47274758

47284759
pub fn setup_button(

0 commit comments

Comments
 (0)