ebiten/graphicscontext.go

84 lines
2.0 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-12-09 14:40:54 +01:00
2014-05-03 20:29:45 +02:00
import (
"github.com/hajimehoshi/ebiten/internal/opengl"
2014-12-23 17:15:16 +01:00
"image"
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) {
f, err := opengl.NewZeroFramebuffer(screenWidth*screenScale, screenHeight*screenScale)
if err != nil {
return nil, err
}
2013-12-09 14:40:54 +01:00
2014-12-22 20:32:36 +01:00
texture, err := opengl.NewTexture(screenWidth, screenHeight, opengl.Filter(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-12-22 20:32:36 +01:00
screen, err := newInnerImage(texture)
if err != nil {
return nil, err
}
2014-12-14 13:21:05 +01:00
c := &graphicsContext{
defaultR: &innerImage{f, nil},
screen: screen,
screenScale: screenScale,
2014-12-14 13:21:05 +01:00
}
return c, nil
2014-12-07 20:22:50 +01:00
}
type graphicsContext struct {
screen *innerImage
defaultR *innerImage
screenScale int
2014-12-07 16:07:36 +01:00
}
2014-01-11 03:18:55 +01:00
func (c *graphicsContext) dispose() {
2014-12-07 16:07:36 +01:00
// NOTE: Now this method is not used anywhere.
framebuffer := c.screen.framebuffer
texture := c.screen.texture
framebuffer.Dispose()
texture.Dispose()
2013-12-09 15:52:14 +01:00
}
func (c *graphicsContext) preUpdate() error {
return c.screen.Clear()
2013-12-09 14:40:54 +01:00
}
func (c *graphicsContext) postUpdate() error {
if err := c.defaultR.Clear(); err != nil {
return err
}
2014-12-07 16:07:36 +01:00
scale := float64(c.screenScale)
geo := ScaleGeometry(scale, scale)
clr := ColorMatrixI()
w, h := c.screen.size()
2014-12-23 18:04:56 +01:00
dsts := []image.Rectangle{image.Rect(0, 0, w, h)}
srcs := []image.Rectangle{image.Rect(0, 0, w, h)}
if err := c.defaultR.drawImage(dsts, c.screen, srcs, geo, clr); err != nil {
return err
}
2014-12-07 16:07:36 +01:00
2014-12-22 20:32:36 +01:00
opengl.Flush()
return nil
2014-12-07 16:07:36 +01:00
}