17 Commits

Author SHA1 Message Date
8ce594753e Bumped version 2019-10-01 14:58:00 -05:00
ae44083cf2 Fixed default theme border to thick, fixed BoundingBox#point? order dependent 2019-10-01 14:57:17 -05:00
b25f76e124 Bumped version 2019-10-01 11:48:43 -05:00
8f9e671340 Added Vector#lerp 2019-10-01 11:48:22 -05:00
bb482f4463 Sync 2019-09-26 12:41:34 -05:00
788d987da1 Elements now return :handled when they're supposed to, GameState initialization and setup are now seperated 2019-09-12 08:15:02 -05:00
6fafe98008 Removed 'require "gosu"' from background 2019-09-12 08:03:27 -05:00
002a01abfc Bump version 2019-08-13 09:52:24 -05:00
6f7bac3880 BoundingBox#intersect? now supports Ray 2019-08-13 09:51:53 -05:00
993e59aa94 Added Shader#get to retrieve instance of Shader if shader with name exists 2019-08-13 07:26:06 -05:00
e30ed7c6be Fixed a couple warnings 2019-08-11 09:38:09 -05:00
a172c24d97 Bump version 2019-08-10 09:42:29 -05:00
003715f685 Added Shader#available? method for checking if a shader useable 2019-08-10 09:42:00 -05:00
1458736f7d Bumped version 2019-08-08 21:22:31 -05:00
a2e0c07c6a Added Ray class 2019-08-07 23:03:40 -05:00
3ead2f5daf Added Shader handling class, made Text re-render text_shadow if anything affecting shadow is changed. 2019-08-07 12:02:22 -05:00
8f3d9ff193 added Element#inner_width and Element#inner_height methods, EditLine now resets caret blink cycle when clicked 2019-06-27 17:23:15 -05:00
21 changed files with 438 additions and 57 deletions

View File

@@ -1,4 +1,10 @@
begin
require File.expand_path("../../ffi-gosu/lib/gosu", File.dirname(__FILE__))
rescue LoadError => e
pp e
require "gosu"
end
require_relative "cyberarm_engine/version"
@@ -9,6 +15,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"

View File

@@ -1,5 +1,3 @@
require "gosu"
module CyberarmEngine
class Background
attr_accessor :x, :y, :z, :width, :height, :angle, :debug

View File

@@ -1,35 +1,35 @@
module CyberarmEngine
module Common
def push_state(klass, options={})
$window.push_state(klass, options)
window.push_state(klass, options)
end
def current_state
$window.current_state
window.current_state
end
def previous_state
$window.previous_state
window.previous_state
end
def pop_state
$window.pop_state
window.pop_state
end
def show_cursor
$window.show_cursor
window.show_cursor
end
def show_cursor=boolean
$window.show_cursor = boolean
window.show_cursor = boolean
end
def draw_rect(x, y, width, height, color, z = 0)
$window.draw_rect(x,y,width,height,color,z)
Gosu.draw_rect(x, y, width, height, color, z)
end
def fill(color, z = 0)
draw_rect(0, 0, $window.width, $window.height, color, z)
draw_rect(0, 0, window.width, window.height, color, z)
end
def lighten(color, amount = 25)

View File

@@ -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
@@ -58,11 +58,15 @@ module CyberarmEngine
end
def push_state(klass, options={})
options = {setup: true}.merge(options)
if klass.instance_of?(klass.class) && defined?(klass.options)
@states << klass
klass.setup if options[:setup]
else
@states << klass.new(options) if child_of?(klass, GameState)
@states << klass.new if child_of?(klass, Element::Container)
current_state.setup if current_state.class == klass && options[:setup]
end
end

View File

@@ -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)
@@ -154,7 +155,7 @@ module CyberarmEngine
def button_up(id)
end
def button_down?(id)
def button_down(id)
end
def find_closest(game_object_class)

View File

@@ -12,8 +12,6 @@ module CyberarmEngine
$window.text_input = nil unless options[:preserve_text_input]
@down_keys = {}
setup
end
def setup

View File

@@ -49,11 +49,17 @@ module CyberarmEngine
return temp
end
# returns whether both bounding boxes intersect
# returns whether bounding box intersects other
def intersect?(other)
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)
@@ -62,11 +68,17 @@ module CyberarmEngine
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
# returns whether the 3D vector is inside of the bounding box
def inside?(vector)
(vector.x.between?(@min.x, @max.x) || vector.x.between?(@max.x, @min.x)) &&
(vector.y.between?(@min.y, @max.y) || vector.y.between?(@max.y, @min.y)) &&
(vector.z.between?(@min.z, @max.z) || vector.z.between?(@max.z, @min.z))
end
# returns whether the 2D 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)
(vector.x.between?(@min.x, @max.x) || vector.x.between?(@max.x, @min.x)) &&
(vector.y.between?(@min.y, @max.y) || vector.y.between?(@max.y, @min.y))
end
def volume
@@ -87,26 +99,26 @@ module CyberarmEngine
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.min.x = @min.x.to_f * entity.scale.x
temp.min.y = @min.y.to_f * entity.scale.y
temp.min.z = @min.z.to_f * entity.scale.z
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
temp.max.x = @max.x.to_f * entity.scale.x
temp.max.y = @max.y.to_f * entity.scale.y
temp.max.z = @max.z.to_f * entity.scale.z
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.min.x = @min.x.to_f * entity.scale.x + entity.position.x
temp.min.y = @min.y.to_f * entity.scale.y + entity.position.y
temp.min.z = @min.z.to_f * entity.scale.z + 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
temp.max.x = @max.x.to_f * entity.scale.x + entity.position.x
temp.max.y = @max.y.to_f * entity.scale.y + entity.position.y
temp.max.z = @max.z.to_f * entity.scale.z + entity.position.z
return temp
end

