func initSDL() { if sdl.Init(sdl.INIT_VIDEO) != 0 { panic(sdl.GetError()) } if ttf.Init() != 0 { panic(sdl.GetError()) } font := ttf.OpenFont("testdata/VeraMono.ttf", 20) if font == nil { panic(sdl.GetError()) } appSurface = sdl.SetVideoMode(640, 480, 32, 0) sdlrenderer = NewSDLRenderer(sdl.CreateRGBSurface(sdl.SRCALPHA, 640, 480, 32, 0, 0, 0, 0), font) sdlrenderer.GetSurface().SetAlpha(sdl.SRCALPHA, 0xaa) console = NewConsole(nil) go func() { for { select { case rects := <-sdlrenderer.UpdatedRectsCh(): render(rects) } } }() render(nil) }
func initSDLSubSystems(app *spectrum.Application) error { if sdl.Init(sdl.INIT_VIDEO|sdl.INIT_AUDIO|sdl.INIT_JOYSTICK) != 0 { return errors.New(sdl.GetError()) } if ttf.Init() != 0 { return errors.New(sdl.GetError()) } if sdl.NumJoysticks() > 0 { // Open joystick joystick = sdl.JoystickOpen(DEFAULT_JOYSTICK_ID) if joystick != nil { if app.Verbose { app.PrintfMsg("Opened Joystick %d", DEFAULT_JOYSTICK_ID) app.PrintfMsg("Name: %s", sdl.JoystickName(DEFAULT_JOYSTICK_ID)) app.PrintfMsg("Number of Axes: %d", joystick.NumAxes()) app.PrintfMsg("Number of Buttons: %d", joystick.NumButtons()) app.PrintfMsg("Number of Balls: %d", joystick.NumBalls()) } } else { return errors.New("Couldn't open Joystick!") } } sdl.WM_SetCaption("GoSpeccy - ZX Spectrum Emulator", "") sdl.EnableUNICODE(1) return nil }
func main() { if sdl.Init(sdl.INIT_VIDEO) != 0 { panic(sdl.GetError()) } defer sdl.Quit() screen := sdl.SetVideoMode(400, 300, 32, 0) if screen == nil { panic(sdl.GetError()) } sdl.WM_SetCaption("Template", "") ticker := time.NewTicker(1e9 / 2 /*2 Hz*/) loop: for { select { case <-ticker.C: // Note: For better efficiency, use UpdateRects instead of Flip screen.FillRect(nil /*color*/, rand.Uint32()) //screen.Blit(&sdl.Rect{x,y, 0, 0}, image, nil) screen.Flip() case event := <-sdl.Events: fmt.Printf("%#v\n", event) switch event.(type) { case sdl.QuitEvent: break loop } } } }
func BenchmarkRendering(b *testing.B) { if sdl.Init(sdl.INIT_EVERYTHING) != 0 { log.Fatal(sdl.GetError()) } screen := smslib.NewSDL2xScreen(false) displayLoop := smslib.NewSDLLoop(screen) go displayLoop.Run() sms := smslib.NewSMS(displayLoop) sms.LoadROM("../roms/blockhead.sms") numOfGeneratedFrames := 100 generatedFrames := make([]smslib.DisplayData, numOfGeneratedFrames) for i := 0; i < numOfGeneratedFrames; i++ { generatedFrames = append(generatedFrames, *sms.RenderFrame()) } b.ResetTimer() for i := 0; i < b.N; i++ { for _, frame := range generatedFrames { displayLoop.Display() <- &frame } } }
func NewVideo(caption string, events chan Event, framePool *sync.Pool, fps float64) (video *SDLVideo, err error) { video = &SDLVideo{ input: make(chan []uint8), events: events, framePool: framePool, palette: SDLPalette, overscan: true, caption: caption, fps: fps, } for i, _ := range video.palette { video.palette[i] <<= 8 } if sdl.Init(sdl.INIT_VIDEO|sdl.INIT_JOYSTICK|sdl.INIT_AUDIO) != 0 { err = errors.New(sdl.GetError()) return } video.screen = sdl.SetVideoMode(512, 480, 32, sdl.OPENGL|sdl.RESIZABLE|sdl.GL_DOUBLEBUFFER) if video.screen == nil { err = errors.New("Error setting video mode") return } sdl.WM_SetCaption("nintengo - "+video.caption, "") video.initGL() video.Reshape(int(video.screen.W), int(video.screen.H)) return }
func NewSDLRenderer(surface *sdl.Surface, font *ttf.Font) *SDLRenderer { renderer := &SDLRenderer{ Color: sdl.Color{255, 255, 255, 0}, visibleSurface: surface, Font: font, eventCh: make(chan interface{}), updatedRectsCh: make(chan []sdl.Rect), updatedRects: make([]sdl.Rect, 0), width: uint(surface.W), height: uint(surface.H), } fontWidth, fontHeight, err := font.SizeText("A") if err != 0 { panic("failed to determine font dimensions: " + sdl.GetError()) } renderer.fontWidth = uint(fontWidth) renderer.fontHeight = uint(fontHeight) renderer.lastVisibleLine = (renderer.height / renderer.fontHeight) * MAX_INTERNAL_SIZE_FACTOR renderer.internalSurfaceHeight = uint(renderer.fontHeight) renderer.internalSurfaceMaxHeight = renderer.height * MAX_INTERNAL_SIZE_FACTOR renderer.internalSurface = sdl.CreateRGBSurface(sdl.SWSURFACE, int(renderer.width), int(renderer.internalSurfaceHeight), 32, 0, 0, 0, 0) renderer.calcCommandLineRect() renderer.updatedRects = append(renderer.updatedRects, sdl.Rect{0, 0, uint16(renderer.width), uint16(renderer.height)}) go renderer.loop() return renderer }
func (v *Video) Init(t <-chan []uint32, n string) chan [2]int { v.tick = t v.resize = make(chan [2]int) if sdl.Init(sdl.INIT_VIDEO|sdl.INIT_JOYSTICK|sdl.INIT_AUDIO) != 0 { log.Fatal(sdl.GetError()) } v.screen = sdl.SetVideoMode(512, 480, 32, sdl.OPENGL|sdl.RESIZABLE) if v.screen == nil { log.Fatal(sdl.GetError()) } sdl.WM_SetCaption(fmt.Sprintf("Fergulator - %s", n), "") if gl.Init() != 0 { panic(sdl.GetError()) } gl.Enable(gl.TEXTURE_2D) v.Reshape(int(v.screen.W), int(v.screen.H)) v.tex = gl.GenTexture() v.fpsmanager = gfx.NewFramerate() v.fpsmanager.SetFramerate(70) joy = make([]*sdl.Joystick, sdl.NumJoysticks()) for i := 0; i < sdl.NumJoysticks(); i++ { joy[i] = sdl.JoystickOpen(i) fmt.Println("-----------------") if joy[i] != nil { fmt.Printf("Joystick %d\n", i) fmt.Println(" Name: ", sdl.JoystickName(0)) fmt.Println(" Number of Axes: ", joy[i].NumAxes()) fmt.Println(" Number of Buttons: ", joy[i].NumButtons()) fmt.Println(" Number of Balls: ", joy[i].NumBalls()) } else { fmt.Println(" Couldn't open Joystick!") } } return v.resize }
func newSDLSurface(app *spectrum.Application, w, h int) *SDLSurface { surface := sdl.CreateRGBSurface(sdl.SWSURFACE, w, h, 32, 0, 0, 0, 0) if surface == nil { app.PrintfMsg("%s", sdl.GetError()) app.RequestExit() return nil } return &SDLSurface{surface} }
func loadImage(name string) *sdl.Surface { image := sdl.Load(name) if image == nil { panic(sdl.GetError()) } return image }
func (this *RenderManager) Init() { this.tmx.Load("./test_cute.tmx") if sdl.Init(sdl.INIT_EVERYTHING) != 0 { log.Fatal(sdl.GetError()) } this.screen = sdl.SetVideoMode(707, 600, 32, sdl.RESIZABLE) if this.screen == nil { log.Fatal(sdl.GetError()) } var video_info = sdl.GetVideoInfo() println("HW_available = ", video_info.HW_available) println("WM_available = ", video_info.WM_available) println("Video_mem = ", video_info.Video_mem, "kb") sdl.EnableUNICODE(1) sdl.WM_SetCaption("Go-SDL SDL Test", "") if sdl.GetKeyName(270) != "[+]" { log.Fatal("GetKeyName broken") } this.ressourceManager = NewSpriteLoader() this.ressourceManager.Load("test_tileset.gspr") // this.animMap = ressourceManager.GetAnimMap() }
func main() { verbose := flag.Bool("verbose", false, "verbose mode") debug := flag.Bool("debug", false, "debug mode") fullScreen := flag.Bool("fullscreen", false, "go fullscreen") cpuProfile := flag.String("cpuprofile", "", "write cpu profile to file") help := flag.Bool("help", false, "Show usage") flag.Usage = usage flag.Parse() if *help { usage() return } if *cpuProfile != "" { f, err := os.Create(*cpuProfile) if err != nil { log.Fatal(err) } pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() } application.Verbose = *verbose application.Debug = *debug if sdl.Init(sdl.INIT_EVERYTHING) != 0 { log.Fatal(sdl.GetError()) } screen := sms.NewSDL2xScreen(*fullScreen) sdlLoop := sms.NewSDLLoop(screen) emulatorLoop := newEmulatorLoop(sdlLoop) if emulatorLoop == nil { usage() return } cpuProfiling := *cpuProfile != "" commandLoop := newCommandLoop(emulatorLoop, sdlLoop, cpuProfiling) inputLoop := sms.NewInputLoop(emulatorLoop.sms) application.Register("Emulator loop", emulatorLoop) application.Register("Command loop", commandLoop) application.Register("SDL render loop", sdlLoop) application.Register("SDL input loop", inputLoop) exitCh := make(chan bool) application.Run(exitCh) <-exitCh sdl.Quit() }
func (v *Video) Init(t <-chan []uint32, n string) { v.videoTick = t if sdl.Init(sdl.INIT_VIDEO|sdl.INIT_JOYSTICK|sdl.INIT_AUDIO) != 0 { log.Fatal(sdl.GetError()) } v.screen = sdl.SetVideoMode(512, 480, 32, sdl.OPENGL|sdl.RESIZABLE|sdl.GL_DOUBLEBUFFER) if v.screen == nil { log.Fatal(sdl.GetError()) } sdl.WM_SetCaption(fmt.Sprintf("Fergulator - %s", n), "") v.initGL() v.Reshape(int(v.screen.W), int(v.screen.H)) v.fpsmanager = gfx.NewFramerate() v.fpsmanager.SetFramerate(60) return }
func BenchmarkCPU(b *testing.B) { if sdl.Init(sdl.INIT_EVERYTHING) != 0 { log.Fatal(sdl.GetError()) } screen := smslib.NewSDL2xScreen(false) displayLoop := smslib.NewSDLLoop(screen) go displayLoop.Run() sms := smslib.NewSMS(displayLoop) sms.LoadROM("../roms/blockhead.sms") b.ResetTimer() for i := 0; i < b.N; i++ { sms.RenderFrame() } }
func NewAudio() *Audio { as = sdl_audio.AudioSpec{ Freq: 44100, Format: sdl_audio.AUDIO_S16SYS, Channels: 1, Out_Silence: 0, Samples: uint16(SampleSize), Out_Size: 0, } if sdl_audio.OpenAudio(&as, nil) < 0 { log.Fatal(sdl.GetError()) } sdl_audio.PauseAudio(false) return &Audio{ samples: make([]int16, SampleSize), } }
// Opens SDL audio. // If 'playbackFrequency' is 0, the frequency will be equivalent to PLAYBACK_FREQUENCY. func NewSDLAudio(app *spectrum.Application, playbackFrequency uint, hqAudio bool) (*SDLAudio, error) { if playbackFrequency == 0 { playbackFrequency = PLAYBACK_FREQUENCY } if playbackFrequency < MIN_PLAYBACK_FREQUENCY { return nil, errors.New(fmt.Sprintf("playback frequency of %d Hz is too low", playbackFrequency)) } // Open SDL audio var spec sdl_audio.AudioSpec { spec.Freq = int(playbackFrequency) spec.Format = sdl_audio.AUDIO_S16SYS spec.Channels = 1 spec.Samples = uint16(2048 * float32(playbackFrequency) / PLAYBACK_FREQUENCY) if sdl_audio.OpenAudio(&spec, &spec) != 0 { return nil, errors.New(sdl.GetError()) } if app.Verbose { app.PrintfMsg("%#v", spec) } } audio := &SDLAudio{ data: make(chan *spectrum.AudioData), playback: make(chan *spectrum.AudioData, 2*BUFSIZE_IDEAL), // Use a buffered Go channel playbackLoopFinished: make(chan byte), forwarderLoopFinished: nil, sdlAudioUnpaused: false, bufSize: 0, freq: uint(spec.Freq), virtualFreq: uint(spec.Freq), hqAudio: hqAudio, } go forwarderLoop(app.NewEventLoop(), audio) go playbackLoop(app, audio) return audio, nil }
func (v *Video) initGL() { if gl.Init() != 0 { panic(sdl.GetError()) } gl.ClearColor(0.0, 0.0, 0.0, 1.0) gl.Enable(gl.CULL_FACE) gl.Enable(gl.DEPTH_TEST) v.prog = createProgram(vertShaderSrcDef, fragShaderSrcDef) posAttrib := v.prog.GetAttribLocation("vPosition") texCoordAttr := v.prog.GetAttribLocation("vTexCoord") v.textureUni = v.prog.GetAttribLocation("texture") v.texture = gl.GenTexture() gl.ActiveTexture(gl.TEXTURE0) v.texture.Bind(gl.TEXTURE_2D) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) v.prog.Use() posAttrib.EnableArray() texCoordAttr.EnableArray() vertVBO := gl.GenBuffer() vertVBO.Bind(gl.ARRAY_BUFFER) verts := []float32{-1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0} gl.BufferData(gl.ARRAY_BUFFER, len(verts)*int(unsafe.Sizeof(verts[0])), &verts[0], gl.STATIC_DRAW) textCoorBuf := gl.GenBuffer() textCoorBuf.Bind(gl.ARRAY_BUFFER) texVerts := []float32{0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0} gl.BufferData(gl.ARRAY_BUFFER, len(texVerts)*int(unsafe.Sizeof(texVerts[0])), &texVerts[0], gl.STATIC_DRAW) posAttrib.AttribPointer(2, gl.FLOAT, false, 0, uintptr(0)) texCoordAttr.AttribPointer(2, gl.FLOAT, false, 0, uintptr(0)) }
func newFont(scale2x, fullscreen bool) *ttf.Font { if fullscreen { scale2x = true } var font *ttf.Font { path, err := spectrum.FontPath("VeraMono.ttf") if err != nil { panic(err.Error()) } if scale2x { font = ttf.OpenFont(path, 12) } else { font = ttf.OpenFont(path, 10) } if font == nil { panic(sdl.GetError()) } } return font }
func (a *Audio) Run() { as := sdl_audio.AudioSpec{ Freq: 44100, Format: sdl_audio.AUDIO_S16SYS, Channels: 1, Out_Silence: 0, Samples: uint16(SampleSize), Out_Size: 0, } if sdl_audio.OpenAudio(&as, nil) < 0 { log.Fatal(sdl.GetError()) } sdl_audio.PauseAudio(false) for { select { case s := <-a.sample: a.AppendSample(s) } } }
func NewAudio(frequency int, sampleSize int) (audio *SDLAudio, err error) { spec := sdl_audio.AudioSpec{ Freq: frequency, Format: sdl_audio.AUDIO_S16SYS, Channels: 1, Out_Silence: 0, Samples: uint16(sampleSize), Out_Size: 0, } if sdl_audio.OpenAudio(&spec, nil) < 0 { err = errors.New(sdl.GetError()) return } sdl_audio.PauseAudio(false) audio = &SDLAudio{ samples: make([]int16, sampleSize), input: make(chan int16), } return }
func initSDL() { if sdl.Init(sdl.INIT_VIDEO) != 0 { panic(sdl.GetError()) } }