ebiten/ids.go

162 lines
4.1 KiB
Go
Raw Normal View History

2014-12-09 15:16:04 +01:00
/*
Copyright 2014 Hajime Hoshi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ebiten
2013-10-27 14:58:56 +01:00
import (
2014-12-06 07:47:48 +01:00
"github.com/go-gl/gl"
"github.com/hajimehoshi/ebiten/internal/opengl"
2014-12-09 16:25:54 +01:00
"github.com/hajimehoshi/ebiten/internal/opengl/internal/shader"
2013-10-27 14:58:56 +01:00
"image"
2014-05-03 19:13:43 +02:00
"math"
2013-12-10 13:21:11 +01:00
"sync"
2013-10-27 14:58:56 +01:00
)
2014-10-12 07:07:44 +02:00
type ids struct {
textures map[TextureID]*opengl.Texture
renderTargets map[RenderTargetID]*opengl.RenderTarget
renderTargetToTexture map[RenderTargetID]TextureID
2014-12-14 14:05:44 +01:00
lastID int
currentRenderTargetID RenderTargetID
2014-10-12 07:07:44 +02:00
sync.RWMutex
}
2014-05-02 17:06:20 +02:00
2014-10-12 07:07:44 +02:00
var idsInstance = &ids{
textures: map[TextureID]*opengl.Texture{},
renderTargets: map[RenderTargetID]*opengl.RenderTarget{},
renderTargetToTexture: map[RenderTargetID]TextureID{},
2014-12-14 14:05:44 +01:00
currentRenderTargetID: -1,
2014-05-02 17:06:20 +02:00
}
func (i *ids) textureAt(id TextureID) *opengl.Texture {
2014-05-02 17:06:20 +02:00
i.RLock()
defer i.RUnlock()
2013-10-27 14:58:56 +01:00
return i.textures[id]
}
func (i *ids) renderTargetAt(id RenderTargetID) *opengl.RenderTarget {
2014-05-02 17:06:20 +02:00
i.RLock()
defer i.RUnlock()
2013-10-27 14:58:56 +01:00
return i.renderTargets[id]
}
func (i *ids) toTexture(id RenderTargetID) TextureID {
2014-05-02 17:06:20 +02:00
i.RLock()
defer i.RUnlock()
2013-10-27 14:58:56 +01:00
return i.renderTargetToTexture[id]
}
func (i *ids) createTexture(img image.Image, filter int) (TextureID, error) {
texture, err := opengl.CreateTextureFromImage(img, filter)
2013-10-27 14:58:56 +01:00
if err != nil {
return 0, err
}
2013-12-10 13:21:11 +01:00
2014-05-02 17:06:20 +02:00
i.Lock()
defer i.Unlock()
2014-12-14 14:05:44 +01:00
i.lastID++
textureID := TextureID(i.lastID)
i.textures[textureID] = texture
return textureID, nil
2013-10-27 14:58:56 +01:00
}
func (i *ids) createRenderTarget(width, height int, filter int) (RenderTargetID, error) {
texture, err := opengl.CreateTexture(width, height, filter)
2013-10-27 14:58:56 +01:00
if err != nil {
return 0, err
}
2014-12-14 10:34:47 +01:00
2014-05-03 19:13:43 +02:00
// The current binded framebuffer can be changed.
2014-12-14 14:05:44 +01:00
i.currentRenderTargetID = -1
2014-12-14 13:38:54 +01:00
r, err := opengl.NewRenderTargetFromTexture(texture)
if err != nil {
return 0, err
}
2014-01-08 10:47:38 +01:00
2014-05-02 17:06:20 +02:00
i.Lock()
defer i.Unlock()
2014-12-14 14:05:44 +01:00
i.lastID++
textureID := TextureID(i.lastID)
i.lastID++
renderTargetID := RenderTargetID(i.lastID)
2014-05-11 12:44:36 +02:00
2014-12-14 14:05:44 +01:00
i.textures[textureID] = texture
i.renderTargets[renderTargetID] = r
i.renderTargetToTexture[renderTargetID] = textureID
2013-10-27 14:58:56 +01:00
2014-12-14 14:05:44 +01:00
return renderTargetID, nil
2013-10-27 14:58:56 +01:00
}
2014-01-11 00:59:38 +01:00
// NOTE: renderTarget can't be used as a texture.
func (i *ids) addRenderTarget(renderTarget *opengl.RenderTarget) RenderTargetID {
2014-05-02 17:06:20 +02:00
i.Lock()
defer i.Unlock()
2014-12-14 14:05:44 +01:00
i.lastID++
id := RenderTargetID(i.lastID)
2014-01-11 00:59:38 +01:00
i.renderTargets[id] = renderTarget
return id
}
func (i *ids) deleteRenderTarget(id RenderTargetID) {
2014-05-02 17:06:20 +02:00
i.Lock()
defer i.Unlock()
renderTarget := i.renderTargets[id]
2014-12-14 14:05:44 +01:00
textureID := i.renderTargetToTexture[id]
texture := i.textures[textureID]
renderTarget.Dispose()
texture.Dispose()
2014-01-08 10:47:38 +01:00
delete(i.renderTargets, id)
delete(i.renderTargetToTexture, id)
2014-12-14 14:05:44 +01:00
delete(i.textures, textureID)
}
2014-01-09 16:42:42 +01:00
2014-12-14 13:38:54 +01:00
func (i *ids) fillRenderTarget(id RenderTargetID, r, g, b uint8) error {
if err := i.setViewportIfNeeded(id); err != nil {
return err
}
2014-05-03 19:13:43 +02:00
const max = float64(math.MaxUint8)
2014-12-06 07:47:48 +01:00
gl.ClearColor(gl.GLclampf(float64(r)/max), gl.GLclampf(float64(g)/max), gl.GLclampf(float64(b)/max), 1)
gl.Clear(gl.COLOR_BUFFER_BIT)
2014-12-14 13:38:54 +01:00
return nil
2014-01-11 00:59:38 +01:00
}
2014-12-14 13:38:54 +01:00
func (i *ids) drawTexture(target RenderTargetID, id TextureID, parts []TexturePart, geo GeometryMatrix, color ColorMatrix) error {
2014-01-09 16:42:42 +01:00
texture := i.textureAt(id)
2014-12-14 13:38:54 +01:00
if err := i.setViewportIfNeeded(target); err != nil {
return err
}
2014-05-03 19:13:43 +02:00
r := i.renderTargetAt(target)
projectionMatrix := r.ProjectionMatrix()
quads := textureQuads(parts, texture.Width(), texture.Height())
shader.DrawTexture(texture.Native(), projectionMatrix, quads, &geo, &color)
2014-12-14 13:38:54 +01:00
return nil
2014-05-03 19:13:43 +02:00
}
2014-12-14 13:38:54 +01:00
func (i *ids) setViewportIfNeeded(id RenderTargetID) error {
2014-05-03 19:13:43 +02:00
r := i.renderTargetAt(id)
2014-12-14 14:05:44 +01:00
if i.currentRenderTargetID != id {
2014-12-14 13:38:54 +01:00
if err := r.SetAsViewport(); err != nil {
return err
}
2014-12-14 14:05:44 +01:00
i.currentRenderTargetID = id
2014-05-03 19:13:43 +02:00
}
2014-12-14 13:38:54 +01:00
return nil
2014-01-09 16:42:42 +01:00
}