Refactoring

This commit is contained in:
Hajime Hoshi 2014-12-07 18:25:28 +09:00
parent 6a2229f973
commit 9b05640ddf
6 changed files with 17 additions and 22 deletions

View File

@ -2,7 +2,6 @@ package blocks
import ( import (
"github.com/hajimehoshi/ebiten/graphics" "github.com/hajimehoshi/ebiten/graphics"
_ "image/png"
"sync" "sync"
) )

View File

@ -3,6 +3,7 @@ package blocks
import ( import (
"github.com/hajimehoshi/ebiten/graphics" "github.com/hajimehoshi/ebiten/graphics"
"image" "image"
_ "image/png"
"os" "os"
"sync" "sync"
) )

View File

@ -46,7 +46,7 @@ func NewContext(screenWidth, screenHeight, screenScale int) *Context {
var err error var err error
context.screenId, err = idsInstance.createRenderTarget(screenWidth, screenHeight, graphics.FilterNearest) context.screenId, err = idsInstance.createRenderTarget(screenWidth, screenHeight, graphics.FilterNearest)
if err != nil { if err != nil {
panic("initializing the offscreen failed: " + err.Error()) panic("opengl.NewContext: initializing the offscreen failed: " + err.Error())
} }
context.ResetOffscreen() context.ResetOffscreen()
context.Clear() context.Clear()

View File

@ -10,16 +10,6 @@ import (
"sync" "sync"
) )
func glMatrix(matrix [4][4]float64) [16]float32 {
result := [16]float32{}
for j := 0; j < 4; j++ {
for i := 0; i < 4; i++ {
result[i+j*4] = float32(matrix[i][j])
}
}
return result
}
type ids struct { type ids struct {
textures map[graphics.TextureID]*Texture textures map[graphics.TextureID]*Texture
renderTargets map[graphics.RenderTargetID]*RenderTarget renderTargets map[graphics.RenderTargetID]*RenderTarget
@ -36,15 +26,11 @@ var idsInstance = &ids{
currentRenderTargetId: -1, currentRenderTargetId: -1,
} }
func CreateRenderTarget( func CreateRenderTarget(width, height int, filter graphics.Filter) (graphics.RenderTargetID, error) {
width, height int,
filter graphics.Filter) (graphics.RenderTargetID, error) {
return idsInstance.createRenderTarget(width, height, filter) return idsInstance.createRenderTarget(width, height, filter)
} }
func CreateTexture( func CreateTexture(img image.Image, filter graphics.Filter) (graphics.TextureID, error) {
img image.Image,
filter graphics.Filter) (graphics.TextureID, error) {
return idsInstance.createTexture(img, filter) return idsInstance.createTexture(img, filter)
} }
@ -153,7 +139,7 @@ func (i *ids) drawTexture(
r := i.renderTargetAt(target) r := i.renderTargetAt(target)
projectionMatrix := r.projectionMatrix() projectionMatrix := r.projectionMatrix()
quads := graphics.TextureQuads(parts, texture.width, texture.height) quads := graphics.TextureQuads(parts, texture.width, texture.height)
shader.DrawTexture(texture.native, glMatrix(projectionMatrix), quads, geo, color) shader.DrawTexture(texture.native, projectionMatrix, quads, geo, color)
} }
func (i *ids) setViewportIfNeeded(id graphics.RenderTargetID) { func (i *ids) setViewportIfNeeded(id graphics.RenderTargetID) {

View File

@ -7,9 +7,19 @@ import (
"sync" "sync"
) )
func glMatrix(matrix [4][4]float64) [16]float32 {
result := [16]float32{}
for j := 0; j < 4; j++ {
for i := 0; i < 4; i++ {
result[i+j*4] = float32(matrix[i][j])
}
}
return result
}
var once sync.Once var once sync.Once
func DrawTexture(native gl.Texture, projectionMatrix [16]float32, quads []graphics.TextureQuad, geo matrix.Geometry, color matrix.Color) { func DrawTexture(native gl.Texture, projectionMatrix [4][4]float64, quads []graphics.TextureQuad, geo matrix.Geometry, color matrix.Color) {
once.Do(func() { once.Do(func() {
initialize() initialize()
}) })
@ -18,7 +28,7 @@ func DrawTexture(native gl.Texture, projectionMatrix [16]float32, quads []graphi
return return
} }
// TODO: Check performance // TODO: Check performance
shaderProgram := use(projectionMatrix, geo, color) shaderProgram := use(glMatrix(projectionMatrix), geo, color)
native.Bind(gl.TEXTURE_2D) native.Bind(gl.TEXTURE_2D)
defer gl.Texture(0).Bind(gl.TEXTURE_2D) defer gl.Texture(0).Bind(gl.TEXTURE_2D)

View File

@ -19,7 +19,6 @@ type UI struct {
func (u *UI) Start(width, height, scale int, title string) (ui.Canvas, error) { func (u *UI) Start(width, height, scale int, title string) (ui.Canvas, error) {
if !glfw.Init() { if !glfw.Init() {
// TODO: Use glfw error
return nil, errors.New("glfw.Init() fails") return nil, errors.New("glfw.Init() fails")
} }
glfw.WindowHint(glfw.Resizable, glfw.False) glfw.WindowHint(glfw.Resizable, glfw.False)