Add Monochrome

This commit is contained in:
Hajime Hoshi 2013-06-23 23:43:44 +09:00
parent 97550279ee
commit 8860d315ca
4 changed files with 81 additions and 4 deletions

View File

@ -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())
}

View File

@ -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":

View File

@ -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},
},
}
}

View File

@ -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]))