mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Implement DrawRect
This commit is contained in:
parent
4fc359e08f
commit
215df5d190
@ -123,6 +123,10 @@ func (game *Sprites) Draw(g graphics.GraphicsContext, offscreen graphics.Texture
|
|||||||
geometryMatrix := matrix.IdentityGeometry()
|
geometryMatrix := matrix.IdentityGeometry()
|
||||||
g.DrawTextureParts(texture.ID, locations,
|
g.DrawTextureParts(texture.ID, locations,
|
||||||
geometryMatrix, matrix.IdentityColor())
|
geometryMatrix, matrix.IdentityColor())
|
||||||
|
g.DrawRect(
|
||||||
|
graphics.Rect{10, 5, 200, 200},
|
||||||
|
&color.RGBA{255, 128, 128, 128},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -27,6 +27,7 @@ type TextureLocation struct {
|
|||||||
type GraphicsContext interface {
|
type GraphicsContext interface {
|
||||||
Clear()
|
Clear()
|
||||||
Fill(color color.Color)
|
Fill(color color.Color)
|
||||||
|
DrawRect(rect Rect, clr color.Color)
|
||||||
DrawTexture(texture Texture,
|
DrawTexture(texture Texture,
|
||||||
geometryMatrix matrix.Geometry,
|
geometryMatrix matrix.Geometry,
|
||||||
colorMatrix matrix.Color)
|
colorMatrix matrix.Color)
|
||||||
|
@ -11,18 +11,21 @@ import (
|
|||||||
"github.com/hajimehoshi/go.ebiten/graphics/matrix"
|
"github.com/hajimehoshi/go.ebiten/graphics/matrix"
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
|
"math"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GraphicsContext struct {
|
type GraphicsContext struct {
|
||||||
screenWidth int
|
screenWidth int
|
||||||
screenHeight int
|
screenHeight int
|
||||||
screenScale int
|
screenScale int
|
||||||
textures map[graphics.TextureID]*Texture
|
textures map[graphics.TextureID]*Texture
|
||||||
projectionMatrix [16]float32
|
currentOffscreenWidth int
|
||||||
currentShaderProgram C.GLuint
|
currentOffscreenHeight int
|
||||||
mainFramebuffer C.GLuint
|
projectionMatrix [16]float32
|
||||||
framebuffers map[C.GLuint]C.GLuint
|
currentShaderProgram C.GLuint
|
||||||
|
mainFramebuffer C.GLuint
|
||||||
|
framebuffers map[C.GLuint]C.GLuint
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method should be called on the UI thread.
|
// This method should be called on the UI thread.
|
||||||
@ -48,11 +51,14 @@ func newGraphicsContext(screenWidth, screenHeight, screenScale int) *GraphicsCon
|
|||||||
func (context *GraphicsContext) Clear() {
|
func (context *GraphicsContext) Clear() {
|
||||||
C.glClearColor(0, 0, 0, 1)
|
C.glClearColor(0, 0, 0, 1)
|
||||||
C.glClear(C.GL_COLOR_BUFFER_BIT)
|
C.glClear(C.GL_COLOR_BUFFER_BIT)
|
||||||
|
C.glDisableClientState(C.GL_TEXTURE_COORD_ARRAY)
|
||||||
|
C.glDisableClientState(C.GL_VERTEX_ARRAY)
|
||||||
|
C.glDisableClientState(C.GL_COLOR_ARRAY)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *GraphicsContext) Fill(clr color.Color) {
|
func (context *GraphicsContext) Fill(clr color.Color) {
|
||||||
r, g, b, a := clr.RGBA()
|
r, g, b, a := clr.RGBA()
|
||||||
max := 65535.0
|
max := float64(math.MaxUint16)
|
||||||
C.glClearColor(
|
C.glClearColor(
|
||||||
C.GLclampf(float64(r)/max),
|
C.GLclampf(float64(r)/max),
|
||||||
C.GLclampf(float64(g)/max),
|
C.GLclampf(float64(g)/max),
|
||||||
@ -61,8 +67,50 @@ func (context *GraphicsContext) Fill(clr color.Color) {
|
|||||||
C.glClear(C.GL_COLOR_BUFFER_BIT)
|
C.glClear(C.GL_COLOR_BUFFER_BIT)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *GraphicsContext) DrawRect(x, y, width, height int, clr color.Color) {
|
func (context *GraphicsContext) DrawRect(rect graphics.Rect, clr color.Color) {
|
||||||
// TODO: implement!
|
width := float32(context.currentOffscreenWidth)
|
||||||
|
height := float32(context.currentOffscreenHeight)
|
||||||
|
|
||||||
|
// Normalize the coord between -1.0 and 1.0.
|
||||||
|
x1 := float32(rect.X)/width*2.0 - 1.0
|
||||||
|
x2 := float32(rect.X+rect.Width)/width*2.0 - 1.0
|
||||||
|
y1 := float32(rect.Y)/height*2.0 - 1.0
|
||||||
|
y2 := float32(rect.Y+rect.Height)/height*2.0 - 1.0
|
||||||
|
vertex := [...]float32{
|
||||||
|
x1, y1,
|
||||||
|
x2, y1,
|
||||||
|
x1, y2,
|
||||||
|
x2, y2,
|
||||||
|
}
|
||||||
|
|
||||||
|
origR, origG, origB, origA := clr.RGBA()
|
||||||
|
max := float32(math.MaxUint16)
|
||||||
|
r := float32(origR) / max
|
||||||
|
g := float32(origG) / max
|
||||||
|
b := float32(origB) / max
|
||||||
|
a := float32(origA) / max
|
||||||
|
color := [...]float32{
|
||||||
|
r, g, b, a,
|
||||||
|
r, g, b, a,
|
||||||
|
r, g, b, a,
|
||||||
|
r, g, b, a,
|
||||||
|
}
|
||||||
|
|
||||||
|
C.glUseProgram(0)
|
||||||
|
context.currentShaderProgram = 0
|
||||||
|
C.glDisable(C.GL_TEXTURE_2D)
|
||||||
|
C.glEnableClientState(C.GL_VERTEX_ARRAY)
|
||||||
|
C.glEnableClientState(C.GL_COLOR_ARRAY)
|
||||||
|
C.glVertexPointer(2, C.GL_FLOAT, C.GL_FALSE, unsafe.Pointer(&vertex[0]))
|
||||||
|
C.glColorPointer(4, C.GL_FLOAT, C.GL_FALSE, unsafe.Pointer(&color[0]))
|
||||||
|
C.glDrawArrays(C.GL_TRIANGLE_STRIP, 0, 4)
|
||||||
|
C.glDisableClientState(C.GL_COLOR_ARRAY)
|
||||||
|
C.glDisableClientState(C.GL_VERTEX_ARRAY)
|
||||||
|
C.glEnable(C.GL_TEXTURE_2D)
|
||||||
|
|
||||||
|
if glError := C.glGetError(); glError != C.GL_NO_ERROR {
|
||||||
|
panic("OpenGL error")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *GraphicsContext) DrawTexture(
|
func (context *GraphicsContext) DrawTexture(
|
||||||
@ -90,6 +138,7 @@ func (context *GraphicsContext) DrawTextureParts(
|
|||||||
C.glEnableClientState(C.GL_TEXTURE_COORD_ARRAY)
|
C.glEnableClientState(C.GL_TEXTURE_COORD_ARRAY)
|
||||||
C.glEnableVertexAttribArray(C.GLuint(vertexAttrLocation))
|
C.glEnableVertexAttribArray(C.GLuint(vertexAttrLocation))
|
||||||
C.glEnableVertexAttribArray(C.GLuint(textureAttrLocation))
|
C.glEnableVertexAttribArray(C.GLuint(textureAttrLocation))
|
||||||
|
// TODO: Refactoring
|
||||||
for _, location := range locations {
|
for _, location := range locations {
|
||||||
x1 := float32(location.LocationX)
|
x1 := float32(location.LocationX)
|
||||||
x2 := float32(location.LocationX + location.Source.Width)
|
x2 := float32(location.LocationX + location.Source.Width)
|
||||||
@ -134,6 +183,9 @@ func abs(x int) int {
|
|||||||
|
|
||||||
func (context *GraphicsContext) SetOffscreen(textureID graphics.TextureID) {
|
func (context *GraphicsContext) SetOffscreen(textureID graphics.TextureID) {
|
||||||
texture := context.textures[textureID]
|
texture := context.textures[textureID]
|
||||||
|
context.currentOffscreenWidth = texture.width
|
||||||
|
context.currentOffscreenHeight = texture.height
|
||||||
|
|
||||||
framebuffer := context.getFramebuffer(texture.id)
|
framebuffer := context.getFramebuffer(texture.id)
|
||||||
if framebuffer == context.mainFramebuffer {
|
if framebuffer == context.mainFramebuffer {
|
||||||
panic("invalid framebuffer")
|
panic("invalid framebuffer")
|
||||||
@ -180,6 +232,8 @@ func (context *GraphicsContext) setOffscreenFramebuffer(framebuffer C.GLuint,
|
|||||||
|
|
||||||
func (context *GraphicsContext) resetOffscreen() {
|
func (context *GraphicsContext) resetOffscreen() {
|
||||||
context.setOffscreenFramebuffer(context.mainFramebuffer, 0, 0)
|
context.setOffscreenFramebuffer(context.mainFramebuffer, 0, 0)
|
||||||
|
context.currentOffscreenWidth = context.screenWidth
|
||||||
|
context.currentOffscreenHeight = context.screenHeight
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method should be called on the UI thread.
|
// This method should be called on the UI thread.
|
||||||
|
Loading…
Reference in New Issue
Block a user