From 8860d315ca57218bc2d45292210e14a0f1cec5bc Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 23 Jun 2013 23:43:44 +0900 Subject: [PATCH] Add Monochrome --- example/game/monochrome/monochrome.go | 60 +++++++++++++++++++++++++++ example/glut/main.go | 3 ++ graphics/matrix/color.go | 14 +++++++ graphics/opengl/graphics_context.go | 8 ++-- 4 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 example/game/monochrome/monochrome.go diff --git a/example/game/monochrome/monochrome.go b/example/game/monochrome/monochrome.go new file mode 100644 index 000000000..d20506e40 --- /dev/null +++ b/example/game/monochrome/monochrome.go @@ -0,0 +1,60 @@ +package monochrome + +import ( + "github.com/hajimehoshi/go.ebiten/graphics" + "github.com/hajimehoshi/go.ebiten/graphics/matrix" + "image" + "image/color" + _ "image/png" + "os" +) + +type Monochrome struct { + ebitenTexture graphics.Texture +} + +func New() *Monochrome { + return &Monochrome{} +} + +func (game *Monochrome) ScreenWidth() int { + return 256 +} + +func (game *Monochrome) ScreenHeight() int { + return 240 +} + +func (game *Monochrome) Fps() int { + return 60 +} + +func (game *Monochrome) Init(tf graphics.TextureFactory) { + file, err := os.Open("ebiten.png") + if err != nil { + panic(err) + } + defer file.Close() + + img, _, err := image.Decode(file) + if err != nil { + panic(err) + } + if game.ebitenTexture, err = tf.NewTextureFromImage(img); err != nil { + panic(err) + } +} + +func (game *Monochrome) Update() { +} + +func (game *Monochrome) Draw(g graphics.GraphicsContext, offscreen graphics.Texture) { + g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255}) + + geometryMatrix := matrix.IdentityGeometry() + tx := game.ScreenWidth() / 2 - game.ebitenTexture.Width / 2 + ty := game.ScreenHeight() / 2 - game.ebitenTexture.Height / 2 + geometryMatrix.Translate(float64(tx), float64(ty)) + g.DrawTexture(game.ebitenTexture.ID, + geometryMatrix, matrix.Monochrome()) +} diff --git a/example/glut/main.go b/example/glut/main.go index 55a3a386e..3bf71146e 100644 --- a/example/glut/main.go +++ b/example/glut/main.go @@ -18,6 +18,7 @@ package main import "C" import ( "github.com/hajimehoshi/go.ebiten" + "github.com/hajimehoshi/go.ebiten/example/game/monochrome" "github.com/hajimehoshi/go.ebiten/example/game/rects" "github.com/hajimehoshi/go.ebiten/example/game/rotating" "github.com/hajimehoshi/go.ebiten/example/game/sprites" @@ -89,6 +90,8 @@ func main() { var gm ebiten.Game switch gameName { + case "monochrome": + gm = monochrome.New() case "rects": gm = rects.New() case "rotating": diff --git a/graphics/matrix/color.go b/graphics/matrix/color.go index 5e4bc85b6..4cc4c28ed 100644 --- a/graphics/matrix/color.go +++ b/graphics/matrix/color.go @@ -38,3 +38,17 @@ func (matrix *Color) element(i, j int) float64 { func (matrix *Color) setElement(i, j int, element float64) { matrix.Elements[i][j] = element } + +func Monochrome() Color { + const r float64 = 6968.0 / 32768.0 + const g float64 = 23434.0 / 32768.0 + const b float64 = 2366.0 / 32768.0 + return Color{ + [colorDim - 1][colorDim]float64{ + {r, g, b, 0, 0}, + {r, g, b, 0, 0}, + {r, g, b, 0, 0}, + {0, 0, 0, 1, 0}, + }, + } +} diff --git a/graphics/opengl/graphics_context.go b/graphics/opengl/graphics_context.go index 906342863..16acb78b5 100644 --- a/graphics/opengl/graphics_context.go +++ b/graphics/opengl/graphics_context.go @@ -293,10 +293,10 @@ func (context *GraphicsContext) setShaderProgram( } glColorMatrix := [...]float32{ - e[0][0], e[0][1], e[0][2], e[0][3], - e[1][0], e[1][1], e[1][2], e[1][3], - e[2][0], e[2][1], e[2][2], e[2][3], - e[3][0], e[3][1], e[3][2], e[3][3], + e[0][0], e[1][0], e[2][0], e[3][0], + e[0][1], e[1][1], e[2][1], e[3][1], + e[0][2], e[1][2], e[2][2], e[3][2], + e[0][3], e[1][3], e[2][3], e[3][3], } C.glUniformMatrix4fv(getUniformLocation(program, "color_matrix"), 1, C.GL_FALSE, (*C.GLfloat)(&glColorMatrix[0]))