mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
Add image.ReplacePixels; Add example/noise (#89)
This commit is contained in:
parent
d2d32d3956
commit
d994f34d53
69
example/noise/main.go
Normal file
69
example/noise/main.go
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
// 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 main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/hajimehoshi/ebiten"
|
||||||
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
|
"image"
|
||||||
|
"log"
|
||||||
|
//"math/rand"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
screenWidth = 320
|
||||||
|
screenHeight = 240
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
noiseImage *image.RGBA
|
||||||
|
)
|
||||||
|
|
||||||
|
type rand struct {
|
||||||
|
x, y, z, w uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *rand) next() uint32 {
|
||||||
|
// math/rand is too slow to keep 60 FPS on web browsers.
|
||||||
|
// Use Xorshift instead: http://en.wikipedia.org/wiki/Xorshift
|
||||||
|
t := r.x ^ (r.x << 11)
|
||||||
|
r.x, r.y, r.z = r.y, r.z, r.w
|
||||||
|
r.w = (r.w ^ (r.w >> 19)) ^ (t ^ (t >> 8))
|
||||||
|
return r.w
|
||||||
|
}
|
||||||
|
|
||||||
|
var randInstance = &rand{12345678, 4185243, 776511, 45411}
|
||||||
|
|
||||||
|
func update(screen *ebiten.Image) error {
|
||||||
|
const l = screenWidth * screenHeight
|
||||||
|
for i := 0; i < l; i++ {
|
||||||
|
x := randInstance.next()
|
||||||
|
noiseImage.Pix[4*i] = uint8(x >> 24)
|
||||||
|
noiseImage.Pix[4*i+1] = uint8(x >> 16)
|
||||||
|
noiseImage.Pix[4*i+2] = uint8(x >> 8)
|
||||||
|
noiseImage.Pix[4*i+3] = 0xff
|
||||||
|
}
|
||||||
|
screen.ReplacePixels(noiseImage.Pix)
|
||||||
|
ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %f", ebiten.CurrentFPS()))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
noiseImage = image.NewRGBA(image.Rect(0, 0, screenWidth, screenHeight))
|
||||||
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Noise (Ebiten Demo)"); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
@ -69,6 +69,11 @@ func IsGamepadButtonPressed(id int, button GamepadButton) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewImage returns an empty image.
|
// NewImage returns an empty image.
|
||||||
|
//
|
||||||
|
// NewImage generates a new texture and a new framebuffer.
|
||||||
|
// Be careful that image objects will never be released
|
||||||
|
// even though nothing refers the image object and GC works.
|
||||||
|
// It is because there is no way to define finalizers for Go objects if you use GopherJS.
|
||||||
func NewImage(width, height int, filter Filter) (*Image, error) {
|
func NewImage(width, height int, filter Filter) (*Image, error) {
|
||||||
var img *Image
|
var img *Image
|
||||||
var err error
|
var err error
|
||||||
|
21
image.go
21
image.go
@ -16,6 +16,7 @@ package ebiten
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"github.com/hajimehoshi/ebiten/internal"
|
"github.com/hajimehoshi/ebiten/internal"
|
||||||
"github.com/hajimehoshi/ebiten/internal/graphics"
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
||||||
"github.com/hajimehoshi/ebiten/internal/opengl"
|
"github.com/hajimehoshi/ebiten/internal/opengl"
|
||||||
@ -162,6 +163,26 @@ func (i *Image) At(x, y int) color.Color {
|
|||||||
return color.RGBA{r, g, b, a}
|
return color.RGBA{r, g, b, a}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReplacePixels replaces the pixels of image with p.
|
||||||
|
//
|
||||||
|
// The given p must represent RGBA pre-multiplied alpha values.
|
||||||
|
//
|
||||||
|
// len(p) must equal to 4 * (image width) * (image height)
|
||||||
|
//
|
||||||
|
// This function may be slow.
|
||||||
|
func (i *Image) ReplacePixels(p []uint8) error {
|
||||||
|
w, h := i.Size()
|
||||||
|
l := 4 * w * h
|
||||||
|
if len(p) != l {
|
||||||
|
return errors.New(fmt.Sprintf("p's length must be %d", l))
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
ui.Use(func(c *opengl.Context) {
|
||||||
|
err = i.texture.ReplacePixels(c, p)
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// A DrawImageOptions represents options to render an image on an image.
|
// A DrawImageOptions represents options to render an image on an image.
|
||||||
type DrawImageOptions struct {
|
type DrawImageOptions struct {
|
||||||
ImageParts ImageParts
|
ImageParts ImageParts
|
||||||
|
@ -90,3 +90,9 @@ func NewTextureFromImage(c *opengl.Context, img image.Image, filter opengl.Filte
|
|||||||
func (t *Texture) Dispose(c *opengl.Context) {
|
func (t *Texture) Dispose(c *opengl.Context) {
|
||||||
c.DeleteTexture(t.native)
|
c.DeleteTexture(t.native)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Texture) ReplacePixels(c *opengl.Context, p []uint8) error {
|
||||||
|
c.BindTexture(t.native)
|
||||||
|
c.TexSubImage2D(p, t.width, t.height)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -105,6 +105,10 @@ func (c *Context) DeleteTexture(t Texture) {
|
|||||||
gl.Texture(t).Delete()
|
gl.Texture(t).Delete()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Context) TexSubImage2D(p []uint8, width, height int) {
|
||||||
|
gl.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, p)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Context) NewFramebuffer(texture Texture) (Framebuffer, error) {
|
func (c *Context) NewFramebuffer(texture Texture) (Framebuffer, error) {
|
||||||
f := gl.GenFramebuffer()
|
f := gl.GenFramebuffer()
|
||||||
f.Bind()
|
f.Bind()
|
||||||
|
@ -103,6 +103,8 @@ func (c *Context) NewTexture(width, height int, pixels []uint8, filter Filter) (
|
|||||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int(filter))
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int(filter))
|
||||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int(filter))
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int(filter))
|
||||||
|
|
||||||
|
// TODO: Can we use glTexSubImage2D with linear filtering?
|
||||||
|
|
||||||
// void texImage2D(GLenum target, GLint level, GLenum internalformat,
|
// void texImage2D(GLenum target, GLint level, GLenum internalformat,
|
||||||
// GLsizei width, GLsizei height, GLint border, GLenum format,
|
// GLsizei width, GLsizei height, GLint border, GLenum format,
|
||||||
// GLenum type, ArrayBufferView? pixels);
|
// GLenum type, ArrayBufferView? pixels);
|
||||||
@ -140,13 +142,12 @@ func (c *Context) DeleteTexture(t Texture) {
|
|||||||
gl.DeleteTexture(t.Object)
|
gl.DeleteTexture(t.Object)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) GlslHighpSupported() bool {
|
func (c *Context) TexSubImage2D(p []uint8, width, height int) {
|
||||||
gl := c.gl
|
gl := c.gl
|
||||||
// headless-gl library may not define getShaderPrecisionFormat.
|
// void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
|
||||||
if gl.Get("getShaderPrecisionFormat") == js.Undefined {
|
// GLsizei width, GLsizei height,
|
||||||
return false
|
// GLenum format, GLenum type, ArrayBufferView? pixels);
|
||||||
}
|
gl.Call("texSubImage2D", gl.TEXTURE_2D, 0, 0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, p)
|
||||||
return gl.Call("getShaderPrecisionFormat", gl.FRAGMENT_SHADER, gl.HIGH_FLOAT).Get("precision").Int() != 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) NewFramebuffer(t Texture) (Framebuffer, error) {
|
func (c *Context) NewFramebuffer(t Texture) (Framebuffer, error) {
|
||||||
@ -216,6 +217,15 @@ func (c *Context) DeleteShader(s Shader) {
|
|||||||
gl.DeleteShader(s.Object)
|
gl.DeleteShader(s.Object)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Context) GlslHighpSupported() bool {
|
||||||
|
gl := c.gl
|
||||||
|
// headless-gl library may not define getShaderPrecisionFormat.
|
||||||
|
if gl.Get("getShaderPrecisionFormat") == js.Undefined {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return gl.Call("getShaderPrecisionFormat", gl.FRAGMENT_SHADER, gl.HIGH_FLOAT).Get("precision").Int() != 0
|
||||||
|
}
|
||||||
|
|
||||||
var lastProgramID ProgramID = 0
|
var lastProgramID ProgramID = 0
|
||||||
|
|
||||||
func (c *Context) NewProgram(shaders []Shader) (Program, error) {
|
func (c *Context) NewProgram(shaders []Shader) (Program, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user