Background

Welcome back! This is the first release of a few bits of SDL2/Go prototype code. The plan is to combine those bits together into a whole at some point. This method may work out well. We've had a good start on our build and release tooling. As part of that, we also have a very simple example of displaying an image on screen.


Looking Back

The Windows build script worked as expected! You may need to install the SDL2 binaries but after that, you should be good to go. We will have to look into distributing the DLLs along with the package. The previous SDL logo test Windows and macOS versions are over on itch.io.


Next Steps

This time around we're going to put together a small 240x160 window. It will act window (or camera) into a larger 1440x960 background image. We even have a small 8x16 “thing” that we can move around the space. This will serve us well down the road and is usable in all types of games.

sdl2-test01

Camera

wow look at that drawing

The basic idea is we have a large background but we only need to render a small part of that to the screen. First we need our camera, so we use sdl.Rect{} and create a rectangle the same size as our display window. Note that this is an example, not the full implementation. At some point most of this code should be on GitHub.

  camera := sdl.Rect{X: 0, Y: 0, W: wWidth, H: wHeight}

We want to constrain the camera in such a way that the “thing” we are moving around will be near the center of the window. We do this by taking the “things” current position (plus half its width/height) and dividing the X and Y by half the windows width and height.

  camera.X = int32((d.posX + d.width/2) - wWidth/2)
  camera.Y = int32((d.posY + d.height/2) - wHeight/2)

We then constrain the camera to keep it from rendering “off-screen”. This allows the “thing” to continue moving toward the edges and have the camera unbound so it doesn't follow.

  if camera.X < 0 {
    camera.X = 0
  }
  if camera.Y < 0 {
    camera.Y = 0
  }
  if camera.X > bgWidth-camera.W {
    camera.X = bgWidth - camera.W
  }
  if camera.Y > bgHeight-camera.H {
    camera.Y = bgHeight - camera.H
  }

Now we are free to take a chunk of our background texture, which matches the cameras coordinates. renderer.Copy() allows us to copy that chunk to the destination is our current renderer.

  renderer.Copy(bg, &sdl.Rect{
    X: camera.X,
    Y: camera.Y,
    W: wWidth,
    H: wHeight,
  },
    &sdl.Rect{
      X: 0,
      Y: 0,
      W: wWidth,
      H: wHeight,
    })

We then do the same thing with our “thing”. We draw it to the screen on top of the background as centered as need be. And that's all there is to it!

moving the thing around


Give It A Try

Like last time I have release the demo version over on itch.io. Both Windows and macOS versions are up for download. Please give them a try and let me know if they run at all. The movement code is a bit off when the “thing” gets near the edges. I will look into updating that as I have time.


Next Up

I'm not quite sure what I'm going to work on next. I do want to do some internal tooling to make the initial project setup easier. But that won't count toward next months release. Maybe I'll work on a little bit of procedural generation. It'd be nice to generate the background or map rather then drawing a shoddy picture in Aseprite. Or we could work on adding camera shake - can't call yourself indie if you don't have that massive shake!


Enjoy this post?
How about buying me a coffee?