Exemple #1
0
func Init(width, height int32) *Engine {
	window, err := sdl.CreateWindow("test", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
		int(width), int(height), sdl.WINDOW_SHOWN)

	if err != nil {
		fmt.Fprint(os.Stderr, "Failed to create window", sdl.GetError())
		os.Exit(1)
	}

	renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED|sdl.RENDERER_PRESENTVSYNC)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", sdl.GetError())
		os.Exit(2)
	}

	img.Init(0)
	ttf.Init()

	return &Engine{
		Window:   window,
		Renderer: renderer,
		Running:  true,
		width:    width,
		height:   height,
		entities: make([]Drawable, 0),
	}
}
Exemple #2
0
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)
	}

}
Exemple #3
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, 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()
}
Exemple #4
0
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()
}
Exemple #5
0
func Start(windowText string, W, H int) (*sdl.Window, *sdl.Renderer) {
	window, err := sdl.CreateWindow(windowText, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
		W, H, sdl.WINDOW_SHOWN)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", sdl.GetError())
		os.Exit(1)
	}
	renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED|sdl.RENDERER_PRESENTVSYNC)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", sdl.GetError())
		os.Exit(1)
	}
	return window, renderer
}
Exemple #6
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()
}
Exemple #7
0
// SetMusicPosition
// (https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer_65.html)
func SetMusicPosition(position int64) error {
	_position := (C.double)(position)
	if C.Mix_SetMusicPosition(_position) == -1 {
		return sdl.GetError()
	}
	return nil
}
Exemple #8
0
func NewWindowedWindow(title string, width, height int) Window {
	// FOR GLFW event handling
	runtime.LockOSThread()

	windowSDL, err := sdl.CreateWindow(title, sdl.WINDOWPOS_UNDEFINED,
		sdl.WINDOWPOS_UNDEFINED,
		width, height, sdl.WINDOW_OPENGL)

	if err != nil {
		panic(err)
	}
	if windowSDL == nil {
		panic(sdl.GetError())
	}

	context, err := sdl.GL_CreateContext(windowSDL)
	sdl.GL_MakeCurrent(windowSDL, context)

	if err != nil {
		panic("Error loading window: " + err.Error())
	}

	w := Window{title: title, Width: width, Height: height, full: false, windowSDL: windowSDL, contextSDL: context, running: false}

	w.init()
	var current sdl.DisplayMode
	if err := sdl.GetCurrentDisplayMode(0, &current); err != nil {
		fmt.Println("COuld not get display mode: " + err.Error())
	}
	fmt.Printf("Display #%d: current display mode is %dx%dpx @ %dhz. \n", 0, current.W, current.H, current.RefreshRate)

	return w
}
Exemple #9
0
func Init(flags int) error {
	initted := int(C.Mix_Init(C.int(flags)))
	if initted&flags != flags {
		return sdl.GetError()
	}
	return nil
}
Exemple #10
0
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()
}
Exemple #11
0
func loadTexture(path string) *sdl.Texture {
	bmp := img.Load(path)
	if bmp == nil {
		fmt.Fprintf(os.Stderr, "Unable to load image %s! SDL Error: %s\n", path, img.GetError())
	} else {
		if bmp.SetColorKey(1, sdl.Color{0xFF, 0xFF, 0x00, 0xFF}.Uint32()) != 0 {
			fmt.Fprintf(os.Stderr, "Unable to set Color Key %s! SDL Error: %s\n", path, sdl.GetError())
		}
		texture := renderer.CreateTextureFromSurface(bmp)
		if texture == nil {
			fmt.Fprintf(os.Stderr, "Unable to create texture %s! SDL Error: %s\n", path, sdl.GetError())
		}
		bmp.Free()
		return texture
	}
	return nil
}
Exemple #12
0
// Play (https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer_57.html)
func (music *Music) Play(loops int) error {
	_music := (*C.Mix_Music)(unsafe.Pointer(music))
	_loops := (C.int)(loops)
	if C.Mix_PlayMusic(_music, _loops) == -1 {
		return sdl.GetError()
	}
	return nil
}
Exemple #13
0
func LoadTextureTransparent(renderer *sdl.Renderer, path string, r, g, b uint8) *sdl.Texture {
	bmp, err := img.Load(path)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to load image %s! SDL Error: %s\n", path, img.GetError())
	} else {
		if bmp.SetColorKey(1, sdl.MapRGB(bmp.Format, r, g, b)) != 0 {
			fmt.Fprintf(os.Stderr, "Unable to set Color Key %s! SDL Error: %s\n", path, sdl.GetError())
		}
		texture, err := renderer.CreateTextureFromSurface(bmp)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Unable to create texture %s! SDL Error: %s\n", path, sdl.GetError())
		}
		bmp.Free()
		return texture
	}
	return nil
}
Exemple #14
0
// SetMusicCMD
// (https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer_66.html)
func SetMusicCMD(command string) error {
	_command := C.CString(command)
	defer C.free(unsafe.Pointer(_command))
	if C.Mix_SetMusicCMD(_command) == -1 {
		return sdl.GetError()
	}
	return nil
}
Exemple #15
0
// SetDistance
// (https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer_81.html)
func SetDistance(channel int, distance uint8) error {
	_channel := (C.int)(channel)
	_distance := (C.Uint8)(distance)
	if (C.Mix_SetDistance(_channel, _distance)) == 0 {
		return sdl.GetError()
	}
	return nil
}
Exemple #16
0
func SdlError() {
	fmt.Println("errtest", counter)
	counter = counter + 1
	err := sdl.GetError()
	if err != nil {
		panic(err)
	}
}
Exemple #17
0
// SetReverseStereo
// (https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer_83.html)
func SetReverseStereo(channel, flip int) error {
	_channel := (C.int)(channel)
	_flip := (C.int)(flip)
	if (C.Mix_SetReverseStereo(_channel, _flip)) == 0 {
		return sdl.GetError()
	}
	return nil
}
Exemple #18
0
func QuickLoad_WAV(mem unsafe.Pointer) (chunk *Chunk, err error) {
	_mem := (*C.Uint8)(mem)
	chunk = (*Chunk)(unsafe.Pointer(C.Mix_QuickLoad_WAV(_mem)))
	if chunk == nil {
		err = sdl.GetError()
	}
	return
}
Exemple #19
0
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

	if 0 != sdl.Init(sdl.INIT_EVERYTHING) {
		panic(sdl.GetError())
	}
	window = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
		winWidth, winHeight, sdl.WINDOW_OPENGL)
	if window == nil {
		panic(sdl.GetError())
	}
	context = sdl.GL_CreateContext(window)
	if context == nil {
		panic(sdl.GetError())
	}

	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)
	}

	sdl.GL_DeleteContext(context)
	window.Destroy()
	sdl.Quit()
}
Exemple #20
0
func LoadMUS_RW(src *sdl.RWops, freesrc int) (mus *Music, err error) {
	_src := (*C.SDL_RWops)(unsafe.Pointer(src))
	_freesrc := (C.int)(freesrc)
	mus = (*Music)(unsafe.Pointer(C.Mix_LoadMUS_RW(_src, _freesrc)))
	if mus == nil {
		err = sdl.GetError()
	}
	return
}
Exemple #21
0
// SetPanning
// (https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer_80.html)
func SetPanning(channel int, left, right uint8) error {
	_channel := (C.int)(channel)
	_left := (C.Uint8)(left)
	_right := (C.Uint8)(right)
	if C.Mix_SetPanning(_channel, _left, _right) == 0 {
		return sdl.GetError()
	}
	return nil
}
Exemple #22
0
func LoadWAV_RW(src *sdl.RWops, freesrc bool) (chunk *Chunk, err error) {
	_src := (*C.SDL_RWops)(unsafe.Pointer(src))
	_freesrc := cint(freesrc)
	chunk = (*Chunk)(unsafe.Pointer(C.Mix_LoadWAV_RW(_src, _freesrc)))
	if chunk == nil {
		err = sdl.GetError()
	}
	return
}
Exemple #23
0
func LoadMUS(file string) (mus *Music, err error) {
	_file := C.CString(file)
	defer C.free(unsafe.Pointer(_file))
	mus = (*Music)(unsafe.Pointer(C.Mix_LoadMUS(_file)))
	if mus == nil {
		err = sdl.GetError()
	}
	return
}
Exemple #24
0
func LoadFont(name string, size int) *ttf.Font {
	font, err := ttf.OpenFont(name, size)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to load font: %s\n", sdl.GetError())
		os.Exit(3)
	}

	return font
}
Exemple #25
0
// UnregisterAllEffects
// (https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer_78.html)
func UnregisterAllEffects(channel int) error {
	// release all effect functions
	allEffectFunc = nil
	allEffectDone = nil
	if C.Mix_UnregisterAllEffects(C.int(channel)) == 0 {
		return sdl.GetError()
	}
	return nil
}
Exemple #26
0
func main() {
	rom, err := LoadROM(os.Args[1])

	if err != nil {
		panic(err)
	}

	frameDelay := 1000 / frameRate

	window := sdl.CreateWindow(
		"Go NES",
		sdl.WINDOWPOS_UNDEFINED,
		sdl.WINDOWPOS_UNDEFINED,
		256,
		240,
		sdl.WINDOW_SHOWN,
	)

	if window == nil {
		panic(sdl.GetError())
	}

	defer window.Destroy()

	controller := NewController()
	ppu := NewPPU(window, NewVRAM(rom.Mapper()))
	cpu := NewCPU(NewMMU(rom.Mapper(), ppu, controller), ppu)

	ticksPerFrame := uint32(341 * 262)

	for {
		expectedTicks := sdl.GetTicks() + frameDelay

		for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch event := event.(type) {
			case *sdl.KeyDownEvent:
				controller.SetKeyState(event.Keysym.Sym, true)
			case *sdl.KeyUpEvent:
				controller.SetKeyState(event.Keysym.Sym, false)
			case *sdl.QuitEvent:
				return
			}
		}

		ppuTicks := uint32(0)

		for ppuTicks < ticksPerFrame {
			ppuTicks += ppu.Tick(cpu.Tick())
		}

		actualTicks := sdl.GetTicks()

		if actualTicks < expectedTicks {
			sdl.Delay(expectedTicks - actualTicks)
		}
	}
}
Exemple #27
0
func QuickLoad_RAW(mem *uint8, len_ uint32) (chunk *Chunk, err error) {
	_mem := (*C.Uint8)(mem)
	_len := (C.Uint32)(len_)
	chunk = (*Chunk)(unsafe.Pointer(C.Mix_QuickLoad_RAW(_mem, _len)))
	if chunk == nil {
		err = sdl.GetError()
	}
	return
}
Exemple #28
0
// FadeIn (https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer_58.html)
func (music *Music) FadeIn(loops, ms int) error {
	_music := (*C.Mix_Music)(unsafe.Pointer(music))
	_loops := (C.int)(loops)
	_ms := (C.int)(ms)
	if C.Mix_FadeInMusic(_music, _loops, _ms) == -1 {
		return sdl.GetError()
	}
	return nil
}
Exemple #29
0
// SetPosition
// (https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer_82.html)
func SetPosition(channel int, angle int16, distance uint8) error {
	_channel := (C.int)(channel)
	_angle := (C.Sint16)(angle)
	_distance := (C.Uint8)(distance)
	if (C.Mix_SetPosition(_channel, _angle, _distance)) == 0 {
		return sdl.GetError()
	}
	return nil
}
Exemple #30
0
func (chunk *Chunk) PlayChannel(channel, loops int) (channel_ int, err error) {
	_channel := (C.int)(channel)
	_chunk := (*C.Mix_Chunk)(unsafe.Pointer(chunk))
	_loops := (C.int)(loops)
	channel_ = int(C.Mix_PlayChannelTimed(_channel, _chunk, _loops, -1))
	if channel_ == -1 {
		err = sdl.GetError()
	}
	return
}