func (this *WorldRenderer) Render(ww *gamestate.World, options *settings.BoolOptions, window *sdl.Window) { camera := ww.Player.Camera p0 := camera.Pos4f() w, h := window.GetSize() camera.MoveRelative(mgl.Vec4{-0.1, 0, 0, 0}) this.View = (ww.PortalTransform(p0, camera.Pos4f()).Mul4(camera.Model())).Inv() viewport := Viewport{0, 0, w / 2, h} viewport.Activate() this.render(ww, options, viewport, 0, nil) camera.MoveRelative(mgl.Vec4{+0.2, 0, 0, 0}) this.View = (ww.PortalTransform(p0, camera.Pos4f()).Mul4(camera.Model())).Inv() viewport.X = w / 2 viewport.Activate() this.render(ww, options, viewport, 0, nil) gl.Viewport(0, 0, w, h) gl.ActiveTexture(gl.TEXTURE0) this.Framebuffer[0].RenderTexture.Bind(gl.TEXTURE_RECTANGLE) this.ScreenQuadRenderer.Render(this.ScreenQuad, this.Proj, this.View, this.ClippingPlane_ws, nil) if this.screenShot { this.screenShot = false helpers.SaveTexture(gl.TEXTURE_RECTANGLE, 0, "screenshot.png") } }
func NewGameState(window *sdl.Window, world *World) (gamestate *GameState) { gl.ClearColor(0., 0., 0.4, 0.0) gl.Enable(gl.DEPTH_TEST) bar := tw.NewBar("TweakBar") gamestate = &GameState{ Window: window, Camera: nil, Bar: bar, World: world, Fps: 0, Options: settings.BoolOptions{}, } opt := &gamestate.Options opt.Load() gamestate.Camera = gamestate.World.Player.GetCamera() tw.Define(" GLOBAL help='This example shows how to integrate AntTweakBar with SDL2 and OpenGL.' ") bar.AddVarRO("fps", tw.TYPE_FLOAT, unsafe.Pointer(&gamestate.Fps), "") opt.CreateGui(bar) for i, portal := range gamestate.World.KdTree { ptr := &(portal.(*Portal).Orientation) bar.AddVarRW(fmt.Sprintf("Rotation %d", i), tw.TYPE_QUAT4F, unsafe.Pointer(ptr), "") } //window.GetSize(w, h) w, h := window.GetSize() tw.WindowSize(w, h) return }
func CreateMenus(window *sdl.Window, surface *sdl.Surface, renderer *sdl.Renderer, controlManager *inputManager.ControlManager) inputManager.Update { progress := float64(0) mutex := sync.Mutex{} dx := 4 * surface.W / 8 startRect := sdl.Rect{surface.W / 4, 4 * surface.H / 7, 0, 2 * surface.H / 7} midBackgroundRect := sdl.Rect{surface.W / 4, 4 * surface.H / 7, dx, 2 * surface.H / 7} backgroundRect := sdl.Rect{surface.W/4 - 5, 4*surface.H/7 - 5, dx + 10, 2*surface.H/7 + 10} var mainMenu *Menu go asyncMenuCreator(renderer, &mutex, &progress, &mainMenu, surface.W, surface.H, window, controlManager) return func(state int) int { if controlManager.Player1.BF || controlManager.Player2.BF { controlManager.Running = false return -1 } mutex.Lock() startRect.W = int32(float64(dx) * progress) surface.FillRect(&backgroundRect, 0xffff0000) surface.FillRect(&midBackgroundRect, 0xff000000) surface.FillRect(&startRect, 0xffff0000) window.UpdateSurface() if progress == 1 { menuInfo := MenuInfo{0, controlManager, renderer, &sdl.Rect{0, 0, surface.W, surface.H}} inputManager.UpdateFunctions = append(inputManager.UpdateFunctions, mainMenu.Open(0, &menuInfo)) return -1 } mutex.Unlock() return 0 } }
func createMainWindow() { var err error var mainWindow *sdl.Window gc := gamecontext.GContext gc.WindowWidth = 800 gc.WindowHeight = 600 mainWindow, err = sdl.CreateWindow("Eggdrop!", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, int(gc.WindowWidth), int(gc.WindowHeight), sdl.WINDOW_SHOWN) if err != nil { panic(err) } gc.MainSurface, err = mainWindow.GetSurface() if err != nil { panic(err) } gc.PixelFormatEnum, err = mainWindow.GetPixelFormat() if err != nil { panic(err) } gc.PixelFormat, err = sdl.AllocFormat(uint(gc.PixelFormatEnum)) // TODO why the cast? Seems to work? if err != nil { panic(err) } gc.MainWindow = mainWindow }
func main() { var window *sdl.Window var renderer *sdl.Renderer var event sdl.Event var running bool window = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_SHOWN) if window == nil { fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", sdl.GetError()) os.Exit(1) } renderer = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED) if renderer == nil { fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", sdl.GetError()) os.Exit(2) } var peepArray []sdl.Event = make([]sdl.Event, 5) running = true for running { sdl.PumpEvents() numEventsRetrieved := sdl.PeepEvents(peepArray, sdl.PEEKEVENT, sdl.FIRSTEVENT, sdl.LASTEVENT) if numEventsRetrieved < 0 { fmt.Printf("PeepEvents error: %s\n", sdl.GetError()) } else { for i := 0; i < numEventsRetrieved; i++ { fmt.Printf("Event Peeked Value: %v\n", peepArray[i]) // primitive printing of event } } for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch t := event.(type) { case *sdl.QuitEvent: running = false case *sdl.MouseMotionEvent: fmt.Printf("[%d ms] MouseMotion\ttype:%d\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y, t.XRel, t.YRel) case *sdl.MouseButtonEvent: fmt.Printf("[%d ms] MouseButton\ttype:%d\tid:%d\tx:%d\ty:%d\tbutton:%d\tstate:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y, t.Button, t.State) case *sdl.MouseWheelEvent: fmt.Printf("[%d ms] MouseWheel\ttype:%d\tid:%d\tx:%d\ty:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y) case *sdl.KeyUpEvent: fmt.Printf("[%d ms] Keyboard\ttype:%d\tsym:%c\tmodifiers:%d\tstate:%d\trepeat:%d\n", t.Timestamp, t.Type, t.Keysym.Sym, t.Keysym.Mod, t.State, t.Repeat) } } sdl.Delay(1000 / 30) } renderer.Destroy() window.Destroy() }
func run() int { var window *sdl.Window var renderer *sdl.Renderer var points []sdl.Point var rect sdl.Rect var rects []sdl.Rect window, err := sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_SHOWN) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err) return 1 } defer window.Destroy() renderer, err = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", err) return 2 } defer renderer.Destroy() renderer.Clear() renderer.SetDrawColor(255, 255, 255, 255) renderer.DrawPoint(150, 300) renderer.SetDrawColor(0, 0, 255, 255) renderer.DrawLine(0, 0, 200, 200) points = []sdl.Point{{0, 0}, {100, 300}, {100, 300}, {200, 0}} renderer.SetDrawColor(255, 255, 0, 255) renderer.DrawLines(points) rect = sdl.Rect{300, 0, 200, 200} renderer.SetDrawColor(255, 0, 0, 255) renderer.DrawRect(&rect) rects = []sdl.Rect{{400, 400, 100, 100}, {550, 350, 200, 200}} renderer.SetDrawColor(0, 255, 255, 255) renderer.DrawRects(rects) rect = sdl.Rect{250, 250, 200, 200} renderer.SetDrawColor(0, 255, 0, 255) renderer.FillRect(&rect) rects = []sdl.Rect{{500, 300, 100, 100}, {200, 300, 200, 200}} renderer.SetDrawColor(255, 0, 255, 255) renderer.FillRects(rects) renderer.Present() sdl.Delay(2000) return 0 }
func GetMouseDirection(window *sdl.Window, mx, my int) (dir_cs mgl.Vec4) { if !sdl.GetRelativeMouseMode() { W, H := window.GetSize() x := (2*float32(mx) - float32(W)) / float32(H) y := (float32(H) - 2*float32(my)) / float32(H) dir_cs = mgl.Vec4{x, y, -1, 0} } else { dir_cs = mgl.Vec4{0, 0, -1, 0} } return }
func main() { var window *sdl.Window var renderer *sdl.Renderer var points []sdl.Point var rect sdl.Rect var rects []sdl.Rect window = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_SHOWN) if window == nil { fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", sdl.GetError()) os.Exit(1) } renderer = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED) if renderer == nil { fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", sdl.GetError()) os.Exit(2) } renderer.SetDrawColor(255, 255, 255, 255) renderer.DrawPoint(150, 300) renderer.SetDrawColor(0, 0, 255, 255) renderer.DrawLine(0, 0, 200, 200) points = []sdl.Point{{0, 0}, {100, 300}, {100, 300}, {200, 0}} renderer.SetDrawColor(255, 255, 0, 255) renderer.DrawLines(points) rect = sdl.Rect{300, 0, 200, 200} renderer.SetDrawColor(255, 0, 0, 255) renderer.DrawRect(&rect) rects = []sdl.Rect{{400, 400, 100, 100}, {550, 350, 200, 200}} renderer.SetDrawColor(0, 255, 255, 255) renderer.DrawRects(rects) rect = sdl.Rect{250, 250, 200, 200} renderer.SetDrawColor(0, 255, 0, 255) renderer.FillRect(&rect) rects = []sdl.Rect{{500, 300, 100, 100}, {200, 300, 200, 200}} renderer.SetDrawColor(255, 0, 255, 255) renderer.FillRects(rects) renderer.Present() sdl.Delay(2000) renderer.Destroy() window.Destroy() }
func main() { var winTitle string = "Go-SDL2 + Go-GL" var winWidth, winHeight int = 800, 600 var window *sdl.Window var context sdl.GLContext var event sdl.Event var running bool var err error if err = sdl.Init(sdl.INIT_EVERYTHING); err != nil { panic(err) } defer sdl.Quit() if err = gl.Init(); err != nil { panic(err) } window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_OPENGL) if err != nil { panic(err) } defer window.Destroy() context, err = sdl.GL_CreateContext(window) if err != nil { panic(err) } defer sdl.GL_DeleteContext(context) gl.Enable(gl.DEPTH_TEST) gl.ClearColor(0.2, 0.2, 0.3, 1.0) gl.ClearDepth(1) gl.DepthFunc(gl.LEQUAL) gl.Viewport(0, 0, int32(winWidth), int32(winHeight)) running = true for running { for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch t := event.(type) { case *sdl.QuitEvent: running = false case *sdl.MouseMotionEvent: fmt.Printf("[%d ms] MouseMotion\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n", t.Timestamp, t.Which, t.X, t.Y, t.XRel, t.YRel) } } drawgl() sdl.GL_SwapWindow(window) } }
func run() int { var window *sdl.Window var renderer *sdl.Renderer var texture *sdl.Texture var src, dst sdl.Rect var err error window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_SHOWN) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err) return 1 } defer window.Destroy() renderer, err = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", err) return 2 } defer renderer.Destroy() image, err := img.Load(imageName) if err != nil { fmt.Fprintf(os.Stderr, "Failed to load PNG: %s\n", err) return 3 } defer image.Free() texture, err = renderer.CreateTextureFromSurface(image) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create texture: %s\n", err) return 4 } defer texture.Destroy() src = sdl.Rect{0, 0, 512, 512} dst = sdl.Rect{100, 50, 512, 512} renderer.Clear() renderer.SetDrawColor(255, 0, 0, 255) renderer.FillRect(&sdl.Rect{0, 0, int32(winWidth), int32(winHeight)}) renderer.Copy(texture, &src, &dst) renderer.Present() sdl.Delay(2000) return 0 }
func main() { var window *sdl.Window var renderer *sdl.Renderer var event sdl.Event var running bool window = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_SHOWN) if window == nil { fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", sdl.GetError()) os.Exit(1) } renderer = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED) if renderer == nil { fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", sdl.GetError()) os.Exit(2) } running = true for running { event = sdl.WaitEventTimeout(1000) // wait here until an event is in the event queue if event == nil { fmt.Println("WaitEventTimeout timed out") continue } switch t := event.(type) { case *sdl.QuitEvent: running = false case *sdl.MouseMotionEvent: fmt.Printf("[%d ms] MouseMotion\ttype:%d\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y, t.XRel, t.YRel) case *sdl.MouseButtonEvent: fmt.Printf("[%d ms] MouseButton\ttype:%d\tid:%d\tx:%d\ty:%d\tbutton:%d\tstate:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y, t.Button, t.State) case *sdl.MouseWheelEvent: fmt.Printf("[%d ms] MouseWheel\ttype:%d\tid:%d\tx:%d\ty:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y) case *sdl.KeyUpEvent: fmt.Printf("[%d ms] Keyboard\ttype:%d\tsym:%c\tmodifiers:%d\tstate:%d\trepeat:%d\n", t.Timestamp, t.Type, t.Keysym.Sym, t.Keysym.Mod, t.State, t.Repeat) } } renderer.Destroy() window.Destroy() }
func main() { var window *sdl.Window var renderer *sdl.Renderer var texture *sdl.Texture var src, dst sdl.Rect window = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_SHOWN) if window == nil { fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", sdl.GetError()) os.Exit(1) } renderer = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED) if renderer == nil { fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", sdl.GetError()) os.Exit(2) } image := img.Load(imageName) if image == nil { fmt.Fprintf(os.Stderr, "Failed to load PNG: %s\n", sdl.GetError()) os.Exit(3) } texture = renderer.CreateTextureFromSurface(image) if texture == nil { fmt.Fprintf(os.Stderr, "Failed to create texture: %s\n", sdl.GetError()) os.Exit(4) } src = sdl.Rect{0, 0, 512, 512} dst = sdl.Rect{100, 50, 512, 512} renderer.Clear() renderer.SetDrawColor(255, 0, 0, 255) renderer.FillRect(&sdl.Rect{0, 0, int32(winWidth), int32(winHeight)}) renderer.Copy(texture, &src, &dst) renderer.Present() sdl.Delay(2000) image.Free() texture.Destroy() renderer.Destroy() window.Destroy() }
func Draw(window *sdl.Window, parsedInput *ParsedInput, loop *bool) inputManager.Update { surface, err := window.GetSurface() if err != nil { panic(err) } surface.FillRect(&sdl.Rect{0, 0, surface.W, surface.H}, 0xff000000) var player2Rects [6]sdl.Rect for i := 0; i < 6; i++ { player2Rects[i] = sdl.Rect{(256 + player1Rects[i].X), (player1Rects[i].Y), (player1Rects[i].W), (player1Rects[i].H)} } return func(state int) int { if *loop { for i := 0; i < 6; i++ { if parsedInput[i] { surface.FillRect(&player1Rects[i], 0xffff0000) } else { surface.FillRect(&player1Rects[i], 0xffffff00) } if parsedInput[i+6] { surface.FillRect(&player2Rects[i], 0xffff0000) } else { surface.FillRect(&player2Rects[i], 0xffffff00) } } if parsedInput[12] { surface.FillRect(&sdl.Rect{192, 160, 64, 64}, 0xffff0000) } else { surface.FillRect(&sdl.Rect{192, 160, 64, 64}, 0xffffff00) } if parsedInput[13] { surface.FillRect(&sdl.Rect{192, 224, 64, 64}, 0xffff0000) } else { surface.FillRect(&sdl.Rect{192, 224, 64, 64}, 0xffffff00) } err := window.UpdateSurface() if err != nil { panic(err) } } else { return -1 } return 0 } }
func main() { var window *sdl.Window var renderer *sdl.Renderer var image *sdl.Surface var texture *sdl.Texture var src, dst sdl.Rect var err error window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_SHOWN) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err) os.Exit(1) } defer window.Destroy() renderer, err = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", err) os.Exit(2) } defer renderer.Destroy() image, err = sdl.LoadBMP(imageName) if err != nil { fmt.Fprintf(os.Stderr, "Failed to load BMP: %s\n", err) os.Exit(3) } defer image.Free() texture, err = renderer.CreateTextureFromSurface(image) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create texture: %s\n", err) os.Exit(4) } defer texture.Destroy() src = sdl.Rect{0, 0, 512, 512} dst = sdl.Rect{100, 50, 512, 512} renderer.Clear() renderer.Copy(texture, &src, &dst) renderer.Present() sdl.Delay(2000) }
func run() int { var window *sdl.Window var renderer *sdl.Renderer var event sdl.Event var running bool var err error window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_SHOWN) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err) return 1 } defer window.Destroy() renderer, err = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", err) return 2 } defer renderer.Destroy() running = true for running { for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch t := event.(type) { case *sdl.QuitEvent: running = false case *sdl.MouseMotionEvent: fmt.Printf("[%d ms] MouseMotion\ttype:%d\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y, t.XRel, t.YRel) case *sdl.MouseButtonEvent: fmt.Printf("[%d ms] MouseButton\ttype:%d\tid:%d\tx:%d\ty:%d\tbutton:%d\tstate:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y, t.Button, t.State) case *sdl.MouseWheelEvent: fmt.Printf("[%d ms] MouseWheel\ttype:%d\tid:%d\tx:%d\ty:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y) case *sdl.KeyUpEvent: fmt.Printf("[%d ms] Keyboard\ttype:%d\tsym:%c\tmodifiers:%d\tstate:%d\trepeat:%d\n", t.Timestamp, t.Type, t.Keysym.Sym, t.Keysym.Mod, t.State, t.Repeat) } } } return 0 }
// DestroyWindow closes the window, freeing any resources as it does so. // It must be called for each window that has been created before the program // exists. // // DestroyWindow will panic if: // // 1. The toolbox has not been initialised. // // 2. The window is invalid // // 3. CreateWindow has not been called. func DestroyWindow(window Window) { if !initialised { // this stops execution here, so ne need for an else after the if panic(notInitialisedMessage) } if renderer == nil { // this stops execution here, so ne need for an else after the if panic(windowNotInitialisedMessage) } if window == nil { panic("The Window is invalid. Have you used toolbox.CreateWindow(...)?") } var w *sdl.Window w = window if renderer != nil { // should always be true - see createWindow - but be defensive renderer.Destroy() } w.Destroy() }
// Create the graphics window using the SDl library or crash trying func createWindow(w, h int, title string) *sdl.Window { var window *sdl.Window var err error window, err = sdl.CreateWindow(title, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, w, h, sdl.WINDOW_SHOWN) if err != nil { panic(err) } // we're all good to create the renderer.... renderer, err = createRenderer(window) if err != nil { // Oops, we failed to create the renderer failed // cleanup the window before we panic window.Destroy() panic(err) } // at this point we have both a good renderer and a good window return window }
func main() { var window *sdl.Window var info sdl.SysWMInfo var subsystem string var err error window, err = sdl.CreateWindow("", 0, 0, 0, 0, sdl.WINDOW_HIDDEN) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err) os.Exit(1) } defer window.Destroy() sdl.VERSION(&info.Version) if window.GetWMInfo(&info) { switch info.Subsystem { case sdl.SYSWM_UNKNOWN: subsystem = "An unknown system!" case sdl.SYSWM_WINDOWS: subsystem = "Microsoft Windows(TM)" case sdl.SYSWM_X11: subsystem = "X Window System" case sdl.SYSWM_DIRECTFB: subsystem = "DirectFB" case sdl.SYSWM_COCOA: subsystem = "Apple OS X" case sdl.SYSWM_UIKIT: subsystem = "UIKit" } fmt.Printf("This program is running SDL version %d.%d.%d on %s\n", info.Version.Major, info.Version.Minor, info.Version.Patch, subsystem) } else { fmt.Fprintf(os.Stderr, "Couldn't get window information: %s\n", sdl.GetError()) } }
func main() { var ( err error window *sdl.Window context sdl.GLContext ) if err = sdl.Init(sdl.INIT_EVERYTHING); err != nil { panic(err) } defer sdl.Quit() sdl.GL_SetAttribute(sdl.GL_CONTEXT_PROFILE_MASK, sdl.GL_CONTEXT_PROFILE_COMPATIBILITY) window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_OPENGL) if err != nil { panic(err) } defer window.Destroy() context, err = sdl.GL_CreateContext(window) if err != nil { panic(err) } defer sdl.GL_DeleteContext(context) if err = sdl.GL_MakeCurrent(window, context); err != nil { panic(err) } if err = gl.Init(); err != nil { panic(err) } sdl.GL_SetSwapInterval(1) printGLInfo() setupGL() windowLoop(window) }
func main() { var ( err error window *sdl.Window context sdl.GLContext ) if err = sdl.Init(sdl.INIT_EVERYTHING); err != nil { panic(err) } defer sdl.Quit() sdl.GL_SetAttribute(sdl.GL_CONTEXT_MAJOR_VERSION, 3) sdl.GL_SetAttribute(sdl.GL_CONTEXT_MINOR_VERSION, 2) sdl.GL_SetAttribute(sdl.GL_CONTEXT_PROFILE_MASK, sdl.GL_CONTEXT_PROFILE_CORE) window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_OPENGL) if err != nil { panic(err) } defer window.Destroy() context, err = sdl.GL_CreateContext(window) if err != nil { panic(err) } defer sdl.GL_DeleteContext(context) if err = sdl.GL_MakeCurrent(window, context); err != nil { panic(err) } if err = gl.Init(); err != nil { panic(err) } printGLInfo() cameraMatrixUniform := setupGL() windowLoop(window, cameraMatrixUniform) }
func toggleFullscreen(window *sdl.Window) { isFullscreen := (window.GetFlags() & sdl.WINDOW_FULLSCREEN) != 0 if isFullscreen { window.SetFullscreen(0) } else { window.SetFullscreen(sdl.WINDOW_FULLSCREEN_DESKTOP) } }
func main() { var window *sdl.Window var renderer *sdl.Renderer sdl.Init(sdl.INIT_EVERYTHING) window = createWindow(winTitle, winHeight, winWidth) defer window.Destroy() renderer = createRenderer(window) defer renderer.Destroy() var secTickChan = time.Tick(time.Second) var events = &Events{} var fps = 0 var view = NewGameView() for { // Pump events events.GetEvents() // Pass events and renderer to view view.Render(renderer, events) // This structure logs the fps select { case <-secTickChan: log.Println("fps:", fps) fps = 0 default: fps++ } // Delay the next frame rendering to free up CPU sdl.Delay(13) } }
func NewWorldRenderer(window *sdl.Window, w *gamestate.World) *WorldRenderer { width, height := window.GetSize() return &WorldRenderer{ Proj: mgl.Perspective(90, float32(width)/float32(height), 0.3, 1000), View: mgl.Ident4(), ClippingPlane_ws: mgl.Vec4{1, 0, 0, -1000000}, Textures: NewTextures(w.HeightMap), HeightMapRenderer: NewHeightMapRenderer(), WaterRendererA: NewSurfaceWaterRenderer(), WaterRendererB: NewDebugWaterRenderer(), MeshRenderer: NewMeshRenderer(), PortalRenderer: NewPortalRenderer(), TreeRenderer: NewTreeRenderer(), SkyboxRenderer: NewSkyboxRenderer(), Skybox: &Skybox{}, ParticleSystem: particles.NewParticleSystem(w, 10000, mgl.Vec3{32, 32, 32}, 1, 250), Framebuffer: [2]*FrameBuffer{NewFrameBuffer(window.GetSize()), NewFrameBuffer(window.GetSize())}, ScreenQuad: &ScreenQuad{}, ScreenQuadRenderer: NewScreenQuadRenderer(), DebugRenderer: NewLineRenderer(), MaxRecursion: 1, } }
func run() int { var window *sdl.Window var font *ttf.Font var surface *sdl.Surface var solid *sdl.Surface var err error sdl.Init(sdl.INIT_VIDEO) if err := ttf.Init(); err != nil { fmt.Fprintf(os.Stderr, "Failed to initialize TTF: %s\n", err) return 1 } if window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_SHOWN); err != nil { fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err) return 2 } defer window.Destroy() if font, err = ttf.OpenFont("../../assets/test.ttf", 32); err != nil { fmt.Fprint(os.Stderr, "Failed to open font: %s\n", err) return 4 } defer font.Close() if solid, err = font.RenderUTF8_Solid("Hello, World!", sdl.Color{255, 0, 0, 255}); err != nil { fmt.Fprint(os.Stderr, "Failed to render text: %s\n", err) return 5 } defer solid.Free() if surface, err = window.GetSurface(); err != nil { fmt.Fprint(os.Stderr, "Failed to get window surface: %s\n", err) return 6 } if err = solid.Blit(nil, surface, nil); err != nil { fmt.Fprint(os.Stderr, "Failed to put text on window surface: %s\n", err) return 7 } // Show the pixels for a while window.UpdateSurface() sdl.Delay(3000) return 0 }
func main() { var window *sdl.Window var renderer *sdl.Renderer var event sdl.Event var running bool window = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_SHOWN) if window == nil { fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", sdl.GetError()) os.Exit(1) } renderer = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED) if renderer == nil { fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", sdl.GetError()) os.Exit(2) } var peepArray []sdl.Event = make([]sdl.Event, 2) peepArray[0] = &sdl.UserEvent{sdl.USEREVENT, sdl.GetTicks(), window.GetID(), 1331, nil, nil} peepArray[1] = &sdl.UserEvent{sdl.USEREVENT, sdl.GetTicks(), window.GetID(), 10101, nil, nil} running = true lastPushTime := sdl.GetTicks() for running { if lastPushTime+pushTime < sdl.GetTicks() { lastPushTime = sdl.GetTicks() sdl.PumpEvents() numEventsHandled := sdl.PeepEvents(peepArray, sdl.ADDEVENT, sdl.FIRSTEVENT, sdl.LASTEVENT) if numEventsHandled < 0 { fmt.Printf("PeepEvents error: %s\n", sdl.GetError()) } else { fmt.Printf("Successful push of %d events\n", numEventsHandled) } } for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch t := event.(type) { case *sdl.QuitEvent: running = false case *sdl.MouseMotionEvent: fmt.Printf("[%d ms] MouseMotion\ttype:%d\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y, t.XRel, t.YRel) case *sdl.MouseButtonEvent: fmt.Printf("[%d ms] MouseButton\ttype:%d\tid:%d\tx:%d\ty:%d\tbutton:%d\tstate:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y, t.Button, t.State) case *sdl.MouseWheelEvent: fmt.Printf("[%d ms] MouseWheel\ttype:%d\tid:%d\tx:%d\ty:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y) case *sdl.KeyUpEvent: fmt.Printf("[%d ms] Keyboard\ttype:%d\tsym:%c\tmodifiers:%d\tstate:%d\trepeat:%d\n", t.Timestamp, t.Type, t.Keysym.Sym, t.Keysym.Mod, t.State, t.Repeat) case *sdl.UserEvent: fmt.Printf("[%d ms] UserEvent\tcode:%d\n", t.Timestamp, t.Code) } } sdl.Delay(1000 / 30) } renderer.Destroy() window.Destroy() }
func main() { var window *sdl.Window var context sdl.GLContext var event sdl.Event var running bool var err error runtime.LockOSThread() if err = sdl.Init(sdl.INIT_EVERYTHING); err != nil { panic(err) } defer sdl.Quit() window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_OPENGL) if err != nil { panic(err) } defer window.Destroy() context, err = sdl.GL_CreateContext(window) if err != nil { panic(err) } defer sdl.GL_DeleteContext(context) gl.Init() gl.Viewport(0, 0, gl.Sizei(winWidth), gl.Sizei(winHeight)) // OPENGL FLAGS gl.ClearColor(0.0, 0.1, 0.0, 1.0) gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LESS) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) // VERTEX BUFFER var vertexbuffer gl.Uint gl.GenBuffers(1, &vertexbuffer) gl.BindBuffer(gl.ARRAY_BUFFER, vertexbuffer) gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(len(triangle_vertices)*4), gl.Pointer(&triangle_vertices[0]), gl.STATIC_DRAW) // COLOUR BUFFER var colourbuffer gl.Uint gl.GenBuffers(1, &colourbuffer) gl.BindBuffer(gl.ARRAY_BUFFER, colourbuffer) gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(len(triangle_colours)*4), gl.Pointer(&triangle_colours[0]), gl.STATIC_DRAW) // GUESS WHAT program := createprogram() // VERTEX ARRAY var VertexArrayID gl.Uint gl.GenVertexArrays(1, &VertexArrayID) gl.BindVertexArray(VertexArrayID) gl.EnableVertexAttribArray(0) gl.BindBuffer(gl.ARRAY_BUFFER, vertexbuffer) gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 0, nil) // VERTEX ARRAY HOOK COLOURS gl.EnableVertexAttribArray(1) gl.BindBuffer(gl.ARRAY_BUFFER, colourbuffer) gl.VertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, 0, nil) //UNIFORM HOOK unistring := gl.GLString("scaleMove") UniScale = gl.GetUniformLocation(program, unistring) fmt.Printf("Uniform Link: %v\n", UniScale+1) gl.UseProgram(program) running = true for running { for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch t := event.(type) { case *sdl.QuitEvent: running = false case *sdl.MouseMotionEvent: xrot = float32(t.Y) / 2 yrot = float32(t.X) / 2 fmt.Printf("[%dms]MouseMotion\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n", t.Timestamp, t.Which, t.X, t.Y, t.XRel, t.YRel) } } drawgl() sdl.GL_SwapWindow(window) } }
func NewPPU(window *sdl.Window, vram *VRAM) *PPU { return &PPU{window: window, surface: window.GetSurface(), vram: vram} }
// Initialize func main() { var window *sdl.Window var renderer *sdl.Renderer var err error window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, int(winWidth), int(winHeight), sdl.WINDOW_SHOWN) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err) os.Exit(1) } defer window.Destroy() renderer, err = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", err) os.Exit(2) } defer renderer.Destroy() tilesetImg, err := img.Load("assets/textures/ts1.png") if err != nil { fmt.Fprintf(os.Stderr, "Failed to load PNG: %s\n", err) os.Exit(3) } defer tilesetImg.Free() tilesetTxt, err := renderer.CreateTextureFromSurface(tilesetImg) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create texture: %s\n", err) os.Exit(4) } defer tilesetTxt.Destroy() spritesheetImg, err := img.Load("assets/textures/actor3.png") if err != nil { fmt.Fprintf(os.Stderr, "Failed to load PNG: %s\n", err) os.Exit(3) } defer spritesheetImg.Free() spritesheetTxt, err := renderer.CreateTextureFromSurface(spritesheetImg) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create texture: %s\n", err) os.Exit(4) } defer spritesheetTxt.Destroy() err = ttf.Init() font, err = ttf.OpenFont("assets/textures/PressStart2P.ttf", 18) if err != nil { fmt.Fprintf(os.Stderr, "Failed to load font file: %s\n", err) os.Exit(6) } if err := sdl.Init(sdl.INIT_AUDIO); err != nil { fmt.Fprintf(os.Stderr, "Failed to load music file: %s\n", err) os.Exit(7) } if err := mix.Init(mix.INIT_MP3); err != nil { fmt.Fprintf(os.Stderr, "Failed to load music file: %s\n", err) os.Exit(8) } currScene = &eng.Scene{ Window: eng.Window{winWidth, winHeight}, StartTime: time.Now(), Cam: eng.Camera{0, 0}, EnemyCount: 99, TsTxt: tilesetTxt, SsTxt: spritesheetTxt, Font: font, } currScene.Init(renderer) var running bool = true for running { then := time.Now() running = catchEvents() //currScene.Update() currScene.Render(renderer) dur := time.Since(then) sdl.Delay(40 - uint32(dur.Nanoseconds()/1000000)) currScene.FrameCounter++ } }
func run(serverStr, name string) int { var window *sdl.Window var renderer *sdl.Renderer var running bool var event sdl.Event var inputState InputState url := "ws://" + serverStr origin := "http://" + serverStr ws, err := websocket.Dial(url, "", origin) if err != nil { fmt.Printf("%v\n", err) os.Exit(1) } window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_SHOWN) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err) return 1 } defer window.Destroy() renderer, err = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", err) return 2 } defer renderer.Destroy() renderer.Clear() websocket.Message.Send(ws, joinMessage(name)) running = true var data map[string]map[string][]map[string]interface{} decoder := msgpack.NewDecoder(ws) renderer.SetDrawColor(1, 1, 1, 255) renderer.Clear() for running { renderer.SetDrawColor(1, 1, 1, 255) renderer.Clear() newInputState := inputState for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch event.(type) { case *sdl.QuitEvent: running = false case *sdl.KeyDownEvent: switch event.(*sdl.KeyDownEvent).Keysym.Sym { case sdl.K_ESCAPE: running = false case sdl.K_LEFT: newInputState.Left = true case sdl.K_RIGHT: newInputState.Right = true case sdl.K_UP: newInputState.Thrust = true } case *sdl.KeyUpEvent: switch event.(*sdl.KeyUpEvent).Keysym.Sym { case sdl.K_LEFT: newInputState.Left = false case sdl.K_RIGHT: newInputState.Right = false case sdl.K_UP: newInputState.Thrust = false } } } if !newInputState.IsSameAs(inputState) { websocket.Message.Send(ws, newInputState.Message()) inputState = newInputState } decoder.Decode(&data) for _, ship := range data["state"]["ships"] { colourhex, _ := hex.DecodeString(ship["colour"].(string)[1:7]) rot, _ := ship["rotation"].(float64) s := Ship{ Position: V2{float64(ship["x"].(int64)), float64(ship["y"].(int64))}, Size: 30, Rotation: rot, Doge: ship["doge"].(bool), } renderer.SetDrawColor(uint8(colourhex[0]), uint8(colourhex[1]), uint8(colourhex[2]), 255) renderer.DrawLines(s.Points()) } renderer.Present() } return 0 }
// win==nil requests a full-screen window func Run(win WindowSpec) int { // All SDL calls must come from same thread. runtime.LockOSThread() defer runtime.UnlockOSThread() err := sdl.Init(sdl.INIT_EVERYTHING) if err != nil { panic(err) } defer sdl.Quit() // Install audio callback spec := &sdl.AudioSpec{ Freq: SampleRate, Format: sdl.AUDIO_F32SYS, Channels: 1, Samples: 4096, Callback: sdl.AudioCallback(C.getSoundSamplesAdaptor), } audioDevice, err := sdl.OpenAudioDevice("", false, spec, nil, 0) if err != nil { fmt.Fprintf(os.Stderr, "Failed to open audio device: %v\n", err) panic(err) } if audioDevice < 2 { fmt.Fprintf(os.Stderr, "Audio device=%v < 2 contrary to SDL-2 documentation\n", audioDevice, err) } sdl.PauseAudioDevice(audioDevice, false) // Create window var window *sdl.Window if win != nil { // Partial screen winWidth, winHeight := win.Size() window, err = sdl.CreateWindow(win.Title(), sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, int(winWidth), int(winHeight), sdl.WINDOW_SHOWN) } else { // Full screen if isMacOS { // Contrary to https://wiki.libsdl.org/SDL_CreateWindow, on MacOS 10.11.1 // a call to sdl.CreateWindow in fullscreen mode *does* use the w and h parameters. // So ask what the display size is. var mode sdl.DisplayMode err = sdl.GetDesktopDisplayMode(0, &mode) if err != nil { fmt.Fprintf(os.Stderr, "Cannot get desktop display mode") panic(err) } window, err = sdl.CreateWindow("", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, int(mode.W), int(mode.H), sdl.WINDOW_SHOWN|sdl.WINDOW_FULLSCREEN_DESKTOP) } else { window, err = sdl.CreateWindow("", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 0, 0, sdl.WINDOW_SHOWN|sdl.WINDOW_FULLSCREEN_DESKTOP) } } if err != nil { fmt.Fprintf(os.Stderr, "Failed to create window: %v\n", err) panic(err) } defer window.Destroy() // Create renderer width, height := window.GetSize() renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED|sdl.RENDERER_PRESENTVSYNC) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create renderer: %v\n", err) panic(err) } defer renderer.Destroy() var nTex = 1 if isMacOS { // Work around MacOS bug via double buffering nTex = 2 } // Create texture tex := make([]*sdl.Texture, nTex) for i := range tex { tex[i], err = renderer.CreateTexture(sdl.PIXELFORMAT_ARGB8888, sdl.TEXTUREACCESS_STREAMING, width, height) if err != nil { fmt.Fprintf(os.Stderr, "renderer.CreateTexture: %v\n", err) panic(err) } defer tex[i].Destroy() } for _, r := range renderClientList { r.Init(int32(width), int32(height)) } // Loop until quit for i := 0; ; i ^= nTex - 1 { for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch e := event.(type) { case *sdl.QuitEvent: return 0 case *sdl.MouseMotionEvent: // Go equivalent of SDL_PRESSED seems to be missing, so compare with zero. if e.State != 0 { forwardMouseEvent(MouseDrag, int32(e.X), int32(e.Y)) } else { forwardMouseEvent(MouseMove, int32(e.X), int32(e.Y)) } case *sdl.MouseButtonEvent: switch e.Type { case sdl.MOUSEBUTTONDOWN: forwardMouseEvent(MouseDown, int32(e.X), int32(e.Y)) case sdl.MOUSEBUTTONUP: forwardMouseEvent(MouseUp, int32(e.X), int32(e.Y)) } case *sdl.KeyDownEvent: var k Key if 0x20 <= e.Keysym.Sym && e.Keysym.Sym < 0x7F { // Printable ASCII k = Key(e.Keysym.Sym) } else { // Try special character table k = keyMap[e.Keysym.Sym] } if k != 0 { forwardKeyEvent(k) } } } pixels, pitch := lockTexture(tex[i], width, height) pm := MakePixMap(int32(width), int32(height), pixels, int32(pitch)) for _, r := range renderClientList { r.Render(pm) } tex[i].Unlock() err := renderer.Clear() if err != nil { fmt.Fprintf(os.Stderr, "renderer.Clear: %v", err) panic(err) } renderer.Copy(tex[i], nil, nil) if err != nil { fmt.Fprintf(os.Stderr, "renderer.Copy: %v", err) panic(err) } renderer.Present() } }