Bunch of stuff

This commit is contained in:
2018-04-23 21:13:16 -05:00
parent bc8c78c90b
commit 473a597b66
9 changed files with 244 additions and 31 deletions

View File

@@ -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"

View File

View File

@@ -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

View File

@@ -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

View File

@@ -18,17 +18,20 @@ class IMICFPS
end
def generate
@width.times do |x|
@length.times do |z|
# TRIANGLE STRIP (BROKEN)
#@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|
# 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)

95
lib/objects/text.rb Normal file
View File

@@ -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

43
lib/ui/menu/menu.rb Normal file
View File

@@ -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

View File

@@ -74,12 +74,14 @@ 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
glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices_size/4)
glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_COLOR_ARRAY)
glDisableClientState(GL_NORMAL_ARRAY)

View File

@@ -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: <c=992200>#{$debug}</b>~"
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: <c=992200>#{$debug}</b>
eos
@text.text = string
ObjectManager.objects.each do |object|
object.update