Class: Gosu::Texture

Inherits:
Object
  • Object
show all
Defined in:
lib/gosu_android/graphics/texture.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size, gl) ⇒ Texture

Returns a new instance of Texture.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gosu_android/graphics/texture.rb', line 8

def initialize(size, gl)
  #Set finalize
  ObjectSpace.define_finalizer(self,
                      self.class.method(:finalize).to_proc)
  @size = size
  @allocator = BlockAllocator.new(@size, @size)
  @num = 0
  @gl = gl
  @name = [0].to_java(:int)
  @gl.glGenTextures(1, @name, 0)
  if @name[0] == -1
    raise "Couldn't create OpenGL texture"
  end
  @gl.glBindTexture(JavaImports::GL10::GL_TEXTURE_2D, @name[0])

  @gl.glTexImage2D(JavaImports::GL10::GL_TEXTURE_2D, 0, JavaImports::GL10::GL_RGBA, @allocator.width,
    @allocator.height, 0, JavaImports::GL10::GL_RGBA, JavaImports::GL10::GL_UNSIGNED_BYTE, nil)
=begin
  #TODO Should create an empty texture to be filled later
  pixel = [0]
  pbb = JavaImports::ByteBuffer.allocateDirect(pixel.length*4)
  pbb.order(JavaImports::ByteOrder.nativeOrder)
  pixel_buffer = pbb.asIntBuffer
  pixel_buffer.put(pixel.to_java(:int))
  pixel_buffer.position(0)
  JavaImports::GLUtils.texImage2D(JavaImports::GL10::GL_TEXTURE_2D, 0, 4, @allocator.width, @allocator.height, 0,
             JavaImports::GL10::GL_RGBA, JavaImports::GL10::GL_UNSIGNED_BYTE, pixel_buffer)

  @gl.glTexImage2D(JavaImports::GL10::GL_TEXTURE_2D, 0, 4, @allocator.width, @allocator.height, 0,
             JavaImports::GL10::GL_RGBA, JavaImports::GL10::GL_UNSIGNED_BYTE, pixel_buffer)

  @gl.glTexParameterf(JavaImports::GL10::GL_TEXTURE_2D,
    JavaImports::GL10::GL_TEXTURE_MIN_FILTER, JavaImports::GL10::GL_LINEAR)
  @gl.glTexParameteri(JavaImports::GL10::GL_TEXTURE_2D,
    JavaImports::GL10::GL_TEXTURE_WRAP_S, JavaImports::GL10::GL_CLAMP_TO_EDGE)
  @gl.glTexParameteri(JavaImports::GL10::GL_TEXTURE_2D,
    JavaImports::GL10::GL_TEXTURE_WRAP_T, JavaImports::GL10::GL_CLAMP_TO_EDGE)
=end

  @gl.glTexParameterf(JavaImports::GL10::GL_TEXTURE_2D, JavaImports::GL10::GL_TEXTURE_MIN_FILTER, JavaImports::GL10::GL_NEAREST)
  @gl.glTexParameterf(JavaImports::GL10::GL_TEXTURE_2D, JavaImports::GL10::GL_TEXTURE_MAG_FILTER, JavaImports::GL10::GL_LINEAR)
end

Class Method Details

.finalize(id) ⇒ Object



50
51
52
# File 'lib/gosu_android/graphics/texture.rb', line 50

def Texture.finalize(id)
  @gl.glDeleteTextures(1, @name)
end

Instance Method Details

#free(x, y) ⇒ Object



80
81
82
83
# File 'lib/gosu_android/graphics/texture.rb', line 80

def free(x, y)
  @allocator.free(x, y)
  @num -= 1
end

#sizeObject



58
59
60
# File 'lib/gosu_android/graphics/texture.rb', line 58

def size
  @allocator.width
end

#tex_nameObject



54
55
56
# File 'lib/gosu_android/graphics/texture.rb', line 54

def tex_name
  @name[0]
end

#try_alloc(graphics, queues, ptr, bmp, padding) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gosu_android/graphics/texture.rb', line 62

def try_alloc(graphics, queues, ptr, bmp, padding)
  alloc_info = @allocator.alloc(bmp.width, bmp.height)
  if (not alloc_info[0])
      return nil
  end
  block = alloc_info[1]
  result = TexChunk.new(graphics, queues, ptr, block.left + padding, block.top + padding,
                            block.width - 2 * padding, block.height - 2 * padding, padding)

  @gl.glTexSubImage2D(JavaImports::GL10::GL_TEXTURE_2D, 0, block.left, block.top, block.width, block.height,
    Color::GL_FORMAT, JavaImports::GL10::GL_UNSIGNED_BYTE, bmp.data_java)
  #@gl.glBindTexture(JavaImports::GL10::GL_TEXTURE_2D, @name[0])
  #JavaImports::GLUtils.texImage2D(JavaImports::GL10::GL_TEXTURE_2D, 0, bmp.to_open_gl, 0)

  @num += 1
  result
end