2013-06-19 01:49:54 +02:00
|
|
|
package opengl
|
2013-06-15 10:07:14 +02:00
|
|
|
|
|
|
|
// #cgo LDFLAGS: -framework OpenGL
|
|
|
|
//
|
|
|
|
// #include <OpenGL/gl.h>
|
|
|
|
// #include <stdlib.h>
|
|
|
|
import "C"
|
2013-06-18 17:48:41 +02:00
|
|
|
import (
|
2013-06-20 16:29:51 +02:00
|
|
|
"github.com/hajimehoshi/go.ebiten/graphics"
|
2013-06-20 18:47:39 +02:00
|
|
|
"github.com/hajimehoshi/go.ebiten/graphics/matrix"
|
2013-06-18 17:48:41 +02:00
|
|
|
)
|
2013-06-15 10:07:14 +02:00
|
|
|
|
2013-06-17 16:10:55 +02:00
|
|
|
type Device struct {
|
2013-06-19 16:51:41 +02:00
|
|
|
screenWidth int
|
|
|
|
screenHeight int
|
|
|
|
screenScale int
|
|
|
|
graphicsContext *GraphicsContext
|
2013-06-19 16:08:24 +02:00
|
|
|
offscreenTexture graphics.Texture
|
2013-06-20 18:56:24 +02:00
|
|
|
drawFunc func(graphics.GraphicsContext, graphics.Texture)
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
|
|
|
|
2013-06-17 16:10:55 +02:00
|
|
|
func NewDevice(screenWidth, screenHeight, screenScale int,
|
2013-06-20 18:56:24 +02:00
|
|
|
drawFunc func(graphics.GraphicsContext, graphics.Texture)) *Device {
|
2013-06-17 16:10:55 +02:00
|
|
|
device := &Device{
|
2013-06-19 16:51:41 +02:00
|
|
|
screenWidth: screenWidth,
|
|
|
|
screenHeight: screenHeight,
|
|
|
|
screenScale: screenScale,
|
2013-06-15 10:07:14 +02:00
|
|
|
graphicsContext: newGraphicsContext(screenWidth, screenHeight, screenScale),
|
2013-06-19 16:51:41 +02:00
|
|
|
drawFunc: drawFunc,
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
2013-06-19 16:08:24 +02:00
|
|
|
device.offscreenTexture =
|
|
|
|
device.graphicsContext.NewTexture(screenWidth, screenHeight)
|
2013-06-17 16:10:55 +02:00
|
|
|
return device
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
|
|
|
|
2013-06-17 16:10:55 +02:00
|
|
|
func (device *Device) Update() {
|
|
|
|
g := device.graphicsContext
|
2013-06-15 10:07:14 +02:00
|
|
|
C.glEnable(C.GL_TEXTURE_2D)
|
|
|
|
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, C.GL_NEAREST)
|
|
|
|
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MAG_FILTER, C.GL_NEAREST)
|
2013-06-19 16:08:24 +02:00
|
|
|
g.SetOffscreen(device.offscreenTexture.ID)
|
2013-06-15 10:07:14 +02:00
|
|
|
g.Clear()
|
2013-06-20 18:56:24 +02:00
|
|
|
device.drawFunc(g, device.offscreenTexture)
|
2013-06-15 10:07:14 +02:00
|
|
|
g.flush()
|
2013-06-19 16:51:41 +02:00
|
|
|
|
2013-06-15 10:07:14 +02:00
|
|
|
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, C.GL_LINEAR)
|
|
|
|
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MAG_FILTER, C.GL_LINEAR)
|
|
|
|
g.resetOffscreen()
|
|
|
|
g.Clear()
|
2013-06-20 18:47:39 +02:00
|
|
|
|
|
|
|
scale := float64(g.screenScale)
|
|
|
|
geometryMatrix := matrix.Geometry{
|
|
|
|
[2][3]float64{
|
2013-06-21 04:16:19 +02:00
|
|
|
{scale, 0, 0},
|
|
|
|
{0, scale, 0},
|
2013-06-20 18:47:39 +02:00
|
|
|
},
|
|
|
|
}
|
2013-06-19 16:08:24 +02:00
|
|
|
g.DrawTexture(device.offscreenTexture.ID,
|
2013-06-21 17:03:45 +02:00
|
|
|
graphics.Rectangle{0, 0, device.screenWidth, device.screenHeight},
|
2013-06-20 18:47:39 +02:00
|
|
|
geometryMatrix, matrix.IdentityColor())
|
2013-06-15 10:07:14 +02:00
|
|
|
g.flush()
|
|
|
|
}
|
2013-06-19 16:08:24 +02:00
|
|
|
|
|
|
|
func (device *Device) TextureFactory() graphics.TextureFactory {
|
|
|
|
return device.graphicsContext
|
|
|
|
}
|