summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorTom Black <[email protected]>2015-10-04 20:12:32 -0400
committerTom Black <[email protected]>2015-10-04 20:12:32 -0400
commitd70e4d425eab3848226215d42b3a81b8e191de1a (patch)
tree197bd95fe7bf5a6479aceb646a8549cb1cf57183 /lib
downloadruby2d-d70e4d425eab3848226215d42b3a81b8e191de1a.tar.gz
ruby2d-d70e4d425eab3848226215d42b3a81b8e191de1a.zip
First!
Diffstat (limited to 'lib')
-rw-r--r--lib/ruby2d.rb17
-rw-r--r--lib/ruby2d/application.rb39
-rw-r--r--lib/ruby2d/color.rb72
-rw-r--r--lib/ruby2d/dsl.rb27
-rw-r--r--lib/ruby2d/exceptions.rb12
-rw-r--r--lib/ruby2d/image.rb23
-rw-r--r--lib/ruby2d/quad.rb50
-rw-r--r--lib/ruby2d/rectangle.rb57
-rw-r--r--lib/ruby2d/sound.rb19
-rw-r--r--lib/ruby2d/square.rb26
-rw-r--r--lib/ruby2d/text.rb51
-rw-r--r--lib/ruby2d/triangle.rb45
-rw-r--r--lib/ruby2d/version.rb5
-rw-r--r--lib/ruby2d/window.rb132
14 files changed, 575 insertions, 0 deletions
diff --git a/lib/ruby2d.rb b/lib/ruby2d.rb
new file mode 100644
index 0000000..3e8ab78
--- /dev/null
+++ b/lib/ruby2d.rb
@@ -0,0 +1,17 @@
+# ruby2d.rb
+
+require 'ruby2d/window'
+require 'ruby2d/application'
+require 'ruby2d/color'
+require 'ruby2d/quad'
+require 'ruby2d/rectangle'
+require 'ruby2d/square'
+require 'ruby2d/triangle'
+require 'ruby2d/image'
+require 'ruby2d/text'
+require 'ruby2d/dsl'
+require 'ruby2d/exceptions'
+require 'ruby2d/ruby2d' # load native extension
+
+include Ruby2D::DSL
+include Ruby2D
diff --git a/lib/ruby2d/application.rb b/lib/ruby2d/application.rb
new file mode 100644
index 0000000..ebef2b5
--- /dev/null
+++ b/lib/ruby2d/application.rb
@@ -0,0 +1,39 @@
+# application.rb
+
+module Ruby2D::Application
+ class << self
+ @@window = Ruby2D::Window.new
+
+ def get(sym)
+ @@window.get(sym)
+ end
+
+ def set(opts)
+ @@window.set(opts)
+ end
+
+ def on(mouse: nil, key: nil, &proc)
+ @@window.on(mouse: mouse, key: key, &proc)
+ end
+
+ def add(o)
+ @@window.add(o)
+ end
+
+ def remove(o)
+ @@window.remove(o)
+ end
+
+ def clear
+ @@window.clear
+ end
+
+ def update(&proc)
+ @@window.update(&proc)
+ end
+
+ def show
+ @@window.show
+ end
+ end
+end
diff --git a/lib/ruby2d/color.rb b/lib/ruby2d/color.rb
new file mode 100644
index 0000000..9c7bc32
--- /dev/null
+++ b/lib/ruby2d/color.rb
@@ -0,0 +1,72 @@
+# color.rb
+
+module Ruby2D
+ class Color
+
+ attr_reader :r, :g, :b, :a
+
+ def initialize(c)
+ case c
+ when 'black'
+ @r, @g, @b, @a = to_f([0, 0, 0, 255])
+ when 'gray'
+ @r, @g, @b, @a = to_f([170, 170, 170, 255])
+ when 'silver'
+ @r, @g, @b, @a = to_f([221, 221, 221, 255])
+ when 'white'
+ @r, @g, @b, @a = to_f([255, 255, 255, 255])
+ when 'navy'
+ @r, @g, @b, @a = to_f([0, 31, 63, 255])
+ when 'blue'
+ @r, @g, @b, @a = to_f([0, 116, 217, 255])
+ when 'aqua'
+ @r, @g, @b, @a = to_f([127, 219, 255, 255])
+ when 'teal'
+ @r, @g, @b, @a = to_f([57, 204, 204, 255])
+ when 'olive'
+ @r, @g, @b, @a = to_f([61, 153, 112, 255])
+ when 'green'
+ @r, @g, @b, @a = to_f([46, 204, 64, 255])
+ when 'lime'
+ @r, @g, @b, @a = to_f([1, 255, 112, 255])
+ when 'yellow'
+ @r, @g, @b, @a = to_f([255, 220, 0, 255])
+ when 'orange'
+ @r, @g, @b, @a = to_f([255, 133, 27, 255])
+ when 'red'
+ @r, @g, @b, @a = to_f([255, 65, 54, 255])
+ when 'maroon'
+ @r, @g, @b, @a = to_f([133, 20, 75, 255])
+ when 'fuchsia'
+ @r, @g, @b, @a = to_f([240, 18, 190, 255])
+ when 'purple'
+ @r, @g, @b, @a = to_f([177, 13, 201, 255])
+ when 'brown'
+ @r, @g, @b, @a = to_f([102, 51, 0, 255])
+ when 'random'
+ @r, @g, @b, @a = rand(0..1.0), rand(0..1.0), rand(0..1.0), 1.0
+ when Array
+ @r, @g, @b, @a = to_f([c[0], c[1], c[2], c[3]])
+ else
+ # raise Error, "Color does not exist!"
+ puts "Error! Bad color."
+ end
+ end
+
+ private
+
+ # Convert from Uint8 (0..255) to Float (0..1.0)
+ def to_f(a)
+ b = []
+ a.each do |n|
+ if n.class == Fixnum
+ b.push(n / 255.0)
+ else
+ b.push(n)
+ end
+ end
+ return b
+ end
+
+ end
+end
diff --git a/lib/ruby2d/dsl.rb b/lib/ruby2d/dsl.rb
new file mode 100644
index 0000000..c5bed09
--- /dev/null
+++ b/lib/ruby2d/dsl.rb
@@ -0,0 +1,27 @@
+# dsl.rb
+
+module Ruby2D::DSL
+ def hello
+ puts "hi"
+ end
+
+ def get(sym)
+ Ruby2D::Application.get(sym)
+ end
+
+ def set(opts)
+ Ruby2D::Application.set(opts)
+ end
+
+ def on(mouse: nil, key: nil, &proc)
+ Ruby2D::Application.on(mouse: mouse, key: key, &proc)
+ end
+
+ def update(&proc)
+ Ruby2D::Application.update(&proc)
+ end
+
+ def show
+ Ruby2D::Application.show
+ end
+end
diff --git a/lib/ruby2d/exceptions.rb b/lib/ruby2d/exceptions.rb
new file mode 100644
index 0000000..6f8416b
--- /dev/null
+++ b/lib/ruby2d/exceptions.rb
@@ -0,0 +1,12 @@
+# exceptions.rb
+
+module Ruby2D
+ class Error < StandardError
+ def initialize(msg)
+ super(msg)
+ puts msg
+ puts "Occurred in:"
+ puts " " + caller.last, "\n"
+ end
+ end
+end
diff --git a/lib/ruby2d/image.rb b/lib/ruby2d/image.rb
new file mode 100644
index 0000000..280d742
--- /dev/null
+++ b/lib/ruby2d/image.rb
@@ -0,0 +1,23 @@
+# image.rb
+
+module Ruby2D
+ class Image
+
+ attr_accessor :x, :y
+
+ def initialize(x, y, path)
+
+ unless File.exists? path
+ raise Ruby2D::Error, "Cannot find image file!"
+ end
+
+ @type_id = 3
+ @x, @y, @path = x, y, path
+
+ if defined? Ruby2D::DSL
+ Ruby2D::Application.add(self)
+ end
+ end
+
+ end
+end
diff --git a/lib/ruby2d/quad.rb b/lib/ruby2d/quad.rb
new file mode 100644
index 0000000..f496cc1
--- /dev/null
+++ b/lib/ruby2d/quad.rb
@@ -0,0 +1,50 @@
+# quad.rb
+
+module Ruby2D
+ class Quad
+ # Coordinates in clockwise order, starting at top left:
+ # x1,y1 == top left
+ # x2,y2 == top right
+ # x3,y3 == bottom right
+ # x4,y4 == bottom left
+ attr_accessor :x1, :y1, :c1,
+ :x2, :y2, :c2,
+ :x3, :y3, :c3,
+ :x4, :y4, :c4
+
+ attr_reader :color
+
+ def initialize(x1, y1, x2, y2, x3, y3, x4, y4, c='white')
+ @type_id = 2
+ @x1, @y1, @x2, @y2, @x3, @y3, @x4, @y4 = x1, y1, x2, y2, x3, y3, x4, y4
+ @color = c
+ update_color(c)
+
+ if defined? Ruby2D::DSL
+ Ruby2D::Application.add(self)
+ end
+ end
+
+ def color=(c)
+ @color = c
+ update_color(c)
+ end
+
+ private
+
+ def update_color(c)
+ # If a 2D array
+ if c.class == Array && c.all? { |el| el.class == Array }
+ @c1 = Ruby2D::Color.new(c[0])
+ @c2 = Ruby2D::Color.new(c[1])
+ @c3 = Ruby2D::Color.new(c[2])
+ @c4 = Ruby2D::Color.new(c[3])
+ else
+ @c1 = Ruby2D::Color.new(c)
+ @c2 = Ruby2D::Color.new(c)
+ @c3 = Ruby2D::Color.new(c)
+ @c4 = Ruby2D::Color.new(c)
+ end
+ end
+ end
+end
diff --git a/lib/ruby2d/rectangle.rb b/lib/ruby2d/rectangle.rb
new file mode 100644
index 0000000..45a0cee
--- /dev/null
+++ b/lib/ruby2d/rectangle.rb
@@ -0,0 +1,57 @@
+# rectangle.rb
+
+module Ruby2D
+ class Rectangle < Ruby2D::Quad
+
+ attr_reader :x, :y, :width, :height
+
+ def initialize(x, y, w, h, c='white')
+ @type_id = 2
+ @x, @y, @width, @height, @color = x, y, w, h, c
+ update_coords(x, y, w, h)
+ update_color(c)
+
+ if defined? Ruby2D::DSL
+ Ruby2D::Application.add(self)
+ end
+ end
+
+ def x=(x)
+ @x = @x1 = x
+ @x2 = x + @width
+ @x3 = x + @width
+ @x4 = x
+ end
+
+ def y=(y)
+ @y = @y1 = y
+ @y2 = y
+ @y3 = y + @height
+ @y4 = y + @height
+ end
+
+ def width=(w)
+ @width = w
+ update_coords(@x, @y, w, @height)
+ end
+
+ def height=(h)
+ @height = h
+ update_coords(@x, @y, @width, h)
+ end
+
+ private
+
+ def update_coords(x, y, w, h)
+ @x1 = x
+ @y1 = y
+ @x2 = x + w
+ @y2 = y
+ @x4 = x
+ @y4 = y + h
+ @x3 = x + w
+ @y3 = y + h
+ end
+
+ end
+end
diff --git a/lib/ruby2d/sound.rb b/lib/ruby2d/sound.rb
new file mode 100644
index 0000000..edbbef9
--- /dev/null
+++ b/lib/ruby2d/sound.rb
@@ -0,0 +1,19 @@
+# sound.rb
+
+module Ruby2D
+ class Sound
+
+ def initialize(window, path)
+ unless File.exists? path
+ raise Error, "Cannot find sound file!"
+ end
+ window.create_audio(self, path)
+ @window, @path = window, path
+ end
+
+ def play
+ @window.play_audio(self)
+ end
+
+ end
+end
diff --git a/lib/ruby2d/square.rb b/lib/ruby2d/square.rb
new file mode 100644
index 0000000..6614c06
--- /dev/null
+++ b/lib/ruby2d/square.rb
@@ -0,0 +1,26 @@
+# square.rb
+
+module Ruby2D
+ class Square < Ruby2D::Rectangle
+
+ attr_reader :size
+
+ def initialize(x=0, y=0, s=100, c='white')
+ @type_id = 2
+ @x, @y, @color = x, y, c
+ @width = @height = @size = s
+ update_coords(x, y, s, s)
+ update_color(c)
+
+ if defined? Ruby2D::DSL
+ Ruby2D::Application.add(self)
+ end
+ end
+
+ def size=(s)
+ self.width = self.height = @size = s
+ end
+
+ private :width=, :height=
+ end
+end
diff --git a/lib/ruby2d/text.rb b/lib/ruby2d/text.rb
new file mode 100644
index 0000000..bd4637f
--- /dev/null
+++ b/lib/ruby2d/text.rb
@@ -0,0 +1,51 @@
+# text.rb
+
+module Ruby2D
+ class Text
+
+ attr_accessor :x, :y, :size, :text
+
+ def initialize(x=0, y=0, size=20, text="Hello World!", font="Arial", c="white")
+ if font.include? '.'
+ unless File.exists? font
+ raise Error, "Cannot find font file!"
+ else
+ @font = font
+ end
+ else
+ @font = resolve_path(font)
+ end
+
+ @type_id = 4
+ @x, @y, @size = x, y, size
+ @text, @color = text, c
+ update_color(c)
+
+ if defined? Ruby2D::DSL
+ Ruby2D::Application.add(self)
+ end
+ end
+
+ def color=(c)
+ @color = c
+ update_color(c)
+ end
+
+ private
+
+ def resolve_path(font)
+ if RUBY_PLATFORM =~ /darwin/
+ font_path = "/Library/Fonts/#{font}.ttf"
+ unless File.exists? font_path
+ raise Error, "Cannot find system font!"
+ end
+ end
+ font_path
+ end
+
+ def update_color(c)
+ @c = Ruby2D::Color.new(c)
+ end
+
+ end
+end
diff --git a/lib/ruby2d/triangle.rb b/lib/ruby2d/triangle.rb
new file mode 100644
index 0000000..a4754ed
--- /dev/null
+++ b/lib/ruby2d/triangle.rb
@@ -0,0 +1,45 @@
+# triangle.rb
+
+module Ruby2D
+ class Triangle
+
+ attr_accessor :x1, :y1, :c1,
+ :x2, :y2, :c2,
+ :x3, :y3, :c3
+ attr_reader :color
+
+ def initialize(x1, y1, x2, y2, x3, y3, c='white')
+ @type_id = 1
+ @x1, @y1 = x1, y1
+ @x2, @y2 = x2, y2
+ @x3, @y3 = x3, y3
+ @color = c
+ update_color(c)
+
+ if defined? Ruby2D::DSL
+ Ruby2D::Application.add(self)
+ end
+ end
+
+ def color=(c)
+ update_color(c)
+ @color = c
+ end
+
+ private
+
+ def update_color(c)
+ # If a 2D array
+ if c.class == Array && c.all? { |el| el.class == Array }
+ @c1 = Ruby2D::Color.new(c[0])
+ @c2 = Ruby2D::Color.new(c[1])
+ @c3 = Ruby2D::Color.new(c[2])
+ else
+ @c1 = Ruby2D::Color.new(c)
+ @c2 = Ruby2D::Color.new(c)
+ @c3 = Ruby2D::Color.new(c)
+ end
+ end
+
+ end
+end
diff --git a/lib/ruby2d/version.rb b/lib/ruby2d/version.rb
new file mode 100644
index 0000000..a6ab2f1
--- /dev/null
+++ b/lib/ruby2d/version.rb
@@ -0,0 +1,5 @@
+# version.rb
+
+module Ruby2D
+ VERSION = '0.0.0'
+end
diff --git a/lib/ruby2d/window.rb b/lib/ruby2d/window.rb
new file mode 100644
index 0000000..6011d42
--- /dev/null
+++ b/lib/ruby2d/window.rb
@@ -0,0 +1,132 @@
+# window.rb
+
+module Ruby2D
+ class Window
+ attr_reader :title, :width, :height, :mouse_x, :mouse_y
+
+ def initialize(width: 640, height: 480, title: "Ruby 2D", fps: 60, vsync: true)
+ @width, @height, @title = width, height, title
+ @mouse_x = @mouse_y = 0
+ @fps_cap = fps
+ @vsync = vsync
+
+ @objects = []
+ @keys = {}
+ @keys_down = {}
+ @update_proc = Proc.new {}
+ end
+
+ def get(sym)
+ case sym
+ when :window
+ return self
+ when :title
+ return @title
+ when :width
+ return @width
+ when :height
+ return @height
+ when :mouse_x
+ return @mouse_x
+ when :mouse_y
+ return @mouse_y
+ end
+ end
+
+ def set(opts)
+ if opts.include? :title
+ @title = opts[:title]
+ end
+
+ if opts.include? :width
+ @width = opts[:width]
+ end
+
+ if opts.include? :height
+ @height = opts[:height]
+ end
+ end
+
+ def on(mouse: nil, key: nil, &proc)
+ puts "mouse: #{mouse}"
+ puts "key: #{key}"
+ # proc.call
+ key(key, &proc)
+ end
+
+ def add(o)
+ case o
+ when nil
+ raise Error, "Cannot add '#{o.class}' to window!"
+ when Array
+ o.each { |x| add_object(x) }
+ else
+ add_object(o)
+ end
+ end
+
+ def remove(o)
+ if o == nil
+ raise Error, "Cannot remove '#{o.class}' from window!"
+ end
+
+ if i = @objects.index(o)
+ @objects.slice!(i)
+ true
+ else
+ false
+ end
+ end
+
+ def clear
+ @objects.clear
+ end
+
+ # Register key string with proc
+ def key(key, &proc)
+ @keys[key] = proc
+ true
+ end
+
+ def key_callback(key)
+ key.downcase!
+ if @keys.has_key? key
+ @keys[key].call
+ end
+ end
+
+ # Register key string with proc
+ def key_down(key, &proc)
+ @keys_down[key] = proc
+ true
+ end
+
+ def key_down_callback(key)
+ key.downcase!
+ if @keys_down.has_key? key
+ @keys_down[key].call
+ end
+ end
+
+ def update(&proc)
+ @update_proc = proc
+ true
+ end
+
+ def update_callback
+ @update_proc.call
+ end
+
+ private
+
+ def add_object(o)
+ @objects.push(o)
+ true
+ else
+ false
+ end
+ end
+
+ end
+end