GOingTunneling/hud.go

84 lines
2.1 KiB
Go
Raw Permalink Normal View History

2024-08-29 00:05:28 +02:00
package main
import "github.com/veandco/go-sdl2/sdl"
2024-08-29 18:48:27 +02:00
var letterOffsets = []int32{
2,
10,
18,
}
var letterColors []uint32
// Map function to convert value from range [0, 6] to range [0, 88]
2024-08-30 19:07:56 +02:00
func mapRange(x, minX, maxX, minY, maxY uint32) int32 {
2024-08-29 18:48:27 +02:00
// Perform conversion to float64 to ensure proper division
return int32((float64(x-minX)/float64(maxX-minX))*float64(maxY-minY) + float64(minY))
}
var rects = [][]sdl.Rect{
// Define the rects with modified Y-values
{
{X: 5, Y: 0, W: 6, H: 1},
{X: 5, Y: 0, W: 1, H: 5},
{X: 5, Y: 2, W: 6, H: 1},
{X: 5, Y: 4, W: 6, H: 1},
},
{
{X: 6, Y: 0, W: 4, H: 1}, // Top bar of "A"
{X: 5, Y: 1, W: 1, H: 4}, // Left vertical line of "A"
{X: 10, Y: 1, W: 1, H: 4}, // Right vertical line of "A"
{X: 6, Y: 3, W: 4, H: 1}, // Middle bar of "A"
},
2024-08-29 00:05:28 +02:00
2024-08-29 18:48:27 +02:00
{
{X: 5, Y: 0, W: 6, H: 1},
{X: 5, Y: 0, W: 1, H: 3},
{X: 5, Y: 2, W: 6, H: 1},
{X: 10, Y: 2, W: 1, H: 3},
{X: 5, Y: 4, W: 6, H: 1},
},
}
func initHud(surface *sdl.Surface) {
letterColors = []uint32{
sdl.MapRGBA(surface.Format, 243, 235, 28, 255),
sdl.MapRGBA(surface.Format, 255, 80, 40, 255),
sdl.MapRGBA(surface.Format, 40, 243, 243, 255),
2024-08-29 00:05:28 +02:00
}
2024-08-29 18:48:27 +02:00
HUDColor := sdl.MapRGBA(surface.Format, 101, 101, 101, 255)
2024-09-01 20:13:53 +02:00
_ = surface.FillRect(nil, HUDColor)
2024-08-29 00:05:28 +02:00
2024-08-29 18:48:27 +02:00
for letterIndex, letter := range rects {
for _, letterRect := range letter {
offsetRect := letterRect
offsetRect.Y += letterOffsets[letterIndex]
2024-09-01 20:13:53 +02:00
_ = surface.FillRect(&offsetRect, letterColors[letterIndex])
2024-08-29 18:48:27 +02:00
}
2024-08-29 00:05:28 +02:00
}
2024-08-29 18:48:27 +02:00
}
func renderHud(player *Player, surface *sdl.Surface) {
for _, letterOffset := range letterOffsets {
2024-09-01 20:13:53 +02:00
_ = surface.FillRect(&sdl.Rect{X: 16, Y: letterOffset - 1, W: 90, H: 7}, 0)
2024-08-29 00:05:28 +02:00
}
2024-08-29 18:48:27 +02:00
HUDValues := []int32{
2024-08-30 19:07:56 +02:00
mapRange(player.energy, 0, serverConfig.MaxEnergy, 0, 88),
mapRange(player.ammunition, 0, serverConfig.MaxAmmunition, 0, 88),
mapRange(player.shields, 0, serverConfig.MaxShields, 0, 88),
2024-08-29 18:48:27 +02:00
}
2024-08-29 00:05:28 +02:00
2024-08-29 18:48:27 +02:00
for HUDValueIndex, HUDValue := range HUDValues {
2024-09-01 20:13:53 +02:00
_ = surface.FillRect(
2024-08-29 18:48:27 +02:00
&sdl.Rect{
X: 17,
Y: letterOffsets[HUDValueIndex],
W: HUDValue,
H: 5},
letterColors[HUDValueIndex])
}
2024-08-29 00:05:28 +02:00
}