Example #1
0
func main() {
	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		panic(sdl.GetError())
	}

	defer sdl.Quit()

	screen := sdl.SetVideoMode(640, 480, 32, 0)

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

	sdl.WM_SetCaption("Ohai", "")

	sdl.EnableKeyRepeat(20, 20)

	fmt.Println("Launching mainloop")

	loadTextures()

	loadLevel()

	gameloop(screen)
}
Example #2
0
// Runs everything, calling the methods required to get the game
// running, runs the main loop, and then cleanly exits.
func (g *game) run() (err error) {
	err = g.load()
	if err != nil {
		return
	}
	defer g.quit()

	if sdl.Init(sdl.INIT_EVERYTHING) < 0 {
		return errors.New(sdl.GetError())
	}
	defer sdl.Quit()

	g.screen = sdl.SetVideoMode(640, 480, 32, sdl.DOUBLEBUF)
	if g.screen == nil {
		return errors.New(sdl.GetError())
	}

	sdl.WM_SetCaption(WinCap, "")

	err = g.main()
	if err != nil {
		return
	}

	return
}
func main() {
	if sdl.Init(sdl.INIT_VIDEO) < 0 {
		panic("Video initialization failed: " + sdl.GetError())
	}

	if sdl.EnableKeyRepeat(100, 25) != 0 {
		panic("Setting keyboard repeat failed: " + sdl.GetError())
	}

	videoFlags := sdl.OPENGL    // Enable OpenGL in SDL
	videoFlags |= sdl.DOUBLEBUF // Enable double buffering
	videoFlags |= sdl.HWPALETTE // Store the palette in hardware
	// FIXME: this causes segfault.
	// videoFlags |= sdl.RESIZABLE // Enable window resizing

	surface = sdl.SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, uint32(videoFlags))

	if surface == nil {
		panic("Video mode set failed: " + sdl.GetError())
	}

	sdl.GL_SetAttribute(sdl.GL_DOUBLEBUFFER, 1)
	initGL()
	resizeWindow(SCREEN_WIDTH, SCREEN_HEIGHT)

	SetupWorld("data/world.txt")

	// wait for events
	running := true
	isActive := true
	for running {
		for ev := sdl.PollEvent(); ev != nil; ev = sdl.PollEvent() {
			switch e := ev.(type) {
			case *sdl.ActiveEvent:
				isActive = e.Gain != 0
			case *sdl.ResizeEvent:
				width, height := int(e.W), int(e.H)
				surface = sdl.SetVideoMode(width, height, SCREEN_BPP, uint32(videoFlags))
				if surface == nil {
					fmt.Println("Could not get a surface after resize:", sdl.GetError())
					Quit(1)
				}
				resizeWindow(width, height)
			case *sdl.KeyboardEvent:
				if e.Type == sdl.KEYDOWN {
					handleKeyPress(e.Keysym)
				}
			case *sdl.QuitEvent:
				running = false
			}
		}

		// draw the scene
		if isActive {
			drawGLScene(sector1)
		}
	}
}
Example #4
0
func Play() {
	sdl.Init(sdl.INIT_AUDIO)
	if mixer.OpenAudio(mixer.DEFAULT_FREQUENCY, mixer.DEFAULT_FORMAT,
		mixer.DEFAULT_CHANNELS, 4096) != 0 {
		panic(sdl.GetError())
	}
	var m = mixer.LoadMUS("data/track.ogg")
	if m == nil {
		panic(sdl.GetError())
	}
	m.PlayMusic(-1)
}
Example #5
0
func main() {
	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		panic(sdl.GetError())
	}

	if ttf.Init() != 0 {
		panic(sdl.GetError())
	}

	var screen = sdl.SetVideoMode(1400, 300, 32, 0)
	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)

	font := ttf.OpenFont("euler.otf", 24)

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

	black := sdl.Color{0, 0, 0, 0}
	text := ttf.RenderUTF8_Blended(font, "!!", black)
	fmt.Println(text.Bounds())
	sdl.WM_SetCaption("Go-SDL SDL Test", "")
	running := true
	num := NewNumberShower("b+c", "", "euler.otf", 24, 0)
	denom := NewNumberShower("a", "", "euler.otf", 24, 0)
	testy := NewFractionShower(num, denom, "euler.otf", 24, 0)
	testy.CursorPosition = 3
	testy.RenderEmptyIndextxt = true
	str := ""
	for running {
		switch e := sdl.PollEvent().(type) {
		case *sdl.QuitEvent:
			running = false
			break
		case *sdl.KeyboardEvent:

			if e.State == 1 {
				str = handlekeypress2(e.Keysym, str)
				fmt.Println("rendering string", str)
				text = ttf.RenderUTF8_Blended(font, str, black)
			}
		}
		screen.FillRect(nil, 0xFFFFFF)
		screen.Blit(&sdl.Rect{23, 0, 0, 0}, testy.Render(), nil)
		screen.Flip()
		sdl.Delay(5)
	}
}
Example #6
0
func NewScreen(w, h int) *Screen {
	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		log.Fatal("failed to init sdl", sdl.GetError())
		return nil
	}

	if ttf.Init() != 0 {
		log.Fatal("failed to init ttf", sdl.GetError())
		return nil
	}

	var ctx Screen
	if err := ctx.setSurface(w, h); err != nil {
		log.Fatal(err)
	}

	var vInfo = sdl.GetVideoInfo()
	log.Println("HW_available = ", vInfo.HW_available)
	log.Println("WM_available = ", vInfo.WM_available)
	log.Println("Video_mem = ", vInfo.Video_mem, "kb")

	/* title := "Subtitle Player" */
	title := os.Args[0]
	icon := "" // path/to/icon
	sdl.WM_SetCaption(title, icon)

	sdl.EnableUNICODE(1)

	ctx.fps = opt.fps

	err := ctx.setFont(opt.fontPath, opt.fontSize)
	if err != nil {
		log.Fatal("failed to set default font")
		return nil
	}

	ctx.debugFont = ttf.OpenFont(DFLT_FONT_PATH, 20)
	if ctx.debugFont == nil {
		errMsg := fmt.Sprintf("failed to open debug font: %s",
			sdl.GetError())
		/* return errors.New(errMsg) */
		log.Fatal(errMsg)
	}
	ctx.debugLineHeight = uint16(ctx.debugFont.LineSkip())

	ctx.updateC = make(chan int)

	return &ctx
}
func writeSDLstr(newStr string) {

	if ttf.Init() != 0 {
		panic(sdl.GetError())
	}
	var font = ttf.OpenFont("FontinSans.otf", int(fontsize))

	blue := sdl.Color{185, 230, 255, 0}
	white := sdl.Color{255, 255, 255, 0}
	text0 := ttf.RenderText_Blended(font, previousStr, blue)
	font.SetStyle(ttf.STYLE_UNDERLINE)
	text1 := ttf.RenderText_Blended(font, newStr, white)

	introw = introw - fontsize - fontsize/2
	screen.FillRect(&sdl.Rect{3, introw, uint16(winWidth - 3), uint16(fontsize + fontsize/2)}, bgcolor)
	screen.Blit(&sdl.Rect{3, introw, 0, 0}, text0, nil)
	introw = introw + fontsize + fontsize/2
	screen.Blit(&sdl.Rect{3, introw, 0, 0}, text1, nil)
	introw = introw + fontsize + fontsize/2
	if introw > (int16(winHeight) - fontsize) {
		introw = 0
	}
	screen.FillRect(&sdl.Rect{3, introw, uint16(winWidth - 3), uint16(fontsize + fontsize/2)}, bgcolor)

	screen.Flip()
	previousStr = newStr

}
Example #8
0
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)
}
Example #9
0
func NewSurface(height int, width int) (surface *sdl.Surface) {
	surface = sdl.SetVideoMode(height, width, 32, 0)
	if surface == nil {
		panic(sdl.GetError())
	}
	return
}
Example #10
0
func NewFont(name string, size int) (font *ttf.Font) {
	font = ttf.OpenFont("/usr/share/fonts/TTF/"+name, size)
	if font == nil {
		panic(sdl.GetError())
	}
	return font
}
Example #11
0
func (self *AppStateManager) Setup(state AppState, caption string) {
	if *FLAG_profile {
		pfile, _ := os.Create("gowar.prof")
		pprof.StartCPUProfile(pfile)
	}

	// Lock, so we got no GL-in-wrong-thread foolery
	sdl.Init(sdl.INIT_VIDEO)

	self.Screen = sdl.SetVideoMode(800, 600, 32, sdl.OPENGL|sdl.RESIZABLE)

	gl.Init()

	if self.Screen == nil {
		sdl.Quit()
		panic("Couldn't set 300x300 GL video mode: " + sdl.GetError() + "\n")
	}

	sdl.WM_SetCaption(caption, caption)

	Setup()

	GetViewport().SetScreenSize(float32(self.Screen.W), float32(self.Screen.H))

	self.Push(state)
}
Example #12
0
// Draws everything.
func (g *game) draw() (err error) {
	if g.screen.Blit(nil, g.board.Image(), nil) < 0 {
		return errors.New(sdl.GetError())
	}

	if (g.selX >= 0) || (g.selY >= 0) {
		sx, sy := g.board.CoordToXY(g.selX, g.selY)
		//timg := g.turn.Image()
		//sx -= int(timg.W / 2)
		//sy -= int(timg.H / 2)
		//timg.SetAlpha(sdl.SRCALPHA, 128)
		//g.screen.Blit(&sdl.Rect{X: int16(sx), Y: int16(sy)}, timg, nil)
		//timg.SetAlpha(sdl.SRCALPHA, 255)
		switch g.turn {
		case g.pieces["black"]:
			g.screen.FillRect(&sdl.Rect{int16(sx - 10), int16(sy - 10), 20, 20},
				sdl.MapRGBA(g.screen.Format, 0, 0, 0, 128),
			)
		case g.pieces["white"]:
			g.screen.FillRect(&sdl.Rect{int16(sx - 10), int16(sy - 10), 20, 20},
				sdl.MapRGBA(g.screen.Format, 255, 255, 255, 128),
			)
		}
	}

	return
}
Example #13
0
// Wrap the SDL into one single thread
func (s SdlWrap) wrap() {
	runtime.LockOSThread()

	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		panic("Unable to init SDL. Cause: " + sdl.GetError())
	}
	sdl.EnableUNICODE(1)

	s.InitDone <- struct{}{}

	for {
		select {
		case t := <-s.Title:
			sdl.WM_SetCaption(t, t)
		case <-s.Quit:
			close(s.Events)
			sdl.Quit()
		case sz := <-s.Size:
			s.Surface <- sdl.SetVideoMode(int(sz.X), int(sz.Y), 32, sdl.HWSURFACE|sdl.DOUBLEBUF|sdl.RESIZABLE)
		default:
			e := sdl.PollEvent()
			if e != nil {
				s.Events <- e
			}
		}
	}
}
Example #14
0
func CreateRGBSurface(width, height int) *Surface {
	var rmask, gmask, bmask, amask uint32

	byteorder := 0
	bigendian := 0

	if byteorder == bigendian {
		rmask = 0xff000000
		gmask = 0x00ff0000
		bmask = 0x0000ff00
		amask = 0x000000ff
	} else {
		rmask = 0x000000ff
		gmask = 0x0000ff00
		bmask = 0x00ff0000
		amask = 0xff000000
	}

	inner := sdl.CreateRGBSurface(
		sdl.SRCALPHA, // flags
		width, height,
		32, // depth
		rmask, gmask, bmask, amask,
	)

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

	return &Surface{inner}
}
Example #15
0
func (s *Sound) GetEffect(path string) (c *mixer.Chunk, err error) {
	c = mixer.LoadWAV(path)
	if c == nil {
		err = fmt.Errorf(sdl.GetError())
		return
	}
	return
}
Example #16
0
func (s *Sound) GetMusic(path string) (m *mixer.Music, err error) {
	m = mixer.LoadMUS(path)
	if m == nil {
		err = fmt.Errorf(sdl.GetError())
		return
	}
	return
}
Example #17
0
// Plays the supplied sound file on repeat.
func (s *Sound) PlayMusic(path string) (err error) {
	var m = mixer.LoadMUS(path)
	if m == nil {
		err = fmt.Errorf(sdl.GetError())
		return
	}
	m.PlayMusic(-1)
	return
}
Example #18
0
func sdlSetup() {
	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		panic(sdl.GetError())
	}

	Screen = &Surface{sdl.SetVideoMode(Width, Height, 32, 0)}
	if Screen == nil {
		panic(sdl.GetError())
	}

	if sdl.EnableKeyRepeat(25, 25) != 0 {
		panic(sdl.GetError())
	}

	if ttf.Init() != 0 {
		panic(sdl.GetError())
	}
}
Example #19
0
func sdlSetup() (world *World) {
	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		panic(sdl.GetError())
	}
	sdl.EnableUNICODE(1)
	sdl.EnableKeyRepeat(25, 25)

	return
}
Example #20
0
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
}
Example #21
0
// Creates / Initializes the sound system.
func NewSound() (s *Sound, err error) {
	sdl.Init(sdl.INIT_AUDIO)
	if mixer.OpenAudio(mixer.DEFAULT_FREQUENCY, mixer.DEFAULT_FORMAT,
		mixer.DEFAULT_CHANNELS, 4096) != 0 {
		err = fmt.Errorf(sdl.GetError())
		return
	}
	s = &Sound{}
	return
}
Example #22
0
func loadImage(name string) *sdl.Surface {
	image := sdl.Load(name)

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

	return image

}
Example #23
0
// Initializes a new board of the given size using the given ko rule.
func NewBoard(size BoardSize, ko KoType) (*Board, error) {
	b := new(Board)

	b.size = int(size)
	b.pieces = make([]*Piece, b.size*b.size)
	b.tmp = make([]*Piece, b.size*b.size)

	switch ko {
	case SimpleKo:
		b.ko = (*Board).simpleKo
	case SuperKo:
		b.ko = (*Board).superKo
	default:
		return nil, fmt.Errorf("Unknown ko type: %v", ko)
	}

	b.bg = sdl.Load(path.Join(BoardPath, fmt.Sprintf("%v.png", b.size)))
	if b.bg == nil {
		return nil, errors.New(sdl.GetError())
	}

	//b.img = sdl.CreateRGBSurface(sdl.HWSURFACE,
	//	int(b.bg.W),
	//	int(b.bg.H),
	//	int(b.bg.Format.BitsPerPixel),
	//	b.bg.Format.Rmask,
	//	b.bg.Format.Gmask,
	//	b.bg.Format.Bmask,
	//	b.bg.Format.Amask,
	//)
	//if b.img == nil {
	//	return nil, os.NewError(sdl.GetError())
	//}

	b.img = sdl.Load(path.Join(BoardPath, fmt.Sprintf("%v.png", b.size)))
	if b.img == nil {
		return nil, errors.New(sdl.GetError())
	}

	runtime.SetFinalizer(b, (*Board).free)

	return b, nil
}
Example #24
0
// Load bitmap from path as GL texture
func LoadGLTexture(path string) {
	image := sdl.Load(path)
	if image == nil {
		panic(sdl.GetError())
	}

	// Check that the image's width is a power of 2
	if image.W&(image.W-1) != 0 {
		fmt.Println("warning:", path, "has a width that is not a power of 2")
	}

	// Also check if the height is a power of 2
	if image.H&(image.H-1) != 0 {
		fmt.Println("warning:", path, "has an height that is not a power of 2")
	}

	// get the number of channels in the SDL surface
	nOfColors := image.Format.BytesPerPixel
	var textureFormat gl.GLenum

	if nOfColors == 4 { // contains alpha channel
		if image.Format.Rmask == 0x000000ff {
			textureFormat = gl.RGBA
		} else {
			textureFormat = gl.BGRA
		}
	} else if nOfColors == 3 { // no alpha channel
		if image.Format.Rmask == 0x000000ff {
			textureFormat = gl.RGB
		} else {
			textureFormat = gl.BGR
		}
	} else {
		fmt.Println("warning:", path, "is not truecolor, this will probably break")
	}

	texture = gl.GenTexture()

	// Typical texture generation using data from the bitmap
	gl.BindTexture(gl.TEXTURE_2D, uint(texture))

	// Generate the texture
	gl.TexImage2D(gl.TEXTURE_2D, 0, int(image.Format.BytesPerPixel),
		int(image.W), int(image.H),
		0, textureFormat, gl.UNSIGNED_BYTE, image.Pixels,
	)

	// linear filtering
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)

	// free up memory we have used.
	image.Free()
}
Example #25
0
File: gl_test.go Project: shogg/gl
func init() {
	sdl.Init(sdl.INIT_VIDEO)

	screen := sdl.SetVideoMode(320, 200, 32, sdl.OPENGL)
	if screen == nil {
		panic("Couldn't set video mode: " + sdl.GetError() + "\n")
	}

	if err := gl.Init(); err != 0 {
		panic("glInit error")
	}
}
Example #26
0
func initinc() error {
	if initnum == 0 {
		errn := sdl.Init(sdl.INIT_VIDEO)
		if errn < 0 {
			return errors.New(sdl.GetError())
		}
	}

	initnum++

	return nil
}
Example #27
0
// Initializes a new piece.
func NewPiece(t string) (*Piece, error) {
	p := new(Piece)

	p.img = sdl.Load(path.Join(PiecePath, t+".png"))
	if p.img == nil {
		return nil, errors.New(sdl.GetError())
	}

	runtime.SetFinalizer(p, (*Piece).free)

	return p, nil
}
Example #28
0
func main() {
	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		panic(sdl.GetError())
	}

	// The actual resolution (screenspace)
	W := 800
	H := 600

	// The virtual resolution (worldspace)
	MAXX := 400
	MAXY := 300

	screen := sdl.SetVideoMode(W, H, 32, sdl.FULLSCREEN)
	if screen == nil {
		panic(sdl.GetError())
	}
	sdl.EnableUNICODE(1)
	sdl.ShowCursor(0)

	sdl.WM_SetCaption("Random Lines", "")

	red := sdl.Color{255, 0, 0, 255}
	color := red

	rand.Seed(time.Now().UnixNano())

	for {
		color = sdl.Color{uint8(rand.Intn(255)), uint8(rand.Intn(255)), uint8(rand.Intn(255)), 255}
		DoublePixelLine(screen, rand.Intn(MAXX), rand.Intn(MAXY), rand.Intn(MAXX), rand.Intn(MAXY), color)
		if escPressed() {
			break
		}
		screen.Flip()
		sdl.PollEvent()
		//sdl.Delay(10)
	}

	sdl.Quit()
}
func ui_main() {

	if sdl.Init(sdl.INIT_VIDEO) != 0 {
		panic(sdl.GetError())
	}

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

	sdl.EnableUNICODE(1)

	sdl.WM_SetCaption("Go-SDL SDL Test", "")

	running := true

	for running {
		for ev := sdl.PollEvent(); ev != nil; ev = sdl.PollEvent() {
			switch e := ev.(type) {
			case *sdl.QuitEvent:
				running = false
				break

			case *sdl.ResizeEvent:
				println("resize screen ", e.W, e.H)

				screen = sdl.SetVideoMode(int(e.W), int(e.H), 32, sdl.RESIZABLE)

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

	}

	ttf.Quit()
	sdl.Quit()
}
Example #30
0
func CreateImage(path string) *Surface {
	gopaths := strings.Split(os.Getenv("GOPATH"), ":")

	var img *sdl.Surface
	for _, gopath := range gopaths {
		img = sdl.Load(gopath + "/src/github.com/manveru/raptgo/" + path)
		if img != nil {
			return &Surface{img}
		}
	}

	panic(sdl.GetError())
}