mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-16 13:12:34 +00:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 002a01abfc | |||
| 6f7bac3880 | |||
| 993e59aa94 | |||
| e30ed7c6be | |||
| a172c24d97 | |||
| 003715f685 | |||
| 1458736f7d | |||
| a2e0c07c6a | |||
| 3ead2f5daf | |||
| 8f3d9ff193 |
@@ -9,6 +9,8 @@ require_relative "cyberarm_engine/engine"
|
||||
|
||||
require_relative "cyberarm_engine/lib/bounding_box"
|
||||
require_relative "cyberarm_engine/lib/vector"
|
||||
require_relative "cyberarm_engine/lib/ray"
|
||||
require_relative "cyberarm_engine/lib/shader" if defined?(OpenGL)
|
||||
require_relative "cyberarm_engine/background"
|
||||
|
||||
require_relative "cyberarm_engine/objects/text"
|
||||
|
||||
@@ -5,7 +5,7 @@ module CyberarmEngine
|
||||
SONGS = {}
|
||||
|
||||
attr_accessor :show_cursor
|
||||
attr_reader :current_state, :last_frame_time
|
||||
attr_reader :last_frame_time
|
||||
|
||||
def self.now
|
||||
Gosu.milliseconds
|
||||
|
||||
@@ -3,7 +3,8 @@ module CyberarmEngine
|
||||
include Common
|
||||
|
||||
attr_accessor :image, :angle, :position, :velocity, :center_x, :center_y, :scale_x, :scale_y,
|
||||
:color, :alpha, :mode, :options, :paused, :radius, :last_position
|
||||
:color, :mode, :options, :paused, :radius, :last_position
|
||||
attr_reader :alpha
|
||||
def initialize(options={})
|
||||
if options[:auto_manage] || options[:auto_manage] == nil
|
||||
$window.current_state.add_game_object(self)
|
||||
@@ -141,7 +142,7 @@ module CyberarmEngine
|
||||
self.angle%=360
|
||||
end
|
||||
|
||||
def alpha=int # 0-255
|
||||
def alpha=(int) # 0-255
|
||||
@alpha = int
|
||||
@alpha = 255 if @alpha > 255
|
||||
@color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, int)
|
||||
|
||||
@@ -49,11 +49,17 @@ module CyberarmEngine
|
||||
return temp
|
||||
end
|
||||
|
||||
# returns whether both bounding boxes intersect
|
||||
# returns whether bounding box intersects other
|
||||
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)
|
||||
if other.is_a?(Ray)
|
||||
other.intersect?(self)
|
||||
elsif other.is_a?(BoundingBox)
|
||||
(@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)
|
||||
else
|
||||
raise "Unknown collider: #{other.class}"
|
||||
end
|
||||
end
|
||||
|
||||
# does this bounding box envelop other bounding box? (inclusive of border)
|
||||
|
||||
55
lib/cyberarm_engine/lib/ray.rb
Normal file
55
lib/cyberarm_engine/lib/ray.rb
Normal file
@@ -0,0 +1,55 @@
|
||||
module CyberarmEngine
|
||||
class Ray
|
||||
def initialize(origin, direction)
|
||||
raise "Origin must be a Vector!" unless origin.is_a?(Vector)
|
||||
raise "Direction must be a Vector!" unless direction.is_a?(Vector)
|
||||
|
||||
@origin = origin
|
||||
@direction = direction
|
||||
|
||||
@inverse_direction = @direction.inverse
|
||||
end
|
||||
|
||||
def intersect?(intersectable)
|
||||
if intersectable.is_a?(BoundingBox)
|
||||
intersect_bounding_box?(intersectable)
|
||||
else
|
||||
raise NotImplementedError, "Ray intersection test for #{intersectable.class} not implemented."
|
||||
end
|
||||
end
|
||||
|
||||
# Based on: https://tavianator.com/fast-branchless-raybounding-box-intersections/
|
||||
def intersect_bounding_box?(box)
|
||||
tmin = -Float::INFINITY
|
||||
tmax = Float::INFINITY
|
||||
|
||||
tx1 = (box.min.x - @origin.x) * @inverse_direction.x
|
||||
tx2 = (box.max.x - @origin.x) * @inverse_direction.x
|
||||
|
||||
tmin = max(tmin, min(tx1, tx2))
|
||||
tmax = min(tmax, max(tx1, tx2))
|
||||
|
||||
ty1 = (box.min.y - @origin.y) * @inverse_direction.y
|
||||
ty2 = (box.max.y - @origin.y) * @inverse_direction.y
|
||||
|
||||
tmin = max(tmin, min(ty1, ty2))
|
||||
tmax = min(tmax, max(ty1, ty2))
|
||||
|
||||
tz1 = (box.min.z - @origin.z) * @inverse_direction.z
|
||||
tz2 = (box.max.z - @origin.z) * @inverse_direction.z
|
||||
|
||||
tmin = max(tmin, min(tz1, tz2))
|
||||
tmax = min(tmax, max(tz1, tz2))
|
||||
|
||||
return tmax >= max(tmin, 0.0);
|
||||
end
|
||||
|
||||
def min(x, y)
|
||||
((x) < (y) ? (x) : (y))
|
||||
end
|
||||
|
||||
def max(x, y)
|
||||
((x) > (y) ? (x) : (y))
|
||||
end
|
||||
end
|
||||
end
|
||||
205
lib/cyberarm_engine/lib/shader.rb
Normal file
205
lib/cyberarm_engine/lib/shader.rb
Normal file
@@ -0,0 +1,205 @@
|
||||
module CyberarmEngine
|
||||
# Ref: https://github.com/vaiorabbit/ruby-opengl/blob/master/sample/OrangeBook/brick.rb
|
||||
class Shader
|
||||
include OpenGL
|
||||
@@shaders = {}
|
||||
|
||||
def self.add(name, instance)
|
||||
@@shaders[name] = instance
|
||||
end
|
||||
|
||||
def self.use(name, &block)
|
||||
shader = @@shaders.dig(name)
|
||||
if shader
|
||||
shader.use(&block)
|
||||
else
|
||||
raise ArgumentError, "Shader '#{name}' not found!"
|
||||
end
|
||||
end
|
||||
|
||||
def self.available?(name)
|
||||
@@shaders.dig(name).is_a?(Shader)
|
||||
end
|
||||
|
||||
def self.get(name)
|
||||
@@shaders.dig(name)
|
||||
end
|
||||
|
||||
def self.active_shader
|
||||
@active_shader
|
||||
end
|
||||
|
||||
def self.active_shader=(instance)
|
||||
@active_shader = instance
|
||||
end
|
||||
|
||||
def self.stop
|
||||
shader = Shader.active_shader
|
||||
|
||||
if shader
|
||||
shader.stop
|
||||
else
|
||||
raise ArgumentError, "No active shader to stop!"
|
||||
end
|
||||
end
|
||||
|
||||
def self.attribute_location(variable)
|
||||
raise RuntimeError, "No active shader!" unless Shader.active_shader
|
||||
Shader.active_shader.attribute_location(variable)
|
||||
end
|
||||
|
||||
def self.set_uniform(variable, value)
|
||||
raise RuntimeError, "No active shader!" unless Shader.active_shader
|
||||
Shader.active_shader.set_uniform(variable, value)
|
||||
end
|
||||
|
||||
attr_reader :name, :program
|
||||
def initialize(name:, vertex: "shaders/default.vert", fragment:)
|
||||
@name = name
|
||||
@vertex_file = vertex
|
||||
@fragment_file = fragment
|
||||
@compiled = false
|
||||
|
||||
@program = nil
|
||||
|
||||
@error_buffer_size = 1024
|
||||
@variable_missing = {}
|
||||
|
||||
raise ArgumentError, "Shader files not found: #{@vertex_file} or #{@fragment_file}" unless shader_files_exist?
|
||||
|
||||
create_shaders
|
||||
compile_shaders
|
||||
|
||||
# Only add shader if it successfully compiles
|
||||
if @compiled
|
||||
Shader.add(@name, self)
|
||||
else
|
||||
puts "FAILED to compile shader: #{@name}", ""
|
||||
end
|
||||
end
|
||||
|
||||
def shader_files_exist?
|
||||
File.exist?(@vertex_file) && File.exist?(@fragment_file)
|
||||
end
|
||||
|
||||
def create_shaders
|
||||
@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 " Vectex 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?
|
||||
raise "Another shader is already in use! #{Shader.active_shader.name.inspect}" if Shader.active_shader
|
||||
Shader.active_shader=self
|
||||
|
||||
glUseProgram(@program)
|
||||
|
||||
if block
|
||||
block.call(self)
|
||||
stop
|
||||
end
|
||||
end
|
||||
|
||||
def stop
|
||||
Shader.active_shader = nil if Shader.active_shader == self
|
||||
glUseProgram(0)
|
||||
end
|
||||
|
||||
def compiled?
|
||||
@compiled
|
||||
end
|
||||
|
||||
def attribute_location(variable)
|
||||
glGetUniformLocation(@program, variable)
|
||||
end
|
||||
|
||||
def set_uniform(variable, value, location = nil)
|
||||
attr_loc = location ? location : attribute_location(variable)
|
||||
|
||||
case value.class.to_s.downcase.to_sym
|
||||
when :integer
|
||||
glUniform1i(attr_loc, value)
|
||||
when :float
|
||||
glUniform1f(attr_loc, value)
|
||||
when :string
|
||||
when :array
|
||||
else
|
||||
raise NotImplementedError, "Shader support for #{value.class.inspect} not implemented."
|
||||
end
|
||||
|
||||
Window.handle_gl_error
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -2,8 +2,8 @@ module CyberarmEngine
|
||||
class Text
|
||||
CACHE = {}
|
||||
|
||||
attr_accessor :x, :y, :z, :size, :factor_x, :factor_y, :color, :shadow, :shadow_size, :options
|
||||
attr_reader :text, :textobject
|
||||
attr_accessor :x, :y, :z, :size, :options
|
||||
attr_reader :text, :textobject, :factor_x, :factor_y, :color, :shadow, :shadow_size, :shadow_alpha, :shadow_color
|
||||
|
||||
def initialize(text, options={})
|
||||
@text = text.to_s || ""
|
||||
@@ -22,6 +22,8 @@ module CyberarmEngine
|
||||
@shadow = true if options[:shadow] == nil
|
||||
@shadow_size = options[:shadow_size] ? options[:shadow_size] : 1
|
||||
@shadow_alpha= options[:shadow_alpha] ? options[:shadow_alpha] : 30
|
||||
@shadow_alpha= options[:shadow_alpha] ? options[:shadow_alpha] : 30
|
||||
@shadow_color= options[:shadow_color]
|
||||
@textobject = check_cache(@size, @font)
|
||||
|
||||
if @alignment
|
||||
@@ -67,6 +69,35 @@ module CyberarmEngine
|
||||
@text = string
|
||||
end
|
||||
|
||||
def factor_x=(n)
|
||||
@rendered_shadow = nil
|
||||
@factor_x = n
|
||||
end
|
||||
def factor_y=(n)
|
||||
@rendered_shadow = nil
|
||||
@factor_y = n
|
||||
end
|
||||
def color=(color)
|
||||
@rendered_shadow = nil
|
||||
@color = color
|
||||
end
|
||||
def shadow=(boolean)
|
||||
@rendered_shadow = nil
|
||||
@shadow = boolean
|
||||
end
|
||||
def shadow_size=(n)
|
||||
@rendered_shadow = nil
|
||||
@shadow_size = n
|
||||
end
|
||||
def shadow_alpha=(n)
|
||||
@rendered_shadow = nil
|
||||
@shadow_alpha = n
|
||||
end
|
||||
def shadow_color=(n)
|
||||
@rendered_shadow = nil
|
||||
@shadow_color = n
|
||||
end
|
||||
|
||||
def width
|
||||
textobject.text_width(@text)
|
||||
end
|
||||
@@ -77,9 +108,8 @@ module CyberarmEngine
|
||||
|
||||
def draw
|
||||
if @shadow && !ARGV.join.include?("--no-shadow")
|
||||
@shadow_alpha = 30 if @color.alpha > 30
|
||||
@shadow_alpha = @color.alpha if @color.alpha <= 30
|
||||
shadow_color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, @shadow_alpha)
|
||||
shadow_alpha = @color.alpha <= 30 ? @color.alpha : @shadow_alpha
|
||||
shadow_color = @shadow_color ? @shadow_color : Gosu::Color.rgba(@color.red, @color.green, @color.blue, shadow_alpha)
|
||||
|
||||
_x = @shadow_size
|
||||
_y = @shadow_size
|
||||
|
||||
@@ -162,7 +162,7 @@ module CyberarmEngine
|
||||
|
||||
def width
|
||||
if visible?
|
||||
(@style.border_thickness_left + @style.padding_left) + @width + (@style.padding_right + @style.border_thickness_right)
|
||||
inner_width + @width
|
||||
else
|
||||
0
|
||||
end
|
||||
@@ -172,9 +172,13 @@ module CyberarmEngine
|
||||
@style.margin_left + width + @style.margin_right
|
||||
end
|
||||
|
||||
def inner_width
|
||||
(@style.border_thickness_left + @style.padding_left) + (@style.padding_right + @style.border_thickness_right)
|
||||
end
|
||||
|
||||
def height
|
||||
if visible?
|
||||
(@style.border_thickness_top + @style.padding_top) + @height + (@style.padding_bottom + @style.border_thickness_bottom)
|
||||
inner_height + @height
|
||||
else
|
||||
0
|
||||
end
|
||||
@@ -184,6 +188,10 @@ module CyberarmEngine
|
||||
@style.margin_top + height + @style.margin_bottom
|
||||
end
|
||||
|
||||
def inner_height
|
||||
(@style.border_thickness_top + @style.padding_top) + (@style.padding_bottom + @style.border_thickness_bottom)
|
||||
end
|
||||
|
||||
private def dimensional_size(size, dimension)
|
||||
raise "dimension must be either :width or :height" unless dimension == :width || dimension == :height
|
||||
if size && size.is_a?(Numeric)
|
||||
|
||||
@@ -43,6 +43,9 @@ module CyberarmEngine
|
||||
def left_mouse_button(sender, x, y)
|
||||
super
|
||||
window.text_input = @text_input
|
||||
|
||||
@caret_last_interval = Gosu.milliseconds
|
||||
@show_caret = true
|
||||
end
|
||||
|
||||
def enter(sender)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
module CyberarmEngine
|
||||
NAME = "InDev"
|
||||
VERSION = "0.9.0"
|
||||
VERSION = "0.10.2"
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user