<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Matthew Michelotti on Medium]]></title>
        <description><![CDATA[Stories by Matthew Michelotti on Medium]]></description>
        <link>https://medium.com/@michelotti.matthew?source=rss-7a3231ff1bed------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*QFpHuu_rc1w54a7yK2x7wA.jpeg</url>
            <title>Stories by Matthew Michelotti on Medium</title>
            <link>https://medium.com/@michelotti.matthew?source=rss-7a3231ff1bed------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 03:42:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@michelotti.matthew/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Rendering Pixel Art]]></title>
            <link>https://medium.com/@michelotti.matthew/rendering-pixel-art-c07a85d2dc43?source=rss-7a3231ff1bed------2</link>
            <guid isPermaLink="false">https://medium.com/p/c07a85d2dc43</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[pixel-art]]></category>
            <dc:creator><![CDATA[Matthew Michelotti]]></dc:creator>
            <pubDate>Thu, 07 Feb 2019 14:03:28 GMT</pubDate>
            <atom:updated>2019-02-07T14:03:28.002Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’ve ever tried to make a game with pixel art, perhaps you’ve run into this problem: how can you make your low-resolution pixel art appear crisp and clean when displayed at higher resolutions? The graphics need to be scaled up, so some sort of interpolation is necessary. The most common types of interpolation are “nearest” and “linear”, but these will cause your beautiful pixel art to look distorted or blurry, respectively, assuming a non-integer scale factor. Rendering a rotated sprite has the same problem. There are several approaches to address this. In this post, I’ll describe my personal experience with this problem and how my approach to solving it changed over time.</p><p>In my early games, I took the easiest way out and forced the game window to be a specific predetermined resolution, and scaled all images by a predetermined integer scale factor. This is, of course, very limiting. It does not make use of the full device display, and attempting arbitrary scaling and rotation of sprites leads to the graphical artifacts described above.</p><p>When I started work on <a href="https://youtu.be/x8osnS-INP4">Weaponless</a> in 2014, I knew that I needed a more flexible solution. My first thought was that I could use a shader of some kind, but I was a novice in OpenGL and completely unfamiliar with other graphics libraries, and the OpenGL shader language intimidated me. To make this more complicated, I was using Java and <a href="https://libgdx.badlogicgames.com/">libGDX</a>, but most of the tutorials for learning OpenGL were in C/C++. So I came up with another solution that did not involve writing a shader. First, I would scale all of my image files by 200% using nearest interpolation, overwriting the old image files. Then, when rendering these images, I would use linear interpolation. The combined effect was something halfway between nearest and linear interpolation, so I dubbed it “hybrid” interpolation. It looked acceptable, although still a little fuzzy. Also, the images, of course, occupied 4 times as much memory as they needed, which was not a big deal in practice.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/264/1*kSN777vvYTeGdzibN4_ZqA.png" /><figcaption>hybrid scaling I used in Weaponless, 2014</figcaption></figure><p>Later, my language of choice shifted from using Java and libGDX to using <a href="https://www.rust-lang.org/">Rust</a> and <a href="https://www.libsdl.org/">SDL2</a>. At this point, I decided it was high time for me to learn how to write OpenGL shaders properly. SDL2 interoperates well with OpenGL, giving the user the option of invoking all OpenGL methods directly. I went through tutorials on making simple shaders to render textures. Then I looked around to see if I could find a shader to render pixel art cleanly. I stumbled across Wikipedia’s page on <a href="https://en.wikipedia.org/wiki/Pixel-art_scaling_algorithms">pixel art scaling</a>, but the shaders mentioned were designed to make pixel art look less like pixel art, which was not the kind of thing I was looking for.</p><p>Having grasped the basics of the shader language, I decided to see what I could come up with on my own. The first step in solving a problem like this is to define the desired output. If we wanted to scale a sprite by e.g. a factor of 3.5, what colors should ideally be assigned to each pixel value? I decided the image should take on the same colors as it would if it were <a href="https://en.wikipedia.org/wiki/Supersampling">super-sampled</a> using nearest interpolation. The basic idea is that instead of sampling one color for each screen pixel, you sample several colors at different locations within each screen pixel and average the colors together. This is very expensive, but the results look good. If you want to try it without writing a shader, open some pixel art in a painting program, scale by 1600% using nearest/no interpolation, and then scale by 22% using something like linear interpolation.</p><p>In our case, excessive super-sampling is redundant. Think of this problem as two grids overlaid on each other: the texels (texture pixels) overlaid on the pixels (screen pixels). In general, we expect a texel to be much larger than a pixel. If we ignore the possibility of rotation for the moment and assume a scale factor greater than 1.0, then each pixel will overlap at most 4 texels, and then only at a corner of the texels. Thus, we should only need to sample the texture 4 times per pixel and compute a weighted average of these colors.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/520/1*fHLRFjKObA_h_KTMBX75tA.png" /><figcaption>weighted average of neighboring texels</figcaption></figure><p>So, this was my initial approach, but even 4 samples per pixel seemed like a higher cost than necessary. I wondered if it could be improved, but after thinking about it some more, I was convinced that taking a weighted average of 4 neighboring texels was definitely necessary. Then I had an epiphany moment: I realized that there is a builtin mechanism in OpenGL for taking a weighted average of 4 neighboring texels, and it is called linear interpolation! This seems a little counter-intuitive at first, but it is possible to use linear interpolation while carefully adjusting the sampling position to yield a result that looks like nearest interpolation with super-sampling.</p><p>This works for arbitrary scaling, but what about arbitrary rotation? Well, although it is no longer an exact replication of the super-sampling effect when applied to rotation, it is a close approximation. The same shader code can be used for rotations, and it is hard to notice any deficiency.</p><p>The code I wrote to do this is part of my open-source <a href="https://github.com/SergiusIW/gate">Gate</a> library. The current version of the fragment shader is located <a href="https://github.com/SergiusIW/gate/blob/678b1375ff4ba5285535bffb71ab42fe7d48ba2b/gate/src/renderer/shaders/sprite.frag">here</a>. However, one thing I learned about OpenGL is that a shader should not be considered in isolation; it is important to know the expectations on the OpenGL settings, textures, and how the vertex buffers are filled. This blog post is not meant to be a tutorial, and given all of the subtleties, I’m not going to try to explain the shader code here. If you’re interested in all of the specifics, you can browse my code. But concerning the surrounding expectations, I will say that OpenGL is set to use linear interpolation, the alpha values are <a href="http://apoorvaj.io/alpha-compositing-opengl-blending-and-premultiplied-alpha.html">pre-multiplied</a> and the appropriate blend function is used, the texture is a sprite atlas with 2 pixels of padding between sprites, and each sprite is drawn with a 1 pixel padding to account for the tweaking of sampled coordinates.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/304/1*E0rQiDr-xOf-h5HkB8Z91w.png" /><figcaption>Gate’s pixel art shader</figcaption></figure><p>After I wrote this shader and noticed how short the code ended up being, I decided that other people must have done something similar. So I searched around some more, and came upon a <a href="https://colececil.io/blog/2017/scaling-pixel-art-without-destroying-it/">blog post</a> that described a shader to achieve exactly the same effect as I did, although the arithmetic is a little different. I find it interesting how I arrived at the same result from a different perspective: trying to achieve a super-sampling effect without the cost of super-sampling.</p><p>That’s pretty much all I have to say about rendering pixel art. I also wanted to touch on how I dealt with the problem of tiling. Graphical artifacts can crop up when drawing a mesh of tiles if you’re not careful. But I think I’ll save this discussion for a future blog post.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c07a85d2dc43" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Rust for Gamedev]]></title>
            <link>https://medium.com/@michelotti.matthew/using-rust-for-gamedev-2f60b0e4cc5c?source=rss-7a3231ff1bed------2</link>
            <guid isPermaLink="false">https://medium.com/p/2f60b0e4cc5c</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[webassembly]]></category>
            <category><![CDATA[game-development]]></category>
            <dc:creator><![CDATA[Matthew Michelotti]]></dc:creator>
            <pubDate>Fri, 04 Jan 2019 06:15:37 GMT</pubDate>
            <atom:updated>2019-01-04T15:32:22.274Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve recently created a <a href="http://www.luduminis.com/">new website</a> and finished an online game named <a href="http://www.luduminis.com/pascal/about/">Adventures of Pascal Penguin</a>. One of the most unique things about this game is that it was written in <a href="https://www.rust-lang.org/">Rust</a> and runs in web browsers thanks to <a href="https://webassembly.org/">WebAssembly</a>. WebAssembly has only been mainstream for about a year or so, so not many games have been created like this. I want to share a little about my journey with Rust: how I started with it and how I got to this point.</p><p>Game development has been a hobby of mine for a long time. Prior to mid-2015 when Rust 1.0 was released, I had been doing gamedev in Java with <a href="https://libgdx.badlogicgames.com/">LibGDX</a>. Since this was a hobby, I chose the technologies I enjoyed using, but that isn’t to say there weren’t frustration. The biggest frustration I had with Java, aside from its verbosity, was that I found myself forced to choose between writing idiomatic Java code and writing Java code that got decent performance. For example, the LibGDX API encourages using their object pooling utilities and specialized resizable array implementations with public native arrays as fields. These are not idiomatic Java practices, but they are useful for making a game that runs smoothly. When you need to be stingy about creating objects, I’d say it’s time to find a new language.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wL9FvRCwlO8X0ysJ8348kw.png" /></figure><p>I learned Rust by reading their online book, which <a href="https://doc.rust-lang.org/1.0.0/book/">looked a lot different</a> at the time. It was shorter back then, more like a tutorial than a full book. There were many things that I found appealing: zero-cost abstractions, traits and generics with monomorphization, the ownership paradigm, flexible enums combined with pattern matching, ergonomic error handling, memory-efficient collections, etc. But on a fundamental level, Rust is a safe language, meaning developing in Rust is not error-prone, while still being a highly and predictably performant language. <strong>I observed that when I write Rust code and I get to the point where my code compiles without error messages, it probably works correctly and is efficient. I think this is what makes the language seem appealing and empowering to a lot of people.</strong></p><p>There is also some excellent tooling around Rust. The Rust compiler itself gives helpful warning and error messages. The Cargo dependency management tool is refreshingly simple and creates reproducible builds. The crates.io central repository is easy for anyone to upload to. The generated html API docs are the most useful I have seen in any language, and docs for all public crates are built and hosted on <a href="https://docs.rs/">docs.rs</a>.</p><p>So I experimented with game development in Rust, but not (initially) with the intention of running a game through a web browser. At the time, there was an excellent tutorial named ArcadeRS that demonstrated how to make a Rust game using <a href="https://www.libsdl.org/">SDL2</a>. Unfortunately, that tutorial is no longer up-to-date and <a href="https://www.reddit.com/r/rust/comments/67724c/where_i_can_find_the_arcaders_tutorial_where/">the site has been taken down</a>. While working on a project, I decided it would be good practice to extract a reusable “layer”, specific to my needs, that separated the game logic from the resource management, rendering, audio, and other interfacing that is needed for a game. I also decided to make this layer open-source, and it became the <a href="https://github.com/SergiusIW/gate">Gate</a> library. To go along with this, I created a <a href="https://github.com/SergiusIW/chirperjax">brief open-source 2D platformer</a> that has since been named Chirperjax.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/1*laM9PVwuuwXfpkH3ZVNbtA.png" /></figure><p>Around this time, I heard about a new technology called WebAssembly. Thanks to this, Rust could be compiled to run in a browser and achieve near-native performance. I knew I had to try it, although it was further along the cutting edge than I normally like to work with. <strong>One of the issues with developing games as a hobby is that people tend to be wary about downloading and running an executable on their computers, and rightfully so. Hosting a game that runs in a sandboxed environment in a browser allows more people to play the game.</strong> My first attempt was to compile Chirperjax using emscripten’s SDL2 support, but I was not able to get it to work correctly. So I went through some tutorials for Rust’s wasm32-unknown-unknown target and learned how to call JavaScript methods from Rust and how to call Rust methods from JavaScript. Since modern html is equipped to create an interactive audiovisual experience on its own, I did not need to depend on SDL2 or other additional libraries, with the exception of <a href="https://howlerjs.com/">howler.js</a>. This made the whole application lightweight and gave me a large amount of control.</p><p>Keep in mind I was doing this while Rust’s WebAssembly tooling was still immature. But I did manage to get Gate building to WebAssembly, and I posted Chirperjax online in January 2018. After doing that and seeing people enjoy playing it, I decided to create a more ambitious game with the same tooling. That was the beginning of Adventures of Pascal Penguin, which was completed in December 2018. It was essentially a year-long project for me.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/1*2h9u8L7heya0WQgwawngaw.png" /></figure><p>So that was my experience in a nutshell. There are certainly other paths I might have taken, such as learning MonoGame or Unity. But when I stumbled upon Rust for the first time, I felt it was exactly what I wanted from a programming language, and since I was just doing this for fun I decided to learn it over the more mainstream gamedev options. I did not have much difficulty learning Rust. Sure, there was a time when I was, quote, “fighting the borrow checker,” but I learned that I could re-arrange my object models and my thinking to work more naturally with the borrow checker, which also improved my code quality. If I had some complaints, it would be the lack of a quick tutorial and immature libraries. But overall I have been very productive with Rust, and I encourage others to try it out.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f60b0e4cc5c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>