mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-15 15:42:35 +00:00
Added default texture, refactored Texture to cache textures which have a file path
This commit is contained in:
BIN
assets/base/shared/textures/default.png
Normal file
BIN
assets/base/shared/textures/default.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 681 B |
@@ -1,5 +1,6 @@
|
|||||||
class IMICFPS
|
class IMICFPS
|
||||||
class Texture
|
class Texture
|
||||||
|
DEFAULT_TEXTURE = "#{IMICFPS.assets_path}/base/shared/textures/default.png"
|
||||||
include CommonMethods
|
include CommonMethods
|
||||||
|
|
||||||
CACHE = {}
|
CACHE = {}
|
||||||
@@ -10,6 +11,10 @@ class IMICFPS
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.from_cache(path, retro)
|
||||||
|
return CACHE.dig("#{path}?retro=#{retro}")
|
||||||
|
end
|
||||||
|
|
||||||
attr_reader :id
|
attr_reader :id
|
||||||
def initialize(path: nil, image: nil, retro: false)
|
def initialize(path: nil, image: nil, retro: false)
|
||||||
raise "keyword :path or :image must be provided!" if path.nil? && image.nil?
|
raise "keyword :path or :image must be provided!" if path.nil? && image.nil?
|
||||||
@@ -23,33 +28,44 @@ class IMICFPS
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@id = create_from_image(path ? path : image)
|
@path = path
|
||||||
|
|
||||||
|
if @path
|
||||||
|
unless File.exist?(@path)
|
||||||
|
warn "Missing texture at: #{@path}" if window.config.get(:debug_options, :stats)
|
||||||
|
@retro = true # override retro setting
|
||||||
|
@path = DEFAULT_TEXTURE
|
||||||
end
|
end
|
||||||
|
|
||||||
def from_cache(path)
|
if texture = Texture.from_cache(@path, @retro)
|
||||||
CACHE[path] = create_from_image(path) unless CACHE[path]
|
@id = texture.id
|
||||||
|
puts "Using cached texture: #{@path}" if window.config.get(:debug_options, :stats)
|
||||||
return CACHE[path]
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_from_image(path_or_image)
|
puts "Allocating texture for: #{@path}" if window.config.get(:debug_options, :stats)
|
||||||
puts "Allocating texture for: #{path_or_image}" if window.config.get(:debug_options, :stats)
|
image = load_image(@path)
|
||||||
|
@id = create_from_image(image)
|
||||||
texture = nil
|
|
||||||
if path_or_image.is_a?(Gosu::Image)
|
|
||||||
texture = path_or_image
|
|
||||||
else
|
else
|
||||||
texture = Gosu::Image.new(path_or_image, retro: false)
|
puts "Allocating texture for: #{image}" if window.config.get(:debug_options, :stats)
|
||||||
|
@id = create_from_image(image)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
array_of_pixels = texture.to_blob
|
def load_image(path)
|
||||||
|
CACHE["#{path}?retro=#{@retro}"] = self
|
||||||
|
Gosu::Image.new(path, retro: @retro)
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_from_image(image)
|
||||||
|
array_of_pixels = image.to_blob
|
||||||
|
|
||||||
tex_names_buf = ' ' * 4
|
tex_names_buf = ' ' * 4
|
||||||
glGenTextures(1, tex_names_buf)
|
glGenTextures(1, tex_names_buf)
|
||||||
texture_id = tex_names_buf.unpack('L2').first
|
texture_id = tex_names_buf.unpack('L2').first
|
||||||
|
|
||||||
glBindTexture(GL_TEXTURE_2D, texture_id)
|
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)
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width, image.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_S, GL_REPEAT)
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) if @retro
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) if @retro
|
||||||
|
|||||||
Reference in New Issue
Block a user