Last active
July 3, 2022 15:12
-
-
Save gitcrtn/40ececad90ad4e1a354876e21237e950 to your computer and use it in GitHub Desktop.
A cross compiler of rust-sdl2 project for GameShell and Raspberry Pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "sdl2demo" | |
version = "0.1.0" | |
edition = "2021" | |
[dependencies.sdl2] | |
version = "0.35.2" | |
default-features = false | |
features = ["image"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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