124 lines
3.4 KiB
Go
124 lines
3.4 KiB
Go
package game
|
|
|
|
import (
|
|
"log"
|
|
"runtime"
|
|
|
|
"git.wisellama.rocks/Wisellama/carpy-breakout/pkg/breakout"
|
|
"git.wisellama.rocks/Wisellama/carpy-breakout/pkg/config"
|
|
"git.wisellama.rocks/Wisellama/carpy-breakout/pkg/globject"
|
|
"git.wisellama.rocks/Wisellama/gosimpleconf"
|
|
gl "github.com/go-gl/gl/v3.1/gles2"
|
|
"github.com/go-gl/mathgl/mgl32"
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
)
|
|
|
|
func Run(configMap gosimpleconf.ConfigMap) {
|
|
// TODO can potentially rework stuff to not need LockOSThread()
|
|
// here. SDL has the built-in sdl.Do(), but I would need to create
|
|
// my own similar thing for OpenGL. Essentially they both need to
|
|
// be locked to the main thread, but anything else can be in a
|
|
// goroutine.
|
|
runtime.LockOSThread()
|
|
|
|
err := sdl.Init(config.SDL_INIT_FLAGS)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer sdl.Quit()
|
|
|
|
err = config.SdlSettings()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
gameWindow, err := NewGameWindow(configMap["game.title"])
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer gameWindow.Destroy()
|
|
|
|
// GL Init requires an active OpenGL context (e.g. the game window)
|
|
err = gl.Init()
|
|
if err != nil {
|
|
log.Fatalf("Failed to initialize OpenGL: %v", err)
|
|
}
|
|
glVersion := gl.GetString(gl.VERSION)
|
|
if glVersion == nil {
|
|
log.Printf("Error getting OpenGL version.\n")
|
|
}
|
|
log.Printf("OpenGL Version: %v\n", gl.GoStr(glVersion))
|
|
|
|
// Then we can setup the OpenGL specific stuff in the game window
|
|
err = gameWindow.GLInitDefault()
|
|
if err != nil {
|
|
log.Printf("error in gameWindow GLInitDefault: %v", err)
|
|
}
|
|
|
|
sunLight := globject.NewPointLight(mgl32.Vec3{-10, 10, 30})
|
|
sunLight.GLInit(gameWindow.GLProgram)
|
|
|
|
camera := globject.NewCamera(gameWindow.GLProgram)
|
|
camera.Position = mgl32.Vec3{0, 0, 30}
|
|
gameWindow.SetCamera(camera)
|
|
gameWindow.AddObject(camera)
|
|
|
|
// textureId, err := glhelpers.NewTexture("square.png")
|
|
// if err != nil {
|
|
// log.Fatal(err)
|
|
// }
|
|
|
|
// Brick Targets
|
|
targets := breakout.NewTargets(4, 5, mgl32.Vec3{0, 6, 0})
|
|
targets.GLInit(gameWindow.GLProgram)
|
|
gameWindow.SetTargets(targets)
|
|
for _, brick := range targets.Bricks {
|
|
gameWindow.AddObject(brick.Box)
|
|
}
|
|
|
|
gameWindowAABB := globject.NewAABB(targets.TopRight, targets.TopRight.Mul(-1))
|
|
gameWindow.AABB = gameWindowAABB
|
|
|
|
// Side Walls
|
|
sideWallsMaterial := globject.NewMaterial()
|
|
sideWallsMaterial.Color = mgl32.Vec4{0.3, 0.3, 0.3, 1}
|
|
sideWalls := breakout.NewSideWalls(gameWindowAABB, sideWallsMaterial)
|
|
sideWalls.GLInit(gameWindow.GLProgram)
|
|
gameWindow.SetSideWalls(sideWalls)
|
|
for _, wall := range sideWalls.Boxes {
|
|
gameWindow.AddObject(wall)
|
|
}
|
|
|
|
// Paddle
|
|
paddleMaterial := globject.NewMaterial()
|
|
paddleMaterial.Color = mgl32.Vec4{0, 1, 0, 1}
|
|
paddleBox := globject.NewBox(6.0, 1.0, 4.0, mgl32.Vec3{0, -8, 0}, paddleMaterial)
|
|
paddle := breakout.NewPaddle(paddleBox)
|
|
paddle.GLInit(gameWindow.GLProgram)
|
|
gameWindow.SetPaddle(paddle)
|
|
gameWindow.AddObject(paddle)
|
|
|
|
// Ball
|
|
ballMaterial := globject.NewMaterial()
|
|
ballMaterial.Color = mgl32.Vec4{1, 1, 1, 1}
|
|
ballBox := globject.NewBox(1, 1, 1, mgl32.Vec3{-8, 0, 0}, ballMaterial)
|
|
ball := breakout.NewBall(ballBox)
|
|
ball.GLInit(gameWindow.GLProgram)
|
|
gameWindow.SetBall(ball)
|
|
gameWindow.AddObject(ball)
|
|
ball.Box.Velocity = mgl32.Vec3{.1, -.1, 0}
|
|
|
|
config.GlSettings()
|
|
for gameWindow.IsRunning() {
|
|
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
|
|
|
|
gameWindow.Update()
|
|
gameWindow.GLDraw()
|
|
|
|
gameWindow.SDLWindow.GLSwap()
|
|
|
|
gameWindow.HandleEvents()
|
|
sdl.Delay(16)
|
|
}
|
|
}
|