Go SDL2 Lesson 1
Creating a window
June 18, 2016
This post is about using the Go programming language with the Simple DirectMedia Layer library (SDL 2). This post assumes you have Go installed and have a working Go Workspace as outlined here. I also assume you have the sdl library (including the necessary developer files!) installed for your platform. In order for Go to use sdl you must have the needed bindings that can be found here. Follow the directions on that page exactly. Many thanks to Veandco for creating the bindings that make this possible. The source code for Lesson 1 can also be found on Github.
Okay once everything is installed and setup it is time for Lesson 1 - using SDL to create a window and draw two rectangles on the screen.
Gaze upon it’s magnificent rectangularness!
This window is created and lasts for five seconds.
time.Sleep(time.Second * 5)
This program has no way of handling input so clicking the close button currently does nothing. Also worth paying attention to is that the two rectangles are created in slightly different ways. With the first (blue) one a rect is created and then placed into the FillRect function. The second (green) one has a rect created inside the FillRect function.
Lastly, if the first function in the FillRect function is nil instead of a rect then sdl will fill the entire surface area with the specified color.
screenSurface.FillRect(nil, sdl.MapRGB(screenSurface.Format, 255, 0, 0))
This will turn the entire screen red.