func (this *Window) initGL() { runtime.LockOSThread() if gl.Init() != 0 { panic("gl init error") } gl.ShadeModel(gl.SMOOTH) gl.CullFace(gl.BACK) gl.FrontFace(gl.CCW) gl.Enable(gl.CULL_FACE) gl.Enable(gl.DEPTH_TEST) gl.Enable(gl.LIGHTING) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.Enable(gl.TEXTURE_2D) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT) var magFilter, minFilter = gl.LINEAR, gl.LINEAR if globals.CreateMipmaps { minFilter = gl.LINEAR_MIPMAP_LINEAR } gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter) gl.Enable(gl.NORMALIZE) gl.ClearColor(.1, .1, .1, 1) this.setGLViewport() }
func (this *Window) initGL() { gl.ShadeModel(gl.SMOOTH) //SMOOTH or FLAT gl.CullFace(gl.BACK) gl.FrontFace(gl.CCW) gl.Enable(gl.CULL_FACE) gl.Enable(gl.DEPTH_TEST) gl.Enable(gl.LIGHTING) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) /*gl.Enable(gl.FOG) gl.Fogi(gl.FOG_MODE, gl.EXP) gl.Fogfv(gl.FOG_COLOR, []float32{0.5,0.5,0.5,1.0}) gl.Fogf(gl.FOG_DENSITY, 0.0035) gl.Hint(gl.FOG_HINT, gl.DONT_CARE) gl.Fogf(gl.FOG_START, 1.0) gl.Fogf(gl.FOG_END , 5000.0)//*/ gl.Enable(gl.TEXTURE_2D) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE) //before: decal, std: modulate //gl.TexParameteri(gl.TEXTURE_2D, gl.GENERATE_MIPMAP, true) //mipmaps (dont use it!, its bad!) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) //GL_LINEAR or GL_NEAREST, no mipmap here gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR) //*/ gl.ClearColor(.1, .1, .1, 1) //gl.Enable(gl.PRIMITIVE_RESTART) //gl.PrimitiveRestartIndex() this.setGLViewport() }
func main() { var err error if err = glfw.Init(); err != nil { fmt.Fprintf(os.Stderr, "[e] %v\n", err) return } defer glfw.Terminate() if err = glfw.OpenWindow(Width, Height, 8, 8, 8, 8, 0, 8, glfw.Windowed); err != nil { fmt.Fprintf(os.Stderr, "[e] %v\n", err) return } defer glfw.CloseWindow() glfw.SetSwapInterval(1) glfw.SetWindowTitle(Title) glfw.SetWindowSizeCallback(onResize) glfw.SetKeyCallback(onKey) gl.ShadeModel(gl.SMOOTH) gl.ClearColor(0, 0, 0, 0) gl.ClearDepth(1) gl.Enable(gl.TEXTURE_2D) for running && glfw.WindowParam(glfw.Opened) == 1 { update() draw() } }
// OpenWindow opens a new window with the given size. func OpenWindow(w, h int) error { glfw.OpenWindowHint(glfw.WindowNoResize, 1) r, g, b := 0, 0, 0 // defaults a := 8 // 8-bit alpha channel d, s := 0, 0 // no depth or stencil buffers m := glfw.Windowed if err := glfw.OpenWindow(w, h, r, g, b, a, d, s, m); err != nil { return err } if gl.Init() != 0 { return errors.New("Failed to initialize OpenGL") } gl.Enable(gl.TEXTURE_2D) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.ClearColor(0.0, 0.0, 0.0, 0.0) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.Ortho(0, float64(w), 0, float64(-h), -1, 1) gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() gl.Translated(0, float64(-h), 0) return nil }
func main() { glfw.Init() defer glfw.Terminate() glfw.OpenWindow(640, 480, 8, 8, 8, 8, 0, 0, glfw.Windowed) defer glfw.CloseWindow() glfw.SetWindowTitle("Tile test") glfw.Enable(glfw.StickyKeys) glfw.SetSwapInterval(1) glfw.SetKeyCallback(inputCallback) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.Ortho(0, GridWidth, GridHeight, 0, -1, 1) gl.MatrixMode(gl.MODELVIEW) gl.Disable(gl.DEPTH_TEST) gl.Enable(gl.TEXTURE_2D) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.ClearColor(0.0, 0.0, 0.0, 1.0) initResources() initWorld() for Running { if (time.Since(DT).Nanoseconds() / 1000000) > 15 { //don't loop faster than every 15ms DT = time.Now() gl.Clear(gl.COLOR_BUFFER_BIT) player.update() renderScene() glfw.SwapBuffers() } } }
func initScreen() { sdl.Init(sdl.INIT_VIDEO) const ( resx int = 640 resy int = 480 ) var ( screen = sdl.SetVideoMode(resx, resy, 16, sdl.OPENGL) ) if screen == nil { sdl.Quit() panic("Couldn't set GL video mode: " + sdl.GetError() + "\n") } if gl.Init() != 0 { panic("gl error") } gl.MatrixMode(gl.PROJECTION) //gl.MatrixMode(gl.MODELVIEW) gl.Viewport(0, 0, int(screen.W), int(screen.H)) gl.LoadIdentity() gl.Ortho(0, float64(screen.W), float64(screen.H), 0, -1.0, 1.0) //gl.DepthRange(-1, 1) gl.ClearColor(0, 0, 0, 0) gl.Clear(gl.COLOR_BUFFER_BIT) }
func initGL() { gl.ShadeModel(gl.SMOOTH) gl.ClearColor(0, 0, 0, 0) gl.ClearDepth(1) gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LEQUAL) gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST) }
func onResize(w, h int) { gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.Viewport(0, 0, w, h) gl.Ortho(0, float64(w), float64(h), 0, -1.0, 1.0) gl.ClearColor(1, 1, 1, 0) gl.Clear(gl.COLOR_BUFFER_BIT) gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() }
func initGL() { gl.EnableClientState(gl.VERTEX_ARRAY) gl.ShadeModel(gl.SMOOTH) gl.ClearColor(0, 0, 0, 0) gl.ClearDepth(1) gl.DepthFunc(gl.LEQUAL) gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST) gl.Enable(gl.DEPTH_TEST) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) }
// general OpenGL initialization func initGL() { LoadGLTexture("data/star.bmp") gl.Enable(gl.TEXTURE_2D) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE) gl.ShadeModel(gl.SMOOTH) gl.ClearColor(0.0, 0.0, 0.0, 0.5) gl.ClearDepth(1.0) gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST) }
func main() { glut.InitDisplayMode(glut.SINGLE | glut.RGB) glut.InitWindowSize(465, 250) glut.CreateWindow("GLUT bitmap & stroke font example") gl.ClearColor(1.0, 1.0, 1.0, 1.0) gl.Color3f(0, 0, 0) gl.LineWidth(3.0) glut.DisplayFunc(display) glut.ReshapeFunc(reshape) glut.MainLoop() }
func main() { sdl.Init(sdl.INIT_VIDEO) var screen = sdl.SetVideoMode(640, 480, 32, sdl.OPENGL) if screen == nil { panic("sdl error") } if gl.Init() != 0 { panic("gl error") } pen := Pen{} gl.MatrixMode(gl.PROJECTION) gl.Viewport(0, 0, int(screen.W), int(screen.H)) gl.LoadIdentity() gl.Ortho(0, float64(screen.W), float64(screen.H), 0, -1.0, 1.0) gl.ClearColor(1, 1, 1, 0) gl.Clear(gl.COLOR_BUFFER_BIT) var running = true for running { for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() { switch ev := e.(type) { case *sdl.QuitEvent: running = false case *sdl.KeyboardEvent: if ev.Keysym.Sym == sdl.K_ESCAPE { running = false } case *sdl.MouseMotionEvent: if ev.State != 0 { pen.lineTo(Point{int(ev.X), int(ev.Y)}) } else { pen.moveTo(Point{int(ev.X), int(ev.Y)}) } } } sdl.GL_SwapBuffers() sdl.Delay(25) } sdl.Quit() }
func initGL() (err error) { if err = loadTextures(); err != nil { return } gl.ShadeModel(gl.SMOOTH) gl.ClearColor(0, 0, 0, 0) gl.ClearDepth(1) gl.DepthFunc(gl.LEQUAL) gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST) gl.Enable(gl.DEPTH_TEST) gl.Enable(gl.TEXTURE_2D) return }
func loop() { e := initTriangleShaders() if e != nil { log.Fatalln(e.Error()) } for esc { gl.ClearColor(0.0, 0.0, 0.0, 0.0) gl.Clear(gl.COLOR_BUFFER_BIT) drawTriangle() glfw.SwapBuffers() timeSince() } }
// general OpenGL initialization func initGL() { gl.Enable(gl.TEXTURE_2D) gl.ShadeModel(gl.SMOOTH) gl.ClearColor(0.0, 0.0, 0.0, 0.5) gl.ClearDepth(1.0) gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LEQUAL) gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST) // Setup the light gl.Lightfv(gl.LIGHT1, gl.AMBIENT, lightAmbient[:]) // ambient lighting gl.Lightfv(gl.LIGHT1, gl.DIFFUSE, lightDiffuse[:]) // make it diffuse gl.Lightfv(gl.LIGHT1, gl.POSITION, lightPosition[:]) // and place it gl.Enable(gl.LIGHT1) // and finally turn it on. gl.Color4f(1.0, 1.0, 1.0, 0.5) // Full Brightness, 50% Alpha ( NEW ) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE) // Blending Function For Translucency Based On Source Alpha Value ( NEW ) }
func InitUI() *UI { ui := &UI{ running: false, } if sdl.Init(sdl.INIT_EVERYTHING) != 0 { panic(sdl.GetError()) } if ttf.Init() != 0 { panic(sdl.GetError()) } ui.screen = sdl.SetVideoMode(300, 300, 32, sdl.OPENGL) if ui.screen == nil { panic("sdl error") } if gl.Init() != 0 { panic("gl error") } gl.MatrixMode(gl.PROJECTION) gl.Viewport(0, 0, int(ui.screen.W), int(ui.screen.H)) gl.LoadIdentity() gl.Ortho(0, float64(ui.screen.W), float64(ui.screen.H), 0, -1.0, 1.0) gl.ClearColor(1, 1, 1, 0) gl.Clear(gl.COLOR_BUFFER_BIT) // TTF sdl.EnableUNICODE(1) ui.font = ttf.OpenFont("FontinSans.otf", 20) ui.font.SetStyle(ttf.STYLE_UNDERLINE) if ui.font == nil { panic(sdl.GetError()) } ui.running = true return ui }
// general OpenGL initialization func initGL() { LoadGLTextures("data/mud.bmp") gl.Enable(gl.TEXTURE_2D) gl.ShadeModel(gl.SMOOTH) gl.ClearColor(0.0, 0.0, 0.0, 0.0) gl.ClearDepth(1.0) gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LEQUAL) gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST) gl.Lightfv(gl.LIGHT1, gl.AMBIENT, lightAmbient[:]) gl.Lightfv(gl.LIGHT1, gl.DIFFUSE, lightDiffuse[:]) gl.Lightfv(gl.LIGHT1, gl.POSITION, lightPosition[:]) gl.Enable(gl.LIGHT1) gl.Color4f(1.0, 1.0, 1.0, 0.5) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE) }
// general OpenGL initialization func initGL() { // enable smooth shading gl.ShadeModel(gl.SMOOTH) // Set the background to black gl.ClearColor(0.0, 0.0, 0.0, 0.0) // Depth buffer setup gl.ClearDepth(1.0) // Enable depth testing gl.Enable(gl.DEPTH_TEST) // The type of test gl.DepthFunc(gl.LEQUAL) // Nicest perspective correction gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST) }
func initGL() (err error) { if err = loadTextures(); err != nil { return } gl.ShadeModel(gl.SMOOTH) gl.ClearColor(0, 0, 0, 0) gl.ClearDepth(1) gl.DepthFunc(gl.LEQUAL) gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST) gl.Enable(gl.DEPTH_TEST) gl.Enable(gl.TEXTURE_2D) gl.Lightfv(gl.LIGHT1, gl.AMBIENT, ambient) gl.Lightfv(gl.LIGHT1, gl.AMBIENT, diffuse) gl.Lightfv(gl.LIGHT1, gl.POSITION, lightpos) gl.Enable(gl.LIGHT1) return }
func setup_opengl(width, height int) { ratio := float64(width) / float64(height) gl.ShadeModel(gl.SMOOTH) gl.CullFace(gl.BACK) gl.FrontFace(gl.CCW) gl.Enable(gl.CULL_FACE) gl.ClearColor(0, 0, 0, 0) gl.Viewport(0, 0, width, height) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gluPerspective := func(fovy, aspect, zNear, zFar float64) { top := math.Tan(fovy*0.5) * zNear bottom := -top left := aspect * bottom right := -left gl.Frustum(left, right, bottom, top, zNear, zFar) } gluPerspective(60.0, ratio, 1.0, 1024.0) }
// general OpenGL initialization func initGL() { gl.Enable(gl.TEXTURE_2D) gl.ShadeModel(gl.SMOOTH) gl.ClearColor(0.0, 0.0, 0.0, 0.5) gl.ClearDepth(1.0) gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LEQUAL) gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST) // Setup the light gl.Lightfv(gl.LIGHT1, gl.AMBIENT, lightAmbient1[:]) // ambient lighting gl.Lightfv(gl.LIGHT1, gl.DIFFUSE, lightDiffuse1[:]) // make it diffuse gl.Lightfv(gl.LIGHT1, gl.POSITION, lightPosition1[:]) // and place it gl.Enable(gl.LIGHT1) // and finally turn it on. gl.Lightfv(gl.LIGHT2, gl.AMBIENT, lightAmbient2[:]) // ambient lighting gl.Lightfv(gl.LIGHT2, gl.DIFFUSE, lightDiffuse2[:]) // make it diffuse gl.Lightfv(gl.LIGHT2, gl.POSITION, lightPosition2[:]) // and place it gl.Enable(gl.LIGHT2) // and finally turn it on. }
func initGL() { gl.ShadeModel(gl.SMOOTH) gl.ClearColor(0, 0, 0, 0) gl.ClearDepth(1) gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LEQUAL) gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST) globalState.light[0] = 0 globalState.light[1] = 20 globalState.light[2] = -10 globalState.light[3] = 1 gl.Lightfv(gl.LIGHT1, gl.AMBIENT, []float32{1, 1, 1}) gl.Lightfv(gl.LIGHT1, gl.DIFFUSE, []float32{1, 1, 1}) gl.Lightfv(gl.LIGHT1, gl.POSITION, globalState.light[:]) gl.Enable(gl.LIGHT1) gl.Enable(gl.LIGHTING) }
func main() { runtime.LockOSThread() flag.Parse() buildPalette() sdl.Init(sdl.INIT_VIDEO) defer sdl.Quit() sdl.GL_SetAttribute(sdl.GL_SWAP_CONTROL, 1) if sdl.SetVideoMode(512, 512, 32, sdl.OPENGL) == nil { panic("sdl error") } if gl.Init() != 0 { panic("gl error") } sdl.WM_SetCaption("Gomandel", "Gomandel") gl.Enable(gl.TEXTURE_2D) gl.Viewport(0, 0, 512, 512) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.Ortho(0, 512, 512, 0, -1, 1) gl.ClearColor(0, 0, 0, 0) //----------------------------------------------------------------------------- var dndDragging bool = false var dndStart Point var dndEnd Point var tex gl.Texture var tc TexCoords var lastProgress int initialRect := Rect{-1.5, -1.5, 3, 3} rect := initialRect rc := new(MandelbrotRequest) rc.MakeRequest(512, 512, rect) rc.WaitFor(Small, &tex, &tc) running := true for running { for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() { switch e.(type) { case *sdl.QuitEvent: running = false case *sdl.MouseButtonEvent: mbe := e.(*sdl.MouseButtonEvent) if mbe.Type == sdl.MOUSEBUTTONDOWN { dndDragging = true sdl.GetMouseState(&dndStart.X, &dndStart.Y) dndEnd = dndStart } else { dndDragging = false sdl.GetMouseState(&dndEnd.X, &dndEnd.Y) if mbe.Which == 3 { rect = initialRect } else { rect = rectFromSelection(dndStart, dndEnd, 512, 512, rect) tc = texCoordsFromSelection(dndStart, dndEnd, 512, 512, tc) } // make request rc.MakeRequest(512, 512, rect) } case *sdl.MouseMotionEvent: if dndDragging { sdl.GetMouseState(&dndEnd.X, &dndEnd.Y) } } } // if we're waiting for a result, check if it's ready p := rc.Update(&tex, &tc) if p != -1 { lastProgress = p } gl.Clear(gl.COLOR_BUFFER_BIT) tex.Bind(gl.TEXTURE_2D) drawQuad(0, 0, 512, 512, tc.TX, tc.TY, tc.TX2, tc.TY2) gl.BindTexture(gl.TEXTURE_2D, 0) if dndDragging { drawSelection(dndStart, dndEnd) } drawProgress(512, 512, lastProgress, rc.Pending) sdl.GL_SwapBuffers() } }
func main() { w := 500 h := 500 fov := 45.0 clip_min := 0.1 clip_max := 1000.0 aspect_ratio := float64(w) / float64(h) glfw.Init() glfw.OpenWindow(w, h, 8, 8, 8, 8, 8, 0, glfw.Windowed) glfw.SetWindowTitle("Shader Test") glfw.SetWindowSizeCallback(resize_window) glfw.SetKeyCallback(key_callback) gl.ClearColor(0.3, 0.3, 0.3, 1.0) gl.ShadeModel(gl.SMOOTH) size_window(w, h, fov, aspect_ratio, clip_min, clip_max) //Testing sprites test_sprite := sprite{height: 1.0, width: 1.0} test_sprite.tex = texture("./assets/hedge.gif") test_sprite.Y = 3 new_guy := sprite{height: 0.5, width: 0.5} new_guy.tex = texture("./assets/red2.png") new_guy.Y = 5 renderees := new(list.List) renderees.PushBack(&new_guy) renderees.PushBack(&test_sprite) my_camera.init(Vector{0.0, -1.0, 0.0}) my_camera.front = Vector{0.0, 1.0, 0.0} my_camera.top = Vector{0.0, 0.0, 1.0} //End testing sprites physics_objects := new(list.List) physics_objects.PushBack(my_camera) dt, err := time.ParseDuration("16.67ms") if err != nil { die(err) } fmt.Println("Timestep: ", dt) var t float64 = 0 running := true for running { t0 := time.Now() update(physics_objects, float64(dt.Nanoseconds())/1e6) t += 0.025 new_guy.X = math.Cos(t) new_guy.Y = math.Sin(t) + 3.0 //my_camera.point_at(Vector{new_guy.X, new_guy.Y, new_guy.Z}) general_render(renderees, my_camera) glfw.SwapBuffers() dt = time.Since(t0) rate := time.Microsecond * 16670 if dt < rate { time.Sleep((rate - dt)) //fmt.Println("This frame took", float64(dt.Nanoseconds())/1000000, "ms to render.") } running = glfw.Key(glfw.KeyEsc) == 0 && glfw.WindowParam(glfw.Opened) != 0 } }
func main() { runtime.LockOSThread() flag.Parse() sdl.Init(sdl.INIT_VIDEO) defer sdl.Quit() sdl.GL_SetAttribute(sdl.GL_SWAP_CONTROL, 1) if sdl.SetVideoMode(640, 480, 32, sdl.OPENGL) == nil { panic("sdl error") } sdl.WM_SetCaption("Gotris", "Gotris") sdl.EnableKeyRepeat(250, 45) gl.Enable(gl.TEXTURE_2D) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.Viewport(0, 0, 640, 480) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.Ortho(0, 640, 480, 0, -1, 1) gl.ClearColor(0, 0, 0, 0) //----------------------------------------------------------------------------- font, err := LoadFontFromFile("dejavu.font") if err != nil { panic(err) } rand.Seed(int64(sdl.GetTicks())) gs := NewGameSession(*initLevel, font) lastTime := sdl.GetTicks() ticker := time.NewTicker(10 * time.Millisecond) stop := make(chan byte, 1) go func() { for { switch e := (<-sdl.Events).(type) { case sdl.QuitEvent: stop <- 0 case sdl.KeyboardEvent: if e.Type == sdl.KEYDOWN { running := gs.HandleKey(e.Keysym.Sym) if !running { stop <- 0 } gs.update <- 0 } } } }() loop: for { select { case <-ticker.C: gs.update <- 0 case <-gs.update: now := sdl.GetTicks() delta := now - lastTime lastTime = now gs.Update(delta) gl.Clear(gl.COLOR_BUFFER_BIT) font.Draw(5, 5, fmt.Sprintf("Level: %d | Score: %d", gs.Level, gs.Score)) gs.Draw() gl.Color3ub(255, 255, 255) sdl.GL_SwapBuffers() case <-stop: break loop } } }
func main() { log.SetFlags(log.Lshortfile) //parse flags flag.Parse() if flags.cpuprofile != "" { f, err := os.Create(flags.cpuprofile) if err != nil { log.Fatal(err) } pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() } loadSettingsFile() Settings.Paused = flags.startPaused if flags.buildExamples { allExamples() return } wx, wy := 800, 600 //initialize opengl & glfw { //init glfw if err := glfw.Init(); err != nil { log.Printf("Error initializing glfw: %v\n", err) return } defer glfw.Terminate() //set window hints glfw.OpenWindowHint(glfw.WindowNoResize, 1) //create the window if err := glfw.OpenWindow(wx, wy, 8, 8, 8, 8, 0, 8, glfw.Windowed); err != nil { log.Printf("Error opening Window: %v\n", err) return } defer glfw.CloseWindow() //init opengl if gl.Init() != 0 { panic("gl error") } //glfw config { glfw.SetSwapInterval(1) glfw.SetWindowTitle("mater test") } //set additional opengl stuff { gl.ClearColor(0, 0, 0, 0) gl.Enable(gl.BLEND) //gl.BlendFunc (gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); gl.Enable(gl.TEXTURE_2D) } } //setup scene related stuff { //create empty space space = collision.NewSpace() } //reload settings so they take effect reloadSettings() saveSettingsFile() //set callbacks { glfw.SetWindowSizeCallback(OnResize) glfw.SetKeyCallback(OnKey) } //init debug console console.Init() //load savefile passed from the commandline if any if flags.file != "" { err := loadSpace(flags.file) Settings.Paused = true if err != nil { panic(err) } } //if set to true once a second printFPS := false //fix timestep to given fps const expectedFps = 30.0 const expectedFrameTime = 1.0 / expectedFps //the time at the start of the last frame lastTime := 0.0 acc := 0.0 updateAcc := 0.0 frameCount := 0 updateFrameCount := 0 fps := 0 updateFps := 0 Settings.Running = true for Settings.Running && glfw.WindowParam(glfw.Opened) == 1 { time := glfw.Time() //get the time elapsed since the last frame dt := time - lastTime lastTime = time //advance framecount and accumulators frameCount++ acc += dt updateAcc += dt //execute console commands if any select { case command := <-console.Command: console.ExecuteCommand(command) default: } //update the scene at a fixed timestep for updateAcc >= expectedFrameTime { updateFrameCount++ //if one second has passed update the fps and reset the framecount if acc > 1 { updateFps = updateFrameCount updateFrameCount = 0 } //only update if not paused or if set to advance a single frame if !Settings.Paused || Settings.SingleStep { space.Step(expectedFrameTime) Settings.SingleStep = false } updateAcc -= expectedFrameTime } //draw debug data Draw() glfw.SwapBuffers() //if one second has passed update the fps and reset the framecount if acc > 1 { fps = frameCount frameCount = 0 if printFPS { fmt.Printf("---\n") fmt.Printf("FPS: %v\n", fps) fmt.Printf("Update FPS: %v\n", updateFps) fmt.Printf("Average frametime: %v\n", acc/float64(fps)) fmt.Printf("---\n") } acc -= 1 } } }
func main() { runtime.LockOSThread() if sdl.Init(sdl.INIT_EVERYTHING) != 0 { log.Fatal(sdl.GetError()) } var screen = sdl.SetVideoMode(screenWidth, screenHeight, 32, sdl.OPENGL|sdl.HWSURFACE|sdl.GL_DOUBLEBUFFER|sdl.FULLSCREEN) if screen == nil { log.Fatal(sdl.GetError()) } sdl.WM_SetCaption("Lecture Hall Games", "") sdl.EnableUNICODE(1) if gl.Init() != 0 { log.Fatal("could not initialize OpenGL") } gl.Viewport(0, 0, int(screen.W), int(screen.H)) gl.ClearColor(1, 1, 1, 0) gl.Clear(gl.COLOR_BUFFER_BIT) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.Ortho(0, float64(screen.W), float64(screen.H), 0, -1.0, 1.0) gl.Disable(gl.LIGHTING) gl.Disable(gl.DEPTH_TEST) gl.TexEnvi(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE) if mixer.OpenAudio(mixer.DEFAULT_FREQUENCY, mixer.DEFAULT_FORMAT, mixer.DEFAULT_CHANNELS, 4096) != 0 { log.Fatal(sdl.GetError()) } if ttf.Init() != 0 { log.Fatal(sdl.GetError()) } if p, err := build.Default.Import(basePkg, "", build.FindOnly); err == nil { os.Chdir(p.Dir) } var err error rand.Seed(time.Now().UnixNano()) levelDir := fmt.Sprintf("data/levels/demolevel%d", 3+rand.Intn(numberLevels)) //carsDir := fmt.Sprintf(" data/cars/car%d/", 1+rand.Intn(numberCars)) if game, err = NewRacer(levelDir); err != nil { log.Fatal(err) } go func() { listen, err := net.Listen("tcp", ":8001") if err != nil { log.Fatal(err) } for { conn, err := listen.Accept() if err != nil { log.Println(err) continue } go handleConnection(conn) } }() running := true last := time.Now() for running { select { case event := <-sdl.Events: switch e := event.(type) { case sdl.QuitEvent: running = false case sdl.ResizeEvent: screen = sdl.SetVideoMode(int(e.W), int(e.H), 32, sdl.RESIZABLE) case sdl.KeyboardEvent: if e.Type == sdl.KEYDOWN { if e.Keysym.Sym == sdl.K_ESCAPE { running = false } else { game.KeyPressed(e.Keysym) } } } default: } current := time.Now() t := current.Sub(last) last = current mu.Lock() game.Update(t) game.Render(screen) mu.Unlock() sdl.GL_SwapBuffers() } sdl.Quit() }