Class: CyberarmEngine::Texture
- Inherits:
-
Object
- Object
- CyberarmEngine::Texture
- Defined in:
- lib/cyberarm_engine/opengl/texture.rb
Constant Summary collapse
- DEFAULT_TEXTURE =
"#{CYBERARM_ENGINE_ROOT_PATH}/assets/textures/default.png".freeze
- CACHE =
{}
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
Instance Method Summary collapse
- #create_from_image(image) ⇒ Object
-
#initialize(path: nil, image: nil, retro: false) ⇒ Texture
constructor
A new instance of Texture.
- #load_image(path) ⇒ Object
Constructor Details
#initialize(path: nil, image: nil, retro: false) ⇒ Texture
Returns a new instance of Texture.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/cyberarm_engine/opengl/texture.rb', line 19 def initialize(path: nil, image: nil, retro: false) raise "keyword :path or :image must be provided!" if path.nil? && image.nil? @retro = retro @path = path if @path unless File.exist?(@path) warn "Missing texture at: #{@path}" @retro = true # override retro setting @path = DEFAULT_TEXTURE end if texture = Texture.from_cache(@path, @retro) @id = texture.id return end image = load_image(@path) @id = create_from_image(image) else @id = create_from_image(image) end end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
17 18 19 |
# File 'lib/cyberarm_engine/opengl/texture.rb', line 17 def id @id end |
Class Method Details
.from_cache(path, retro) ⇒ Object
13 14 15 |
# File 'lib/cyberarm_engine/opengl/texture.rb', line 13 def self.from_cache(path, retro) CACHE.dig("#{path}?retro=#{retro}") end |
.release_textures ⇒ Object
7 8 9 10 11 |
# File 'lib/cyberarm_engine/opengl/texture.rb', line 7 def self.release_textures CACHE.values.each do |id| glDeleteTextures(id) end end |
Instance Method Details
#create_from_image(image) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/cyberarm_engine/opengl/texture.rb', line 49 def create_from_image(image) array_of_pixels = image.to_blob tex_names_buf = " " * 4 glGenTextures(1, tex_names_buf) texture_id = tex_names_buf.unpack1("L2") glBindTexture(GL_TEXTURE_2D, texture_id) glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB_ALPHA, 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_T, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) if @retro glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) unless @retro glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR) glGenerateMipmap(GL_TEXTURE_2D) gl_error? texture_id end |