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

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

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