gosge

package module
v0.1.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 18, 2020 License: Apache-2.0 Imports: 12 Imported by: 13

README

gosge

Go Simple Game Engine using an ECS

go version godoc Build Status conduct

Info

gosge is an opinionated 2D only game engine the uses GOECS for easily develop games with an ECS paradigm.

Internally uses the go port of raylib for most of the device functionalities, including rendering.

Simple Hello World

package main

import (
	"github.com/juan-medina/gosge"
	"github.com/juan-medina/gosge/components/color"
	"github.com/juan-medina/gosge/components/geometry"
	"github.com/juan-medina/gosge/components/ui"
	"github.com/juan-medina/gosge/options"
	"log"
)

// game options
var opt = options.Options{
	Title:      "Hello Game",
	BackGround: color.Black,
}

const (
	fontName = "resources/go_regular.fnt"
	fontSize = 100
)

var (
	// designResolution is how our game is designed
	designResolution = geometry.Size{Width: 1920, Height: 1080}
)

// Simple Usage
func main() {
	if err := gosge.Run(opt, loadGame); err != nil {
		log.Fatalf("error running the game: %v", err)
	}
}

func loadGame(eng *gosge.Engine) error {
	// Preload font
	if err := eng.LoadFont(fontName); err != nil {
		return err
	}

	// get the ECS world
	world := eng.World()

	// gameScale from the real screen size to our design resolution
	gameScale := eng.GetScreenSize().CalculateScale(designResolution)

	// add the centered text
	world.AddEntity(
		ui.Text{
			String:     "Hello World",
			HAlignment: ui.CenterHAlignment,
			VAlignment: ui.MiddleVAlignment,
			Font:       fontName,
			Size:       fontSize * gameScale.Min,
		},
		geometry.Point{
			X: designResolution.Width / 2 * gameScale.Point.X,
			Y: designResolution.Height / 2 * gameScale.Point.Y,
		},
		color.White,
	)
	return nil
}

Run examples

The examples are available on this folder, and can be run using make:

$ make run example=hello
$ make run example=eyes
$ make run example=layers
$ make run example=stages
$ make run example=animation
$ make run example=audio
$ make run example=tiled
$ make run example=draw
$ make run example=draw
$ make run example=collision

Alternatively you could run them with :

$ go run examples/hello/hello.go
$ go run examples/eyes/eyes.go
$ go run examples/layers/layers.go
$ go run examples/stages/stages.go
$ go run examples/animation/animation.go
$ go run examples/audio/audio.go
$ go run examples/tiled/tiled.go
$ go run examples/draw/draw.go
$ go run examples/collision/collision.go

Requirements

Ubuntu
X11
apt-get install libgl1-mesa-dev libxi-dev libxcursor-dev libxrandr-dev libxinerama-dev
Wayland
apt-get install libgl1-mesa-dev libwayland-dev libxkbcommon-dev
Fedora
X11
dnf install mesa-libGL-devel libXi-devel libXcursor-devel libXrandr-devel libXinerama-devel
Wayland
dnf install mesa-libGL-devel wayland-devel libxkbcommon-devel
macOS

On macOS, you need Xcode or Command Line Tools for Xcode.

Windows

On Windows, you need C compiler, like Mingw-w64 or TDM-GCC. You can also build binary in MSYS2 shell.

Installation

go get -v -u github.com/juan-medina/gosge

Build Tags

  • opengl21 : uses OpenGL 2.1 backend (default is 3.3)
  • wayland : builds against Wayland libraries

Examples Resources

Documentation

Overview

Package gosge is a opinionated 2D only game engine that use an ECS

gosge uses https://github.com/juan-medina/goecs) and https://github.com/gen2brain/raylib-go, the go port of https://www.raylib.com/ for rendering and device manager

Example

Simple Usage

/*
 * Copyright (c) 2020 Juan Medina.
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 */

package main

import (
	"github.com/juan-medina/gosge"
	"github.com/juan-medina/gosge/components/color"
	"github.com/juan-medina/gosge/components/geometry"
	"github.com/juan-medina/gosge/components/ui"
	"github.com/juan-medina/gosge/options"
	"log"
)

// game options
var opt = options.Options{
	Title:      "Hello Game",
	BackGround: color.Black,
}

const (
	fontName = "resources/go_regular.fnt"
	fontSize = 100
)

var (
	// designResolution is how our game is designed
	designResolution = geometry.Size{Width: 1920, Height: 1080}
)

// Simple Usage
func main() {
	if err := gosge.Run(opt, loadGame); err != nil {
		log.Fatalf("error running the game: %v", err)
	}
}

