Building a Commercial Game Engine using mRuby and SDL

Summarized using AI

Building a Commercial Game Engine using mRuby and SDL

Amir Rajan • November 13, 2022 • Houston, TX • Talk

In his talk at RubyConf 2022, Amir Rajan presents a detailed exploration of building a commercial game engine using Ruby, specifically through the Dragon Ruby platform. He expresses his passion for utilizing Ruby, emphasizing its beauty and expressiveness, which enhances the joy of game development. The talk addresses the growing potential of Ruby outside traditional server-side applications. Rajan introduces key concepts around game engines, SDL (Simple DirectMedia Layer), and mRuby, an embedded version of Ruby. He discusses the practical implications of using these technologies, including:

  • Game Development Experience: Rajan describes the game development process and the reasons behind choosing Ruby, primarily its enjoyable programming experience compared to other languages.
  • Technical Innovations: He illustrates how Dragon Ruby boasts significant performance advantages, such as rendering capabilities surpassing those of Unity and handling complex physics simulations efficiently.
  • The Role of SDL: The importance of SDL in creating a robust cross-platform engine is highlighted, showcasing how it allows game developers to target multiple platforms effectively, including consoles like the Nintendo Switch.
  • mRuby Functionality: He explains mRuby's ability to compile Ruby code into bytecode, enhancing performance and compatibility with various chipsets, which facilitates cross-platform deployment for games.
  • Community and Collaboration: Amir emphasizes the supportive community around Dragon Ruby, encouraging collaboration and creativity within game development. He shares achievements of the community and notable projects such as "A Noble Circle," the best-selling iOS game.
  • Educational Resources: The presentation includes information on accessibility and educational resources available to new developers, such as example apps and structured courses.

In conclusion, Rajan invites developers to explore Dragon Ruby as a viable platform for game development that fosters innovation and creativity, encouraging participation in the community. The essence of his message is to leverage Ruby's attributes to create engaging and fun gaming experiences while collaborating within a supportive environment.

Building a Commercial Game Engine using mRuby and SDL
Amir Rajan • November 13, 2022 • Houston, TX • Talk

What does it take to build a cross platform game engine in Ruby? How do you render to the screen? How is the simulation and rendering pipeline orchestrated? Why is Ruby a viable option is to begin with? These questions and more will be answered by Amir. Be a part of this renaissance and see how Ruby can be used for so much more than server side web development.

RubyConf 2022

