Skip to content

Commit

Permalink
Unify packages to ebiten (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Dec 9, 2014
1 parent 7311369 commit 164b320
Show file tree
Hide file tree
Showing 39 changed files with 378 additions and 399 deletions.
8 changes: 4 additions & 4 deletions graphics/matrix/affine.go → affine.go
@@ -1,16 +1,16 @@
package matrix
package ebiten

type affine interface {
dim() int
element(i, j int) float64
setElement(i, j int, element float64)
}

func isIdentity(matrix affine) bool {
dim := matrix.dim()
func isIdentity(ebiten affine) bool {
dim := ebiten.dim()
for i := 0; i < dim-1; i++ {
for j := 0; j < dim; j++ {
element := matrix.element(i, j)
element := ebiten.element(i, j)
if i == j && element != 1 {
return false
} else if i != j && element != 0 {
Expand Down
85 changes: 85 additions & 0 deletions colormatrix.go
@@ -0,0 +1,85 @@
package ebiten

import (
"image/color"
"math"
)

const ColorMatrixDim = 5

type ColorMatrix struct {
Elements [ColorMatrixDim - 1][ColorMatrixDim]float64
}

func ColorMatrixI() ColorMatrix {
return ColorMatrix{
[ColorMatrixDim - 1][ColorMatrixDim]float64{
{1, 0, 0, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
},
}
}

func (c *ColorMatrix) dim() int {
return ColorMatrixDim
}

func (c *ColorMatrix) Concat(other ColorMatrix) {
result := ColorMatrix{}
mul(&other, c, &result)
*c = result
}

func (c *ColorMatrix) IsIdentity() bool {
return isIdentity(c)
}

func (c *ColorMatrix) element(i, j int) float64 {
return c.Elements[i][j]
}

func (c *ColorMatrix) setElement(i, j int, element float64) {
c.Elements[i][j] = element
}

func Monochrome() ColorMatrix {
const r float64 = 6968.0 / 32768.0
const g float64 = 23434.0 / 32768.0
const b float64 = 2366.0 / 32768.0
return ColorMatrix{
[ColorMatrixDim - 1][ColorMatrixDim]float64{
{r, g, b, 0, 0},
{r, g, b, 0, 0},
{r, g, b, 0, 0},
{0, 0, 0, 1, 0},
},
}
}

func rgba(clr color.Color) (float64, float64, float64, float64) {
r, g, b, a := clr.RGBA()
rf := float64(r) / float64(math.MaxUint16)
gf := float64(g) / float64(math.MaxUint16)
bf := float64(b) / float64(math.MaxUint16)
af := float64(a) / float64(math.MaxUint16)
return rf, gf, bf, af
}

func (c *ColorMatrix) Scale(clr color.Color) {
rf, gf, bf, af := rgba(clr)
for i, e := range []float64{rf, gf, bf, af} {
for j := 0; j < 4; j++ {
c.Elements[i][j] *= e
}
}
}

func (c *ColorMatrix) Translate(clr color.Color) {
rf, gf, bf, af := rgba(clr)
c.Elements[0][4] = rf
c.Elements[1][4] = gf
c.Elements[2][4] = bf
c.Elements[3][4] = af
}
6 changes: 3 additions & 3 deletions graphics/matrix/color_test.go → colormatrix_test.go
@@ -1,13 +1,13 @@
package matrix_test
package ebiten_test

import (
. "."
"testing"
)

func TestColorIdentity(t *testing.T) {
matrix := ColorI()
got := matrix.IsIdentity()
ebiten := ColorMatrixI()
got := ebiten.IsIdentity()
want := true
if want != got {
t.Errorf("matrix.IsIdentity() = %t, want %t", got, want)
Expand Down
6 changes: 3 additions & 3 deletions example/blocks.go
Expand Up @@ -2,9 +2,9 @@ package main

import (
"flag"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/example/blocks"
"github.com/hajimehoshi/ebiten/ui"
"github.com/hajimehoshi/ebiten/ui/glfw"
"github.com/hajimehoshi/ebiten/glfw"
"log"
"os"
"runtime"
Expand All @@ -30,7 +30,7 @@ func main() {

u := new(glfw.UI)
game := blocks.NewGame()
if err := ui.Run(u, game, blocks.ScreenWidth, blocks.ScreenHeight, 2, "Blocks (Ebiten Demo)", 60); err != nil {
if err := ebiten.Run(u, game, blocks.ScreenWidth, blocks.ScreenHeight, 2, "Blocks (Ebiten Demo)", 60); err != nil {
log.Fatal(err)
}
}
5 changes: 2 additions & 3 deletions example/blocks/field.go
@@ -1,8 +1,7 @@
package blocks

import (
"github.com/hajimehoshi/ebiten/graphics"
"github.com/hajimehoshi/ebiten/graphics/matrix"
"github.com/hajimehoshi/ebiten"
)

type Field struct {
Expand Down Expand Up @@ -97,7 +96,7 @@ func (f *Field) flushLine(j int) bool {
return true
}

func (f *Field) Draw(context graphics.Context, textures *Textures, geo matrix.Geometry) {
func (f *Field) Draw(context ebiten.GraphicsContext, textures *Textures, geo ebiten.GeometryMatrix) {
blocks := make([][]BlockType, len(f.blocks))
for i, blockCol := range f.blocks {
blocks[i] = make([]BlockType, len(blockCol))
Expand Down
17 changes: 8 additions & 9 deletions example/blocks/font.go
@@ -1,8 +1,7 @@
package blocks

import (
"github.com/hajimehoshi/ebiten/graphics"
"github.com/hajimehoshi/ebiten/graphics/matrix"
"github.com/hajimehoshi/ebiten"
"image/color"
)

Expand All @@ -17,9 +16,9 @@ func textWidth(str string) int {
return charWidth * len(str)
}

func drawText(context graphics.Context, textures *Textures, str string, x, y, scale int, clr color.Color) {
func drawText(context ebiten.GraphicsContext, textures *Textures, str string, x, y, scale int, clr color.Color) {
fontTextureId := textures.GetTexture("font")
parts := []graphics.TexturePart{}
parts := []ebiten.TexturePart{}

locationX := 0
locationY := 0
Expand All @@ -32,23 +31,23 @@ func drawText(context graphics.Context, textures *Textures, str string, x, y, sc
code := int(c)
x := (code % 16) * charWidth
y := ((code - 32) / 16) * charHeight
parts = append(parts, graphics.TexturePart{
parts = append(parts, ebiten.TexturePart{
LocationX: locationX,
LocationY: locationY,
Source: graphics.Rect{x, y, charWidth, charHeight},
Source: ebiten.Rect{x, y, charWidth, charHeight},
})
locationX += charWidth
}

geoMat := matrix.GeometryI()
geoMat := ebiten.GeometryMatrixI()
geoMat.Scale(float64(scale), float64(scale))
geoMat.Translate(float64(x), float64(y))
clrMat := matrix.ColorI()
clrMat := ebiten.ColorMatrixI()
clrMat.Scale(clr)
context.Texture(fontTextureId).Draw(parts, geoMat, clrMat)
}

func drawTextWithShadow(context graphics.Context, textures *Textures, str string, x, y, scale int, clr color.Color) {
func drawTextWithShadow(context ebiten.GraphicsContext, textures *Textures, str string, x, y, scale int, clr color.Color) {
drawText(context, textures, str, x+1, y+1, scale, color.RGBA{0, 0, 0, 0x80})
drawText(context, textures, str, x, y, scale, clr)
}
12 changes: 6 additions & 6 deletions example/blocks/game.go
@@ -1,7 +1,7 @@
package blocks

import (
"github.com/hajimehoshi/ebiten/graphics"
"github.com/hajimehoshi/ebiten"
"sync"
)

Expand All @@ -24,14 +24,14 @@ type GameState struct {

type Game struct {
sceneManager *SceneManager
input *Input
ebiten *Input
textures *Textures
}

func NewGame() *Game {
game := &Game{
sceneManager: NewSceneManager(NewTitleScene()),
input: NewInput(),
ebiten: NewInput(),
textures: NewTextures(),
}
return game
Expand Down Expand Up @@ -65,15 +65,15 @@ func (game *Game) Update() error {
if !game.isInitialized() {
return nil
}
game.input.Update()
game.ebiten.Update()
game.sceneManager.Update(&GameState{
SceneManager: game.sceneManager,
Input: game.input,
Input: game.ebiten,
})
return nil
}

func (game *Game) Draw(context graphics.Context) error {
func (game *Game) Draw(context ebiten.GraphicsContext) error {
if !game.isInitialized() {
return nil
}
Expand Down
24 changes: 11 additions & 13 deletions example/blocks/gamescene.go
@@ -1,9 +1,7 @@
package blocks

import (
"github.com/hajimehoshi/ebiten/graphics"
"github.com/hajimehoshi/ebiten/graphics/matrix"
"github.com/hajimehoshi/ebiten/input"
"github.com/hajimehoshi/ebiten"
"image/color"
"math/rand"
"time"
Expand Down Expand Up @@ -64,26 +62,26 @@ func (s *GameScene) Update(state *GameState) {
y := s.currentPieceY
angle := s.currentPieceAngle
moved := false
if state.Input.StateForKey(input.KeySpace) == 1 {
if state.Input.StateForKey(ebiten.KeySpace) == 1 {
s.currentPieceAngle = s.field.RotatePieceRight(piece, x, y, angle)
moved = angle != s.currentPieceAngle
}
if l := state.Input.StateForKey(input.KeyLeft); l == 1 || (10 <= l && l%2 == 0) {
if l := state.Input.StateForKey(ebiten.KeyLeft); l == 1 || (10 <= l && l%2 == 0) {
s.currentPieceX = s.field.MovePieceToLeft(piece, x, y, angle)
moved = x != s.currentPieceX
}
if r := state.Input.StateForKey(input.KeyRight); r == 1 || (10 <= r && r%2 == 0) {
if r := state.Input.StateForKey(ebiten.KeyRight); r == 1 || (10 <= r && r%2 == 0) {
s.currentPieceX = s.field.MovePieceToRight(piece, x, y, angle)
moved = y != s.currentPieceX
}
if d := state.Input.StateForKey(input.KeyDown); (d-1)%2 == 0 {
if d := state.Input.StateForKey(ebiten.KeyDown); (d-1)%2 == 0 {
s.currentPieceY = s.field.DropPiece(piece, x, y, angle)
moved = y != s.currentPieceY
}
if moved {
s.landingCount = 0
} else if !s.field.PieceDroppable(piece, x, y, angle) {
if 0 < state.Input.StateForKey(input.KeyDown) {
if 0 < state.Input.StateForKey(ebiten.KeyDown) {
s.landingCount += 10
} else {
s.landingCount++
Expand All @@ -97,20 +95,20 @@ func (s *GameScene) Update(state *GameState) {
}
}

func (s *GameScene) Draw(context graphics.Context, textures *Textures) {
func (s *GameScene) Draw(context ebiten.GraphicsContext, textures *Textures) {
context.Fill(0xff, 0xff, 0xff)

field := textures.GetTexture("empty")
geoMat := matrix.GeometryI()
geoMat := ebiten.GeometryMatrixI()
geoMat.Scale(
float64(fieldWidth)/float64(emptyWidth),
float64(fieldHeight)/float64(emptyHeight))
geoMat.Translate(20, 20) // magic number?
colorMat := matrix.ColorI()
colorMat := ebiten.ColorMatrixI()
colorMat.Scale(color.RGBA{0, 0, 0, 0x80})
graphics.DrawWhole(context.Texture(field), emptyWidth, emptyHeight, geoMat, colorMat)
ebiten.DrawWhole(context.Texture(field), emptyWidth, emptyHeight, geoMat, colorMat)

geoMat = matrix.GeometryI()
geoMat = ebiten.GeometryMatrixI()
geoMat.Translate(20, 20)
s.field.Draw(context, textures, geoMat)

Expand Down
12 changes: 6 additions & 6 deletions example/blocks/input.go
@@ -1,30 +1,30 @@
package blocks

import (
"github.com/hajimehoshi/ebiten/input"
"github.com/hajimehoshi/ebiten"
)

type Input struct {
states map[input.Key]int
states map[ebiten.Key]int
}

func NewInput() *Input {
states := map[input.Key]int{}
for key := input.Key(0); key < input.KeyMax; key++ {
states := map[ebiten.Key]int{}
for key := ebiten.Key(0); key < ebiten.KeyMax; key++ {
states[key] = 0
}
return &Input{
states: states,
}
}

func (i *Input) StateForKey(key input.Key) int {
func (i *Input) StateForKey(key ebiten.Key) int {
return i.states[key]
}

func (i *Input) Update() {
for key := range i.states {
if !input.IsKeyPressed(key) {
if !ebiten.IsKeyPressed(key) {
i.states[key] = 0
continue
}
Expand Down

0 comments on commit 164b320

Please sign in to comment.