func loadGame(eng *gosge.Engine) error {
	// Preload font
	if err := eng.LoadFont(fontName); err != nil {
		return err
	}

	// get the ECS world
	world := eng.World()

	// gameScale from the real screen size to our design resolution
	gameScale := eng.GetScreenSize().CalculateScale(designResolution)

	// add the centered text
	world.AddEntity(
		ui.Text{
			String:     "Hello World",
			HAlignment: ui.CenterHAlignment,
			VAlignment: ui.MiddleVAlignment,
			Font:       fontName,
			Size:       fontSize * gameScale.Min,
		},
		geometry.Point{
			X: designResolution.Width / 2 * gameScale.Point.X,
			Y: designResolution.Height / 2 * gameScale.Point.Y,
		},
		color.White,
	)
	return nil
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(opt options.Options, init InitFunc) error

Run the game given the game options.Options and the loading InitFunc

Types

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine is our game engine

func New

func New(opt options.Options, init InitFunc) *Engine

New return a Engine

func (*Engine) AddGameStage

func (e *Engine) AddGameStage(name string, init InitFunc)

AddGameStage adds a new game stage to our game with the given name, for changing to that stage send a events.ChangeStage

func (Engine) GeTiledMapSize

func (e Engine) GeTiledMapSize(name string) (size geometry.Size, err error)

GeTiledMapSize returns the geometry.Size of a given tile map

func (*Engine) GetScreenSize

func (e *Engine) GetScreenSize() geometry.Size

GetScreenSize returns the current screen size

func (*Engine) GetSpriteSize

func (e *Engine) GetSpriteSize(sheet string, name string) (geometry.Size, error)

GetSpriteSize returns the geometry.Size of a given sprite

func (*Engine) Listener

func (e *Engine) Listener(_ *goecs.World, event interface{}, _ float32) error

Listener listen to game events

func (Engine) LoadFont

func (e Engine) LoadFont(fileName string) error

LoadFont preloads a font

func (Engine) LoadMusic

func (e Engine) LoadMusic(filename string) error

LoadMusic preloads a music stream

func (Engine) LoadSound

func (e Engine) LoadSound(filename string) error

LoadSound preload a sound wave

func (Engine) LoadSprite

func (e Engine) LoadSprite(filename string, pivot geometry.Point) error

LoadSprite preloads a single sprite.Sprite

func (*Engine) LoadSpriteSheet

func (e *Engine) LoadSpriteSheet(fileName string) error

LoadSpriteSheet preloads a sprite.

func (Engine) LoadTiledMap

func (e Engine) LoadTiledMap(filename string) error

LoadTiledMap preload a tiled map

func (Engine) MeasureText

func (e Engine) MeasureText(font string, str string, size float32) (result geometry.Size, err error)

MeasureText return the geometry.Size of a string with a defined size and spacing

func (*Engine) Run

func (e *Engine) Run() error

Run a game within the engine

func (*Engine) SetBackgroundColor

func (e *Engine) SetBackgroundColor(color color.Solid)

SetBackgroundColor changes the current background color.Solid

func (Engine) SpriteAtContains

func (e Engine) SpriteAtContains(spr sprite.Sprite, at geometry.Point, point geometry.Point) bool

SpriteAtContains indicates if a sprite.Sprite at a given geometry.Point contains a geometry.Point

func (Engine) SpritesCollides added in v0.1.7

func (e Engine) SpritesCollides(spr1 sprite.Sprite, at1 geometry.Point, spr2 sprite.Sprite, at2 geometry.Point) bool

SpritesCollides indicates if two sprite.Sprite collides

func (*Engine) World

func (e *Engine) World() *goecs.World

World returns the game goecs.World

type InitFunc

type InitFunc func(eng *Engine) error

InitFunc is a function that will get call for our game to load

Directories

Path Synopsis
Package components contains the components already defined
Package components contains the components already defined
animation
Package animation are the components for running and sprite.Sprite animations
Package animation are the components for running and sprite.Sprite animations
audio
Package audio are the components for audio and music
Package audio are the components for audio and music
color
Package color handles Color components
Package color handles Color components
device
Package device include device related types
Package device include device related types
effects
Package effects include different components for adding effects
Package effects include different components for adding effects
geometry
Package geometry handle components with geometry concepts
Package geometry handle components with geometry concepts
shapes
Package shapes contains various drawable shapes
Package shapes contains various drawable shapes
sprite
Package sprite handle the Sprite component
Package sprite handle the Sprite component
tiled
Package tiled has the components for adding tiled maps to our game
Package tiled has the components for adding tiled maps to our game
ui
Package ui contains the components for displaying ui elements
Package ui contains the components for displaying ui elements
Package events contain the events for our engine
Package events contain the events for our engine
examples
Package managers contains the managers for the engine
Package managers contains the managers for the engine
ray
Package ray is the managers.Device implementation using raylib
Package ray is the managers.Device implementation using raylib
Package options contains our game Options
Package options contains our game Options

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL