mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-15 15:42:35 +00:00
Added Texture object for handling creating and caching textures, stubbed ParticleEmitter
This commit is contained in:
62
lib/texture.rb
Normal file
62
lib/texture.rb
Normal file
@@ -0,0 +1,62 @@
|
||||
class IMICFPS
|
||||
class Texture
|
||||
include CommonMethods
|
||||
|
||||
CACHE = {}
|
||||
|
||||
def self.release_textures
|
||||
CACHE.values.each do |id|
|
||||
glDeleteTextures(id)
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :id
|
||||
def initialize(*path)
|
||||
if path.size > 1
|
||||
path = "#{GAME_ROOT_PATH}/assets/#{path.join("/")}"
|
||||
else
|
||||
path = path.first
|
||||
end
|
||||
|
||||
unless path.is_a?(String)
|
||||
@id = create_from_image(path)
|
||||
else
|
||||
@id = from_cache(path)
|
||||
end
|
||||
end
|
||||
|
||||
def from_cache(path)
|
||||
CACHE[path] = create_from_image(path) unless CACHE[path]
|
||||
|
||||
return CACHE[path]
|
||||
end
|
||||
|
||||
def create_from_image(path_or_image)
|
||||
puts "Allocating texture for: #{path_or_image}" if window.config.get(:debug_options, :stats)
|
||||
|
||||
texture = nil
|
||||
if path_or_image.is_a?(Gosu::Image)
|
||||
texture = path_or_image
|
||||
else
|
||||
texture = Gosu::Image.new(path_or_image, retro: false)
|
||||
end
|
||||
|
||||
array_of_pixels = texture.to_blob
|
||||
|
||||
tex_names_buf = ' ' * 8
|
||||
glGenTextures(1, tex_names_buf)
|
||||
texture_id = tex_names_buf.unpack('L2').first
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, texture_id)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture.width, texture.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, array_of_pixels)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
|
||||
glGenerateMipmap(GL_TEXTURE_2D)
|
||||
gl_error?
|
||||
|
||||
return texture_id
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user