From 12a588304bfffde723f70404bcff512fe43b6647 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Wed, 7 Aug 2019 12:00:39 -0500 Subject: [PATCH] Added dependance on cyberarm_engine, removed duplicate code which is in cyberarm_engine --- Gemfile | 2 +- i-mic-fps.rb | 14 +-- lib/math/bounding_box.rb | 124 ----------------------- lib/math/vector.rb | 98 ------------------ lib/objects/multi_line_text.rb | 63 ------------ lib/objects/text.rb | 96 ------------------ lib/renderer/shader.rb | 127 ------------------------ lib/states/game_state.rb | 24 +---- lib/states/game_states/loading_state.rb | 4 +- lib/states/menu.rb | 12 +-- lib/states/menus/main_menu.rb | 2 +- lib/window.rb | 38 +------ 12 files changed, 23 insertions(+), 581 deletions(-) delete mode 100644 lib/math/bounding_box.rb delete mode 100644 lib/math/vector.rb delete mode 100644 lib/objects/multi_line_text.rb delete mode 100644 lib/objects/text.rb delete mode 100644 lib/renderer/shader.rb diff --git a/Gemfile b/Gemfile index 92e7f3d..62493fd 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,3 @@ source "https://rubygems.org" gem "opengl-bindings" -gem "gosu" \ No newline at end of file +gem "cyberarm_engine" \ No newline at end of file diff --git a/i-mic-fps.rb b/i-mic-fps.rb index 24ab757..dfb54b8 100644 --- a/i-mic-fps.rb +++ b/i-mic-fps.rb @@ -3,7 +3,12 @@ require "yaml" require "opengl" require "glu" -require "gosu" + +begin + require_relative "../cyberarm_engine/lib/cyberarm_engine" +rescue LoadError + require "cyberarm_engine" +end Dir.chdir(File.dirname(__FILE__)) @@ -58,11 +63,9 @@ end $debug = ARGV.join.include?("--debug") ? true : false +include CyberarmEngine require_relative "lib/common_methods" -require_relative "lib/math/vector" -require_relative "lib/math/bounding_box" - require_relative "lib/trees/aabb_tree_debug" require_relative "lib/trees/aabb_tree" require_relative "lib/trees/aabb_node" @@ -76,7 +79,6 @@ require_relative "lib/managers/collision_manager" require_relative "lib/managers/physics_manager" require_relative "lib/renderer/renderer" -require_relative "lib/renderer/shader" require_relative "lib/renderer/opengl_renderer" require_relative "lib/renderer/bounding_box_renderer" @@ -86,8 +88,6 @@ require_relative "lib/states/game_states/game" require_relative "lib/states/game_states/loading_state" require_relative "lib/states/menus/main_menu" -require_relative "lib/objects/text" -require_relative "lib/objects/multi_line_text" require_relative "lib/objects/entity" require_relative "lib/objects/model_loader" require_relative "lib/objects/light" diff --git a/lib/math/bounding_box.rb b/lib/math/bounding_box.rb deleted file mode 100644 index 8e5b448..0000000 --- a/lib/math/bounding_box.rb +++ /dev/null @@ -1,124 +0,0 @@ -class IMICFPS - class BoundingBox - attr_accessor :min, :max - - def initialize(minx = 0, miny = 0, minz = 0, maxx = 0, maxy = 0, maxz = 0) - @min = Vector.new(minx, miny, minz) - @max = Vector.new(maxx, maxy, maxz) - end - - def ==(other) - @min == other.min && - @max == other.max - end - - # returns a new bounding box that includes both bounding boxes - def union(other) - temp = BoundingBox.new - temp.min.x = [@min.x, other.min.x].min - temp.min.y = [@min.y, other.min.y].min - temp.min.z = [@min.z, other.min.z].min - - temp.max.x = [@max.x, other.max.x].max - temp.max.y = [@max.y, other.max.y].max - temp.max.z = [@max.z, other.max.z].max - - return temp - end - - # returns the difference between both bounding boxes - def difference(other) - temp = BoundingBox.new - temp.min = @min - other.min - temp.max = @max - other.max - - return temp - end - - # returns whether both bounding boxes intersect - def intersect?(other) - (@min.x <= other.max.x && @max.x >= other.min.x) && - (@min.y <= other.max.y && @max.y >= other.min.y) && - (@min.z <= other.max.z && @max.z >= other.min.z) - end - - # does this bounding box envelop other bounding box? (inclusive of border) - def contains?(other) - other.min.x >= min.x && other.min.y >= min.y && other.min.z >= min.z && - other.max.x <= max.x && other.max.y <= max.y && other.max.z <= max.z - end - - # returns whether the vector is inside of the bounding box - def point?(vector) - vector.x.between?(@min.x, @max.x) && - vector.y.between?(@min.y, @max.y) && - vector.z.between?(@min.z, @max.z) - end - - def volume - width * height * depth - end - - def width - @max.x - @min.x - end - - def height - @max.y - @min.y - end - - def depth - @max.z - @min.z - end - - def normalize(entity) - temp = BoundingBox.new - temp.min.x = @min.x.to_f * entity.scale - temp.min.y = @min.y.to_f * entity.scale - temp.min.z = @min.z.to_f * entity.scale - - temp.max.x = @max.x.to_f * entity.scale - temp.max.y = @max.y.to_f * entity.scale - temp.max.z = @max.z.to_f * entity.scale - - return temp - end - - def normalize_with_offset(entity) - temp = BoundingBox.new - temp.min.x = @min.x.to_f * entity.scale + entity.position.x - temp.min.y = @min.y.to_f * entity.scale + entity.position.y - temp.min.z = @min.z.to_f * entity.scale + entity.position.z - - temp.max.x = @max.x.to_f * entity.scale + entity.position.x - temp.max.y = @max.y.to_f * entity.scale + entity.position.y - temp.max.z = @max.z.to_f * entity.scale + entity.position.z - - return temp - end - - def +(other) - box = BoundingBox.new - box.min = self.min + other.min - box.min = self.max + other.max - - return box - end - - def -(other) - box = BoundingBox.new - box.min = self.min - other.min - box.min = self.max - other.max - - return box - end - - def sum - @min.sum + @max.sum - end - - def clone - BoundingBox.new(@min.x, @min.y, @min.z, @max.x, @max.y, @max.z) - end - end -end \ No newline at end of file diff --git a/lib/math/vector.rb b/lib/math/vector.rb deleted file mode 100644 index a42a23a..0000000 --- a/lib/math/vector.rb +++ /dev/null @@ -1,98 +0,0 @@ -class IMICFPS - class Vector - - def initialize(x = 0, y = 0, z = 0, weight = 0) - @x, @y, @z, @weight = x, y, z, weight - end - - def x; @x; end - def x=(n); @x = n; end - - def y; @y; end - def y=(n); @y = n; end - - def z; @z; end - def z=(n); @z = n; end - - def weight; @weight; end - def weight=(n); @weight = n; end - - # def xy=(nx, ny); @x = nx; @y = ny; end - # def xyz=(nx, ny, nz); @x = nx; @y = ny; @z = nz; end - # def xyzw=(nx, ny, nz, nw); @x = nx; @y = ny; @z = nz; @weight = nw; end - - def ==(other) - if other.is_a?(Numeric) - @x == other && - @y == other && - @z == other && - @weight == other - else - @x == other.x && - @y == other.y && - @z == other.z && - @weight == other.weight - end - end - - def +(other) - Vector.new( - @x + other.x, - @y + other.y, - @z + other.z, - @weight + other.weight - ) - end - - def -(other) - Vector.new( - @x - other.x, - @y - other.y, - @z - other.z, - @weight - other.weight - ) - end - - def *(other) - Vector.new( - @x * other.x, - @y * other.y, - @z * other.z, - @weight * other.weight - ) - end - - def /(other) - # Endeavors to prevent division by zero - Vector.new( - @x == 0 || other.x == 0 ? 0 : @x / other.x, - @y == 0 || other.y == 0 ? 0 : @y / other.y, - @z == 0 || other.z == 0 ? 0 : @z / other.z, - @weight == 0 || other.weight == 0 ? 0 : @weight / other.weight - ) - end - - # returns magnitude of Vector, ignoring #weight - def magnitude - Math.sqrt((@x * @x) + (@y * @y) + (@z * @z)) - end - - def normalized - mag = magnitude - p mag - self / Vector.new(mag, mag, mag) - end - - def sum - @x + @y + @z + @weight - end - - def to_a - [@x, @y, @z, @weight] - end - - def to_s - "X: #{@x}, Y: #{@y}, Z: #{@z}, Weight: #{@weight}" - end - end -end \ No newline at end of file diff --git a/lib/objects/multi_line_text.rb b/lib/objects/multi_line_text.rb deleted file mode 100644 index b610ab0..0000000 --- a/lib/objects/multi_line_text.rb +++ /dev/null @@ -1,63 +0,0 @@ -class MultiLineText - attr_accessor :options, :x, :y, :width, :height - - def initialize(text, options={}) - @texts = [] - text.split("\n").each_with_index do |line, i| - _options = options - @texts << Text.new(line, _options) - end - @options = options - @x = @texts.first ? @texts.first.x : 0 - @y = @texts.first ? @texts.first.y : 0 - @width = 0 - @height = 0 - calculate_boundry - end - - def draw - @texts.each(&:draw) - end - - def text - string = "" - @texts.each {|t| string << t.text} - return string - end - - def text=(text) - text.split("\n").each_with_index do |line, i| - if @texts[i] - @texts[i].text = line - else - @texts << Text.new(line, @options) - end - end - - calculate_stack - calculate_boundry - end - - def x=(int) - @x = int - @texts.each {|t| t.x = int} - end - - def y=(int) - @y = int - @texts.each_with_index {|t, i| t.y=int+(i*t.size)} - end - - def calculate_stack - @texts.each_with_index do |text, index| - text.y = (text.size*index)+@options[:y] - end - end - - def calculate_boundry - @width = 0 - @height= 0 - @texts.each {|t| @width = t.width if t.width > @width} - @texts.each {|t| @height+=t.height} - end -end diff --git a/lib/objects/text.rb b/lib/objects/text.rb deleted file mode 100644 index e5ad38c..0000000 --- a/lib/objects/text.rb +++ /dev/null @@ -1,96 +0,0 @@ -class Text - include IMICFPS::CommonMethods - CACHE = {} - - attr_accessor :text, :x, :y, :z, :size, :factor_x, :factor_y, :color, :shadow, :shadow_size, :options - attr_reader :textobject - - def initialize(text, options={}) - @text = text || "" - @options = options - @size = options[:size] || 18 - @font = options[:font] || "Consolas" - - @x = options[:x] || 0 - @y = options[:y] || 0 - @z = options[:z] || 1025 - @factor_x = options[:factor_x] || 1 - @factor_y = options[:factor_y] || 1 - @color = options[:color] || Gosu::Color::WHITE - @alignment= options[:alignment] || nil - @shadow = true if options[:shadow] == true - @shadow = false if options[:shadow] == false - @shadow = true if options[:shadow] == nil - @shadow_size = options[:shadow_size] ? options[:shadow_size] : 1 - @shadow_alpha= options[:shadow_alpha] ? options[:shadow_alpha] : 30 - @textobject = check_cache(@size, @font) - - if @alignment - case @alignment - when :left - @x = 0+BUTTON_PADDING - when :center - @x = (window.width/2)-(@textobject.text_width(@text)/2) - when :right - @x = window.width-BUTTON_PADDING-@textobject.text_width(@text) - end - end - - return self - end - - def check_cache(size, font_name) - available = false - font = nil - - if CACHE[size] - if CACHE[size][font_name] - font = CACHE[size][font_name] - available = true - else - available = false - end - else - available = false - end - - unless available - font = Gosu::Font.new(@size, name: @font, bold: true) - CACHE[@size] = {} unless CACHE[@size].is_a?(Hash) - CACHE[@size][@font] = font - end - - return font - end - - def width - textobject.text_width(@text) - end - - def height - textobject.height - end - - def draw - if @shadow && !ARGV.join.include?("--no-shadow") - # _color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, @shadow_alpha) if @shadow_alpha <= @color.alpha - # _color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, @color.alpha) unless @shadow_alpha <= @color.alpha - _color = Gosu::Color::BLACK - @textobject.draw_text(@text, @x-@shadow_size, @y, @z, @factor_x, @factor_y, _color) - @textobject.draw_text(@text, @x-@shadow_size, @y-@shadow_size, @z, @factor_x, @factor_y, _color) - - @textobject.draw_text(@text, @x, @y-@shadow_size, @z, @factor_x, @factor_y, _color) - @textobject.draw_text(@text, @x+@shadow_size, @y-@shadow_size, @z, @factor_x, @factor_y, _color) - - @textobject.draw_text(@text, @x, @y+@shadow_size, @z, @factor_x, @factor_y, _color) - @textobject.draw_text(@text, @x-@shadow_size, @y+@shadow_size, @z, @factor_x, @factor_y, _color) - - @textobject.draw_text(@text, @x+@shadow_size, @y, @z, @factor_x, @factor_y, _color) - @textobject.draw_text(@text, @x+@shadow_size, @y+@shadow_size, @z, @factor_x, @factor_y, _color) - end - - @textobject.draw_markup(@text, @x, @y, @z, @factor_x, @factor_y, @color) - end - - def update; end -end diff --git a/lib/renderer/shader.rb b/lib/renderer/shader.rb deleted file mode 100644 index a135361..0000000 --- a/lib/renderer/shader.rb +++ /dev/null @@ -1,127 +0,0 @@ -class IMICFPS - # Ref: https://github.com/vaiorabbit/ruby-opengl/blob/master/sample/OrangeBook/brick.rb - class Shader - include OpenGL - - def initialize(name:, vertex_file:, fragment_file:) - @name = name - @vertex_file = vertex_file - @fragment_file = fragment_file - @compiled = false - - @error_buffer_size = 1024 - @variable_missing = {} - - create_shaders - compile_shaders - - # Only add shader to ShaderManager if it successfully compiles - if @compiled - ShaderManager.add_shader(@name, self) - else - puts "FAILED to compile shader: #{@name}", "" - end - end - - def shader_files_exist? - File.exists?(@vertex_file) && File.exists?(@fragment_file) - end - - def create_shaders - return unless shader_files_exist? - - @vertex = glCreateShader(GL_VERTEX_SHADER) - @fragment = glCreateShader(GL_FRAGMENT_SHADER) - - source = [File.read(@vertex_file)].pack('p') - size = [File.size(@vertex_file)].pack('I') - glShaderSource(@vertex, 1, source, size) - - source = [File.read(@fragment_file)].pack('p') - size = [File.size(@fragment_file)].pack('I') - glShaderSource(@fragment, 1, source, size) - end - - def compile_shaders - return unless shader_files_exist? - - glCompileShader(@vertex) - buffer = ' ' - glGetShaderiv(@vertex, GL_COMPILE_STATUS, buffer) - compiled = buffer.unpack('L')[0] - - if compiled == 0 - log = ' ' * @error_buffer_size - glGetShaderInfoLog(@vertex, @error_buffer_size, nil, log) - puts "Shader Error: Program \"#{@name}\"" - puts " Vector Shader InfoLog:", " #{log.strip.split("\n").join("\n ")}\n\n" - puts " Shader Compiled status: #{compiled}" - puts " NOTE: assignment of uniforms in shaders is illegal!" - puts - return - end - - glCompileShader(@fragment) - buffer = ' ' - glGetShaderiv(@fragment, GL_COMPILE_STATUS, buffer) - compiled = buffer.unpack('L')[0] - - if compiled == 0 - log = ' ' * @error_buffer_size - glGetShaderInfoLog(@fragment, @error_buffer_size, nil, log) - puts "Shader Error: Program \"#{@name}\"" - puts " Fragment Shader InfoLog:", " #{log.strip.split("\n").join("\n ")}\n\n" - puts " Shader Compiled status: #{compiled}" - puts " NOTE: assignment of uniforms in shader is illegal!" - puts - return - end - - @program = glCreateProgram - glAttachShader(@program, @vertex) - glAttachShader(@program, @fragment) - glLinkProgram(@program) - - buffer = ' ' - glGetProgramiv(@program, GL_LINK_STATUS, buffer) - linked = buffer.unpack('L')[0] - - if linked == 0 - log = ' ' * @error_buffer_size - glGetProgramInfoLog(@program, @error_buffer_size, nil, log) - puts "Shader Error: Program \"#{@name}\"" - puts " Program InfoLog:", " #{log.strip.split("\n").join("\n ")}\n\n" - end - - @compiled = linked == 0 ? false : true - end - - # Returns the location of a uniform variable - def variable(variable) - loc = glGetUniformLocation(@program, variable) - if (loc == -1) - puts "Shader Error: Program \"#{@name}\" has no such uniform named \"#{variable}\"", " Is it used in the shader? GLSL may have optimized it out.", " Is it miss spelled?" unless @variable_missing[variable] - @variable_missing[variable] = true - end - return loc - end - - def use(&block) - return unless compiled? - glUseProgram(@program) - - if block - block.call(self) - stop - end - end - - def stop - glUseProgram(0) - end - - def compiled? - @compiled - end - end -end \ No newline at end of file diff --git a/lib/states/game_state.rb b/lib/states/game_state.rb index 3ff2338..91e1556 100644 --- a/lib/states/game_state.rb +++ b/lib/states/game_state.rb @@ -1,36 +1,16 @@ class IMICFPS - class GameState + class GameState < CyberarmEngine::GameState include CommonMethods include EntityManager include LightManager attr_reader :options def initialize(options = {}) - @options = options @delta_time = Gosu.milliseconds @entities = [] @lights = [] - setup - end - - def push_game_state(klass_or_instance) - window.push_game_state(klass_or_instance) - end - - def setup - end - - def draw - end - - def update - end - - def button_down(id) - end - - def button_up(id) + super end end end \ No newline at end of file diff --git a/lib/states/game_states/loading_state.rb b/lib/states/game_states/loading_state.rb index 0fdccdc..ae538d4 100644 --- a/lib/states/game_states/loading_state.rb +++ b/lib/states/game_states/loading_state.rb @@ -16,7 +16,7 @@ class IMICFPS add_asset(:model, "base", "biped") # Currently broken - # Shader.new(name: "lighting", vertex_file: "shaders/vertex/lighting.glsl", fragment_file: "shaders/fragment/lighting.glsl") + Shader.new(name: "lighting", vertex: "shaders/vertex/lighting.glsl", fragment: "shaders/fragment/lighting.glsl") @act = false @cycled = false @@ -59,7 +59,7 @@ class IMICFPS unless @asset_index < @assets.count if @act && Gosu.milliseconds-@completed_for_ms > 250 - push_game_state(@options[:forward]) + push_state(@options[:forward]) else @act = true @completed_for_ms = Gosu.milliseconds unless @lock diff --git a/lib/states/menu.rb b/lib/states/menu.rb index f0cfdf3..6a6b4f5 100644 --- a/lib/states/menu.rb +++ b/lib/states/menu.rb @@ -92,11 +92,8 @@ class IMICFPS end def mouse_over?(object) - if mouse_x.between?(object.x, object.x+object.width) - if mouse_y.between?(object.y, object.y+object.height) - true - end - end + mouse_x.between?(object.x, object.x+object.width) && + mouse_y.between?(object.y, object.y+object.height) end class Link @@ -105,14 +102,17 @@ class IMICFPS @text, @host, @block = text, host, block @color = @text.color @hover_color = Gosu::Color.rgb(64, 127, 255) + @text.shadow_alpha = 100 end def update if @host.mouse_over?(self) @text.color = @hover_color - @text.shadow_size = 2 + @text.shadow_color= Gosu::Color::BLACK + @text.shadow_size = 3 else @text.color = @color + @text.shadow_color = nil @text.shadow_size = 1 end end diff --git a/lib/states/menus/main_menu.rb b/lib/states/menus/main_menu.rb index 8c58735..4fb942e 100644 --- a/lib/states/menus/main_menu.rb +++ b/lib/states/menus/main_menu.rb @@ -3,7 +3,7 @@ class IMICFPS def setup title "I-MIC FPS" link "Single Player" do - push_game_state(LoadingState.new(forward: Game)) + push_state(LoadingState.new(forward: Game)) end link "Settings" do # push_game_state(SettingsMenu) diff --git a/lib/window.rb b/lib/window.rb index 7718f8d..6848b5b 100644 --- a/lib/window.rb +++ b/lib/window.rb @@ -1,51 +1,21 @@ class IMICFPS GRAVITY = 9.8 # m/s - class Window < Gosu::Window + class Window < CyberarmEngine::Engine attr_accessor :number_of_vertices, :needs_cursor attr_reader :camera def initialize(window_width = 1280, window_height = 800, fullscreen = false) fps_target = (ARGV.first.to_i != 0) ? ARGV.first.to_i : 60 if ARGV.join.include?("--native") - super(Gosu.screen_width, Gosu.screen_height, fullscreen: true, resizable: true, update_interval: 1000.0/fps_target) + super(width: Gosu.screen_width, height: Gosu.screen_height, fullscreen: true, resizable: true, update_interval: 1000.0/fps_target) else - super(window_width, window_height, fullscreen: fullscreen, resizable: true, update_interval: 1000.0/fps_target) + super(width: window_width, height: window_height, fullscreen: fullscreen, resizable: true, update_interval: 1000.0/fps_target) end $window = self @needs_cursor = false @number_of_vertices = 0 - @active_state = nil - - push_game_state(MainMenu) - end - - def push_game_state(klass_or_instance) - if klass_or_instance.respond_to?(:draw) - @active_state = klass_or_instance - else - @active_state = klass_or_instance.new - end - end - - def needs_cursor? - @needs_cursor - end - - def draw - @active_state.draw if @active_state - end - - def update - @active_state.update if @active_state - end - - def button_down(id) - @active_state.button_down(id) if @active_state - end - - def button_up(id) - @active_state.button_up(id) if @active_state + push_state(MainMenu) end end end