Add multi monitor multiplayer

This commit is contained in:
2024-08-29 18:48:27 +02:00
parent 352c7af7ce
commit 696b827a7d
10 changed files with 1210 additions and 515 deletions
+41 -49
View File
@@ -1,6 +1,7 @@
package main
import (
"fmt"
"github.com/veandco/go-sdl2/sdl"
)
@@ -10,9 +11,24 @@ func initializeSDL() {
}
}
func setupWindowAndSurface() (*sdl.Window, *sdl.Surface) {
func setupWindowAndSurface(playerIndex uint8) (*sdl.Window, *sdl.Surface) {
const windowWidth, windowHeight = 160, 100
window, err := sdl.CreateWindow("test", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, windowWidth, windowHeight, sdl.WINDOW_SHOWN|sdl.WINDOW_RESIZABLE)
window, err := sdl.CreateWindow(fmt.Sprintf("Tunneler - player %d", playerIndex), sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, windowWidth, windowHeight, sdl.WINDOW_SHOWN|sdl.WINDOW_RESIZABLE)
if err != nil {
panic(err)
}
logicalSurface, err := sdl.CreateRGBSurface(0, windowWidth, windowHeight, 32, 0, 0, 0, 0)
if err != nil {
panic(err)
}
return window, logicalSurface
}
func setupMapWindowAndSurface() (*sdl.Window, *sdl.Surface) {
const windowWidth, windowHeight = MapWidth, MapHeight
window, err := sdl.CreateWindow("Tunneler map", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, windowWidth, windowHeight, sdl.WINDOW_SHOWN|sdl.WINDOW_RESIZABLE)
if err != nil {
panic(err)
}
@@ -81,7 +97,6 @@ func handleWindowResize(window *sdl.Window, logicalSurface *sdl.Surface) {
letterboxX := (windowWidth - newWidth) / 2
letterboxY := (windowHeight - newHeight) / 2
windowSurface.FillRect(nil, 0)
srcRect := &sdl.Rect{X: 0, Y: 0, W: logicalSurface.W, H: logicalSurface.H}
dstRect := &sdl.Rect{X: letterboxX, Y: letterboxY, W: newWidth, H: newHeight}
@@ -89,68 +104,45 @@ func handleWindowResize(window *sdl.Window, logicalSurface *sdl.Surface) {
window.UpdateSurface()
}
func renderScene(logicalSurface, playSurface, HUDSurface *sdl.Surface, gameMap *GameMap, players *[]*Player, bases *[]*Base, bullets *[]*Bullet, bulletParticles *[]*BulletParticle, camera *sdl.Rect, playSurfaceRect, playSurfaceTargetRect, HUDSurfaceRect, HUDSurfaceTargetRect *sdl.Rect) {
HUDColor := sdl.MapRGBA(HUDSurface.Format, 101, 101, 101, 255)
logicalColor := sdl.MapRGBA(HUDSurface.Format, 101, 101, 0, 255)
playColor := sdl.MapRGBA(HUDSurface.Format, 101, 0, 101, 255)
logicalSurface.FillRect(nil, logicalColor)
playSurface.FillRect(nil, playColor)
HUDSurface.FillRect(nil, HUDColor)
(*players)[0].track(camera)
gameMap.render(camera, playSurface)
for _, bullet := range *bullets {
(*bullet).render(camera, playSurface)
(*bullet).tick(gameMap, bulletParticles, bullets, players)
}
for _, base := range *bases {
(*base).render(camera, playSurface)
(*base).tick(players)
}
for _, player := range *players {
(*player).render(camera, playSurface)
(*player).tick()
}
for _, bulletParticle := range *bulletParticles {
bulletParticle.render(camera, playSurface)
bulletParticle.tick(bulletParticles)
}
renderHud((*players)[0], HUDSurface)
playSurface.BlitScaled(playSurfaceRect, logicalSurface, playSurfaceTargetRect)
HUDSurface.BlitScaled(HUDSurfaceRect, logicalSurface, HUDSurfaceTargetRect)
}
func adjustWindow(window *sdl.Window, logicalSurface *sdl.Surface, pixelBG uint32) {
func adjustWindow(window *sdl.Window, logicalSurface *sdl.Surface) {
// Get window surface and handle any errors
windowSurface, err := window.GetSurface()
if err != nil {
panic(err)
}
// Retrieve window dimensions
windowWidth, windowHeight := windowSurface.W, windowSurface.H
aspectRatio := float64(logicalSurface.W) / float64(logicalSurface.H)
newWidth := windowWidth
newHeight := int32(float64(windowWidth) / aspectRatio)
if newHeight > windowHeight {
// Calculate aspect ratio
logicalAspectRatio := float64(logicalSurface.W) / float64(logicalSurface.H)
windowAspectRatio := float64(windowWidth) / float64(windowHeight)
var newWidth, newHeight int32
if windowAspectRatio > logicalAspectRatio {
// Window is wider than logical surface
newHeight = windowHeight
newWidth = int32(float64(windowHeight) * aspectRatio)
newWidth = int32(float64(windowHeight) * logicalAspectRatio)
} else {
// Window is taller or equal in aspect ratio
newWidth = windowWidth
newHeight = int32(float64(windowWidth) / logicalAspectRatio)
}
// Calculate letterbox positions
letterboxX := (windowWidth - newWidth) / 2
letterboxY := (windowHeight - newHeight) / 2
windowSurface.FillRect(nil, pixelBG)
// Fill background
windowSurface.FillRect(nil, sdl.MapRGBA(logicalSurface.Format, 80, 20, 10, 255))
// Set source and destination rectangles
srcRect := &sdl.Rect{X: 0, Y: 0, W: logicalSurface.W, H: logicalSurface.H}
dstRect := &sdl.Rect{X: letterboxX, Y: letterboxY, W: newWidth, H: newHeight}
// Perform the scaled blit
logicalSurface.BlitScaled(srcRect, windowSurface, dstRect)
// Update the window surface
window.UpdateSurface()
}