mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-12 22:17:26 +01:00
Add Rects
This commit is contained in:
parent
99f17c418b
commit
2b04f74614
60
example/game/rects/rects.go
Normal file
60
example/game/rects/rects.go
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
package rects
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/hajimehoshi/go.ebiten/graphics"
|
||||||
|
"github.com/hajimehoshi/go.ebiten/graphics/matrix"
|
||||||
|
"image/color"
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Rects struct {
|
||||||
|
rectsTexture graphics.Texture
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() *Rects {
|
||||||
|
return &Rects{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (game *Rects) ScreenWidth() int {
|
||||||
|
return 256
|
||||||
|
}
|
||||||
|
|
||||||
|
func (game *Rects) ScreenHeight() int {
|
||||||
|
return 240
|
||||||
|
}
|
||||||
|
|
||||||
|
func (game *Rects) Init(tf graphics.TextureFactory) {
|
||||||
|
game.rectsTexture = tf.NewTexture(game.ScreenWidth(), game.ScreenHeight())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (game *Rects) Update() {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (game *Rects) Draw(g graphics.GraphicsContext, offscreen graphics.Texture) {
|
||||||
|
g.SetOffscreen(game.rectsTexture.ID)
|
||||||
|
|
||||||
|
x := rand.Intn(game.ScreenWidth())
|
||||||
|
y := rand.Intn(game.ScreenHeight())
|
||||||
|
width := rand.Intn(game.ScreenWidth() - x)
|
||||||
|
height := rand.Intn(game.ScreenHeight() - y)
|
||||||
|
|
||||||
|
red := uint8(rand.Intn(256))
|
||||||
|
green := uint8(rand.Intn(256))
|
||||||
|
blue := uint8(rand.Intn(256))
|
||||||
|
alpha := uint8(rand.Intn(256))
|
||||||
|
|
||||||
|
g.DrawRect(
|
||||||
|
graphics.Rect{x, y, width, height},
|
||||||
|
&color.RGBA{red, green, blue, alpha},
|
||||||
|
)
|
||||||
|
|
||||||
|
g.SetOffscreen(offscreen.ID)
|
||||||
|
g.DrawTexture(game.rectsTexture,
|
||||||
|
matrix.IdentityGeometry(),
|
||||||
|
matrix.IdentityColor())
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
}
|
@ -6,27 +6,28 @@ import (
|
|||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
|
"math"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RotatingImage struct {
|
type Rotating struct {
|
||||||
ebitenTexture graphics.Texture
|
ebitenTexture graphics.Texture
|
||||||
x int
|
x int
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() *RotatingImage {
|
func New() *Rotating {
|
||||||
return &RotatingImage{}
|
return &Rotating{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (game *RotatingImage) ScreenWidth() int {
|
func (game *Rotating) ScreenWidth() int {
|
||||||
return 256
|
return 256
|
||||||
}
|
}
|
||||||
|
|
||||||
func (game *RotatingImage) ScreenHeight() int {
|
func (game *Rotating) ScreenHeight() int {
|
||||||
return 240
|
return 240
|
||||||
}
|
}
|
||||||
|
|
||||||
func (game *RotatingImage) Init(tf graphics.TextureFactory) {
|
func (game *Rotating) Init(tf graphics.TextureFactory) {
|
||||||
file, err := os.Open("ebiten.png")
|
file, err := os.Open("ebiten.png")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -40,17 +41,17 @@ func (game *RotatingImage) Init(tf graphics.TextureFactory) {
|
|||||||
game.ebitenTexture = tf.NewTextureFromImage(img)
|
game.ebitenTexture = tf.NewTextureFromImage(img)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (game *RotatingImage) Update() {
|
func (game *Rotating) Update() {
|
||||||
game.x++
|
game.x++
|
||||||
}
|
}
|
||||||
|
|
||||||
func (game *RotatingImage) Draw(g graphics.GraphicsContext, offscreen graphics.Texture) {
|
func (game *Rotating) Draw(g graphics.GraphicsContext, offscreen graphics.Texture) {
|
||||||
g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255})
|
g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255})
|
||||||
|
|
||||||
geometryMatrix := matrix.IdentityGeometry()
|
geometryMatrix := matrix.IdentityGeometry()
|
||||||
tx, ty := float64(game.ebitenTexture.Width), float64(game.ebitenTexture.Height)
|
tx, ty := float64(game.ebitenTexture.Width), float64(game.ebitenTexture.Height)
|
||||||
geometryMatrix.Translate(-tx/2, -ty/2)
|
geometryMatrix.Translate(-tx/2, -ty/2)
|
||||||
geometryMatrix.Rotate(float64(game.x) / 60)
|
geometryMatrix.Rotate(float64(game.x) * 2 * math.Pi / (60 * 10))
|
||||||
geometryMatrix.Translate(tx/2, ty/2)
|
geometryMatrix.Translate(tx/2, ty/2)
|
||||||
centerX := float64(game.ScreenWidth()) / 2
|
centerX := float64(game.ScreenWidth()) / 2
|
||||||
centerY := float64(game.ScreenHeight()) / 2
|
centerY := float64(game.ScreenHeight()) / 2
|
||||||
|
@ -123,10 +123,6 @@ 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, 10, 50, 50},
|
|
||||||
&color.RGBA{255, 128, 128, 128},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -16,6 +16,7 @@ package main
|
|||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
"github.com/hajimehoshi/go.ebiten"
|
"github.com/hajimehoshi/go.ebiten"
|
||||||
|
"github.com/hajimehoshi/go.ebiten/example/game/rects"
|
||||||
"github.com/hajimehoshi/go.ebiten/example/game/rotating"
|
"github.com/hajimehoshi/go.ebiten/example/game/rotating"
|
||||||
"github.com/hajimehoshi/go.ebiten/example/game/sprites"
|
"github.com/hajimehoshi/go.ebiten/example/game/sprites"
|
||||||
"github.com/hajimehoshi/go.ebiten/graphics"
|
"github.com/hajimehoshi/go.ebiten/graphics"
|
||||||
@ -81,10 +82,12 @@ func main() {
|
|||||||
|
|
||||||
var gm ebiten.Game
|
var gm ebiten.Game
|
||||||
switch gameName {
|
switch gameName {
|
||||||
case "sprites":
|
case "rects":
|
||||||
gm = sprites.New()
|
gm = rects.New()
|
||||||
case "rotating":
|
case "rotating":
|
||||||
gm = rotating.New()
|
gm = rotating.New()
|
||||||
|
case "sprites":
|
||||||
|
gm = sprites.New()
|
||||||
default:
|
default:
|
||||||
gm = rotating.New()
|
gm = rotating.New()
|
||||||
}
|
}
|
||||||
|
@ -51,6 +51,8 @@ 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_TEXTURE_COORD_ARRAY)
|
||||||
C.glDisableClientState(C.GL_VERTEX_ARRAY)
|
C.glDisableClientState(C.GL_VERTEX_ARRAY)
|
||||||
C.glDisableClientState(C.GL_COLOR_ARRAY)
|
C.glDisableClientState(C.GL_COLOR_ARRAY)
|
||||||
@ -194,6 +196,11 @@ func (context *GraphicsContext) SetOffscreen(textureID graphics.TextureID) {
|
|||||||
}
|
}
|
||||||
context.setOffscreenFramebuffer(framebuffer,
|
context.setOffscreenFramebuffer(framebuffer,
|
||||||
texture.textureWidth, texture.textureHeight)
|
texture.textureWidth, texture.textureHeight)
|
||||||
|
|
||||||
|
if texture.isDirty {
|
||||||
|
context.Clear()
|
||||||
|
texture.isDirty = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *GraphicsContext) setOffscreenFramebuffer(framebuffer C.GLuint,
|
func (context *GraphicsContext) setOffscreenFramebuffer(framebuffer C.GLuint,
|
||||||
|
@ -26,6 +26,7 @@ type Texture struct {
|
|||||||
height int
|
height int
|
||||||
textureWidth int
|
textureWidth int
|
||||||
textureHeight int
|
textureHeight int
|
||||||
|
isDirty bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTexture(width, height int, pixels []uint8) *Texture {
|
func createTexture(width, height int, pixels []uint8) *Texture {
|
||||||
@ -67,8 +68,8 @@ func createTexture(width, height int, pixels []uint8) *Texture {
|
|||||||
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, C.GL_LINEAR)
|
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, C.GL_LINEAR)
|
||||||
C.glBindTexture(C.GL_TEXTURE_2D, 0)
|
C.glBindTexture(C.GL_TEXTURE_2D, 0)
|
||||||
|
|
||||||
// TODO: lock?
|
|
||||||
texture.id = textureID
|
texture.id = textureID
|
||||||
|
texture.isDirty = pixels == nil
|
||||||
|
|
||||||
return texture
|
return texture
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user