mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-27 03:02:49 +01:00
Add adjustPixels
This commit is contained in:
parent
239f008045
commit
9b2e8d023f
@ -20,4 +20,5 @@
|
|||||||
|
|
||||||
package opengl
|
package opengl
|
||||||
|
|
||||||
|
var AdjustPixels = adjustPixels
|
||||||
var Clp2 = clp2
|
var Clp2 = clp2
|
||||||
|
@ -40,6 +40,22 @@ func clp2(x uint64) uint64 {
|
|||||||
return x + 1
|
return x + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func adjustPixels(width, height int, pixels []uint8) []uint8 {
|
||||||
|
textureWidth := int(clp2(uint64(width)))
|
||||||
|
textureHeight := int(clp2(uint64(height)))
|
||||||
|
if width == textureWidth && height == textureHeight {
|
||||||
|
return pixels
|
||||||
|
}
|
||||||
|
|
||||||
|
newPixels := make([]uint8, textureWidth*textureHeight*4)
|
||||||
|
|
||||||
|
for j := 0; j < height; j++ {
|
||||||
|
copy(newPixels[textureWidth*4*j:],
|
||||||
|
pixels[width*4*j:width*4*j+width*4])
|
||||||
|
}
|
||||||
|
return newPixels
|
||||||
|
}
|
||||||
|
|
||||||
type Texture struct {
|
type Texture struct {
|
||||||
id C.GLuint
|
id C.GLuint
|
||||||
width int
|
width int
|
||||||
@ -49,16 +65,11 @@ type Texture struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createTexture(width, height int, pixels []uint8) *Texture {
|
func createTexture(width, height int, pixels []uint8) *Texture {
|
||||||
|
if pixels != nil {
|
||||||
|
pixels = adjustPixels(width, height, pixels)
|
||||||
|
}
|
||||||
textureWidth := int(clp2(uint64(width)))
|
textureWidth := int(clp2(uint64(width)))
|
||||||
textureHeight := int(clp2(uint64(height)))
|
textureHeight := int(clp2(uint64(height)))
|
||||||
if pixels != nil {
|
|
||||||
if width != textureWidth {
|
|
||||||
panic("sorry, but width should be power of 2")
|
|
||||||
}
|
|
||||||
if height != textureHeight {
|
|
||||||
panic("sorry, but height should be power of 2")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
texture := &Texture{
|
texture := &Texture{
|
||||||
id: 0,
|
id: 0,
|
||||||
width: width,
|
width: width,
|
||||||
|
@ -25,6 +25,37 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestAdjustPixels(t *testing.T) {
|
||||||
|
pixels := [...]uint8{
|
||||||
|
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
|
||||||
|
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
|
||||||
|
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
|
||||||
|
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
|
||||||
|
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
|
||||||
|
}
|
||||||
|
result := AdjustPixels(3, 5, pixels[0:len(pixels)])
|
||||||
|
wanted := [...]uint8{
|
||||||
|
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0,
|
||||||
|
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0,
|
||||||
|
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0,
|
||||||
|
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0,
|
||||||
|
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
}
|
||||||
|
if len(wanted) != len(result) {
|
||||||
|
t.Errorf("len(result) = %d, wanted %d",
|
||||||
|
len(result), len(wanted))
|
||||||
|
}
|
||||||
|
for i := 0; i < len(result); i++ {
|
||||||
|
if wanted[i] != result[i] {
|
||||||
|
t.Errorf("result[%d] = %d, wanted %d",
|
||||||
|
i, result[i], wanted[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestClp2(t *testing.T) {
|
func TestClp2(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
expected uint64
|
expected uint64
|
||||||
|
Loading…
Reference in New Issue
Block a user