// Initializes SDL func (e *Engine) Init() (err error) { err = sdl.Init(sdl.INIT_VIDEO | sdl.INIT_AUDIO) if err != nil { return } err = mix.Init(mix.INIT_OGG) if err != nil { return } err = ttf.Init() if err != nil { return } err = mix.OpenAudio(mix.DEFAULT_FREQUENCY, mix.DEFAULT_FORMAT, mix.DEFAULT_CHANNELS, 3072) if err != nil { return } e.Window, err = sdl.CreateWindow(WinTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, WinWidth, WinHeight, sdl.WINDOW_SHOWN) if err != nil { return } e.Renderer, err = sdl.CreateRenderer(e.Window, -1, sdl.RENDERER_ACCELERATED) if err != nil { return } return }
/* Build creates a Controller struct. NOTE: locks the OS thread. Make sure to call all Controller.func from the exact same goroutine, otherwise the SDL context may crash! */ func Build() (*Controller, error) { // MUST guarantee controller always runs in same OS thread runtime.LockOSThread() sdl.Init(sdl.INIT_EVERYTHING) // prepare basic Controller stuff c := &Controller{} c.renderable = makeOrdered() c.entities = make(map[string]interface{}) c.keyreceivers = make(map[string][]*keyReceive) c.textCache = make(map[string]*sdl.Texture) // window window, err := sdl.CreateWindow("Orbiting", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 800, 600, sdl.WINDOW_SHOWN) if err != nil { return nil, err } c.window = window // create renderer (reference kept by window, no need to do it ourselves) renderer, err := sdl.CreateRenderer(c.window, -1, 0) if err != nil { return nil, err } // however, we DO want to keep just a single instance of the wrapper c.wrapped = &Renderer{renderer: renderer} return c, nil }
func main() { if err := sdl.Init(sdl.INIT_AUDIO); err != nil { log.Println(err) return } if err := mix.Init(mix.INIT_MP3); err != nil { log.Println(err) return } defer sdl.Quit() defer mix.Quit() defer mix.CloseAudio() if err := mix.OpenAudio(22050, mix.DEFAULT_FORMAT, 2, 4096); err != nil { log.Println(err) return } if music, err := mix.LoadMUS("test.mp3"); err != nil { log.Println(err) } else if err = music.Play(1); err != nil { log.Println(err) } else { sdl.Delay(5000) music.Free() } }
func start() { if sdl.Init(sdl.INIT_AUDIO) < 0 { fmt.Fprintf(os.Stderr, "Failed to init:", sdl.GetError()) os.Exit(1) } window = sdl.CreateWindow("test", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, int(W), int(H), 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|sdl.RENDERER_PRESENTVSYNC) if renderer == nil { fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", sdl.GetError()) os.Exit(1) } mix.SetSoundFonts("resources/") if !mix.OpenAudio(44100, mix.DEFAULT_FORMAT, 2, 2048) { fmt.Fprintf(os.Stderr, "Failed to open audio:%s\n", sdl.GetError()) os.Exit(1) } }
func (dmg *DMG) Run(file string, width, height int) { sdl.Init(sdl.INIT_VIDEO) defer sdl.Quit() dmg.init(file, width, height) dmg.exec() }
func Init() error { // This is needed to make sure that we are always running the graphics code in the same OS thread as it is initialized in. runtime.LockOSThread() if runtime.GOMAXPROCS(-1) == 1 { runtime.GOMAXPROCS(2) } return sdl.Init(sdl.INIT_EVERYTHING) }
func OpenDisplay(width uint32, height uint32) (*Display, error) { display := Display{} sdl.Init(sdl.INIT_EVERYTHING) window, err := sdl.CreateWindow("test", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, int(width), int(height), sdl.WINDOW_SHOWN) if err != nil { return nil, err } display.window = window return &display, nil }
// NewAdaptor returns a new Joystick Adaptor. func NewAdaptor() *Adaptor { return &Adaptor{ name: "Joystick", connect: func(j *Adaptor) (err error) { sdl.Init(sdl.INIT_JOYSTICK) if sdl.NumJoysticks() > 0 { j.joystick = sdl.JoystickOpen(0) return } return errors.New("No joystick available") }, } }
// NewJoystickAdaptor returns a new JoystickAdaptor with specified name. func NewJoystickAdaptor(name string) *JoystickAdaptor { return &JoystickAdaptor{ name: name, connect: func(j *JoystickAdaptor) (err error) { sdl.Init(sdl.INIT_JOYSTICK) if sdl.NumJoysticks() > 0 { j.joystick = sdl.JoystickOpen(0) return } return errors.New("No joystick available") }, } }
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 initSDL() *sdl.Window { sdl.Init(sdl.INIT_EVERYTHING) window, err := sdl.CreateWindow("Game of Life", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 1280, 720, sdl.WINDOW_OPENGL) if err != nil { panic(err) } return window }
func (engine *GameEngine) Init(name string) { sdl.Init(sdl.INIT_EVERYTHING) ttf.Init() engine.window = sdl.CreateWindow(name, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 800, 600, sdl.WINDOW_SHOWN) engine.renderer = sdl.CreateRenderer(engine.window, -1, sdl.RENDERER_ACCELERATED|sdl.RENDERER_PRESENTVSYNC) engine.Input = &InputHandler{} engine.Assets = &AssetsManager{} engine.Input.Init() engine.Assets.Init(engine) engine.Running = true engine.states = NewStack() }
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 sdlInit() { sdl.Init(sdl.INIT_EVERYTHING) // init the image subsystem imgFlags := img.INIT_PNG | img.INIT_JPG if imgFlags != img.Init(imgFlags) { panic(fmt.Sprintf("Error initializing img: %v\n", img.GetError())) } // init the TTF subsystem if err := ttf.Init(); err != nil { panic(fmt.Sprintf("Error initializing ttf: %v\n", err)) } }
func main() { if len(os.Args) < 2 { fmt.Println("usage: emuChip8 <game>") os.Exit(1) } sdl.Init(sdl.INIT_EVERYTHING) window, err := sdl.CreateWindow("Chip8", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, WIN_WIDTH, WIN_HEIGHT, sdl.WINDOW_SHOWN) if err != nil { panic(err) } defer window.Destroy() surface, err := window.GetSurface() if err != nil { panic(err) } chip := new(Chip8) chip.surface = surface chip.init() chip.loadGame(os.Args[1]) chip.quit = false for !chip.quit { if err := chip.emulateCycle(); err != nil { fmt.Println(err) } if chip.drawFlag { chip.drawGraphics() window.UpdateSurface() chip.drawFlag = false } chip.setKeys() sdl.Delay(1) } chip.cleanup() }
// Init is used for a starting procedure of the game which will do things // such as creating the SDL Window, creating the SDL Renderer, Start the Scene, etc. func Init(title string, x, y, width, height int, fullscreen bool) { var flags uint32 if fullscreen { flags = sdl.WINDOW_FULLSCREEN } err := sdl.Init(sdl.INIT_EVERYTHING) if err != nil { log.Fatalln("Game // Error whie initialising SDL") log.Panic(err) } log.Println("Game // SDL Initialised") win, err := sdl.CreateWindow(title, x, y, width, height, flags) if err != nil { log.Fatalln("Game // Error while creating window") log.Panic(err) } log.Println("Game // Created Window") ren, err := sdl.CreateRenderer(win, -1, 0) if err != nil { log.Fatalln("Game // Error while creating renderer") log.Panic(err) } ren.SetDrawColor(255, 255, 255, 255) log.Println("Game // Created Renderer") log.Println("Game // Init Success") window = win renderer = ren input.InitJoystick() running = true currentScene.Enter() }
func (d *Display) init() (err error) { err = sdl.Init(sdl.INIT_VIDEO) if err != nil { return err } d.window, err = sdl.CreateWindow("anna", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 1400, 500, sdl.WINDOW_SHOWN) if err != nil { return err } d.renderer, err = sdl.CreateRenderer(d.window, -1, sdl.RENDERER_ACCELERATED) if err != nil { return err } return nil }
func Run(cont func()) { runtime.LockOSThread() defer panic("shouldn't exit") sdl.Init(sdl.INIT_EVERYTHING) ttf.Init() err := mix.OpenAudio(mix.DEFAULT_FREQUENCY, mix.DEFAULT_FORMAT, 2, 4096) if err != nil { fmt.Printf("Could not open audio: %v\n", err) } go cont() for { (<-execChan)() } }
func main() { if err := sdl.Init(sdl.INIT_AUDIO); err != nil { log.Println(err) return } defer sdl.Quit() spec := &sdl.AudioSpec{ Freq: sampleHz, Format: sdl.AUDIO_U8, Channels: 2, Samples: sampleHz, Callback: sdl.AudioCallback(C.SineWave), } sdl.OpenAudio(spec, nil) sdl.PauseAudio(false) time.Sleep(1 * time.Second) }
// The programs main function func main() { // ---- This is the start of Owen's graphics setup code ---- // First we have to initalise the SDL library, before we can use it sdl.Init(sdl.INIT_EVERYTHING) // defer is a go keyword and a special feature. // This means that go will automatically call the function sdl.Quit() before // the program exits for us. We don't have to remember to put this at the end! defer sdl.Quit() // if you want to change these try 800 for the width and 600 for the height windowWidth = 1024 windowHeight = 768 // Now we have to create the window we want to use. // We need to tell the SDL library how big to make the window of the correct // size - that's what the bit in the brackets does window = createWindow(windowWidth, windowHeight) // automatically destroy the window when the program finishes defer window.Destroy() // Now we have a window we need to create a renderer so we can draw into // it. In this case we want to use the first graphics card that supports faster // drawing renderer = createRenderer(window) // automatically destroy the renderer when the program exits. defer renderer.Destroy() // Set a black i.e. RGBA (0,0,0,0) background colour and clear the window renderer.SetDrawColor(0, 0, 0, 0) renderer.Clear() // ---- This is the end of Owen's graphics setup code ---- // defer any cleanup actions defer cleanup() // initialise the games variables. initialise() // render everything initially so that we can see the game before it starts render() // now start the main game loop of the game. gameMainLoop() }
func GetJs() (*sdl.Joystick, error) { sdl.Init(sdl.INIT_JOYSTICK) numJs := sdl.NumJoysticks() if numJs == 0 { return nil, errors.New("Joystick missing.") } jsIdx := -1 for i := 0; i < numJs; i++ { if sdl.JoystickNameForIndex(i) == "T.Flight Hotas X" { jsIdx = i break } } if jsIdx == -1 { return nil, errors.New("Unsuported joystick!") } return sdl.JoystickOpen(jsIdx), nil }
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 main() { sdl.Init(sdl.INIT_EVERYTHING) window, err := sdl.CreateWindow("test", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 800, 600, sdl.WINDOW_SHOWN) if err != nil { panic(err) } defer window.Destroy() surface, err := window.GetSurface() if err != nil { panic(err) } rect := sdl.Rect{0, 0, 200, 200} surface.FillRect(&rect, 0xffff0000) window.UpdateSurface() sdl.Delay(1000) sdl.Quit() }
func TestTexture(t *testing.T) { if sdl.Init(sdl.INIT_EVERYTHING) != 0 { panic(sdl.GetError()) } defer sdl.Quit() window := sdl.CreateWindow("test", 0, 0, 320, 200, sdl.WINDOW_HIDDEN|sdl.WINDOW_OPENGL) defer window.Destroy() sdl.GL_SetAttribute(sdl.GL_CONTEXT_PROFILE_MASK, sdl.GL_CONTEXT_PROFILE_ES) sdl.GL_SetAttribute(sdl.GL_CONTEXT_MAJOR_VERSION, 2) sdl.GL_SetAttribute(sdl.GL_CONTEXT_MINOR_VERSION, 0) glctx := sdl.GL_CreateContext(window) defer sdl.GL_DeleteContext(glctx) tex := gl.CreateTexture() gl.BindTexture(gl.TEXTURE_2D, tex) if !gl.IsTexture(tex) { t.Error(tex, "is not a texture") } }
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) } }
// Creates a window with the configuration of it's receiver func (w *Window) Create() { // Initialize and create the window sdl.Init(sdl.INIT_EVERYTHING) window, err := sdl.CreateWindow( w.WindowName, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, w.Width, w.Height, sdl.WINDOW_SHOWN) if err != nil { panic(err) } // If successful, set the window w.Window = window // Grab the window surface w.surface, err = window.GetSurface() if err != nil { panic(err) } // Create and attach a backbuffer to work with w.Backbuffer = backbuffer.New( w.Width, w.Height, w.surface.BytesPerPixel(), w.surface.Pixels()) // Fill background black and update w.Backbuffer.Fill(0xFF000000) w.Update() }
func (d *Display) Reset() { var err error if err = sdl.Init(sdl.INIT_EVERYTHING); err != nil { log.Panicln(err) } d.Window, err = sdl.CreateWindow("go chip8", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, X*10, Y*10, sdl.WINDOW_SHOWN) if err != nil { log.Panicln(err) } d.Surface, err = d.Window.GetSurface() if err != nil { log.Panicln(err) } d.Cells = make([][]byte, Y) for i := range d.Cells { d.Cells[i] = make([]byte, X) } d.Clear() }
func main() { sdl.Init(sdl.INIT_VIDEO) defer sdl.Quit() window, err := sdl.CreateWindow("Tank", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, windowWidth, windowHeight, 0) if err != nil { panic(err) } defer window.Destroy() mainloop: for { for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch event.(type) { case *sdl.QuitEvent: break mainloop } } windowSurface, _ := window.GetSurface() windowSurface.FillRect(nil, sdl.MapRGB(windowSurface.Format, 0, 0, 0)) window.UpdateSurface() sdl.Delay(1000) } }
// Start the game and enter the event loop func (g *Game) Start() { g.playing = true g.gameArea = newGameArea() r := sdl.Rect{650, 150, 300, 300} g.scoreBoard = &scoreBoard{area: &r} //g.scoreBoard.area = &r //TODO: make these values take them from somewhere //g.gameArea.color = 0x000dd0c // get some settings g.settings = &misc.GfxSettings{} running := true // Random seed //s1 := rand.NewSource(time.Now().UnixNano()) sdl.Init(sdl.INIT_EVERYTHING) g.window, err = sdl.CreateWindow("test", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 1200, 800, sdl.WINDOW_SHOWN) if err != nil { panic(err) } // Draw the nice background g.drawGameArea() g.drawScoreBoard() // freshen the screen //g.updateScreen() fmt.Println("it works") 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.KeyDownEvent: if t.Keysym.Sym == 27 { // Quit on ESC, fix this better later running = false } else { g.handleKeyStroke(t.Keysym) } case *sdl.KeyUpEvent: // handleKeyPress(false, t.Keysym) } } g.tick() } sdl.Quit() }