Init
This commit is contained in:
+156
@@ -0,0 +1,156 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
)
|
||||
|
||||
func initializeSDL() {
|
||||
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func setupWindowAndSurface() (*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)
|
||||
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 setupPlaySurface() (*sdl.Surface, *sdl.Rect, *sdl.Rect) {
|
||||
playSurface, err := sdl.CreateRGBSurface(0, 76, 76, 32, 0, 0, 0, 0)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
playSurfaceRect := &sdl.Rect{X: 0, Y: 0, W: 76, H: 76}
|
||||
playSurfaceTargetRect := &sdl.Rect{X: 42, Y: 0, W: 76, H: 76}
|
||||
|
||||
return playSurface, playSurfaceRect, playSurfaceTargetRect
|
||||
}
|
||||
|
||||
func setupHUDSurface() (*sdl.Surface, *sdl.Rect, *sdl.Rect) {
|
||||
HUDSurface, err := sdl.CreateRGBSurface(0, 112, 25, 32, 0, 0, 0, 0)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
HUDSurfaceRect := &sdl.Rect{X: 0, Y: 0, W: 112, H: 25}
|
||||
HUDSurfaceTargetRect := &sdl.Rect{X: 24, Y: 76, W: 112, H: 25}
|
||||
|
||||
return HUDSurface, HUDSurfaceRect, HUDSurfaceTargetRect
|
||||
}
|
||||
|
||||
func handleEvents(window *sdl.Window, logicalSurface *sdl.Surface) bool {
|
||||
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||||
switch e := event.(type) {
|
||||
case *sdl.QuitEvent:
|
||||
return false
|
||||
case *sdl.WindowEvent:
|
||||
if e.Event == sdl.WINDOWEVENT_RESIZED {
|
||||
handleWindowResize(window, logicalSurface)
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func handleWindowResize(window *sdl.Window, logicalSurface *sdl.Surface) {
|
||||
windowSurface, err := window.GetSurface()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
windowWidth, windowHeight := windowSurface.W, windowSurface.H
|
||||
|
||||
aspectRatio := float64(logicalSurface.W) / float64(logicalSurface.H)
|
||||
newWidth := windowWidth
|
||||
newHeight := int32(float64(windowWidth) / aspectRatio)
|
||||
|
||||
if newHeight > windowHeight {
|
||||
newHeight = windowHeight
|
||||
newWidth = int32(float64(windowHeight) * aspectRatio)
|
||||
}
|
||||
|
||||
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}
|
||||
|
||||
logicalSurface.BlitScaled(srcRect, windowSurface, dstRect)
|
||||
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) {
|
||||
windowSurface, err := window.GetSurface()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
windowWidth, windowHeight := windowSurface.W, windowSurface.H
|
||||
aspectRatio := float64(logicalSurface.W) / float64(logicalSurface.H)
|
||||
newWidth := windowWidth
|
||||
newHeight := int32(float64(windowWidth) / aspectRatio)
|
||||
|
||||
if newHeight > windowHeight {
|
||||
newHeight = windowHeight
|
||||
newWidth = int32(float64(windowHeight) * aspectRatio)
|
||||
}
|
||||
|
||||
letterboxX := (windowWidth - newWidth) / 2
|
||||
letterboxY := (windowHeight - newHeight) / 2
|
||||
|
||||
windowSurface.FillRect(nil, pixelBG)
|
||||
srcRect := &sdl.Rect{X: 0, Y: 0, W: logicalSurface.W, H: logicalSurface.H}
|
||||
dstRect := &sdl.Rect{X: letterboxX, Y: letterboxY, W: newWidth, H: newHeight}
|
||||
|
||||
logicalSurface.BlitScaled(srcRect, windowSurface, dstRect)
|
||||
window.UpdateSurface()
|
||||
}
|
||||
Reference in New Issue
Block a user