00:00:00 Ready for takeoff.
00:00:16 Alright, hopefully everyone is in the right place for the talk on building a commercial game engine with Ruby. The standard license for the game engine is available for free; you can download it by simply searching for 'DragonRuby on itch.io.' You'll get a full commercial license, allowing you to use it as you please, including making money off of it.
00:00:29 If you eventually want to scale up and hire a big studio, that’s the plan, right?
00:00:41 I've been in the game industry for around a decade now, and when fellow game developers ask me, 'Oh, you build all your games in Ruby?' it usually leads to the same set of questions.
00:00:54 Over time, I've fine-tuned the perfect response to that question.
00:01:09 But often, after my explanation, they walk away puzzled or we start having a more productive conversation. They say, 'Yeah, but seriously, Amir, why would you choose to do this?'
00:01:22 The answer is actually quite simple: I want to enjoy building video games. After coding for 23 years, I have no desire to use a programming language that is frustrating. I want the experience to be joyful.
00:01:38 As a kid, you dream of building video games, but then you find yourself using languages that aren’t fun, which is why I believe Ruby offers a refreshing perspective.
00:01:56 Another intriguing aspect of Ruby that enhances its viability for game development is that it is often described as a beautiful language. We use that term frequently; you hear people refer to languages as beautiful. Ruby is expressive and it makes me happy. There are facets of this language that correlate closely to art, though sometimes it can be a bit intimidating.
00:02:28 One of Ruby's most significant contributions is its intrinsic ability to foster innovation. Consider how the landscape of technology has changed in the 25 years since Ruby's introduction and how we develop software today. We might still be reliant on Spring if not for Ruby.
00:02:52 This is a crucial aspect of what makes Ruby so powerful, especially in the context of game development.
00:03:04 To illustrate this idea of innovation, this is not just a PowerPoint presentation; this is the game engine running PowerPoint slides.
00:03:09 We're going to elevate this presentation further.
00:03:21 So, this is built using Dragon Ruby. Let me make the boat a little faster and adjust its acceleration. The environment is hot-loaded.
00:03:39 Returning it back to regular speed, I have a built-in REPL, allowing me to perform calculations like one plus one equals two. Now, let’s go back to a simpler slide.
00:04:00 This illustrates what I mean by innovation and the quality of what is possible with this language.
00:04:14 The game industry, in many ways, remains stagnant, especially concerning their development tools and architectures. From my experience, Ruby elevates this aspect to the next level.
00:04:27 I've faced disbelief when demonstrating that you can hot-load your environment while retaining game state in a way that others claim isn't possible. And yet, I show them Dragon Ruby rendering 20,000 sprites on the screen at 60 frames per second, while Unity may only manage nine.
00:04:47 When they ask about physics simulations, I demonstrate many-to-many collision detection for 1,500 objects at 60 frames per second compared to Unity's one frame per second, and they remain skeptical.
00:05:08 We’ll take a closer look at how this is all made possible.
00:05:26 This is the origin story of the game engine. It all started with a simple question—how hard can this be? We have a strong dependency on SDL, which stands for Simple DirectMedia Layer.
00:05:44 SDL is a battle-tested, cross-platform multimedia library and is used in applications like Valve's Steam client, as well as your favorite mini consoles.
00:06:03 Nearly every AAA commercial game engine relies on this impressive library. The approach SDL uses to create a cross-platform window is straightforward: creating a window with a resolution of 1280x720, rendering a black background, and presenting it.
00:06:41 That was my starting point; I was thrilled to get SDL working across every platform: PC, Mac, Linux, iOS, Android, and even consoles. I have games running on the Nintendo Switch utilizing Ruby as well as WASM.
00:07:00 So now I can create a window, but I need more functionality. This is where mRuby excels, as it’s an embedded version of Ruby. The 'm' in mRuby signifies this. I gain access to chipset architectures through the compilation process, specifically utilizing Clang.
00:07:40 By targeting these devices (e.g., ARM 64 and ARM 32), we were ahead of the curve, as this capability was established back in 2013. With the help of mRuby, we can make a WASM game, allowing deployment across various platforms like Raspberry Pi.
00:08:07 The way mRuby functions involves a little machinery. You create your Ruby file and run it through the mRuby compilation process, which generates a C file containing the bytecode.
00:08:33 In this process, the Ruby code is converted into Ruby bytecode, which can then be compiled by referencing that bytecode to create your application.
00:09:06 I have a window where I can load Ruby, but how do I call it? This is where your game loop comes into play. Each video game operates on a simulation loop that typically runs at 60 hertz, where you sleep for 16 milliseconds between each cycle.
00:09:22 Using the mRuby C API, I can construct Ruby objects. For instance, I call on the 'object' class, create a new instance, and invoke the 'tick' method.
00:09:51 I set up my event loop for SDL, polling for events to check if a quit command has been received. If it is, I exit; otherwise, I call the tick function on the newly created Ruby object.
00:10:05 This demonstrates how to invoke Ruby from C, and indeed, you can effectively use all functionalities of Ruby from C.
00:10:15 For example, if you wish to create a class, you can define a class wired to C functions seamlessly. This reflects how Ruby and mRuby are built, pointing you to files like array.c.
00:10:46 In the development of the engine, this API is established for initializing the environment alongside state variables. Within this system, every tick in the game operates at a consistent 60 frames per second.
00:11:08 You can define angles for sprites, pass them output objects, or integrate configuration matrices for streamlined file management.
00:11:31 The game utilizes logic to make objects react: if the enter key is pressed, it may start or stop a spinning action on the sprite.
00:11:50 The engine works to ensure that the sprite’s state is retained, allowing the spinning effect to be visible on screen during subsequent frames.
00:12:14 As the rendering is entirely dependent on hardware capabilities, the engine operates to render scenes as quickly as the hardware allows.
00:12:42 So, here's how it all began, and that's merely the starting point.
00:12:51 Now, let’s discuss what constitutes a runtime and how a simple library or enhancement to mRuby becomes something larger.
00:13:05 The Dragon Ruby runtime is an essential part of this discussion; I won’t go into specifics about the entire thing, but there are fascinating components here.
00:13:15 One key differentiator of Dragon Ruby is its simulation thread, akin to node.js's event loop. In our case, we come in every 16 milliseconds to execute this loop for the developer.
00:14:03 This feature allows for aggressive garbage collection on your behalf, knowing when your code is idle. Additionally, you can take advantage of asynchronicity, enabling you to process requests without having to manage promises or callbacks.
00:14:24 If you incorporate fibers in your game, you can use them iteratively, allowing greater synchronization opportunities and enhancing performance.
00:14:46 Expanding on Ruby's core capabilities, Fizz FS provides access to files for various sandboxed environments. Considering platforms like iOS and Android, standard library calls like file.read may not function appropriately.
00:15:03 With this in mind, we added our own cross-platform standard library that includes intrinsic functionalities and libraries for matrices, parsing JSON/XML, and built-in HTTP support.
00:15:22 This is crucial since we don't possess the standard library in constrained environments. Hence, the Dragon Ruby runtime and rendering thread significantly optimizes for performance.
00:15:37 This component allows the application to run efficiently; it processes textures, audio, and shaders. Additionally, Dragon Ruby facilitates VR development, making it easier to create hot-loaded games.
00:15:56 As far as building your game, you invoke a function to package your project, leading to binaries compatible with multiple platforms, including WASM, PC, Mac, Linux, Raspberry Pi, iOS, Android, and Oculus.
00:16:16 The entire resulting binary, inclusive of the runtime, measures around 4.5 megabytes—an impressive feat given the size of some visual assets.
00:16:38 This minimal footprint is a direct result of leveraging the power of Clang and LLVM infrastructure. If Ruby’s performance seems inadequate, you can also implement foreign function interfaces to C.
00:16:58 All of this functionality essentially falls within a command-line version called 'Firestorm,' linked with a project I'll be discussing further.
00:17:32 With Dragon Ruby, you’ll engage in a community where building fun applications is straightforward. It's all about having a creative outlet.
00:17:59 The community we’ve built is phenomenal. Developers, including those who have never coded in Ruby, find themselves encouraged and enlightened while working through the platform.
00:18:23 We often witness community members sharing 'tweet carts' or ideas for code golf projects in Ruby, igniting curiosity, and inspiring new features.
00:18:46 These innovations reflect some artistic insights we've lost in conventional software craftsmanship. Dragon Ruby can lead to reimagined ways of creating fun and interesting projects.
00:19:13 Alright, I’m here for ten minutes of questions!
00:19:46 A great question concerning performance comparisons between Ruby and Unity arose. Unity has become complacent due to a lack of competition in the space.
00:20:20 Being deeply reliant on a mono runtime built over a decade ago led to significant bloat.
00:20:56 Meanwhile, Dragon Ruby maintains its focus on efficiency and performance through a more streamlined approach.
00:21:17 While Unity's suite grew more complex, we took a much leaner path, empowering us to optimize our performance.
00:21:37 Any other questions?
00:21:48 A question about games built using Dragon Ruby was raised, which is exciting as I'm proud of my community.
00:22:12 The notable title 'A Noble Circle' made headlines, being recognized as the best-selling iOS game.
00:22:22 Overall, I’ve achieved around 3.1 million downloads across all my titles, and as a community, we work towards expanding this platform.
00:22:42 For those involved in the Dragon Ruby community, I'm excited to see your progress and innovations.
00:23:07 If anyone is interested in starting with Dragon Ruby or game development, I recommend downloading the engine from dragonruby.itch.io.
00:23:27 The conference offers a sale, so you can grab a zip file containing over 100 example apps that guide you through various concepts, from simple demonstrations to more refined implementations.
00:23:52 Afterward, you'll find increasing levels of complexity with complete game implementations provided to help your growth.
00:24:21 Our community is always available to assist, be it through Discord or structured courses like Dragon Ruby School.
00:24:51 With plenty of opportunities for support, we encourage new developers to join us.
00:25:10 The energetic collaborative environment fosters the creation of creative and fun projects.
00:25:30 As we aim to iterate our approaches to game development, the interconnectedness enriches everyone involved.
00:26:00 Thanks for your engagement thus far! I'm excited to see what all of you will create!
00:26:20 Just a reminder—feel free to reach out, get involved, and make the most of the Dragon Ruby platform.
00:26:50 All that's left for me is to give away some stickers, so let’s celebrate!
00:27:03 Thanks for being here, everyone!
Explore all talks recorded at RubyConf 2022
+62