clear applies the current solid color to the whole canvasĬanvas has many methods that we can use to deal with individual pixels or textures.set_draw_color defines the current color for all subsequent operations until called again with another color.In the demo above, the three methods we called from canvas are enough to draw a red screen: Let’s create a canvas: WindowCanvas that we can manipulate to render our game: let mut canvas = window.into_canvas().build().map_err(|e| e.to_string())? If we want, we can get to the original object by calling. Note that both VideoSubsystem and Window keep the reference to the original Sdl context object. If you want to read more about all of the above, check the official SDL2 Rust docs. It is also possible to add support for game rendering engines such as OpenGL and Vulkan. The WindowBuilder we call with video_subsystem.window can be used to spawn our main window with many parameters, such as size and position. The video_subsystem: VideoSubsystem groups everything video-related, such as querying display information, spawning new windows, and managing clipboard and text input with proper virtual keyboard support. It can also be used to leverage other operating system resources, such as audio, clipboard and other user inputs. In this case, we used it to access the video resource and keyboard input. The sdl_context: Sdl object manages everything that can happen within the program. When using SDL2 with Rust, we have to create the following intermediate objects to render the red window shown above: Sdl -> VideoSubsystem -> Window -> WindowCanvas The first piece that we will look at is copied below: let sdl_context = sdl2::init()? Using sdl_context, video_subsystem, and window Let’s take a look at our boilerplate code and understand what happened. The result should be a red window that does nothing and exits when you hit esc: This will be our scaffold, so we can paste it into our generated src/main.rs and cargo run the project. Let mut event_pump = sdl_context.event_pump()? If this happens successfully, we’re all set! Understanding a simple SDL programīefore we create our snake game, let’s take a look at the simplest demo from the rust-sdl2 library repository to ensure we understand each component: extern crate sdl2 Run cargo build to download and compile the Rust bindings for SDL2. Next, add sdl2 under the dependencies in Cargo.toml: We are going to implement a very simple snake game: cargo new snake-game Note that some of them require additional development libraries, so refer to each example’s official README for more info. See the docs for setup instructions for other systems.Īs a completely optional - but recommended - step in familiarizing yourself with this library, rust-sdl2 provides many examples you can run by cloning the repository. To set up for Linux through Ubuntu, use the command below: sudo apt install libsdl2-dev To set up for Mac OS, use the command below: brew install sdl2 The library itself is written in C, and we have to install the native development files to compile any program that uses it. Keep in mind that the rust-sdl2 crate just provides the Rust bindings for the new SDL2.0 libraries. Note that this tutorial assumes Rust is already set up with Rustup. Reading user input to control the snake game.Using sdl_context, video_subsystem, and window.In this blog post, we are going to create a simple Rust game that uses the rust-sdl2 crate. These features make SDL a reasonable and popular choice for writing games from scratch, as you can see from this extensive list of games made with SDL. It also supports the most popular desktop and mobile operating systems - and even web browsers with WebAssembly - making it easier to write cross-platform code. SDL stands for Simple DirectMedia Layer, which is an open source library that provides APIs for user input, audio, and graphics. Using SDL2 bindings in Rust for game development Paulo Henrique Cuchi Follow I'm a software engineer who likes to delete code more than I like to write it.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |