func main() { fmt.Println("OpenGL Test!!!") // Init SDL err := sdl.Init(sdl.INIT_VIDEO) if err != nil { fmt.Println("Init error:", err.Error()) Quit(1) } // Init OpenGL sdl.GL_SetAttribute(sdl.GL_CONTEXT_MAJOR_VERSION, 3) sdl.GL_SetAttribute(sdl.GL_CONTEXT_MINOR_VERSION, 3) sdl.GL_SetAttribute(sdl.GL_DOUBLEBUFFER, 1) sdl.GL_SetAttribute(sdl.GL_DEPTH_SIZE, 24) // Create SDL Window window, err := sdl.CreateWindow("OpenGL Test Nick", sdl.WINDOWPOS_CENTRED, sdl.WINDOWPOS_CENTRED, 512, 512, sdl.WINDOW_OPENGL|sdl.WINDOW_SHOWN) if err != nil { fmt.Println("Error while creating window:", err.Error()) Quit(2) } context := sdl.GL_CreateContext(window) err = sdl.GL_SetSwapInterval(1) if err != nil { fmt.Println("Error while setting swap interval:", err.Error()) Quit(3) } //show red gl.ClearColor(1.0, 0.0, 0.0, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT) sdl.GL_SwapWindow(window) sdl.Delay(2000) //show green gl.ClearColor(0.0, 1.0, 0.0, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT) sdl.GL_SwapWindow(window) sdl.Delay(2000) //show blue gl.ClearColor(0.0, 0.0, 1.0, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT) sdl.GL_SwapWindow(window) sdl.Delay(2000) sdl.GL_DeleteContext(context) sdl.DestroyWindow(window) Quit(0) }
//Game loop func (game *Game) Run() { defer game.Exit() if game.initFun == nil { fmt.Println("Go2D Warning: No init function set!") } if game.updateFun == nil { fmt.Println("Go2D Warning: No update function set!") } if game.drawFun == nil { fmt.Println("Go2D Warning: No draw function set!") } //Initialize the game game.initialize() var dt, old_t, now_t uint32 = 0, 0, 0 for g_running { //Check for events and handle them for { event, present := sdl.PollEvent() if present { EventHandler(event) } else { break } } //Calculate time delta now_t = sdl.GetTicks() dt = now_t - old_t old_t = now_t //Update game.update(dt) //Draw game.draw() //Give the CPU some time to do other stuff sdl.Delay(1) } }