Beispiel #1
0
// draw lines of text aligned around center of screen
func DrawTextLines(strings []string) {
	var dstrect, srcrect *sdl.Rect
	var line *sdl.Surface
	var w, h, prevH uint16

	centerX := int16(Width / 2)
	centerY := int16(Height / 2)

	for _, str := range strings {
		line = RenderText(str, Red)
		line.GetClipRect(srcrect)

		w, h = uint16(line.W), uint16(line.H)

		dstrect = &sdl.Rect{
			X: centerX - int16(w/2),
			Y: (centerY + int16(prevH)) - int16(h/2),
			W: w,
			H: h,
		}

		prevH += h

		Screen.Blit(dstrect, line, srcrect)
		line.Free()
	}
}
Beispiel #2
0
func loop(screen *sdl.Surface) {
	for {
		screen.Flip()
		event := sdl.PollEvent()
		if event != nil {
			if _, ok := event.(*sdl.QuitEvent); ok {
				log.Println("Caught quit event. Quiting SDL.")
				break
			}
		}
	}
}
Beispiel #3
0
func (o *Object) Draw(surface *sdl.Surface, gc *GameContext) {
	windowStartX := int16(gc.PlayerPosition - 320)

	// "move" the object according to the window
	drawRect := &sdl.Rect{
		X: o.Rect.X - windowStartX,
		Y: o.Rect.Y,
		W: o.Rect.W,
		H: o.Rect.H,
	}

	surface.FillRect(drawRect, o.Color)
}
Beispiel #4
0
func initFillBox(screen *sdl.Surface) {
	/*{{{*/
	for i := range paint_chan {
		if i.f.T == i.c {
			continue
		}

		i.f.T = i.c
		screen.FillRect(i.f.toRect(), uint32(i.c))
		// Do updates of screen in the main loop.
		//screen.UpdateRect(i.f.ToFourTuple());
	}
	/*}}}*/
}
Beispiel #5
0
func playGame(screen *sdl.Surface, bg *glh.Texture, font *gltext.Font) (int, bool) {
	movingUp := false
	b := bird.NewBird(240, 380)
	score := 0
	var pipes []*pipe.Pipe
	ticker := 0
	for {
		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
		quit := manageEvents(screen, &movingUp)
		if quit {
			return score, true
		}

		renderBackground(screen, bg)
		ticker++
		if ticker > 100 {
			pipes = append(pipes, pipe.NewPipe(int(screen.W), int(screen.H)))
			ticker = 0
		}

		if movingUp {
			b.MoveUp()
		} else {
			b.MoveDown()
		}

		for _, p := range pipes {
			if p.X < 0 {
				p.Destroy()
				pipes = pipes[1:]
			}
			if p.CollidesWith(b.X, b.Y, b.Width, b.Height) {
				return score, false
			}
			if p.GonePast(b.X, b.Y) {
				score++
			}
			p.Tick()
			p.Render()
		}
		b.Render()

		font.Printf(240, 50, strconv.Itoa(score))
		screen.Flip()
		time.Sleep((1 / 30) * time.Second)
		sdl.GL_SwapBuffers()
	}
}
Beispiel #6
0
func gameloop(screen *sdl.Surface) {
	gc := &GameContext{screen, 320, 16}

	for {
		e := sdl.WaitEvent()

		screen.FillRect(nil, 0x0)

		gc.drawFloor()
		gc.drawPlayer()
		gc.drawObjects()

		switch re := e.(type) {
		case *sdl.QuitEvent:
			return

		case *sdl.MouseMotionEvent:

			screen.FillRect(&sdl.Rect{
				int16(re.X),
				int16(re.Y),
				50, 50}, 0xffffff)

			screen.Blit(&sdl.Rect{
				int16(re.X),
				int16(re.Y),
				0, 0}, playerTexture, nil)

			fmt.Println(re.X, re.Y)

		case *sdl.KeyboardEvent:
			if re.Type == sdl.KEYDOWN {
				keyname := sdl.GetKeyName(sdl.Key(re.Keysym.Sym))
				fmt.Println("pressed:", keyname)

				switch keyname {
				case "right":
					gc.moveRight()
				case "left":
					gc.moveLeft()
				case "q":
					return
				}
			} else if re.Type == sdl.KEYUP {
				gc.resetPlayerSpeed()
			}
		default:
			//fmt.Println("What the heck?!")
		}

		gc.Dump()

		screen.Flip()
	}
}
Beispiel #7
0
// Draw a line
func Line(screen *sdl.Surface, x1 int, y1 int, x2 int, y2 int, color sdl.Color) {
	if x1 == x2 || y1 == y2 {
		return
	}

	startx, stopx := minmax(x1, x2)
	starty, stopy := minmax(y1, y2)

	xdiff := stopx - startx
	ydiff := stopy - starty

	if xdiff > ydiff {
		// We're going along X
		y := float32(starty)
		ystep := float32(ydiff) / float32(xdiff)
		if y1 != starty {
			// Move in the other direction along Y
			ystep = -ystep
			y = float32(stopy)
		}
		// Draw the line
		for x := startx; x < stopx; x++ {
			screen.Set(x, int(y), color)
			y += ystep
		}
	} else {
		// We're going along Y
		x := float32(startx)
		xstep := float32(xdiff) / float32(ydiff)
		if x1 != startx {
			// Move in the other direction along X
			xstep = -xstep
			x = float32(stopx)
		}
		// Draw the line
		for y := starty; y < stopy; y++ {
			screen.Set(int(x), y, color)
			x += xstep
		}
	}
}
Beispiel #8
0
/*
 * Draw a grid on the display and return info about the Tile
 */
func drawGrid(screen *sdl.Surface) {
	/*{{{*/
	vid := sdl.GetVideoInfo()

	// First the vertical
	for i := 0; i < int(vid.Current_w); i += SIZE {
		screen.FillRect(
			&sdl.Rect{int16(i), int16(0), 1, uint16(vid.Current_h)},
			0x000000)
		screen.UpdateRect(int32(i), 0, 1, uint32(vid.Current_h))
	}

	// Then the horizontal
	for i := 0; i < int(vid.Current_h); i += SIZE {
		screen.FillRect(
			&sdl.Rect{0, int16(i), uint16(vid.Current_w), 1},
			0x000000)
		screen.UpdateRect(0, int32(i), uint32(vid.Current_w), 1)
	}

	return
	/*}}}*/
}