Chapter 1: Introduction to SDL2, Rust, and goku

Introduction

Welcome to the thrilling world of game development! In this chapter, we introduce you to three powerful tools that are becoming popular among game developers: Rust, Simple DirectMedia Layer (SDL2), and Goku. When combined, these tools offer an unprecedented experience for creating video games. By the chapter's conclusion, you'll understand Rust, SDL2, and goku and have prepared your development environment to kickstart your game-building journey.

What is Rust?

Rust is a programming language that aims to provide the performance of languages like C and C++ while also offering memory safety guarantees. It was first released in 2010 and has since gained popularity, especially among game developers, systems programmers, and web assembly applications. Rust has a strong focus on performance, reliability, and productivity.

Some features that make Rust appealing for game development include:

  • Performance: Rust has a performance profile similar to C and C++.

  • Memory Safety: Rust's ownership system ensures memory safety without a garbage collector.

  • Concurrent Programming: Rust's type system and ownership model allow for fearless concurrency.

  • Interoperability: You can easily use Rust alongside C and other languages.

  • Community and Ecosystem: Rust has an active community and a growing ecosystem of libraries and tools.

What is SDL2?

Simple DirectMedia Layer (SDL) is a cross-platform development library designed to provide low-level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D. SDL2 is the latest version of SDL and is widely used in the game industry. SDL2 helps to provide a uniform API to build games that can run on various operating systems, including Windows, Linux, and macOS.

Features of SDL2:

  • Cross-platform support: Write your game once and deploy it on multiple operating systems.

  • Simple API: SDL2 has a simple API for graphics, sound, and input handling.

  • Hardware Acceleration: SDL2 can use OpenGL and Direct3D for hardware-accelerated rendering.

  • Community and Ecosystem: SDL2 is widely used, and there is an active community creating tutorials, libraries, and tools.

What is goku?

goku is an emerging programming language tailored for game development. Its synergistic capabilities with Rust and SDL2 make it a standout choice for developers seeking a blend of performance, ease, and innovation. Goku simplifies complex game logic and aids in faster development, while still providing the flexibility advanced developers seek.

Features of goku:

Ease of Use: Goku's syntax is designed with clarity and simplicity in mind, making it accessible for beginners while robust for professionals.

Performance Optimization: Goku works efficiently with both Rust and SDL2.

Built-in Game Logic: Standard game mechanics are easier to implement with Goku's built-in functions.

Built-in GUI: GUI based on imgui.

Setting Up Your Rust and goku development environment

Before you can start writing Rust code, you need to set up your development environment. Follow these steps:

  1. Install Rust: Go to the official Rust website and follow the installation instructions for your operating system.

  2. Check Installation: After installation, open a terminal and run the following command to ensure Rust is installed correctly: rustc --version

  3. Update Rust: It's a good practice to make sure you are using the latest version of Rust. Run the following command to update Rust: rustup update

  4. Clone goku game engine from repository https://github.com/ladroid/goku

Writing Your First Program in goku

Having set up your environment, it's time to craft a simple program that opens a window using Goku and Rust. This foundational program will serve as a springboard for the games you'll design throughout the book.

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut window = two_d::Window::new("Game Test", 600, 800)?;
    window.canvas.set_draw_color(sdl2::pixels::Color::RGB(0, 0, 0));
    window.canvas.clear();
    window.canvas.present();
    'mainloop: loop {
        for event in window.sdl_context.event_pump()?.poll_iter() {
            match event {
                sdl2::event::Event::Quit { .. } => break 'mainloop,
                _ => {}
            }
        }
        window.canvas.set_draw_color(sdl2::pixels::Color::RGB(255, 0, 0));
        window.canvas.clear();
        window.canvas.present();
        std::thread::sleep(std::time::Duration::new(0, 1_000_000_000u32 / 60));
    }
    Ok(())
}

Run Your Program: First in your terminal, run the program using Cargo: cargo run copy the code and paste it into the built-in text editor, save and then build from the top bar menu.

This will open a window with a red background. Pressing the Escape key or closing the window will end the program.

Conclusion

Congratulations! You've embarked on an electrifying journey into game development with Rust, SDL2, and goku. Your foundational knowledge of these tools is set, and your development environment is game-ready. The upcoming chapter will delve deeper into textures in game development, unraveling methods to load and manage these using goku. Stay enthusiastic, code meticulously, and tread the path to becoming a proficient game developer!

Last updated