Add SDL GFX usage

This commit is contained in:
2024-04-23 21:23:36 -05:00
parent 4b1f66607e
commit 9c2910aea8
5 changed files with 46 additions and 10 deletions

View File

@@ -6,7 +6,7 @@ rustflags = [
# "-C", "link-args=-g -gsource-map",
"-C", "link-args=-sASYNCIFY -sALLOW_MEMORY_GROWTH=1",
# "-C", "link-args=-sALLOW_MEMORY_GROWTH=1",
"-C", "link-args=-sUSE_SDL=2 -sUSE_SDL_IMAGE=2 -sUSE_SDL_MIXER=2 -sUSE_OGG=1 -sUSE_SDL_TTF=2 -sSDL2_IMAGE_FORMATS=['png']",
"-C", "link-args=-sUSE_SDL=2 -sUSE_SDL_IMAGE=2 -sUSE_SDL_MIXER=2 -sUSE_OGG=1 -sUSE_SDL_GFX=2 -sUSE_SDL_TTF=2 -sSDL2_IMAGE_FORMATS=['png']",
# USE_OGG, USE_VORBIS for OGG/VORBIS usage
"-C", "link-args=--preload-file assets/",
]

7
Cargo.lock generated
View File

@@ -17,6 +17,12 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "c_vec"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fdd7a427adc0135366d99db65b36dae9237130997e560ed61118041fb72be6e8"
[[package]]
name = "cfg-if"
version = "1.0.0"
@@ -221,6 +227,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7959277b623f1fb9e04aea73686c3ca52f01b2145f8ea16f4ff30d8b7623b1a"
dependencies = [
"bitflags",
"c_vec",
"lazy_static",
"libc",
"sdl2-sys",

View File

@@ -9,7 +9,7 @@ edition = "2021"
colors-transform = "0.2.11"
lazy_static = "1.4.0"
rand = "0.8.5"
sdl2 = { version = "0.35", features = ["image", "ttf", "mixer"] }
sdl2 = { version = "0.35", features = ["image", "ttf", "mixer", "gfx"] }
spin_sleep = "1.1.1"
tracing = { version = "0.1.37", features = ["max_level_debug", "release_max_level_debug"]}
tracing-error = "0.2.0"

View File

@@ -14,12 +14,11 @@ This is an experimental repository while testing a Rust + SDL2 project built wit
- [ ] MacOS
- [ ] Linux
- This ensures that the project can iterate safely and be inspected in a safe environment, free from errors. Helps ensure errors are isolated to the machine or build script.
- [ ] SDL2 Extensions
- [X] SDL2 Extensions
- [X] Image
- [X] Mixer
- [X] TTF
- [ ] GFX
- All of these libraries are common and necessary for a lot of projects. Ensuring they work is important.
- [X] GFX
- [X] Example of External Javascript Interop
- The basic ability to provide some kind of Javascript binding would be important for a decent web-based project or game.
- The ability to use localStorage, fetch, or some browser-only API would be important.

View File

@@ -2,6 +2,7 @@ use std::process;
use std::time::Duration;
use sdl2::event::Event;
use sdl2::gfx::primitives::DrawRenderer;
use sdl2::image::LoadTexture;
use sdl2::keyboard::Keycode;
use sdl2::mixer;
@@ -92,10 +93,13 @@ fn main() {
let storage = store::Store {};
let mut volume = storage.volume().map_or_else(|| {
let mut volume = storage.volume().map_or_else(
|| {
println!("No volume stored, using default");
1
}, |v| v);
},
|v| v,
);
print!("Volume: {}", volume);
mixer::Music::set_volume(volume as i32);
@@ -124,11 +128,18 @@ fn main() {
let mut n = 0;
let mut avg_fps = 0f64;
let mut mouse = Point::new(0, 0);
let mut previous_focus = false;
loop {
let start = now();
let mut moved = false;
for event in ctx.event_pump().unwrap().poll_iter() {
match event {
Event::MouseMotion { x, y, .. } => {
mouse = Point::new(x, y);
}
Event::Window { win_event, .. } => match win_event {
sdl2::event::WindowEvent::Resized(w, h) => {
println!("Resized to {}x{}", w, h);
@@ -217,6 +228,25 @@ fn main() {
canvas.set_draw_color(BLACK);
canvas.clear();
let focused = ctx.mouse().focused_window_id().is_some();
if focused != previous_focus {
println!("Focus: {:?}", focused);
previous_focus = focused;
}
// Draw a 32x32 square at the mouse position
if focused {
let mouse_x = (mouse.x / 32 * 32) as i16;
let mouse_y = (mouse.y / 32 * 32) as i16;
let color = Color::RGB(255, 255, 255);
let _ = canvas.line(mouse_x, mouse_y, mouse_x + 32, mouse_y, color);
let _ = canvas.line(mouse_x + 32, mouse_y, mouse_x + 32, mouse_y + 32, color);
let _ = canvas.line(mouse_x + 32, mouse_y + 32, mouse_x, mouse_y + 32, color);
let _ = canvas.line(mouse_x, mouse_y + 32, mouse_x, mouse_y, color);
}
// canvas.line(top_left.x as i16, top_left.y as i16, top_left.x as i16 + 32, top_left.y as i16 + 32, color);
canvas
.copy_ex(
&fruit_atlas,