Skip to content

Commit 6502ffa

Browse files
author
Jay
committed
Allow sprites to "fill" the bounding box (centred)
1 parent ff5dc2d commit 6502ffa

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
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.7.4",
3+
.version = "0.7.5",
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#4b0ad94b68e1d08d805d48f55d1ad7b9ba654574",
21-
.hash = "resources-0.7.0-J0GwI601AgBNv8SUT4U6KMFCxpCsbut72aEo5-RcFvGw",
20+
.url = "git+https://github.com/loftafi/resources.git#0776cc10cdcfa686543c3f92be6cbc2f36384541",
21+
.hash = "resources-0.7.4-J0GwI9E6AgDyFTZKD7THemvJZGHYjueSxU27ZCZ2L1jq",
2222
},
2323
},
2424
.paths = .{

src/engine.zig

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ pub const LayoutSize = enum {
109109
shrinks,
110110
};
111111

112+
pub const Fit = enum {
113+
stretch,
114+
fill,
115+
};
116+
112117
/// An element is ignored when it is hidden. An element is drawn when it is
113118
/// visible. An element is culled when it is visible, but not currently inside
114119
/// thd drawable area on the screen.
@@ -364,6 +369,7 @@ pub const Element = struct {
364369
sprite: struct {
365370
on_click: Callback = .{ .func = null },
366371
update: UpdateCallback = .{ .func = null },
372+
scale: Fit = .stretch,
367373
},
368374
label: struct {
369375
font: *FontInfo = undefined,
@@ -1566,12 +1572,54 @@ pub const Element = struct {
15661572
}
15671573

15681574
// TODO: Sprites might have frames
1569-
const source: sdl.SDL_FRect = .{
1570-
.x = 0,
1571-
.y = 0,
1572-
.w = @as(f32, @floatFromInt(texture.texture.w)),
1573-
.h = @as(f32, @floatFromInt(texture.texture.h)),
1574-
};
1575+
1576+
// Stretch the full image onto the drawing area
1577+
const image_width = @as(f32, @floatFromInt(texture.texture.w));
1578+
const image_height = @as(f32, @floatFromInt(texture.texture.h));
1579+
var source: sdl.SDL_FRect = undefined;
1580+
switch (element.type.sprite.scale) {
1581+
.stretch => {
1582+
source = .{
1583+
.x = 0,
1584+
.y = 0,
1585+
.w = image_width,
1586+
.h = image_height,
1587+
};
1588+
},
1589+
.fill => {
1590+
// We need a slice of the source image that fits the
1591+
// ratio of the destination area.
1592+
const dst_scale: f32 = element.rect.width / element.rect.height;
1593+
const src_scale: f32 = image_width / image_height;
1594+
if (src_scale >= dst_scale) {
1595+
// Slice off some width
1596+
source = .{
1597+
.x = 0,
1598+
.y = 0,
1599+
.h = image_height,
1600+
.w = image_height * dst_scale,
1601+
};
1602+
source.x = (image_width - source.w) / 2;
1603+
} else {
1604+
// Slice off some height
1605+
source = .{
1606+
.x = 0,
1607+
.y = 0,
1608+
.w = image_width,
1609+
.h = image_width / dst_scale,
1610+
};
1611+
source.y = (image_height - source.h) / 2;
1612+
}
1613+
err("img={d}x{d} scale={d} src={d}x{d}", .{
1614+
image_width,
1615+
image_height,
1616+
dst_scale,
1617+
source.w,
1618+
source.h,
1619+
});
1620+
},
1621+
}
1622+
15751623
//const source: Rect = .{
15761624
// .x = 0,
15771625
// .y = 0,
@@ -2612,7 +2660,7 @@ pub const Display = struct {
26122660
app_version: [:0]const u8,
26132661
app_id: [:0]const u8,
26142662
dev_resource_folder: []const u8,
2615-
dev_resource_filter: ?fn (name: []const u8, extension: Resource.Type) bool,
2663+
dev_resource_filter: ?fn (name: []const u8, extension: FileType) bool,
26162664
translation_filename: []const u8,
26172665
gui_flags: usize,
26182666
) (Error || Allocator.Error || Resources.Error || engine.Error || error{ Utf8ExpectedContinuation, Utf8OverlongEncoding, Utf8EncodesSurrogateHalf, Utf8CodepointTooLarge, Utf8InvalidStartByte } || std.fs.Dir.StatError || std.fs.File.StatError || std.fs.File.OpenError)!*Display {
@@ -5885,6 +5933,7 @@ pub const Translation = @import("translation.zig").Translation;
58855933

58865934
const Resources = @import("resources").Resources;
58875935
const Resource = @import("resources").Resource;
5936+
const FileType = @import("resources").FileType;
58885937

58895938
const default_themes = @import("theme.zig").default_themes;
58905939
const Theme = @import("theme.zig").Theme;

src/read_write.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn init_resource_loader(
99
allocator: Allocator,
1010
bundle_filename: []const u8,
1111
dev_repo: []const u8,
12-
dev_repo_filter: ?fn (name: []const u8, extension: Resource.Type) bool,
12+
dev_repo_filter: ?fn (name: []const u8, extension: FileType) bool,
1313
) (Allocator.Error || Resources.Error || engine.Error || std.fs.Dir.StatError || std.fs.File.StatError || std.fs.File.OpenError || error{
1414
ResourceReadError,
1515
Utf8ExpectedContinuation,
@@ -505,7 +505,7 @@ const UniqueWords = @import("resources").UniqueWords;
505505
const Resource = @import("resources").Resource;
506506
const random = @import("resources").random;
507507
const seed = @import("resources").seed;
508-
const ResourceType = @import("resources").ResourceType;
508+
const FileType = @import("resources").FileType;
509509
const engine = @import("engine.zig");
510510
const err = engine.err;
511511
const warn = engine.warn;

0 commit comments

Comments
 (0)