Example #1
0
// Render renders the intro state
func (ps *PlayState) Render(mainWindowSurface *sdl.Surface) {
	ps.update()

	mainWindowSurface.FillRect(nil, ps.bgColor)

	ps.interludeTextEntity.Visible = ps.state.state == stateInterlude

	ps.rootEntity.Render(mainWindowSurface)
}
Example #2
0
func (r *SDLRenderer) drawCell(window_surf, cell_surface *sdl.Surface,
	x, y, w, h int32, transparent bool) error {
	if cell_surface == nil {
		return fmt.Errorf("unknown cell type")
	}
	if !transparent {
		err := r.getCellImage(grid.Cell{Type: grid.Empty}).BlitScaled(nil,
			window_surf, &sdl.Rect{X: x, Y: y + statusSize, W: w, H: h})
		if err != nil {
			return err
		}
	}
	return cell_surface.BlitScaled(nil, window_surf, &sdl.Rect{
		X: x, Y: y + statusSize, W: w, H: h})
}
Example #3
0
func run() int {
	var window *sdl.Window
	var renderer *sdl.Renderer
	var image *sdl.Surface
	var texture *sdl.Texture
	var src, dst sdl.Rect
	var err error

	window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
		winWidth, winHeight, sdl.WINDOW_SHOWN)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err)
		return 1
	}
	defer window.Destroy()

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

	image, err = sdl.LoadBMP(imageName)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to load BMP: %s\n", err)
		return 3
	}
	defer image.Free()

	texture, err = renderer.CreateTextureFromSurface(image)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create texture: %s\n", err)
		return 4
	}
	defer texture.Destroy()

	src = sdl.Rect{0, 0, 512, 512}
	dst = sdl.Rect{100, 50, 512, 512}

	renderer.Clear()
	renderer.Copy(texture, &src, &dst)
	renderer.Present()

	sdl.Delay(2000)

	return 0
}
Example #4
0
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
}
Example #5
0
func main() {
	var window *sdl.Window
	var renderer *sdl.Renderer
	var image *sdl.Surface
	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 = sdl.LoadBMP(imageName)
	if image == nil {
		fmt.Fprintf(os.Stderr, "Failed to load BMP: %s", 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.Copy(texture, &src, &dst)
	renderer.Present()

	sdl.Delay(2000)

	image.Free()
	texture.Destroy()
	renderer.Destroy()
	window.Destroy()
}
Example #6
0
func TestTTF(t *testing.T) {
	var font *Font
	var solid *sdl.Surface
	var err error

	if err := Init(); err != nil {
		t.Errorf("Failed to initialize TTF: %s\n", err)
	}

	if font, err = OpenFont("../assets/test.ttf", 32); err != nil {
		t.Errorf("Failed to open font: %s\n", err)
	}
	defer font.Close()

	for i := 0; i < 10000; i++ {
		if solid, err = font.RenderUTF8_Solid(randomString(t), sdl.Color{255, 0, 0, 255}); err != nil {
			t.Errorf("Failed to render text: %s\n", err)
		}
		defer solid.Free()
	}
}
Example #7
0
func CreateMenus(window *sdl.Window, surface *sdl.Surface, renderer *sdl.Renderer, controlManager *inputManager.ControlManager) inputManager.Update {
	progress := float64(0)
	mutex := sync.Mutex{}
	dx := 4 * surface.W / 8
	startRect := sdl.Rect{surface.W / 4, 4 * surface.H / 7, 0, 2 * surface.H / 7}
	midBackgroundRect := sdl.Rect{surface.W / 4, 4 * surface.H / 7, dx, 2 * surface.H / 7}
	backgroundRect := sdl.Rect{surface.W/4 - 5, 4*surface.H/7 - 5, dx + 10, 2*surface.H/7 + 10}
	var mainMenu *Menu
	go asyncMenuCreator(renderer, &mutex, &progress, &mainMenu, surface.W, surface.H, window, controlManager)
	return func(state int) int {
		if controlManager.Player1.BF || controlManager.Player2.BF {
			controlManager.Running = false
			return -1
		}
		mutex.Lock()
		startRect.W = int32(float64(dx) * progress)
		surface.FillRect(&backgroundRect, 0xffff0000)
		surface.FillRect(&midBackgroundRect, 0xff000000)
		surface.FillRect(&startRect, 0xffff0000)
		window.UpdateSurface()
		if progress == 1 {
			menuInfo := MenuInfo{0, controlManager, renderer, &sdl.Rect{0, 0, surface.W, surface.H}}
			inputManager.UpdateFunctions = append(inputManager.UpdateFunctions, mainMenu.Open(0, &menuInfo))
			return -1
		}
		mutex.Unlock()
		return 0
	}
}
Example #8
0
// surfaceManipulate is an internal function that creates a new surface, gets
// metadata, and then calls a passed-in function to actually do something to it,
// e.g. horizontally flip all the pixels.
//
// Note: this only supports manipulations that leave the Surface the same
// dimensions. It could potentially be generalized to handle manipulations that
// leave the Surface with the same number of pixels.
func surfaceManipulate(src *sdl.Surface, f func(src *sdl.Surface, srcWBytes, bytesPerPixel int32, srcPx, destPx []byte)) (*sdl.Surface, error) {
	pf := src.Format

	pixels := src.Pixels()
	bytesPP := int32(pf.BytesPerPixel)
	bitsPP := int32(pf.BitsPerPixel)

	newSurface, err := sdl.CreateRGBSurface(0, src.W, src.H, bitsPP, pf.Rmask, pf.Gmask, pf.Bmask, pf.Amask)

	if err != nil {
		return nil, err
	}

	pixelsDest := newSurface.Pixels()

	srcWBytes := int32(src.W * bytesPP)

	// Perform the manipulation
	f(src, srcWBytes, bytesPP, pixels, pixelsDest)

	return newSurface, nil
}
Example #9
0
func SurfaceConvertToImage(s *sdl.Surface) (img image.Image, err error) {
	switch s.Format.Format {
	case sdl.PIXELFORMAT_RGBA8888, sdl.PIXELFORMAT_RGBX8888:
		i := &image.RGBA{Rect: image.Rect(0, 0, int(s.W), int(s.H))}
		i.Pix = s.Pixels()
		img = i
	case sdl.PIXELFORMAT_INDEX8:
		i := image.NewRGBA(image.Rect(0, 0, int(s.W), int(s.H)))
		key, err := s.GetColorKey()
		if err != nil {
			return nil, err
		}
		//		fmt.Println(s.PixelNum(), len(s.Pixels()), len(i.Pix), s.W, s.H, s.Pitch, s.Format.BitsPerPixel, s.Format.BytesPerPixel, key)
		l := len(s.Pixels())
		var r, g, b, a uint8
		for n := 0; n < l; n += 1 {
			pixel := s.Pixels()[n]
			if pixel == uint8(key) {
				r, g, b, a = 0, 0, 0, 0
			} else {
				r, g, b, a = sdl.GetRGBA(uint32(pixel), s.Format)
			}

			p := i.PixOffset(n%int(s.Pitch), n/int(s.Pitch))
			i.Pix[p], i.Pix[p+1], i.Pix[p+2], i.Pix[p+3] = r, g, b, a
		}
		img = i
	default:
		sx, err := s.ConvertFormat(sdl.PIXELFORMAT_RGBX8888, 0)
		if err != nil {
			return nil, err
		}
		i := &image.RGBA{Rect: image.Rect(0, 0, int(s.W), int(s.H))}
		i.Pix = sx.Pixels()
		img = i
	}
	return
}
Example #10
0
// LoadGraphic loads a graphic from disk or panics trying. The image can be in
// BMP, GIF, JPEG, LBM, PCX, PNG, PNM, TGA, TIFF, WEBP, XCF, XPM, or XV format.
// The user must supply the filename of the graphic file to load, in the
// parameter filename.
//
// If the function succeeds then a variable of type Graphic will be returned
// back to the calling function. It is the programmers responsibility to
// store this in a variable of type Graphic.
//
// If the function fails it will panic and crash the program.
// The reasons for a panic are:
//
// 1. The toolbox has not been initalised
//
// 2. The filename does not exist, or is otherwise inaccessable. The specific
// reason will be contained in the panic message itself. This message will
// be prefixed with "Failed to load file: ".
//
// 3. The file could not be converted into a Graphic type. Again the specific
// reason will be contained in the panic message itself. This message will be
// prefixed with "Failed to create Graphic: "
func LoadGraphic(filename string) Graphic {
	if !initialised {
		// this stops execution here, so ne need for an else after the if
		panic(notInitialisedMessage)
	}

	var err error
	var image *sdl.Surface
	image, err = img.Load(filename)
	if err != nil {
		fmt.Print("Failed to load file: ")
		fmt.Println(err)
		panic(err)
	}
	defer image.Free()
	var graphic *sdl.Texture
	graphic, err = renderer.CreateTextureFromSurface(image)
	if err != nil {
		fmt.Print("Failed to create Graphic: ")
		fmt.Println(err)
		panic(err)
	}
	return Graphic(graphic)
}
Example #11
0
// Render renders the intro state
func (is *IntroState) Render(mainWindowSurface *sdl.Surface) {
	rootEntity := is.rootEntity

	mainWindowSurface.FillRect(nil, is.bgColor)
	rootEntity.Render(mainWindowSurface)
}