From 18a62b81ccc6b43459e7711ec4db8f0d91587735 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Mon, 23 Mar 2020 08:05:40 -0500 Subject: [PATCH] Added Texture object for handling creating and caching textures, stubbed ParticleEmitter --- assets/base/shared/particles/smoke/smoke.png | Bin 0 -> 2002 bytes .../base/war_factory/scripts/war_factory.rb | 3 + i-mic-fps.rb | 2 + lib/game_objects/entities/player.rb | 16 +---- lib/game_objects/particle_emitter.rb | 10 +++ lib/managers/entity_manager.rb | 3 + lib/model/material.rb | 18 +---- lib/texture.rb | 62 ++++++++++++++++++ 8 files changed, 82 insertions(+), 32 deletions(-) create mode 100644 assets/base/shared/particles/smoke/smoke.png create mode 100644 lib/game_objects/particle_emitter.rb create mode 100644 lib/texture.rb diff --git a/assets/base/shared/particles/smoke/smoke.png b/assets/base/shared/particles/smoke/smoke.png new file mode 100644 index 0000000000000000000000000000000000000000..11fa5893259873414b464d9ce7ace030bf38b70a GIT binary patch literal 2002 zcmV;@2QB!CP)PrdX{G+g=n5k!l@Sl9c9gPtIfSz4l&v zt>xg{dx_CoTiTBNMr*@b`}^(nJ^tT@|92DbW%I)iKfH4F`qih#sfArNA@>-#L8ntgo+QOuaSekAHOV;K5(L+Z$2;Yr}~XC%&_~I{sN% zRmwY$C*ZxqwjIHH1fWzznx-5+a^zr9^iJHE+&FRU`FLgYJ@12%NL=Vh)0Cp{uZRu=>R(dUt_16~V{N%l#y63eE z7cRc}o&X+w^wF>QVE+)s$>)6tBuRvG4(|i&>+2Ln52YlX?Fe|JRE)b?H@-+ue903Lbdk?$Y4WBt5yKJT1EDuFbl0c4(B|kHfSBOzOjD)>e||sH~#*{);j_`{`j{ZI&$>zPn)L2 zm>Q`h1hCf9mgBaWjle)67A zJ$v!u#qL(0@|LwI2K}d&U*O2{oFqw5O4GItNfIv|)dp)V-upd>K%i|~+O`D*&N-x% zXsyZ846QYz(P$_lasTZ%um^DJ)TyId=}&ZB2Lg(spePEoR>VnybB;KUNzw%GJc~ui zVo_pDgAf8C1+&GR*>sK&f}-d#9*xO+Id>kqlU}b!aN*eP0D5;(*Y(|L9b;<4>iQbq zd;0x8)^;>agVvh1ZL!u8LO=+?qAbx$F&quavWzrMna*Z(T}SW%=Nz`PEEXkISrS56 z-4{S9mDY8QZ7o$<5hpQaSz=60-pdI-kfteRS>nAX_&^kCwANT_>GgUfNdh5Ytz}V` zY)>Y1w!_plUDu&>B=-gI-q%8Cgp!n1h4T)j6jfC%0j1gtOIenbrbGxqp67%Rn9t`- zr&D%wNNZ0~^f0xda~;Fs5Yw1=Ux2o4tM!dFym!oIbFA%1k^~_HaU9bx`V2?IotJCo zvpEQXF$N_SrZxluv+0aDNysxr9K|^2kxrtOCIk-d3ost%Zwn!aqIj8jaZD6N2qZev zBxypLrqp$fwU)e>>R?vg_qNeDEyF5`aLU-!DFW zJHTF2oIQJXu|1jG6heRySleN0L({a(rc*9oy24vm-s0xXnsSR-ylVush;c%yXEwh_5k|aV(L1S6~@yaW&9N!n9G3Irp1)L;C{g zIR1T}=Q!{1At0rsZCkpoBTW;EqM%aB_G{cw%AHuzxo11%n|4j|Iwzeh*4s1M__wplImeVxL*{^dQQ54Zxixh$+ zPKo1)x~iDZ=d_K*?+B0&p2irGI3|iCy!X_#Aq0;Uk~oUAci|_mzWQnlx0K!w{&pi=waeGrmkwPUAsnA)j01+lMJbrGh#3-!%Br=DJ9FmDXH_I$Oq1ke)9IASWI~o@z;d4G^?C>)c2w2iz5mnUyN-Tgb8~Yq z1n&y~;QaaX=}W(R>DjyPI&^y1(QZHR-s7BGs%KgK`M|~<51u=BZhGta_YJUXPdxF& zXRdEw|6!JZpurSF0Fg-M}xuO k?Ps2O=H>_O;{&yS0o(}Dw=kGnDgXcg07*qoM6N<$g2!mnga7~l literal 0 HcmV?d00001 diff --git a/assets/base/war_factory/scripts/war_factory.rb b/assets/base/war_factory/scripts/war_factory.rb index 6f19ed4..bf099f4 100644 --- a/assets/base/war_factory/scripts/war_factory.rb +++ b/assets/base/war_factory/scripts/war_factory.rb @@ -5,4 +5,7 @@ on.create do |event| map.insert_entity("base", "information_panel", event.entity.position + Vector.new(0.5, 0, 3), Vector.new(0, 90, 0)) map.insert_entity("base", "door", event.entity.position + Vector.new(3.3, 0, 6), Vector.new(0, 0, 0)) map.insert_entity("base", "door", event.entity.position + Vector.new(3.3, 0, 6), Vector.new(0, 180, 0)) + + map.insert_particle_emitter(Vector.new(3.0, 15.379, 0.029), Texture.new("base", "shared", "particles", "smoke", "smoke.png")) + map.insert_particle_emitter(Vector.new(5.0, 15.379, 0.029), Texture.new("base", "shared", "particles", "smoke", "smoke.png")) end \ No newline at end of file diff --git a/i-mic-fps.rb b/i-mic-fps.rb index 9d7a13f..ece7b59 100644 --- a/i-mic-fps.rb +++ b/i-mic-fps.rb @@ -79,12 +79,14 @@ require_relative "lib/components/building" require_relative "lib/game_objects/entity" require_relative "lib/game_objects/light" +require_relative "lib/game_objects/particle_emitter" require_relative "lib/game_objects/camera" require_relative "lib/game_objects/entities/player" require_relative "lib/game_objects/entities/skydome" require_relative "lib/game_objects/entities/terrain" +require_relative "lib/texture" require_relative "lib/model" require_relative "lib/model_cache" require_relative "lib/model/parser" diff --git a/lib/game_objects/entities/player.rb b/lib/game_objects/entities/player.rb index 7984924..9e796b0 100644 --- a/lib/game_objects/entities/player.rb +++ b/lib/game_objects/entities/player.rb @@ -19,21 +19,7 @@ class IMICFPS @devisor = 500.0 @name_image = Gosu::Image.from_text("#{Etc.getlogin}", 100, font: "Consolas", align: :center) - # @name_image.save("temp.png") - # @name_tex = @name_image.gl_tex_info - array_of_pixels = @name_image.to_blob - - tex_names_buf = ' ' * 8 - glGenTextures(1, tex_names_buf) - @name_texture_id = tex_names_buf.unpack('L2').first - - glBindTexture(GL_TEXTURE_2D, @name_texture_id) - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, @name_image.width, @name_image.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, array_of_pixels) - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) - 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) + @name_texture_id = Texture.new(@name_image).id end def draw_nameplate diff --git a/lib/game_objects/particle_emitter.rb b/lib/game_objects/particle_emitter.rb new file mode 100644 index 0000000..af586a6 --- /dev/null +++ b/lib/game_objects/particle_emitter.rb @@ -0,0 +1,10 @@ +class IMICFPS + def initialize(position:, image:, interval: 1_500, time_to_live: 3_000, max_particles: 500) + end + + def draw + end + + def update + end +end \ No newline at end of file diff --git a/lib/managers/entity_manager.rb b/lib/managers/entity_manager.rb index cebd03c..e44e3d2 100644 --- a/lib/managers/entity_manager.rb +++ b/lib/managers/entity_manager.rb @@ -31,5 +31,8 @@ class IMICFPS def entities @entities end + + def insert_particle_emitter(position, texture) + end end end diff --git a/lib/model/material.rb b/lib/model/material.rb index f09e875..20812fa 100644 --- a/lib/model/material.rb +++ b/lib/model/material.rb @@ -13,23 +13,7 @@ class IMICFPS end def set_texture(texture_path) - # puts "#{name} texture #{texture_path}" - @texture = Gosu::Image.new(texture_path, retro: false) - 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) - - @texture = nil + @texture_id = Texture.new(texture_path).id end end end diff --git a/lib/texture.rb b/lib/texture.rb new file mode 100644 index 0000000..8d10d5f --- /dev/null +++ b/lib/texture.rb @@ -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 \ No newline at end of file