ebiten/opengl/context.go

118 lines
2.9 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.
*/
2013-12-09 14:40:54 +01:00
package opengl
2014-05-03 20:29:45 +02:00
import (
2014-12-06 07:47:48 +01:00
"github.com/go-gl/gl"
2014-12-09 14:09:22 +01:00
"github.com/hajimehoshi/ebiten"
2014-05-03 20:29:45 +02:00
)
2014-05-03 17:40:53 +02:00
2014-12-09 14:09:22 +01:00
func Initialize(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-09 14:09:22 +01:00
c := &GraphicsContext{
2014-05-03 06:58:18 +02:00
screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale,
2013-12-09 14:40:54 +01:00
}
2014-12-06 07:47:48 +01:00
2014-12-07 16:07:36 +01:00
// The defualt framebuffer should be 0.
2014-12-07 20:22:50 +01:00
c.defaultId = idsInstance.addRenderTarget(&renderTarget{
2014-10-12 07:07:44 +02:00
width: screenWidth * screenScale,
height: screenHeight * screenScale,
flipY: true,
2014-12-07 20:22:50 +01:00
})
2013-12-09 14:40:54 +01:00
var err error
2014-12-09 14:09:22 +01:00
c.screenId, err = idsInstance.createRenderTarget(screenWidth, screenHeight, ebiten.FilterNearest)
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-05-03 06:58:18 +02:00
c.ResetOffscreen()
c.Clear()
2013-12-09 15:52:14 +01:00
return c, nil
2014-12-07 20:22:50 +01:00
}
2014-12-09 14:09:22 +01:00
type GraphicsContext struct {
screenId ebiten.RenderTargetID
defaultId ebiten.RenderTargetID
currentId ebiten.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-09 14:09:22 +01:00
var _ ebiten.GraphicsContext = new(GraphicsContext)
func (c *GraphicsContext) dispose() {
2014-12-07 16:07:36 +01:00
// NOTE: Now this method is not used anywhere.
idsInstance.deleteRenderTarget(c.screenId)
2013-12-09 15:52:14 +01:00
}
2014-12-09 14:09:22 +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-09 14:09:22 +01:00
func (c *GraphicsContext) Fill(r, g, b uint8) {
2014-10-12 07:07:44 +02:00
idsInstance.fillRenderTarget(c.currentId, r, g, b)
2013-12-09 14:40:54 +01:00
}
2014-12-09 14:09:22 +01:00
func (c *GraphicsContext) Texture(id ebiten.TextureID) ebiten.Drawer {
2014-05-03 08:25:41 +02:00
return &textureWithContext{id, c}
2013-12-09 14:40:54 +01:00
}
2014-12-09 14:09:22 +01:00
func (c *GraphicsContext) RenderTarget(id ebiten.RenderTargetID) ebiten.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-09 14:09:22 +01:00
func (c *GraphicsContext) ResetOffscreen() {
2014-05-03 06:58:18 +02:00
c.currentId = c.screenId
2013-12-09 14:40:54 +01:00
}
2014-12-09 14:09:22 +01:00
func (c *GraphicsContext) SetOffscreen(renderTargetId ebiten.RenderTargetID) {
2014-05-03 06:58:18 +02:00
c.currentId = renderTargetId
2013-12-09 14:40:54 +01:00
}
2014-05-01 15:45:48 +02:00
2014-12-09 14:09:22 +01:00
func (c *GraphicsContext) PreUpdate() {
2014-12-07 16:07:36 +01:00
c.ResetOffscreen()
c.Clear()
}
2014-12-07 16:07:36 +01:00
2014-12-09 14:09:22 +01:00
func (c *GraphicsContext) PostUpdate() {
2014-12-07 16:07:36 +01:00
c.SetOffscreen(c.defaultId)
c.Clear()
scale := float64(c.screenScale)
2014-12-09 14:09:22 +01:00
geo := ebiten.GeometryMatrixI()
2014-12-07 16:07:36 +01:00
geo.Scale(scale, scale)
2014-12-09 14:09:22 +01:00
ebiten.DrawWhole(c.RenderTarget(c.screenId), c.screenWidth, c.screenHeight, geo, ebiten.ColorMatrixI())
2014-12-07 16:07:36 +01:00
gl.Flush()
}
2014-05-03 08:25:41 +02:00
type textureWithContext struct {
2014-12-09 14:09:22 +01:00
id ebiten.TextureID
context *GraphicsContext
2014-05-01 15:45:48 +02:00
}
2014-12-09 14:09:22 +01:00
func (t *textureWithContext) Draw(parts []ebiten.TexturePart, geo ebiten.GeometryMatrix, color ebiten.ColorMatrix) {
2014-10-12 07:07:44 +02:00
idsInstance.drawTexture(t.context.currentId, t.id, parts, geo, color)
2014-05-01 15:45:48 +02:00
}