2013-07-01 15:25:41 +02:00
|
|
|
// Copyright 2013 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
// SOFTWARE.
|
|
|
|
|
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-07-08 17:49:21 +02:00
|
|
|
"runtime"
|
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-10-06 11:22:45 +02:00
|
|
|
screenScale int
|
|
|
|
context *Context
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
|
|
|
|
2013-10-05 18:47:19 +02:00
|
|
|
func NewDevice(screenWidth, screenHeight, screenScale int) *Device {
|
|
|
|
graphicsContext := newContext(screenWidth, screenHeight, screenScale)
|
2013-06-17 16:10:55 +02:00
|
|
|
device := &Device{
|
2013-10-06 11:22:45 +02:00
|
|
|
screenScale: screenScale,
|
|
|
|
context: graphicsContext,
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
2013-06-17 16:10:55 +02:00
|
|
|
return device
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
|
|
|
|
2013-10-05 18:47:19 +02:00
|
|
|
func (device *Device) Init() {
|
|
|
|
device.context.Init()
|
|
|
|
}
|
|
|
|
|
2013-10-06 11:22:45 +02:00
|
|
|
func (device *Device) Update(draw func(graphics.Context)) {
|
2013-07-04 17:49:17 +02:00
|
|
|
context := device.context
|
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-07-12 18:36:01 +02:00
|
|
|
context.SetOffscreen(context.Screen().ID())
|
2013-07-04 17:49:17 +02:00
|
|
|
context.Clear()
|
2013-06-25 17:56:01 +02:00
|
|
|
|
2013-10-06 11:22:45 +02:00
|
|
|
draw(context)
|
2013-06-25 17:56:01 +02:00
|
|
|
|
2013-07-04 17:49:17 +02:00
|
|
|
context.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)
|
2013-07-04 17:49:17 +02:00
|
|
|
context.resetOffscreen()
|
|
|
|
context.Clear()
|
2013-06-20 18:47:39 +02:00
|
|
|
|
2013-07-04 17:49:17 +02:00
|
|
|
scale := float64(context.screenScale)
|
2013-06-20 18:47:39 +02:00
|
|
|
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-10-11 18:29:19 +02:00
|
|
|
context.DrawTexture(context.Screen().Texture().ID(),
|
2013-06-20 18:47:39 +02:00
|
|
|
geometryMatrix, matrix.IdentityColor())
|
2013-07-04 17:49:17 +02:00
|
|
|
context.flush()
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
2013-06-19 16:08:24 +02:00
|
|
|
|
|
|
|
func (device *Device) TextureFactory() graphics.TextureFactory {
|
2013-06-27 17:33:03 +02:00
|
|
|
return device.context
|
2013-06-19 16:08:24 +02:00
|
|
|
}
|
2013-07-08 17:49:21 +02:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
}
|