View File

@@ -0,0 +1,56 @@
module CyberarmEngine
class Ray
def initialize(origin, direction, range = Float::INFINITY)
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
@range = range
@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 = -@range
tmax = @range
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

View 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

View File

@@ -1,5 +1,29 @@
module CyberarmEngine
class Vector
def self.up
Vector.new(0, 1, 0)
end
def self.down
Vector.new(0, -1, 0)
end
def self.left
Vector.new(-1, 0, 0)
end
def self.right
Vector.new(1, 0, 0)
end
def self.forward
Vector.new(0, 0, 1)
end
def self.backward
Vector.new(0, 0, -1)
end
def initialize(x = 0, y = 0, z = 0, weight = 0)
@x, @y, @z, @weight = x, y, z, weight
end
@@ -138,6 +162,10 @@ module CyberarmEngine
@x + @y + @z
end
def lerp(other, factor)
(self - other) * factor.clamp(0.0, 1.0)
end
# 2D distance using X and Y
def distance(other)
Math.sqrt((@x-other.x)**2 + (@y-other.y)**2)

View File

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

View File

@@ -32,6 +32,9 @@ module CyberarmEngine
@style.width = default(:width) || nil
@style.height = default(:height) || nil
@style.background_canvas = Background.new
@style.border_canvas = BorderCanvas.new(element: self)
stylize
default_events
@@ -44,9 +47,6 @@ module CyberarmEngine
set_margin(@style.margin)
@style.background_canvas = Background.new
@style.border_canvas = BorderCanvas.new(element: self)
set_background(@style.background)
set_border_color(@style.border_color)
end
@@ -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)

View File

@@ -25,6 +25,8 @@ module CyberarmEngine
@style.background_canvas.background = default(:hover, :background)
@text.color = default(:hover, :color)
end
return :handled
end
def left_mouse_button(sender, x, y)
@@ -32,23 +34,33 @@ module CyberarmEngine
@style.background_canvas.background = default(:active, :background)
window.current_state.focus = self
@text.color = default(:active, :color)
return :handled
end
def released_left_mouse_button(sender,x, y)
enter(sender)
return :handled
end
def clicked_left_mouse_button(sender, x, y)
@block.call(self) if @block
return :handled
end
def leave(sender)
@style.background_canvas.background = default(:background)
@text.color = default(:color)
return :handled
end
def blur(sender)
@focus = false
return :handled
end
end
end

View File

@@ -67,8 +67,11 @@ module CyberarmEngine
@width = @style.width = window.width
@height = @style.height = window.height
else
@width, @height = 0, 0
_width = dimensional_size(@style.width, :width)
_height= dimensional_size(@style.height,:height)
@width = _width ? _width : (@children.map {|c| c.x + c.outer_width }.max || 0).round
@height = _height ? _height : (@children.map {|c| c.y + c.outer_height}.max || 0).round
end

View File

@@ -43,6 +43,11 @@ module CyberarmEngine
def left_mouse_button(sender, x, y)
super
window.text_input = @text_input
@caret_last_interval = Gosu.milliseconds
@show_caret = true
return :handled
end
def enter(sender)
@@ -53,12 +58,16 @@ module CyberarmEngine
@style.background_canvas.background = default(:hover, :background)
@text.color = default(:hover, :color)
end
return :handled
end
def leave(sender)
unless @focus
super
end
return :handled
end
def blur(sender)
@@ -66,6 +75,8 @@ module CyberarmEngine
@style.background_canvas.background = default(:background)
@text.color = default(:color)
window.text_input = nil
return :handled
end
# TODO: Fix caret rendering in wrong position unless caret_pos is at end of text

View File

@@ -19,6 +19,8 @@ module CyberarmEngine
def clicked_left_mouse_button(sender, x, y)
@block.call(self) if @block
return :handled
end
def recalculate

View File

@@ -13,11 +13,16 @@ module CyberarmEngine
def clicked_left_mouse_button(sender, x, y)
@block.call(self) if @block
return :handled
end
def recalculate
@width, @height = 0, 0
_width = dimensional_size(@style.width, :width)
_height= dimensional_size(@style.height,:height)
@width = _width ? _width : @text.width.round
@height= _height ? _height : @text.height.round

View File

@@ -24,6 +24,8 @@ module CyberarmEngine
toggle
@block.call(self) if @block
return :handled
end
def toggle

View File

@@ -22,8 +22,6 @@ module CyberarmEngine
@mouse_down_on = {}
@mouse_down_position = {}
@pending_recalculate_request = false
setup
end
# throws :blur event to focused element and sets GuiState focused element

View File

@@ -65,7 +65,7 @@ module CyberarmEngine
Button: { # < Label
margin: 1,
padding: 4,
border_thickness: 4,
border_thickness: 1,
border_color: ["ffd59674".hex, "ffff8746".hex],
border_radius: 0,
background: ["ffc75e61".to_i(16), "ffe26623".to_i(16)],

View File

@@ -1,4 +1,4 @@
module CyberarmEngine
NAME = "InDev"
VERSION = "0.9.0"
VERSION = "0.11.1"
end