Skip to content

C, Lua and Rust examples

These runnable examples come from the repository. All three open a 160 × 120 window, load the shared tile.png asset and draw it at (32, 32). They use the same core scheduler, and print the image size:

image: 32x32

Each repository source starts with its license header; the headers are omitted here.

examples/features/image/lua/main.lua
local mv = require("malvado")
local show = mv.process(function(self)
for _ = 1, 120 do
mv.draw_image(self.image, 32, 32)
mv.frame()
end
end)
return {
title = "Malvado Image",
width = 160,
height = 120,
init = function()
mv.clear_screen(0, 0, 0)
local image = mv.load_image("tile.png")
local width, height = mv.image_size(image)
print(("image: %dx%d"):format(width, height))
show({image = image}) -- The process keeps the image for its draw calls.
end,
}
examples/features/image/c/main.c
#include <stdio.h>
#include "malvado/platform.h"
int
main(void)
{
if (!mv_window_open(160, 120, "Malvado Image"))
return 1;
MvImage *image = mv_image_load("tile.png");
if (!image) {
mv_window_close();
return 1;
}
printf("image: %dx%d\n", mv_image_width(image), mv_image_height(image));
for (int frame = 0; frame < 120 && !mv_window_should_close(); ++frame) {
mv_window_begin((MvColor){0, 0, 0, 255});
mv_image_draw(image, 32, 32);
mv_window_end();
}
/* A direct image reference belongs to the caller and needs a live window. */
mv_image_destroy(image);
mv_window_close();
return 0;
}

Rust gameplay support is experimental in Malvado 2.0.

examples/features/image/rust/main.rs
use malvado::{
Error,
image::Image,
window::{Color, Window},
};
use std::rc::Rc;
fn main() -> Result<(), Error> {
let window = Rc::new(Window::open(160, 120, "Malvado Image")?);
// The image keeps its window alive until the image is dropped.
let image = Image::load(window.clone(), "tile.png")?;
let (width, height) = image.size();
println!("image: {width}x{height}");
for _ in 0..120 {
if window.should_close() {
break;
}
let frame = window.frame(Color {
r: 0,
g: 0,
b: 0,
a: 255,
})?;
frame.image(&image, (32, 32))?;
}
Ok(())
}

Copy the shared asset as described in the lesson instructions. See the Draw a PNG image guide, and the Lua processes, C process API and Rust processes guides.