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.
|
|
|
|
*/
|
|
|
|
|
2014-12-14 07:26:10 +01:00
|
|
|
package ebiten
|
2013-12-09 14:40:54 +01:00
|
|
|
|
2014-05-03 20:29:45 +02:00
|
|
|
import (
|
2014-12-06 07:47:48 +01:00
|
|
|
"github.com/go-gl/gl"
|
2014-12-14 07:26:10 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/opengl"
|
2014-05-03 20:29:45 +02:00
|
|
|
)
|
2014-05-03 17:40:53 +02:00
|
|
|
|
2014-12-14 10:34:47 +01:00
|
|
|
func newGraphicsContext(screenWidth, screenHeight, screenScale int) (*graphicsContext, error) {
|
2014-12-07 20:22:50 +01:00
|
|
|
gl.Init()
|
|
|
|
gl.Enable(gl.TEXTURE_2D)
|
|
|
|
gl.Enable(gl.BLEND)
|
2014-12-06 07:47:48 +01:00
|
|
|
|
2014-12-07 16:07:36 +01:00
|
|
|
// The defualt framebuffer should be 0.
|
2014-12-14 10:34:47 +01:00
|
|
|
r := opengl.NewRenderTarget(screenWidth*screenScale, screenHeight*screenScale, true)
|
2013-12-09 14:40:54 +01:00
|
|
|
|
2014-12-14 13:21:05 +01:00
|
|
|
screenID, err := idsInstance.createRenderTarget(screenWidth, screenHeight, gl.NEAREST)
|
2013-12-09 14:40:54 +01:00
|
|
|
if err != nil {
|
2014-12-07 20:22:50 +01:00
|
|
|
return nil, err
|
2013-12-09 14:40:54 +01:00
|
|
|
}
|
2014-12-10 19:50:35 +01:00
|
|
|
|
2014-12-14 13:21:05 +01:00
|
|
|
c := &graphicsContext{
|
|
|
|
currentIDs: make([]RenderTargetID, 1),
|
|
|
|
defaultID: idsInstance.addRenderTarget(r),
|
|
|
|
screenID: screenID,
|
|
|
|
screenWidth: screenWidth,
|
|
|
|
screenHeight: screenHeight,
|
|
|
|
screenScale: screenScale,
|
|
|
|
}
|
|
|
|
|
|
|
|
idsInstance.fillRenderTarget(c.screenID, 0, 0, 0)
|
2013-12-09 15:52:14 +01:00
|
|
|
|
2014-12-08 17:08:47 +01:00
|
|
|
return c, nil
|
2014-12-07 20:22:50 +01:00
|
|
|
}
|
|
|
|
|
2014-12-14 07:26:10 +01:00
|
|
|
type graphicsContext struct {
|
|
|
|
screenID RenderTargetID
|
|
|
|
defaultID RenderTargetID
|
|
|
|
currentIDs []RenderTargetID
|
2014-12-07 20:22:50 +01:00
|
|
|
screenWidth int
|
|
|
|
screenHeight int
|
|
|
|
screenScale int
|
2014-12-07 16:07:36 +01:00
|
|
|
}
|
2014-01-11 03:18:55 +01:00
|
|
|
|
2014-12-14 07:26:10 +01:00
|
|
|
var _ GraphicsContext = new(graphicsContext)
|
2014-12-09 14:09:22 +01:00
|
|
|
|
2014-12-14 07:26:10 +01:00
|
|
|
func (c *graphicsContext) dispose() {
|
2014-12-07 16:07:36 +01:00
|
|
|
// NOTE: Now this method is not used anywhere.
|
2014-12-10 19:50:35 +01:00
|
|
|
idsInstance.deleteRenderTarget(c.screenID)
|
2013-12-09 15:52:14 +01:00
|
|
|
}
|
|
|
|
|
2014-12-14 07:26:10 +01:00
|
|
|
func (c *graphicsContext) Clear() {
|
2014-05-01 15:45:48 +02:00
|
|
|
c.Fill(0, 0, 0)
|
2013-12-09 14:40:54 +01:00
|
|
|
}
|
|
|
|
|
2014-12-14 07:26:10 +01:00
|
|
|
func (c *graphicsContext) Fill(r, g, b uint8) {
|
2014-12-10 19:50:35 +01:00
|
|
|
idsInstance.fillRenderTarget(c.currentIDs[len(c.currentIDs)-1], r, g, b)
|
2013-12-09 14:40:54 +01:00
|
|
|
}
|
|
|
|
|
2014-12-14 07:26:10 +01:00
|
|
|
func (c *graphicsContext) Texture(id TextureID) Drawer {
|
2014-05-03 08:25:41 +02:00
|
|
|
return &textureWithContext{id, c}
|
2013-12-09 14:40:54 +01:00
|
|
|
}
|
|
|
|
|
2014-12-14 07:26:10 +01:00
|
|
|
func (c *graphicsContext) RenderTarget(id RenderTargetID) Drawer {
|
2014-10-12 07:07:44 +02:00
|
|
|
return &textureWithContext{idsInstance.toTexture(id), c}
|
2013-12-09 14:40:54 +01:00
|
|
|
}
|
|
|
|
|
2014-12-14 07:26:10 +01:00
|
|
|
func (c *graphicsContext) PushRenderTarget(renderTargetID RenderTargetID) {
|
2014-12-10 19:50:35 +01:00
|
|
|
c.currentIDs = append(c.currentIDs, renderTargetID)
|
2013-12-09 14:40:54 +01:00
|
|
|
}
|
|
|
|
|
2014-12-14 07:26:10 +01:00
|
|
|
func (c *graphicsContext) PopRenderTarget() {
|
2014-12-10 19:50:35 +01:00
|
|
|
c.currentIDs = c.currentIDs[:len(c.currentIDs)-1]
|
2013-12-09 14:40:54 +01:00
|
|
|
}
|
2014-05-01 15:45:48 +02:00
|
|
|
|
2014-12-14 10:57:29 +01:00
|
|
|
func (c *graphicsContext) preUpdate() {
|
2014-12-14 13:21:05 +01:00
|
|
|
c.currentIDs = c.currentIDs[0:1]
|
|
|
|
c.currentIDs[0] = c.defaultID
|
2014-12-10 20:02:46 +01:00
|
|
|
c.PushRenderTarget(c.screenID)
|
2014-12-07 16:07:36 +01:00
|
|
|
c.Clear()
|
2014-12-08 17:08:47 +01:00
|
|
|
}
|
2014-12-07 16:07:36 +01:00
|
|
|
|
2014-12-14 10:57:29 +01:00
|
|
|
func (c *graphicsContext) postUpdate() {
|
2014-12-10 20:02:46 +01:00
|
|
|
c.PopRenderTarget()
|
2014-12-07 16:07:36 +01:00
|
|
|
c.Clear()
|
|
|
|
|
|
|
|
scale := float64(c.screenScale)
|
2014-12-14 07:26:10 +01:00
|
|
|
geo := GeometryMatrixI()
|
2014-12-07 16:07:36 +01:00
|
|
|
geo.Scale(scale, scale)
|
2014-12-14 07:26:10 +01:00
|
|
|
DrawWhole(c.RenderTarget(c.screenID), c.screenWidth, c.screenHeight, geo, ColorMatrixI())
|
2014-12-07 16:07:36 +01:00
|
|
|
|
|
|
|
gl.Flush()
|
|
|
|
}
|
|
|
|
|
2014-05-03 08:25:41 +02:00
|
|
|
type textureWithContext struct {
|
2014-12-14 07:26:10 +01:00
|
|
|
id TextureID
|
|
|
|
context *graphicsContext
|
2014-05-01 15:45:48 +02:00
|
|
|
}
|
|
|
|
|
2014-12-14 07:26:10 +01:00
|
|
|
func (t *textureWithContext) Draw(parts []TexturePart, geo GeometryMatrix, color ColorMatrix) {
|
2014-12-10 19:50:35 +01:00
|
|
|
currentID := t.context.currentIDs[len(t.context.currentIDs)-1]
|
|
|
|
idsInstance.drawTexture(currentID, t.id, parts, geo, color)
|
2014-05-01 15:45:48 +02:00
|
|
|
}
|