Skip to content

Instantly share code, notes, and snippets.

@gitcrtn
Last active July 3, 2022 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gitcrtn/40ececad90ad4e1a354876e21237e950 to your computer and use it in GitHub Desktop.
Save gitcrtn/40ececad90ad4e1a354876e21237e950 to your computer and use it in GitHub Desktop.
A cross compiler of rust-sdl2 project for GameShell and Raspberry Pi
#!/usr/bin/env bash
source $HOME/.cargo/env
cd /build
mkdir -p .cargo
cat <<EOF > .cargo/config
[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"
EOF
cargo build --target=armv7-unknown-linux-gnueabihf
[package]
name = "sdl2demo"
version = "0.1.0"
edition = "2021"
[dependencies.sdl2]
version = "0.35.2"
default-features = false
features = ["image"]
FROM debian:stretch
ENV USER root
RUN rm /bin/sh \
&& ln -s /bin/bash /bin/sh
RUN mkdir /build \
&& cd \
&& apt update \
&& apt install -y curl build-essential \
&& curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y \
&& source $HOME/.cargo/env \
&& rustup target add armv7-unknown-linux-gnueabihf \
&& dpkg --add-architecture armhf \
&& apt update \
&& apt install -y gcc-arm-linux-gnueabihf libsdl2-dev:armhf libsdl2-image-dev:armhf libsdl2-ttf-dev:armhf
COPY build.sh /
ENTRYPOINT ["/build.sh"]
# 1. Build docker image.
docker build -t rust-builder-armv7 .
# 2. Build rust project with sdl2.
cd /path/to/project
docker run --rm -v $PWD:/build rust-builder-armv7
// Example SDL2 program for GameShell
extern crate sdl2;
use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use std::time::Duration;
use sdl2::image::{InitFlag, LoadTexture};
pub const SCREEN_WIDTH: u32 = 320;
pub const SCREEN_HEIGHT: u32 = 240;
pub fn main() {
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
let _image_context = sdl2::image::init(InitFlag::PNG | InitFlag::JPG).unwrap();
let window = video_subsystem.window(
"rust-sdl2 demo",
SCREEN_WIDTH,
SCREEN_HEIGHT)
.position_centered()
.build()
.unwrap();
let mut canvas = window
.into_canvas()
.software()
.build()
.unwrap();
let texture_creator = canvas.texture_creator();
// Prepare png image by yourself.
let image_bytes = include_bytes!("../resources/images/hello_world.png");
let texture = texture_creator
.load_texture_bytes(image_bytes)
.unwrap();
canvas.set_draw_color(Color::RGB(0, 0, 0));
canvas.copy(&texture, None, None).unwrap();
canvas.present();
let mut event_pump = sdl_context.event_pump().unwrap();
'running: loop {
for event in event_pump.poll_iter() {
match event {
Event::Quit {..} |
Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
break 'running
},
_ => {}
}
}
canvas.present();
std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment