Tony
Tony

Reputation: 344

SDL2 how to install and use the "gfx" feature

I'm using the SLD2 crate in order to create a simple physics simulation/engine. I want to use the "gfx" feature for getting the framerate and drawing primitives. I understand that I need to include the "SDL2_gfx.lib" file in C:\Users\{Your Username}\.rustup\toolchains\{current toolchain}\lib\rustlib\{current toolchain}\lib exactly like the "SLD2.lib" installation. The problem is that I can't find the required "SDL2_gfx.lib" file. The repository directs me to this gfx download source. However, I can't seem to find any ".lib" file in that folder whatsoever. I tried building the "SLD2_gfx" solution (found in said folder) with Visual Studio Code in order to get a ".lib" file but that failed because of missing header files. I'm unfamiliar with C/C++ or Visual Studio Code, so I can't fix the solution either. How can I get the "SDL2_gfx.lib" file and/or associated ".dll" files?

This is the code I've got so far:

use sdl2::{pixels::Color, event::Event, keyboard::Keycode, rect::{Rect, Point}}; 
use sdl2::gfx::*;

const WIDTH: u32 = 1000; 
const HEIGHT: u32 = 700;

const RECT_SIZE: u32 = 30;

pub fn main() {
    let sdl_context = sdl2::init().unwrap();
    let video_subsystem = sdl_context.video().unwrap();

    let window = video_subsystem.window("Physics Sim", WIDTH, HEIGHT)
        .position_centered()
        .build()
        .unwrap();

    let width = window.size().0;
    let height = window.size().1;

    let mut canvas = window.into_canvas().build().unwrap();

    canvas.set_draw_color(Color::RGB(0, 0, 0));
    canvas.clear();
    canvas.present();
    let mut event_pump = sdl_context.event_pump().unwrap();
    let mut i = 0;

    let mut rect = Rect::from_center(Point::new(width as i32 / 4, height as i32 / 4), RECT_SIZE, RECT_SIZE);
    let rect_border_width = 1;

    'running: loop {
        i = (i + 1) % 255;

        canvas.set_draw_color(Color::RGB(0, 0, 0));
        canvas.clear();

        // Inside of rect
        canvas.set_draw_color(Color::RGB(i, 64, 255 - i));
        canvas.fill_rect(rect).unwrap();
        
        // Border of rect
        canvas.set_draw_color(Color::RGB(255, 255, 255));
        canvas.draw_rect(rect).unwrap();

        for event in event_pump.poll_iter() {
            match event {
                Event::Quit {..} |
                Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
                    break 'running
                },
                _ => {}
            }
        }
        // game loop
        let rect_y = rect.y();
        if rect_y + rect_border_width + RECT_SIZE as i32 <= height as i32 {
            rect.set_y(rect_y);
        }

        canvas.present();
        ::std::thread::sleep(std::time::Duration::new(0, 1_000_000_000u32 / 60));
    } }

The Cargo.toml:

[dependencies]
sdl2 = {version = "0.35.2", default-features = false, features = ["gfx"]}

And the error I'm getting:

note: LINK : fatal error LNK1181: cannot open input file 'SDL2_gfx.lib'

Upvotes: 2

Views: 1563

Answers (1)

nitinmdsu
nitinmdsu

Reputation: 21

I have had success following the vcpkg section in the README.md file: https://github.com/Rust-SDL2/rust-sdl2#windows-msvc-with-vcpkg and the instructions are a bit clearer there.

vcpkg is a package manager to help manage C and C++ libraries. I didn't need to install it directly, but through a rust cargo command called cargo-vcpkg which clones and builds vcpkg from the github repo as necessary.

My Cargo.toml file looks like this (note the "use-vcpkg" feature and specific instructions for vcpkg such as the github repo location and the git rev to check out (501db0f is a recent release version)):

[package]
name = "sdl2-gfx-game"
version = "0.1.0"
edition = "2021"

[dependencies.sdl2]
version = "0.35.2"
default-features = false
features = ["static-link", "use-vcpkg", "gfx"]

[package.metadata.vcpkg]
dependencies = ["sdl2", "sdl2-gfx"]
git = "https://github.com/microsoft/vcpkg"
rev = "501db0f"

[package.metadata.vcpkg.target]
x86_64-pc-windows-msvc = { triplet = "x64-windows-static-md" }

After that, it was a matter of running

cargo install cargo-vcpkg

This install cargo-vcpkg, a one-time task.

Then,

cargo vcpkg build

This will need to be re-run when there is any new C or C++ lib to be included, for example sdl2 ttf. This usually takes some time.

Finally,

cargo build

This will build your project as usual, with the sdl2-gfx packages installed and able to run.

Upvotes: 2

Related Questions