From 473a597b668bb1935c397d20c41c0488b8e1802c Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Mon, 23 Apr 2018 21:13:16 -0500 Subject: [PATCH] Bunch of stuff --- i-mic-fps.rb | 4 ++ lib/managers/network_manager.rb | 0 lib/objects/multi_line_text.rb | 64 ++++++++++++++++++++++ lib/objects/player.rb | 4 +- lib/objects/terrain.rb | 16 +++--- lib/objects/text.rb | 95 +++++++++++++++++++++++++++++++++ lib/ui/menu/menu.rb | 43 +++++++++++++++ lib/wavefront/model.rb | 8 +-- lib/window.rb | 41 +++++++------- 9 files changed, 244 insertions(+), 31 deletions(-) create mode 100644 lib/managers/network_manager.rb create mode 100644 lib/objects/multi_line_text.rb create mode 100644 lib/objects/text.rb create mode 100644 lib/ui/menu/menu.rb diff --git a/i-mic-fps.rb b/i-mic-fps.rb index 9bd1532..8d61862 100644 --- a/i-mic-fps.rb +++ b/i-mic-fps.rb @@ -2,6 +2,8 @@ require "opengl" require "glu" require "gosu" +Dir.chdir(File.dirname(__FILE__)) + case OpenGL.get_platform when :OPENGL_PLATFORM_WINDOWS OpenGL.load_lib("opengl32.dll", "C:/Windows/System32") @@ -24,6 +26,8 @@ require_relative "lib/common_methods" require_relative "lib/managers/object_manager" require_relative "lib/managers/light_manager" +require_relative "lib/objects/text" +require_relative "lib/objects/multi_line_text" require_relative "lib/objects/game_object" require_relative "lib/objects/light" require_relative "lib/objects/camera" diff --git a/lib/managers/network_manager.rb b/lib/managers/network_manager.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/objects/multi_line_text.rb b/lib/objects/multi_line_text.rb new file mode 100644 index 0000000..eae4c49 --- /dev/null +++ b/lib/objects/multi_line_text.rb @@ -0,0 +1,64 @@ +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 + _options[:y]+=_options[:size] + @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 + 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/player.rb b/lib/objects/player.rb index 25e90a1..945adc6 100644 --- a/lib/objects/player.rb +++ b/lib/objects/player.rb @@ -1,3 +1,5 @@ +require "etc" + class IMICFPS class Player < GameObject @@ -11,7 +13,7 @@ class IMICFPS @first_person_view = true @devisor = 500.0 - @name_image = Gosu::Image.from_text("Player", 100, font: "Consolas", align: :center) + @name_image = Gosu::Image.from_text("#{Etc.getlogin}", 100, font: "Consolas", align: :center) # @name_image.save("temp.png") p @name_image.width/@devisor p @name_image.height/@devisor diff --git a/lib/objects/terrain.rb b/lib/objects/terrain.rb index 3ee8630..1431149 100644 --- a/lib/objects/terrain.rb +++ b/lib/objects/terrain.rb @@ -18,17 +18,20 @@ class IMICFPS end def generate + #@width.times do |x| + # @length.times do |z| + # # TRIANGLE STRIP (BROKEN) + # @map << Vertex.new((x+1)-@width.to_f/2, 0, z-@legth.to_f/2) + # @map << Vertex.new(x-@width.to_f/2, 0, (z+1)-@length.to_f/2) + # end + #end @width.times do |x| @length.times do |z| - # TRIANGLE STRIP (BROKEN) - # @map << Vertex.new((x+1)-@width.to_f/2, 0, z-@legth.to_f/2) - # @map << Vertex.new(x-@width.to_f/2, 0, (z+1)-@length.to_f/2) - # WORKING TRIANGLES @map << Vertex.new(x-@width.to_f/2, @height, z-@length.to_f/2) @map << Vertex.new((x+1)-@width.to_f/2, @height, z-@length.to_f/2) @map << Vertex.new(x-@width.to_f/2, @height, (z+1)-@length.to_f/2) - + # @map << Vertex.new(x-@width.to_f/2, @height, (z+1)-@length.to_f/2) @map << Vertex.new((x+1)-@width.to_f/2, @height, z-@length.to_f/2) @map << Vertex.new((x+1)-@width.to_f/2, @height, (z+1)-@length.to_f/2) @@ -95,11 +98,10 @@ class IMICFPS glColorPointer(3, GL_FLOAT, 0, @colors_packed) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) - # glDrawArrays(GL_TRIANGLE_STRIP, 0, @vertices.size/3) glDrawArrays(GL_TRIANGLES, 0, @vertices.size/3) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) + # glDrawArrays(GL_TRIANGLE_STRIP, 0, @vertices.size/3) - glDrawArrays(GL_TRIANGLES, 0, @vertices.size/3) $window.number_of_faces+=@vertices.size/3 glDisableClientState(GL_VERTEX_ARRAY) diff --git a/lib/objects/text.rb b/lib/objects/text.rb new file mode 100644 index 0000000..0488faf --- /dev/null +++ b/lib/objects/text.rb @@ -0,0 +1,95 @@ +class Text + 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) + 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, @x-@shadow_size, @y, @z, @factor_x, @factor_y, _color) + @textobject.draw(@text, @x-@shadow_size, @y-@shadow_size, @z, @factor_x, @factor_y, _color) + + @textobject.draw(@text, @x, @y-@shadow_size, @z, @factor_x, @factor_y, _color) + @textobject.draw(@text, @x+@shadow_size, @y-@shadow_size, @z, @factor_x, @factor_y, _color) + + @textobject.draw(@text, @x, @y+@shadow_size, @z, @factor_x, @factor_y, _color) + @textobject.draw(@text, @x-@shadow_size, @y+@shadow_size, @z, @factor_x, @factor_y, _color) + + @textobject.draw(@text, @x+@shadow_size, @y, @z, @factor_x, @factor_y, _color) + @textobject.draw(@text, @x+@shadow_size, @y+@shadow_size, @z, @factor_x, @factor_y, _color) + end + + @textobject.draw(@text, @x, @y, @z, @factor_x, @factor_y, @color) + end + + def update; end +end diff --git a/lib/ui/menu/menu.rb b/lib/ui/menu/menu.rb new file mode 100644 index 0000000..3e15260 --- /dev/null +++ b/lib/ui/menu/menu.rb @@ -0,0 +1,43 @@ +class Menu + def initialize + @elements = [] + setup + end + + def setup + end + + def draw + @elements.each(&:draw) + end + + def update + @elements.each(&:update) + end + + def button(text, x:, y:, &block) + @element << Button.new(text, x, y, block) + end + + def label(text, x:, y:) + @element << Text.new(text, x: x, y: y, size: 24) + end + + class Button + PADDING = 10 + def initialize(text, x, y, block) + @text = Text.new(text, x: x, y: y) + end + + def draw + Gosu.draw_rect(x-PADDING, y-PADDING, @text.width+PADDING, @text.height+PADDING, Gosu::Color.rgb(0, 100, 0)) + @text.draw + end + + def update + end + + def mouse_over? + end + end +end diff --git a/lib/wavefront/model.rb b/lib/wavefront/model.rb index 9d14355..3408a32 100644 --- a/lib/wavefront/model.rb +++ b/lib/wavefront/model.rb @@ -74,11 +74,13 @@ class IMICFPS if $debug glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) + #glPolygonOffset(1, 0) glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices_size/4) + #glPolygonOffset(0, 0) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) - else - glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices_size/4) - end + end + + glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices_size/4) glDisableClientState(GL_VERTEX_ARRAY) glDisableClientState(GL_COLOR_ARRAY) diff --git a/lib/window.rb b/lib/window.rb index 2c75a06..bbff6f7 100644 --- a/lib/window.rb +++ b/lib/window.rb @@ -37,8 +37,8 @@ class IMICFPS @crosshair_thickness = 3 @crosshair_color = Gosu::Color.rgb(255,127,0) - @font = Gosu::Font.new(18, name: "DejaVu Sans") - @text = "Hello There" + # @font = Gosu::Font.new(18, name: "DejaVu Sans") + @text = MultiLineText.new("Pending...", x: 10, y: 10, z: 1, size: 18, font: "DejaVu Sans") Light.new(x: 3, y: -6, z: 6) Light.new(x: 0, y: 100, z: 0, diffuse: Color.new(1.0, 0.5, 0.1)) @@ -73,28 +73,29 @@ class IMICFPS draw_rect(width/2-@crosshair_size, (height/2-@crosshair_size)-@crosshair_thickness/2, @crosshair_size*2, @crosshair_thickness, @crosshair_color, 0, :default) draw_rect((width/2)-@crosshair_thickness/2, height/2-(@crosshair_size*2), @crosshair_thickness, @crosshair_size*2, @crosshair_color, 0, :default) - @text.split("~").each_with_index do |bit, i| - @font.draw(bit.strip, 10, @font.height*i, Float::INFINITY) - end + @text.draw end def update @last_frame_time = Gosu.milliseconds - @text = "OpenGL Vendor: #{glGetString(GL_VENDOR)}~ - OpenGL Renderer: #{glGetString(GL_RENDERER)} ~ - OpenGL Version: #{glGetString(GL_VERSION)}~ - OpenGL Shader Language Version: #{glGetString(GL_SHADING_LANGUAGE_VERSION)}~ - ~ - Camera pitch: #{@camera.pitch.round(2)} Yaw: #{@camera.yaw.round(2)} Roll #{@camera.roll.round(2)} ~ - Camera X:#{@camera.x.round(2)} Y:#{@camera.y.round(2)} Z:#{@camera.z.round(2)} ~ - #{if @camera.game_object then "Actor X:#{@camera.game_object.x.round(2)} Y:#{@camera.game_object.y.round(2)} Z:#{@camera.game_object.z.round(2)}";end} ~ - Field Of View: #{@camera.field_of_view} ~ - Mouse Sesitivity: #{@camera.mouse_sensitivity} ~ - Faces: #{@number_of_faces} ~ - Last Frame: #{delta_time}ms (#{Gosu.fps} fps)~ - ~ - Draw Skydome: #{@draw_skydome}~ - Debug mode: #{$debug}~" + string = <<-eos +OpenGL Vendor: #{glGetString(GL_VENDOR)} +OpenGL Renderer: #{glGetString(GL_RENDERER)} +OpenGL Version: #{glGetString(GL_VERSION)} +OpenGL Shader Language Version: #{glGetString(GL_SHADING_LANGUAGE_VERSION)} + +Camera pitch: #{@camera.pitch.round(2)} Yaw: #{@camera.yaw.round(2)} Roll #{@camera.roll.round(2)} +Camera X:#{@camera.x.round(2)} Y:#{@camera.y.round(2)} Z:#{@camera.z.round(2)} +#{if @camera.game_object then "Actor X:#{@camera.game_object.x.round(2)} Y:#{@camera.game_object.y.round(2)} Z:#{@camera.game_object.z.round(2)}";end} +Field Of View: #{@camera.field_of_view} +Mouse Sesitivity: #{@camera.mouse_sensitivity} +Faces: #{@number_of_faces} +Last Frame: #{delta_time}ms (#{Gosu.fps} fps) + +Draw Skydome: #{@draw_skydome} +Debug mode: #{$debug} +eos + @text.text = string ObjectManager.objects.each do |object| object.update