Skip to content

Commit ba4fecf

Browse files
authored
Merge pull request #63 from firefly-zero/tiling
draw_sub_tile and draw_nine_slice
2 parents 98ce3ad + 6431187 commit ba4fecf

3 files changed

Lines changed: 79 additions & 17 deletions

File tree

example/audio/main.mbt

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,5 @@ fn main {
88
pub fn boot() -> Unit {
99
@audio.out.add_file(b"pickupCoin") |> ignore
1010
let gain = @audio.out.add_gain(0.5)
11-
gain.add_sine(@audio.A4, 1) |> ignore
12-
}
13-
14-
///|
15-
/// update is called ~60 times per second.
16-
pub fn update() -> Unit {
17-
18-
}
19-
20-
///|
21-
/// render is called before updating the image on the screen.
22-
///
23-
/// It might be called less often than `update` if the device sees that the game
24-
/// is slow and needs more resources.
25-
/// This is the best place to call all drawing functions.
26-
pub fn render() -> Unit {
27-
11+
gain.add_sine(@audio.A4, 0) |> ignore
2812
}

src/graphics.mbt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,56 @@ pub fn draw_sub_image(sub_image : SubImage, point : Point) -> Unit {
263263
sub_image.image |> @memory.keep
264264
}
265265

266+
///|
267+
/// Tile the given screen area with the provided sub-image.
268+
pub fn draw_sub_tile(sub_image : SubImage, point : Point, size : Size) -> Unit {
269+
@ffi.draw_sub_tile(
270+
@memory.fixedbytes_addr(sub_image.image.0),
271+
@memory.fixedbytes_size(sub_image.image.0),
272+
point.x,
273+
point.y,
274+
size.w,
275+
size.h,
276+
sub_image.point_x,
277+
sub_image.point_y,
278+
sub_image.size_w,
279+
sub_image.size_h,
280+
)
281+
sub_image.image |> @memory.keep
282+
}
283+
284+
///|
285+
/// Fill the given area with the given 9-slice.
286+
///
287+
/// A 9-slice is used to tile an area with 9 sub-images: 4 corners,
288+
/// 4 edges, and 1 middle segment. It is useful for speech bubbles
289+
/// and other stylish boxes.
290+
///
291+
/// The whole image is the 9-slice. The sub-image is the center area of the 9-slice.
292+
///
293+
/// If the target area is bigger than the 9-slice segments,
294+
/// all the segments (except corners) are repeated ("tiled")
295+
/// without stretching or mirroring.
296+
pub fn draw_nine_slice(
297+
sub_image : SubImage,
298+
point : Point,
299+
size : Size,
300+
) -> Unit {
301+
@ffi.draw_nine_slice(
302+
@memory.fixedbytes_addr(sub_image.image.0),
303+
@memory.fixedbytes_size(sub_image.image.0),
304+
point.x,
305+
point.y,
306+
size.w,
307+
size.h,
308+
sub_image.point_x,
309+
sub_image.point_y,
310+
sub_image.size_w,
311+
sub_image.size_h,
312+
)
313+
sub_image.image |> @memory.keep
314+
}
315+
266316
///|
267317
/// Reference to the current image. Kept to make sure the image doesn't
268318
/// get garbage collected.

src/internal/ffi/graphics.mbt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,34 @@ pub fn draw_sub_image(
164164
sub_height : Int,
165165
) = "graphics" "draw_sub_image"
166166

167+
///|
168+
pub fn draw_sub_tile(
169+
image_ptr : UInt,
170+
image_len : Int,
171+
x : Int,
172+
y : Int,
173+
w : Int,
174+
h : Int,
175+
sub_x : Int,
176+
sub_y : Int,
177+
sub_width : Int,
178+
sub_height : Int,
179+
) = "graphics" "draw_sub_tile"
180+
181+
///|
182+
pub fn draw_nine_slice(
183+
image_ptr : UInt,
184+
image_len : Int,
185+
x : Int,
186+
y : Int,
187+
w : Int,
188+
h : Int,
189+
sub_x : Int,
190+
sub_y : Int,
191+
sub_width : Int,
192+
sub_height : Int,
193+
) = "graphics" "draw_nine_slice"
194+
167195
///|
168196
/// Set the target image for all subsequent drawing operations.
169197
///

0 commit comments

Comments
 (0)