mirror of
https://github.com/Kugelschieber/go-game.git
synced 2026-01-18 06:40:28 +00:00
Moved all into one package, added png loader.
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
package gl
|
package goga
|
||||||
|
|
||||||
// The dropable interface is used to clean up GL objects.
|
// The dropable interface is used to clean up GL objects.
|
||||||
// Use the Drop() function to drop a range of objects.
|
// Use the Drop() function to drop a range of objects.
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package gl
|
package goga
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/go-gl/gl/v4.5-core/gl"
|
"github.com/go-gl/gl/v4.5-core/gl"
|
||||||
21
game.go
21
game.go
@@ -1,7 +1,9 @@
|
|||||||
package goga
|
package goga
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/go-gl/gl/v4.5-core/gl"
|
||||||
"github.com/go-gl/glfw/v3.1/glfw"
|
"github.com/go-gl/glfw/v3.1/glfw"
|
||||||
|
"log"
|
||||||
"math"
|
"math"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -35,6 +37,8 @@ var (
|
|||||||
// This function will panic on error.
|
// This function will panic on error.
|
||||||
func Run(game Game, options *RunOptions) {
|
func Run(game Game, options *RunOptions) {
|
||||||
// init glfw
|
// init glfw
|
||||||
|
log.Print("Initializing GLFW")
|
||||||
|
|
||||||
if err := glfw.Init(); err != nil {
|
if err := glfw.Init(); err != nil {
|
||||||
panic("Error initializing GLFW: " + err.Error())
|
panic("Error initializing GLFW: " + err.Error())
|
||||||
}
|
}
|
||||||
@@ -42,6 +46,7 @@ func Run(game Game, options *RunOptions) {
|
|||||||
defer glfw.Terminate()
|
defer glfw.Terminate()
|
||||||
|
|
||||||
// create window
|
// create window
|
||||||
|
log.Print("Creating window")
|
||||||
width := default_width
|
width := default_width
|
||||||
height := default_height
|
height := default_height
|
||||||
title := default_title
|
title := default_title
|
||||||
@@ -60,8 +65,13 @@ func Run(game Game, options *RunOptions) {
|
|||||||
panic("Error creating GLFW window: " + err.Error())
|
panic("Error creating GLFW window: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// start and loop
|
// init go-game
|
||||||
|
log.Print("Initializing goga")
|
||||||
wnd.MakeContextCurrent()
|
wnd.MakeContextCurrent()
|
||||||
|
initGoga()
|
||||||
|
|
||||||
|
// start and loop
|
||||||
|
log.Print("Starting main loop")
|
||||||
delta := time.Duration(0)
|
delta := time.Duration(0)
|
||||||
var deltaSec float64
|
var deltaSec float64
|
||||||
|
|
||||||
@@ -86,16 +96,25 @@ func Run(game Game, options *RunOptions) {
|
|||||||
|
|
||||||
// Stops the game and closes the window.
|
// Stops the game and closes the window.
|
||||||
func Stop() {
|
func Stop() {
|
||||||
|
log.Print("Stopping main loop")
|
||||||
running = false
|
running = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func initGoga() {
|
||||||
|
AddLoader(&PngLoader{gl.LINEAR, false})
|
||||||
|
}
|
||||||
|
|
||||||
func cleanup() {
|
func cleanup() {
|
||||||
// cleanup scenes
|
// cleanup scenes
|
||||||
|
log.Print("Cleaning up scenes")
|
||||||
|
|
||||||
for _, scene := range scenes {
|
for _, scene := range scenes {
|
||||||
scene.Cleanup()
|
scene.Cleanup()
|
||||||
}
|
}
|
||||||
|
|
||||||
// cleanup systems
|
// cleanup systems
|
||||||
|
log.Print("Cleaning up systems")
|
||||||
|
|
||||||
for _, system := range systems {
|
for _, system := range systems {
|
||||||
system.Cleanup()
|
system.Cleanup()
|
||||||
}
|
}
|
||||||
|
|||||||
58
loader.go
Normal file
58
loader.go
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package goga
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/go-gl/gl/v4.5-core/gl"
|
||||||
|
"image"
|
||||||
|
"image/draw"
|
||||||
|
"image/png"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Loads textures from png files.
|
||||||
|
// If keepData is set to true,
|
||||||
|
// pixel data will be stored inside the texture
|
||||||
|
// (additionally to VRAM).
|
||||||
|
type PngLoader struct {
|
||||||
|
Filter int32
|
||||||
|
KeepData bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PngLoader) Load(file string) (Res, error) {
|
||||||
|
// load texture
|
||||||
|
imgFile, err := os.Open(file)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
img, err := png.Decode(imgFile)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
rgba := image.NewRGBA(img.Bounds())
|
||||||
|
draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src)
|
||||||
|
|
||||||
|
// create GL texture
|
||||||
|
tex := NewTex(gl.TEXTURE_2D)
|
||||||
|
tex.Bind()
|
||||||
|
tex.SetDefaultParams(p.Filter)
|
||||||
|
tex.Texture2D(0,
|
||||||
|
gl.RGBA,
|
||||||
|
int32(rgba.Rect.Size().X),
|
||||||
|
int32(rgba.Rect.Size().Y),
|
||||||
|
gl.RGBA,
|
||||||
|
gl.UNSIGNED_BYTE,
|
||||||
|
rgba.Pix)
|
||||||
|
|
||||||
|
if p.KeepData {
|
||||||
|
tex.SetRGBA(rgba)
|
||||||
|
}
|
||||||
|
|
||||||
|
return tex, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PngLoader) Ext() string {
|
||||||
|
return "png"
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package geo
|
package goga
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package geo
|
package goga
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
61
res.go
61
res.go
@@ -10,38 +10,26 @@ import (
|
|||||||
// A generic resource.
|
// A generic resource.
|
||||||
// Must be cast to appropriate type.
|
// Must be cast to appropriate type.
|
||||||
// The name is the file name and must be unique.
|
// The name is the file name and must be unique.
|
||||||
type Res struct {
|
type Res interface {
|
||||||
name string
|
GetName() string
|
||||||
path string
|
SetName(string)
|
||||||
ext string
|
GetPath() string
|
||||||
}
|
SetPath(string)
|
||||||
|
GetExt() string
|
||||||
// Returns the file name of actual resource.
|
SetExt(string)
|
||||||
func (r *Res) GetName() string {
|
|
||||||
return r.name
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns the file path (as passed in) of actual resource.
|
|
||||||
func (r *Res) GetPath() string {
|
|
||||||
return r.path
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns the file extension of actual resource.
|
|
||||||
func (r *Res) GetExt() string {
|
|
||||||
return r.ext
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resource loader interface.
|
// Resource loader interface.
|
||||||
// The loader accepts files by file extension.
|
// The loader accepts files by file extension.
|
||||||
// and loads them if accepted.
|
// and loads them if accepted.
|
||||||
type ResLoader interface {
|
type ResLoader interface {
|
||||||
Load(string) (*Res, error)
|
Load(string) (Res, error)
|
||||||
Ext() string
|
Ext() string
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
resloader []ResLoader
|
resloader []ResLoader
|
||||||
resources []*Res
|
resources []Res
|
||||||
)
|
)
|
||||||
|
|
||||||
// Adds a loader.
|
// Adds a loader.
|
||||||
@@ -110,8 +98,15 @@ func GetLoaderByExt(ext string) ResLoader {
|
|||||||
// Loads a resource by file path.
|
// Loads a resource by file path.
|
||||||
// If no loader is present for given file, an error will be returned.
|
// If no loader is present for given file, an error will be returned.
|
||||||
// If the loader fails to load the resource, an error will be returned.
|
// If the loader fails to load the resource, an error will be returned.
|
||||||
func LoadRes(path string) (*Res, error) {
|
// If the resource name exists already, an error AND the resource will be returned.
|
||||||
|
// This is allows to cleanup on failure.
|
||||||
|
func LoadRes(path string) (Res, error) {
|
||||||
ext := filepath.Ext(path)
|
ext := filepath.Ext(path)
|
||||||
|
|
||||||
|
if len(ext) > 0 {
|
||||||
|
ext = ext[1:]
|
||||||
|
}
|
||||||
|
|
||||||
loader := GetLoaderByExt(ext)
|
loader := GetLoaderByExt(ext)
|
||||||
|
|
||||||
if loader == nil {
|
if loader == nil {
|
||||||
@@ -124,9 +119,13 @@ func LoadRes(path string) (*Res, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
res.SetName(filepath.Base(path))
|
||||||
|
res.SetPath(path)
|
||||||
|
res.SetExt(ext)
|
||||||
|
|
||||||
for _, r := range resources {
|
for _, r := range resources {
|
||||||
if r.name == res.name {
|
if r.GetName() == res.GetName() {
|
||||||
return nil, errors.New("Resource with file name " + res.name + " exists already")
|
return res, errors.New("Resource with file name " + res.GetName() + " exists already")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,9 +158,9 @@ func LoadResFromFolder(path string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Returns a resource by name or nil, if not found.
|
// Returns a resource by name or nil, if not found.
|
||||||
func GetResByName(name string) *Res {
|
func GetResByName(name string) Res {
|
||||||
for _, r := range resources {
|
for _, r := range resources {
|
||||||
if r.name == name {
|
if r.GetName() == name {
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -170,9 +169,9 @@ func GetResByName(name string) *Res {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Returns a resource by path or nil, if not found.
|
// Returns a resource by path or nil, if not found.
|
||||||
func GetResByPath(path string) *Res {
|
func GetResByPath(path string) Res {
|
||||||
for _, r := range resources {
|
for _, r := range resources {
|
||||||
if r.path == path {
|
if r.GetPath() == path {
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -184,7 +183,7 @@ func GetResByPath(path string) *Res {
|
|||||||
// Returns false if resource could not be found.
|
// Returns false if resource could not be found.
|
||||||
func RemoveResByName(name string) bool {
|
func RemoveResByName(name string) bool {
|
||||||
for i, r := range resources {
|
for i, r := range resources {
|
||||||
if r.name == name {
|
if r.GetName() == name {
|
||||||
resources = append(resources[:i], resources[i+1:]...)
|
resources = append(resources[:i], resources[i+1:]...)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -197,7 +196,7 @@ func RemoveResByName(name string) bool {
|
|||||||
// Returns false if resource could not be found.
|
// Returns false if resource could not be found.
|
||||||
func RemoveResByPath(path string) bool {
|
func RemoveResByPath(path string) bool {
|
||||||
for i, r := range resources {
|
for i, r := range resources {
|
||||||
if r.path == path {
|
if r.GetPath() == path {
|
||||||
resources = append(resources[:i], resources[i+1:]...)
|
resources = append(resources[:i], resources[i+1:]...)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -208,5 +207,5 @@ func RemoveResByPath(path string) bool {
|
|||||||
|
|
||||||
// Removes all resources.
|
// Removes all resources.
|
||||||
func RemoveAllRes() {
|
func RemoveAllRes() {
|
||||||
resources = make([]*Res, 0)
|
resources = make([]Res, 0)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
package gl
|
package goga
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"geo"
|
|
||||||
"github.com/go-gl/gl/v4.5-core/gl"
|
"github.com/go-gl/gl/v4.5-core/gl"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -277,7 +276,7 @@ func (s *Shader) SendUniform4x3(name string, data *float32, count int32, transpo
|
|||||||
gl.UniformMatrix4x3fv(s.GetUniformLocation(name), count, transpose, data)
|
gl.UniformMatrix4x3fv(s.GetUniformLocation(name), count, transpose, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Shader) SendMat3(name string, m geo.Mat3) {
|
func (s *Shader) SendMat3(name string, m Mat3) {
|
||||||
var data [9]float32
|
var data [9]float32
|
||||||
|
|
||||||
for i := 0; i < 9; i++ {
|
for i := 0; i < 9; i++ {
|
||||||
@@ -287,7 +286,7 @@ func (s *Shader) SendMat3(name string, m geo.Mat3) {
|
|||||||
gl.UniformMatrix3fv(s.GetUniformLocation(name), 1, false, &data[0])
|
gl.UniformMatrix3fv(s.GetUniformLocation(name), 1, false, &data[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Shader) SendMat4(name string, m geo.Mat4) {
|
func (s *Shader) SendMat4(name string, m Mat4) {
|
||||||
var data [16]float32
|
var data [16]float32
|
||||||
|
|
||||||
for i := 0; i < 16; i++ {
|
for i := 0; i < 16; i++ {
|
||||||
BIN
test/gopher.png
Normal file
BIN
test/gopher.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 40 KiB |
@@ -1,17 +1,19 @@
|
|||||||
package gl
|
package goga
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"geo"
|
|
||||||
"github.com/go-gl/gl/v4.5-core/gl"
|
"github.com/go-gl/gl/v4.5-core/gl"
|
||||||
"image"
|
"image"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Texture object.
|
// Texture object.
|
||||||
type Tex struct {
|
type Tex struct {
|
||||||
|
name string
|
||||||
|
path string
|
||||||
|
ext string
|
||||||
id uint32
|
id uint32
|
||||||
target uint32
|
target uint32
|
||||||
activeTexture uint32
|
activeTexture uint32
|
||||||
size geo.Vec3
|
size Vec3
|
||||||
rgba *image.RGBA // optional, most of the time nil
|
rgba *image.RGBA // optional, most of the time nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,6 +32,36 @@ func (t *Tex) Drop() {
|
|||||||
gl.DeleteBuffers(1, &t.id)
|
gl.DeleteBuffers(1, &t.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns the name of this resource.
|
||||||
|
func (t *Tex) GetName() string {
|
||||||
|
return t.name
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sets the name of this resource.
|
||||||
|
func (t *Tex) SetName(name string) {
|
||||||
|
t.name = name
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the path of this resource.
|
||||||
|
func (t *Tex) GetPath() string {
|
||||||
|
return t.path
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sets the path of this resource.
|
||||||
|
func (t *Tex) SetPath(path string) {
|
||||||
|
t.path = path
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the file extension of this resource.
|
||||||
|
func (t *Tex) GetExt() string {
|
||||||
|
return t.ext
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sets the file extension of this resource.
|
||||||
|
func (t *Tex) SetExt(ext string) {
|
||||||
|
t.ext = ext
|
||||||
|
}
|
||||||
|
|
||||||
// Binds the texture for rendering.
|
// Binds the texture for rendering.
|
||||||
func (t *Tex) Bind() {
|
func (t *Tex) Bind() {
|
||||||
gl.ActiveTexture(t.activeTexture)
|
gl.ActiveTexture(t.activeTexture)
|
||||||
@@ -51,7 +83,7 @@ func (t *Tex) SetDefaultParams(filter int32) {
|
|||||||
|
|
||||||
// Creates a new 1D texture.
|
// Creates a new 1D texture.
|
||||||
func (t *Tex) Texture1D(level, internalFormat, width int32, format, ttype uint32, data []uint8) {
|
func (t *Tex) Texture1D(level, internalFormat, width int32, format, ttype uint32, data []uint8) {
|
||||||
t.size = geo.Vec3{float64(width), 0, 0}
|
t.size = Vec3{float64(width), 0, 0}
|
||||||
t.Bind()
|
t.Bind()
|
||||||
|
|
||||||
if data != nil {
|
if data != nil {
|
||||||
@@ -63,7 +95,7 @@ func (t *Tex) Texture1D(level, internalFormat, width int32, format, ttype uint32
|
|||||||
|
|
||||||
// Creates a new 2D texture.
|
// Creates a new 2D texture.
|
||||||
func (t *Tex) Texture2D(level, internalFormat, width, height int32, format, ttype uint32, data []uint8) {
|
func (t *Tex) Texture2D(level, internalFormat, width, height int32, format, ttype uint32, data []uint8) {
|
||||||
t.size = geo.Vec3{float64(width), float64(height), 0}
|
t.size = Vec3{float64(width), float64(height), 0}
|
||||||
t.Bind()
|
t.Bind()
|
||||||
|
|
||||||
if data != nil {
|
if data != nil {
|
||||||
@@ -75,7 +107,7 @@ func (t *Tex) Texture2D(level, internalFormat, width, height int32, format, ttyp
|
|||||||
|
|
||||||
// Creates a new 3D texture.
|
// Creates a new 3D texture.
|
||||||
func (t *Tex) Texture3D(level, internalFormat, width, height, depth int32, format, ttype uint32, data []uint8) {
|
func (t *Tex) Texture3D(level, internalFormat, width, height, depth int32, format, ttype uint32, data []uint8) {
|
||||||
t.size = geo.Vec3{float64(width), float64(height), float64(depth)}
|
t.size = Vec3{float64(width), float64(height), float64(depth)}
|
||||||
t.Bind()
|
t.Bind()
|
||||||
|
|
||||||
if data != nil {
|
if data != nil {
|
||||||
@@ -122,7 +154,7 @@ func (t *Tex) getActiveTexture() uint32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Returns the size of this texture.
|
// Returns the size of this texture.
|
||||||
func (t *Tex) GetSize() geo.Vec3 {
|
func (t *Tex) GetSize() Vec3 {
|
||||||
return t.size
|
return t.size
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
package gl
|
package goga
|
||||||
|
|
||||||
const NullTerminator = "\x00"
|
const NullTerminator = "\x00"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package geo
|
package goga
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package gl
|
package goga
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/go-gl/gl/v4.5-core/gl"
|
"github.com/go-gl/gl/v4.5-core/gl"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package gl
|
package goga
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/go-gl/gl/v4.5-core/gl"
|
"github.com/go-gl/gl/v4.5-core/gl"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package geo
|
package goga
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package geo
|
package goga
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package geo
|
package goga
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
Reference in New Issue
Block a user