Пример #1
0
//TODO Make sdl_tff work!
func (menuGeneratorInfo *MenuGeneratorInfo) GenerateMenuShellOneColumn(renderer *sdl.Renderer, x, y, dx, dy int32) Menu {
	background, err := renderer.CreateTextureFromSurface(menuGeneratorInfo.menuBackground)
	if err != nil {
		panic(err)
	}

	buttonCount := len(menuGeneratorInfo.buttonTexts)

	menuItems := make([]*MenuItem, buttonCount)
	highlightedBackground, notHighlightedBackground, srcRects, FrameLength := menuGeneratorInfo.menuItemAnimation.Generate(renderer)

	TEXTSURFACE := sdl.Texture{}
	TEXTRECT := sdl.Rect{0, 0, 0, 0}

	for i := 0; i < buttonCount; i++ {
		currentRect := sdl.Rect{x, y + dy*int32(i), dx, dy}
		currentRects := make([]*sdl.Rect, len(srcRects))
		for j := 0; j < len(srcRects); j++ {
			currentRects[j] = &currentRect
		}
		previous := (i - 1 + buttonCount) % buttonCount
		next := (i + 1) % buttonCount
		menuAction := MenuAction{nil, func(menuInfo *MenuInfo, menuItem *MenuItem) int { return previous }, func(menuInfo *MenuInfo, menuItem *MenuItem) int { return next }, nil, nil}
		menuItems[i] = &MenuItem{notHighlightedBackground, highlightedBackground, srcRects, currentRects, &TEXTSURFACE, &TEXTRECT, menuAction, FrameLength}
	}
	return Menu{background, menuItems, nil}
}
Пример #2
0
func (m *Menu) DrawItem(renderer *sdl.Renderer, index int) {
	var text_surface *sdl.Surface
	var text_texture *sdl.Texture
	var text_width, text_height int32
	var text_rect sdl.Rect
	var screen_width, _, _ int = renderer.GetRendererOutputSize()

	for index, menu_item := range m.menu_items {
		if index == m.selected_item {
			text_surface = m.font.RenderText_Solid(menu_item.text, *m.selected_color)
		} else {
			text_surface = m.font.RenderText_Solid(menu_item.text, *m.unselected_color)
		}
		text_width = text_surface.W
		text_height = text_surface.H

		text_texture = renderer.CreateTextureFromSurface(text_surface)

		text_rect = sdl.Rect{
			int32(screen_width) - 10 - text_width, //X position
			(m.font_size_32 * int32(index)),
			text_width,
			text_height,
		}

		renderer.Copy(text_texture, nil, &text_rect)
	}
}
Пример #3
0
func (t *TextEl) Bake(renderer *sdl.Renderer) (*sdl.Texture, int32, int32) {
	surface, _ := t.Font.RenderUTF8_Solid(t.Content, t.Color)
	defer surface.Free()

	txtr, _ := renderer.CreateTextureFromSurface(surface)
	//defer txtr.Destroy()

	return txtr, surface.W, surface.H
}
Пример #4
0
func LoadTexture(renderer *sdl.Renderer, path string) *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 {
		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
}
Пример #5
0
func LoadText(renderer *sdl.Renderer, font *ttf.Font, text string, textColor sdl.Color) *sdl.Texture {
	bmp := font.RenderText_Solid(text, textColor)
	if bmp == nil {
		fmt.Fprintf(os.Stderr, "Unable to load text %s! SDL Error: %s\n", ttf.GetError())
	} else {
		texture, err := renderer.CreateTextureFromSurface(bmp)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Unable to create texture %s! SDL Error: %s\n", sdl.GetError())
		}
		bmp.Free()
		return texture
	}
	return nil
}
Пример #6
0
func run() int {
	var window *sdl.Window
	var renderer *sdl.Renderer
	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 := img.Load(imageName)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to load PNG: %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.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)

	return 0
}
Пример #7
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()
}
Пример #8
0
func main() {
	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)
		os.Exit(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)
		os.Exit(2)
	}
	defer renderer.Destroy()

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

	texture, err = renderer.CreateTextureFromSurface(image)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create texture: %s\n", err)
		os.Exit(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)
}
Пример #9
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
}
Пример #10
0
// Load loads the image into a Texture
func Load(renderer *sdl.Renderer, fileName, id string) error {
	tempSurface, err := img.Load(fileName)

	if err != nil {
		return err
	}

	defer tempSurface.Free()

	texture, err := renderer.CreateTextureFromSurface(tempSurface)

	if err != nil {
		return err
	}

	textureMap[id] = texture

	return nil
}
Пример #11
0
func (t *Label) Draw(renderer *sdl.Renderer) {
	surface, err := t.font.RenderUTF8_Solid(t.text, sdl.Color{255, 255, 255, 255})
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create surface: %s\n", err)
		os.Exit(1)
	}

	texture, err := renderer.CreateTextureFromSurface(surface)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create texture: %s\n", err)
		os.Exit(1)
	}

	src := &sdl.Rect{0, 0, surface.W, surface.H}
	dst := &sdl.Rect{t.rect.X, t.rect.Y, src.W, src.H}

	renderer.Copy(texture, src, dst)
	surface.Free()
}
Пример #12
0
func (m *Menu) DrawTitle(renderer *sdl.Renderer) {
	var text_surface *sdl.Surface
	var text_texture *sdl.Texture
	var text_width, text_height int32
	var title_rect sdl.Rect
	var _, screen_height, _ int = renderer.GetRendererOutputSize()

	text_surface = m.title_font.RenderText_Solid(m.menu_title, *m.title_color)

	text_width = text_surface.W
	text_height = text_surface.H

	text_texture = renderer.CreateTextureFromSurface(text_surface)

	title_rect = sdl.Rect{
		20, //X position
		int32(screen_height) - 20 - text_height, //Y position
		text_width,
		text_height,
	}

	renderer.Copy(text_texture, nil, &title_rect)
}
Пример #13
0
func (animation *MenuItemAnimation) Generate(renderer *sdl.Renderer) (*sdl.Texture, *sdl.Texture, []*sdl.Rect, uint64) {
	highlightedTexture, err := renderer.CreateTextureFromSurface(animation.highlightedAnimation)
	if err != nil {
		panic(err)
	}
	notHighlightedTexture, err := renderer.CreateTextureFromSurface(animation.notHighlightedAnimation)
	rows := animation.highlightedAnimation.W / animation.dx
	columns := animation.highlightedAnimation.H / animation.dy
	rects := make([]*sdl.Rect, rows*columns)
	xMult := columns * int32((animation.AnimationDirection>>YX)%2)
	if xMult == 0 {
		xMult = 1
	}
	yMult := rows * int32(1-(animation.AnimationDirection>>YX)%2)
	if yMult == 0 {
		yMult = 1
	}
	xDir := int32(1)
	xPluss := int32(0)
	yDir := int32(1)
	yPluss := int32(0)
	if (animation.AnimationDirection>>LeftRight)%2 == 0 {
		xDir = -1
		xPluss = rows
	}
	if (animation.AnimationDirection>>UpDown)%2 == 0 {
		yDir = -1
		yPluss = columns
	}
	for y := int32(0); y < columns; y++ {
		for x := int32(0); x < rows; x++ {
			currentRect := sdl.Rect{animation.dx * x, animation.dy * y, animation.dx, animation.dy}
			rects[((xPluss+x*xDir)*xMult + (yPluss+y*yDir)*yMult)] = &currentRect
		}
	}
	return highlightedTexture, notHighlightedTexture, rects, animation.FrameLength
}
Пример #14
0
func (s *Scene) Render(renderer *sdl.Renderer) {

	// Empty CullM
	s.CullM.Zero()

	var init int32 = 0
	var Source *sdl.Rect

	var ofX, ofY int32 = tileSize, tileSize

	renderer.SetDrawColor(0, 0, 0, 255)

	// Rendering the map
	for sh := init; sh < s.Window.Height; sh += ofY {

		for sw := init; sw < s.Window.Width; sw += ofX {

			ofX = (tileSize - ((s.Cam.WX + sw) % tileSize))
			ofY = (tileSize - ((s.Cam.WY + sh) % tileSize))

			var worldCellX uint16 = uint16((s.Cam.WX + sw) / tileSize)
			var worldCellY uint16 = uint16((s.Cam.WY + sh) / tileSize)

			// Draw black box for out of bounds areas
			if worldCellX < 0 || worldCellX > s.WidthCells || worldCellY < 0 || worldCellY > s.HeightCells {
				renderer.FillRect(&sdl.Rect{sw, sh, ofX, ofY})
				continue
			}

			rect := Object{
				Pos: sdl.Rect{sw, sh, ofX, ofY},
			}

			for _, gfx := range s.World[worldCellX][worldCellY].Gfxs {

				if gfx != nil {

					if gfx.Txtr == nil {
						continue
					}

					if ofX != int32(tileSize) || ofY != int32(tileSize) {
						Source = &sdl.Rect{gfx.Source.X + (tileSize - ofX), gfx.Source.Y + (tileSize - ofY), ofX, ofY}
					} else {
						Source = gfx.Source
					}

					renderer.Copy(s.TsTxt, Source, &rect.Pos)

				}
			}

			// Updating CullM with SOLID/COLLIDABLE terrain types
			if s.World[worldCellX][worldCellY].Coll {

				// fabricate fake SOL obj
				tempO := &Object{
					Pos:  rect.Pos,
					Gfx:  nil,
					Type: SOL,
				}
				s.CullM.Add(tempO, s)
			}
		}
	}

	// Rendering the enemies
	for _, e := range s.Enemies {
		in, e_pos := s.CullM.Add(e, s)
		if in {
			renderer.Copy(s.SsTxt, e.GetPose(), &e_pos)
		}
	}

	// Rendering the player character
	pos := WorldToScreen(s.PC.Pos, s.Cam)
	renderer.Copy(s.SsTxt, s.PC.GetPose(), &pos)

	// Rendering FRAME RATE COUNTER
	fps := fmt.Sprintf("%v", s.GetFPS())
	surface, _ := s.Font.RenderUTF8_Solid(fps, sdl.Color{255, 255, 255, 255})
	defer surface.Free()

	txtr, _ := renderer.CreateTextureFromSurface(surface)
	defer txtr.Destroy()
	renderer.Copy(txtr, &sdl.Rect{0, 0, surface.W, surface.H}, &sdl.Rect{0, 0, surface.W, surface.H})

	// Rendering Game Objects
	for _, obj := range s.Objects {

		in, obj_pos := s.CullM.Add(obj, s)
		if in {
			renderer.SetDrawColor(255, 0, 0, 125)
			renderer.FillRect(&obj_pos)
		}
	}

	// Rendering GUI Blocks
	for _, gb := range s.GUIBlocks {
		renderer.Copy(gb.Baked, gb.Pos, gb.Pos)
	}

	renderer.Present()
}
Пример #15
0
func (t *TestState) WriteOnScreen(renderer *sdl.Renderer, text string, X int32, Y int32) {
	text_surface := t.font.RenderText_Solid(text, sdl.Color{255, 255, 255, 255})
	text_texture := renderer.CreateTextureFromSurface(text_surface)
	renderer.Copy(text_texture, nil, &sdl.Rect{X, Y, text_surface.W, text_surface.H})
}
Пример #16
0
func LoadTMX(mapname string, renderer *sdl.Renderer) ([][]Space, []*Object) {

	f, _ := os.Open(mapname)
	output, _ := ioutil.ReadAll(f)
	_ = f.Close()

	tmx := &TMX{}

	err := xml.Unmarshal(output, tmx)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error parsing the tmx map: %s\n", err)
		os.Exit(11)
	}

	var tilesetTxt *sdl.Texture

	for i := 0; i < len(tmx.Tilesets); i++ {

		tmx.Tilesets[i].TTMap = make(map[int][4]*Terrain)

		tilesetImg, err := img.Load("assets/" + tmx.Tilesets[i].Image.Src)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed to load PNG: %s\n", err)
			os.Exit(3)
		}
		defer tilesetImg.Free()

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

		for _, tt := range tmx.Tilesets[i].TerrainTiles {

			var terrList [4]*Terrain

			// Spliting and converting into integer so that it
			// can be used as array idx
			for ti, terr := range strings.Split(tt.TerStr, ",") {
				ttype, _ := strconv.Atoi(terr)
				terrList[ti] = &tmx.Tilesets[i].TerrTypes[ttype]
			}
			tmx.Tilesets[i].TTMap[tt.Id] = terrList
		}

		tmx.Tilesets[i].Txtr = tilesetTxt
	}

	world := make([][]Space, tmx.HeightTiles)
	for i := 0; i < tmx.Layers[0].Height; i++ {
		world[i] = make([]Space, tmx.WidthTiles)
	}

	ts := tmx.Tilesets[0]

	for li, layer := range tmx.Layers {
		for i := 0; i < layer.Height; i++ {
			for j := 0; j < layer.Width; j++ {

				tile := layer.Tiles[(i*layer.Height)+j]

				// RENDER ORDER: RIGHT-DOWN
				world[j][i].Terrains = ts.TTMap[tile.Gid]
				world[j][i].Gfxs[li] = &Gfx{
					Txtr:   ts.Txtr,
					Source: ts.GetGIDRect(tile.Gid),
				}

				for _, terr := range world[i][j].Terrains {
					if terr == nil {
						continue
					}

					for _, prop := range terr.PropList {
						switch prop.Name {
						case "COLL":
							world[i][j].Coll = (prop.Value == "1")
							break
						case "WARP":
							world[i][j].Warp = prop.Value
							break
						}
					}
				}
			}
		}
	}

	// Now loading the Tiled Objects
	var objects []*Object

	for _, g := range tmx.ObjGroups {
		for _, o := range g.Objects {
			obj := &Object{
				Pos:  sdl.Rect{o.X, o.Y, o.W, o.H},
				Gfx:  nil,
				Type: TRIG,
			}
			objects = append(objects, obj)
		}
	}

	//tmx = nil
	return world, objects
}
Пример #17
0
// Initialize
func main() {
	var window *sdl.Window
	var renderer *sdl.Renderer
	var err error

	window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
		int(winWidth), int(winHeight), sdl.WINDOW_SHOWN)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err)
		os.Exit(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)
		os.Exit(2)
	}
	defer renderer.Destroy()

	tilesetImg, err := img.Load("assets/textures/ts1.png")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to load PNG: %s\n", err)
		os.Exit(3)
	}
	defer tilesetImg.Free()

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

	spritesheetImg, err := img.Load("assets/textures/actor3.png")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to load PNG: %s\n", err)
		os.Exit(3)
	}
	defer spritesheetImg.Free()

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

	err = ttf.Init()
	font, err = ttf.OpenFont("assets/textures/PressStart2P.ttf", 18)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to load font file: %s\n", err)
		os.Exit(6)
	}

	if err := sdl.Init(sdl.INIT_AUDIO); err != nil {
		fmt.Fprintf(os.Stderr, "Failed to load music file: %s\n", err)
		os.Exit(7)
	}

	if err := mix.Init(mix.INIT_MP3); err != nil {
		fmt.Fprintf(os.Stderr, "Failed to load music file: %s\n", err)
		os.Exit(8)
	}

	currScene = &eng.Scene{
		Window:     eng.Window{winWidth, winHeight},
		StartTime:  time.Now(),
		Cam:        eng.Camera{0, 0},
		EnemyCount: 99,
		TsTxt:      tilesetTxt,
		SsTxt:      spritesheetTxt,
		Font:       font,
	}

	currScene.Init(renderer)

	var running bool = true

	for running {
		then := time.Now()
		running = catchEvents()
		//currScene.Update()
		currScene.Render(renderer)

		dur := time.Since(then)
		sdl.Delay(40 - uint32(dur.Nanoseconds()/1000000))
		currScene.FrameCounter++
	}
}