Skip to content

Instantly share code, notes, and snippets.

@andrebq
Created June 24, 2012 23:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrebq/2985536 to your computer and use it in GitHub Desktop.
Save andrebq/2985536 to your computer and use it in GitHub Desktop.
Just a basic render loop that uses Go + SDL + OpenGL
package main
import (
"github.com/banthar/Go-SDL/sdl"
"github.com/banthar/gl"
)
type Game struct {
Surface *sdl.Surface
Quit bool
}
var game = &Game{}
func Init() {
if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
panic("Unable to start SDL")
}
ConfigureGL()
game.Surface = sdl.SetVideoMode(640, 480, 32, sdl.HWSURFACE | sdl.GL_DOUBLEBUFFER | sdl.OPENGL)
if game.Surface == nil {
panic("Unable to init surface")
}
InitGL()
}
func ConfigureGL() {
// http://www.sdltutorials.com/sdl-opengl-tutorial-basics
sdl.GL_SetAttribute(sdl.GL_RED_SIZE, 8)
sdl.GL_SetAttribute(sdl.GL_GREEN_SIZE, 8)
sdl.GL_SetAttribute(sdl.GL_BLUE_SIZE, 8)
sdl.GL_SetAttribute(sdl.GL_ALPHA_SIZE, 8)
sdl.GL_SetAttribute(sdl.GL_DEPTH_SIZE, 16)
sdl.GL_SetAttribute(sdl.GL_BUFFER_SIZE, 32)
sdl.GL_SetAttribute(sdl.GL_ACCUM_RED_SIZE, 8)
sdl.GL_SetAttribute(sdl.GL_ACCUM_GREEN_SIZE, 8)
sdl.GL_SetAttribute(sdl.GL_ACCUM_BLUE_SIZE, 8)
sdl.GL_SetAttribute(sdl.GL_ACCUM_ALPHA_SIZE, 8)
sdl.GL_SetAttribute(sdl.GL_MULTISAMPLEBUFFERS, 1)
sdl.GL_SetAttribute(sdl.GL_MULTISAMPLESAMPLES, 2)
/*
These are all basic memory sizes for how much data OpenGL will store (except sdl.GL_MULTISAMPLEBUFFERS/sdl.GL_MULTISAMPLESAMPLES, those are for something else). By increasing these, you will effectively increase how much memory OpenGL will use for color information, and the buffer. As I mentioned before, the default on all of these is usually just fine. If you wish to set these, the values I have up above are pretty good. Be sure to call sdl.GL_SetAttribute before calling sdl.SetVideoMode. One thing I want to mention is on sdl.GL_MULTISAMPLEBUFFERS. This is basically your anti-aliasing flag. By turning this on, you are turning on anti-aliasing. sdl.GL_MULTISAMPLESAMPLES is how much to anti-alias. A value of 2 is pretty standard if turned on, 4 is also commonly used. Just keep in mind that this can be resource intensive for some systems. Also, not every computer is able to perform anti-aliasing. And one last note! Make sure your sdl.GL_DEPTH_SIZE is a power of two (16, 32 works), otherwise anti-aliasing may not work for you.
*/
}
func InitGL() {
gl.ClearColor(0, 0, 0, 0)
gl.ClearDepth(1.0)
gl.Viewport(0, 0, 640, 480)
gl.MatrixMode(gl.PROJECTION)
gl.LoadIdentity()
gl.Ortho(0, 640, 480, 0, 1, -1)
gl.MatrixMode(gl.MODELVIEW)
gl.Enable(gl.TEXTURE_2D);
gl.LoadIdentity()
}
func Quit() {
}
func Event() {
for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() {
switch e.(type) {
case *sdl.QuitEvent:
game.Quit = true
default:
}
}
}
func Draw() {
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
gl.LoadIdentity()
gl.Begin(gl.QUADS)
gl.Color3f(1, 0, 0)
gl.Vertex3f(0, 0, 0)
gl.Color3f(1, 1, 0)
gl.Vertex3f(100, 0, 0)
gl.Color3f(1, 0, 1)
gl.Vertex3f(100, 100, 0)
gl.Color3f(1, 1, 1)
gl.Vertex3f(0, 100, 0)
gl.End()
sdl.GL_SwapBuffers()
}
func main() {
Init()
defer Quit()
for !game.Quit {
Event()
Draw()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment