From 4f1b490600241bf23b95179eb970072d0c9eb933 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Wed, 29 Jan 2020 18:18:46 -0600 Subject: [PATCH] Use persistent hash for config --- .gitignore | 4 +++- data/.gitkeep | 1 + lib/common_methods.rb | 2 +- lib/game_objects/model_loader.rb | 2 +- lib/managers/light_manager.rb | 2 +- lib/model.rb | 8 ++++---- lib/renderer/opengl_renderer.rb | 2 +- lib/renderer/renderer.rb | 6 +++--- lib/states/game_states/game.rb | 4 ++-- lib/states/game_states/loading_state.rb | 2 +- lib/ui/commands/debug_command.rb | 19 ++++++++++--------- lib/ui/commands/fps_command.rb | 8 ++++---- lib/wavefront/parser.rb | 2 +- lib/window.rb | 9 +++++++-- 14 files changed, 40 insertions(+), 31 deletions(-) create mode 100644 data/.gitkeep diff --git a/.gitignore b/.gitignore index 5d75ce1..617d57d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ *.dat profile.* -pkg/* \ No newline at end of file +pkg/* +data/* +!data/.gitkeep diff --git a/data/.gitkeep b/data/.gitkeep new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/data/.gitkeep @@ -0,0 +1 @@ + diff --git a/lib/common_methods.rb b/lib/common_methods.rb index 13ac966..397be67 100644 --- a/lib/common_methods.rb +++ b/lib/common_methods.rb @@ -48,7 +48,7 @@ class IMICFPS if e != GL_NO_ERROR $stderr.puts "OpenGL error detected by handler at: #{caller[0]}" $stderr.puts " #{gluErrorString(e)} (#{e})\n" - exit if $debug && $debug.get(:opengl_error_panic) + exit if window.config.get(:debug_options, :opengl_error_panic) end end end diff --git a/lib/game_objects/model_loader.rb b/lib/game_objects/model_loader.rb index b2961a8..432b65c 100644 --- a/lib/game_objects/model_loader.rb +++ b/lib/game_objects/model_loader.rb @@ -38,7 +38,7 @@ class IMICFPS if CACHE[@type].is_a?(Hash) if CACHE[@type][@model_file] @model = CACHE[@type][@model_file]#.dup # Don't know why, but adding .dup improves performance with Sponza (1 fps -> 20 fps) - puts "Used cached model for: #{@model_file.split('/').last}" if $debug.get(:stats) + puts "Used cached model for: #{@model_file.split('/').last}" if $window.config.get(:debug_options, :stats) found = true end end diff --git a/lib/managers/light_manager.rb b/lib/managers/light_manager.rb index 15e9745..acd0e55 100644 --- a/lib/managers/light_manager.rb +++ b/lib/managers/light_manager.rb @@ -23,7 +23,7 @@ class IMICFPS def available_light raise "Using to many lights, #{light_count}/#{LightManager::MAX_LIGHTS}" if light_count > LightManager::MAX_LIGHTS - puts "OpenGL::GL_LIGHT#{light_count}" if $debug.get(:stats) + puts "OpenGL::GL_LIGHT#{light_count}" if $window.config.get(:debug_options, :stats) Object.const_get "OpenGL::GL_LIGHT#{light_count}" end end diff --git a/lib/model.rb b/lib/model.rb index a7422ec..890ca90 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -33,7 +33,7 @@ class IMICFPS parse(parser) - puts "#{@file_path.split('/').last} took #{((Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)-start_time)/1000.0).round(2)} seconds to parse" if $debug.get(:stats) + puts "#{@file_path.split('/').last} took #{((Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)-start_time)/1000.0).round(2)} seconds to parse" if window.config.get(:debug_options, :stats) if Shader.available?("default") allocate_gl_objects @@ -43,7 +43,7 @@ class IMICFPS @objects.each {|o| @vertex_count+=o.vertices.size} @objects.each_with_index do |o, i| - puts " Model::Object Name: #{o.name}, Vertices: #{o.vertices.size}" if $debug.get(:stats) + puts " Model::Object Name: #{o.name}, Vertices: #{o.vertices.size}" if window.config.get(:debug_options, :stats) end window.number_of_vertices+=@vertex_count @has_texture = false @@ -56,7 +56,7 @@ class IMICFPS start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) # build_collision_tree - puts " Building mesh collision tree took #{((Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)-start_time)/1000.0).round(2)} seconds" if $debug.get(:stats) + puts " Building mesh collision tree took #{((Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)-start_time)/1000.0).round(2)} seconds" if window.config.get(:debug_options, :stats) end def parse(parser) @@ -214,7 +214,7 @@ class IMICFPS @aabb_tree.insert(face, box) end - puts @aabb_tree.inspect if $debug.get(:stats) + puts @aabb_tree.inspect if window.config.get(:debug_options, :stats) end def update diff --git a/lib/renderer/opengl_renderer.rb b/lib/renderer/opengl_renderer.rb index b12f0e3..ebef34d 100644 --- a/lib/renderer/opengl_renderer.rb +++ b/lib/renderer/opengl_renderer.rb @@ -102,7 +102,7 @@ class IMICFPS glColorPointer(3, GL_FLOAT, 0, o.flattened_materials) glNormalPointer(GL_FLOAT, 0, o.flattened_normals) - if $debug.get(:wireframe) # This is kinda expensive + if window.config.get(:debug_options, :wireframe) # This is kinda expensive glDisable(GL_LIGHTING) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) glPolygonOffset(2, 0.5) diff --git a/lib/renderer/renderer.rb b/lib/renderer/renderer.rb index 07311a2..3e5806e 100644 --- a/lib/renderer/renderer.rb +++ b/lib/renderer/renderer.rb @@ -16,14 +16,14 @@ class IMICFPS entities.each do |object| if object.visible && object.renderable # Render bounding boxes before transformation is applied - # @bounding_box_renderer.create_bounding_box(object, object.model.bounding_box, object.debug_color, object.object_id) if $debug.get(:boundingboxes) + # @bounding_box_renderer.create_bounding_box(object, object.model.bounding_box, object.debug_color, object.object_id) if window.config.get(:debug_options, :boundingboxes) @opengl_renderer.draw_object(camera, lights, object) end end - # @bounding_box_renderer.draw_bounding_boxes if $debug.get(:boundingboxes) - # window.number_of_vertices+=@bounding_box_renderer.vertex_count if $debug.get(:boundingboxes) + # @bounding_box_renderer.draw_bounding_boxes if window.config.get(:debug_options, :boundingboxes) + # window.number_of_vertices+=@bounding_box_renderer.vertex_count if window.config.get(:debug_options, :boundingboxes) # @bounding_box_renderer.bounding_boxes.clear end diff --git a/lib/states/game_states/game.rb b/lib/states/game_states/game.rb index 13f8f48..cd74eb0 100644 --- a/lib/states/game_states/game.rb +++ b/lib/states/game_states/game.rb @@ -40,9 +40,9 @@ class IMICFPS @camera.update - if $debug.get(:stats) + if window.config.get(:debug_options, :stats) @text.text = update_text - elsif $debug.get(:fps) + elsif window.config.get(:options, :fps) @text.text = "FPS: #{Gosu.fps}" else @text.text = "" diff --git a/lib/states/game_states/loading_state.rb b/lib/states/game_states/loading_state.rb index 387d15c..3ce2da8 100644 --- a/lib/states/game_states/loading_state.rb +++ b/lib/states/game_states/loading_state.rb @@ -55,7 +55,7 @@ class IMICFPS when :model ModelLoader.new(manifest: hash[:manifest], entity: @dummy_entity) when :shader - if $debug.get(:use_shaders) + if window.config.get(:debug_options, :use_shaders) shader = Shader.new(name: hash[:name], includes_dir: "shaders/include", vertex: "shaders/vertex/#{hash[:name]}.glsl", fragment: "shaders/fragment/#{hash[:name]}.glsl") else warn "Skipping shader: #{hash[:name]}..." diff --git a/lib/ui/commands/debug_command.rb b/lib/ui/commands/debug_command.rb index 1b8a3a4..bba0e15 100644 --- a/lib/ui/commands/debug_command.rb +++ b/lib/ui/commands/debug_command.rb @@ -9,16 +9,17 @@ class IMICFPS :debug end - def setup - $debug = self + def set(key, value) + $window.config[:debug_options, key] = value + end - set(:boundingboxes, false) - set(:wireframe, false) - set(:stats, false) - set(:fps, false) - set(:skydome, true) - set(:use_shaders, true) - set(:opengl_error_panic, false) + def setup + set(:boundingboxes, false) if $window.config.get(:debug_options, :boundingboxes).nil? + set(:wireframe, false) if $window.config.get(:debug_options, :wireframe).nil? + set(:stats, false) if $window.config.get(:debug_options, :stats).nil? + set(:skydome, true) if $window.config.get(:debug_options, :skydome).nil? + set(:use_shaders, true) if $window.config.get(:debug_options, :use_shaders).nil? + set(:opengl_error_panic, false) if $window.config.get(:debug_options, :opengl_error_panic).nil? subcommand(:boundingboxes, :boolean) subcommand(:wireframe, :boolean) diff --git a/lib/ui/commands/fps_command.rb b/lib/ui/commands/fps_command.rb index 70d53a1..429182f 100644 --- a/lib/ui/commands/fps_command.rb +++ b/lib/ui/commands/fps_command.rb @@ -17,12 +17,12 @@ class IMICFPS case arguments.last when "", nil - console.stdin("#{Style.highlight("fps")}: #{$debug.get(:fps)}") + console.stdin("#{Style.highlight("fps")}: #{$window.config.get(:options, :fps)}") when "on" - var = $debug.set(:fps, true) + var = $window.config[:options, :fps] = true console.stdin("fps => #{Style.highlight(var)}") when "off" - var = $debug.set(:fps, false) + var = $window.config[:options, :fps] = false console.stdin("fps => #{Style.highlight(var)}") else console.stdin("Invalid argument for #{Style.highlight("#{command}")}, got #{Style.error(arguments.last)} expected #{Style.notice("on")}, or #{Style.notice("off")}.") @@ -34,4 +34,4 @@ class IMICFPS end end end -end \ No newline at end of file +end diff --git a/lib/wavefront/parser.rb b/lib/wavefront/parser.rb index d949827..e89897d 100644 --- a/lib/wavefront/parser.rb +++ b/lib/wavefront/parser.rb @@ -72,7 +72,7 @@ class IMICFPS end end - puts "Total Lines: #{lines}" if $debug.get(:stats) + puts "Total Lines: #{lines}" if $window.config.get(:debug_options, :stats) @model.calculate_bounding_box(@model.vertices, @model.bounding_box) @model.objects.each do |o| @model.calculate_bounding_box(o.vertices, o.bounding_box) diff --git a/lib/window.rb b/lib/window.rb index 2aa298d..7855e68 100644 --- a/lib/window.rb +++ b/lib/window.rb @@ -1,7 +1,7 @@ class IMICFPS class Window < CyberarmEngine::Engine attr_accessor :number_of_vertices, :needs_cursor - attr_reader :camera + attr_reader :camera, :config attr_reader :console, :delta_time def initialize(window_width = 1280, window_height = 720, fullscreen = false) @@ -18,10 +18,15 @@ class IMICFPS self.caption = "#{IMICFPS::NAME} v#{IMICFPS::VERSION} (#{IMICFPS::RELEASE_NAME})" + @config = CyberarmEngine::ConfigFile.new(file: IMICFPS::GAME_ROOT_PATH + "/data/config.json") @show_console = false @console = Console.new Commands::Command.setup + at_exit do + @config.save! + end + push_state(MainMenu) @delta_time = Gosu.milliseconds @@ -39,7 +44,7 @@ class IMICFPS def draw_cursor size = 16 - + if needs_cursor @cursor.draw(mouse_x, mouse_y, Float::INFINITY) end