ray 0.0.0.pre2 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. data/.gitignore +1 -0
  2. data/.rspec +3 -0
  3. data/README.md +62 -0
  4. data/Rakefile +33 -23
  5. data/VERSION +1 -1
  6. data/ext/audio.c +473 -0
  7. data/ext/color.c +4 -4
  8. data/ext/event.c +25 -3
  9. data/ext/extconf.rb +35 -22
  10. data/ext/font.c +287 -0
  11. data/ext/image.c +682 -33
  12. data/ext/joystick.c +9 -9
  13. data/ext/ray.c +166 -55
  14. data/ext/ray.h +120 -9
  15. data/ext/ray_osx.m +161 -0
  16. data/ext/rect.c +31 -4
  17. data/lib/ray/audio.rb +52 -0
  18. data/lib/ray/color.rb +16 -0
  19. data/lib/ray/dsl.rb +1 -3
  20. data/lib/ray/dsl/event.rb +1 -39
  21. data/lib/ray/dsl/event_listener.rb +38 -0
  22. data/lib/ray/dsl/event_runner.rb +3 -1
  23. data/lib/ray/dsl/event_translator.rb +74 -8
  24. data/lib/ray/dsl/handler.rb +3 -33
  25. data/lib/ray/dsl/matcher.rb +129 -23
  26. data/lib/ray/font.rb +108 -0
  27. data/lib/ray/font_set.rb +37 -0
  28. data/lib/ray/game.rb +171 -34
  29. data/lib/ray/helper.rb +43 -5
  30. data/lib/ray/image.rb +90 -3
  31. data/lib/ray/image_set.rb +35 -0
  32. data/lib/ray/joystick.rb +30 -0
  33. data/lib/ray/music_set.rb +35 -0
  34. data/lib/ray/ray.rb +17 -9
  35. data/lib/ray/rect.rb +51 -0
  36. data/lib/ray/resource_set.rb +92 -0
  37. data/lib/ray/scene.rb +220 -51
  38. data/lib/ray/sound_set.rb +35 -0
  39. data/lib/ray/sprite.rb +184 -0
  40. data/psp/ext.c +4 -0
  41. data/samples/hello_world/hello.rb +35 -0
  42. data/samples/hello_world/hello_dsl.rb +24 -0
  43. data/samples/pong/pong.rb +128 -0
  44. data/samples/sokoban/level_1 +7 -0
  45. data/samples/sokoban/sokoban.rb +370 -0
  46. data/spec/ray/audio_spec.rb +146 -0
  47. data/spec/ray/color_spec.rb +13 -0
  48. data/spec/ray/event_spec.rb +57 -168
  49. data/spec/ray/font_spec.rb +93 -0
  50. data/spec/ray/image_set_spec.rb +48 -0
  51. data/spec/ray/image_spec.rb +130 -44
  52. data/spec/ray/joystick_spec.rb +13 -9
  53. data/spec/ray/matcher_spec.rb +32 -55
  54. data/spec/ray/ray_spec.rb +33 -31
  55. data/spec/ray/rect_spec.rb +80 -0
  56. data/spec/ray/resource_set_spec.rb +105 -0
  57. data/spec/ray/sprite_spec.rb +163 -0
  58. data/spec/res/VeraMono.ttf +0 -0
  59. data/spec/res/aqua2.bmp +0 -0
  60. data/spec/res/pop.wav +0 -0
  61. data/spec/spec.opts +4 -0
  62. data/spec/spec_helper.rb +8 -0
  63. data/yard_ext.rb +91 -0
  64. metadata +104 -38
  65. data/bin/ray +0 -5
  66. data/bin/ray_irb +0 -4
  67. data/ext/SDLMain.h +0 -17
  68. data/ext/SDLMain.m +0 -381
  69. data/lib/ray/config.rb +0 -84
  70. data/lib/ray/dsl/converter.rb +0 -65
  71. data/lib/ray/dsl/listener.rb +0 -30
  72. data/lib/ray/dsl/type.rb +0 -58
  73. data/spec/ray/config_spec.rb +0 -90
  74. data/spec/ray/conversion_spec.rb +0 -43
  75. data/spec/ray/type_spec.rb +0 -17
  76. data/spec_runner.rb +0 -27
data/lib/ray/config.rb DELETED
@@ -1,84 +0,0 @@
1
- require 'singleton'
2
-
3
- module Ray
4
- # A class holding program wide settings.
5
- # @example
6
- # Ray::Config.config do
7
- # set :value, :string => 3 # Conversion
8
- # set :value, "3"
9
- # set :value, {3 => 4} # Ray::Config[:value] == {3 => 4}
10
- # set :value, :string => 3, :hash => {} # No conversion either
11
- # unest :value # set to nil
12
- # end
13
- class Config
14
- include Singleton
15
-
16
- class << self
17
- # @return [Object] the value for key
18
- def [](key)
19
- instance[key]
20
- end
21
-
22
- # Yields the instance of Config if the block takes an argument.
23
- # Uses instance_eval in other cases.
24
- def config(&block)
25
- if block.arity == 1
26
- block.call(instance)
27
- else
28
- instance.instance_eval(&block)
29
- end
30
- end
31
-
32
- # Clears all the settings, and then calls config.
33
- def config!(&block)
34
- instance.clear
35
- config(&block)
36
- end
37
- end
38
-
39
- def initialize
40
- @settings = {}
41
- end
42
-
43
- # @return [Object] The value for key
44
- def [](key)
45
- @settings[key]
46
- end
47
-
48
- # Sets the value for key
49
- def []=(key, value)
50
- @settings[key] = value
51
- end
52
-
53
- # Clears all the settings
54
- def clear
55
- @settings.clear
56
- end
57
-
58
- # Sets the value for a key.
59
- # If value is a Hash, and if it contains only one element, and if the
60
- # only key of this hash is a known type, it will convert the value to
61
- # the one represented by the key.
62
- #
63
- # @example A conversion
64
- # set :foo, :string => 3 # set :foo, "3"
65
- def set(key, value)
66
- if value.is_a?(Hash) && value.size == 1
67
- type, val = value.keys.first, value.values.first
68
-
69
- if Ray.know_type? type
70
- self[key] = Ray.convert(val, type)
71
- else
72
- self[key] = value
73
- end
74
- else
75
- self[key] = value
76
- end
77
- end
78
-
79
- # Removes the value for this key.
80
- def unset(key)
81
- @settings.delete(key)
82
- end
83
- end
84
- end
@@ -1,65 +0,0 @@
1
- module Ray
2
- module DSL
3
- # Module sroing all the regitred converters. You should never
4
- # need to use it directly.
5
- module Converter
6
- @@converters = {}
7
-
8
- class << self
9
- def add_converter(from, to, &block)
10
- @@converters[Ray.resolve_type(from) => Ray.resolve_type(to)] = block
11
- end
12
-
13
- def convert(obj, target)
14
- unless target.is_a? Module
15
- return convert(obj, Ray.resolve_type(target))
16
- end
17
-
18
- if converter = @@converters[obj.class => target] # Direct converter
19
- return converter.call(obj)
20
- end
21
-
22
- @@converters.each do |hash, converter|
23
- from, to = hash.keys.first, hash.values.first
24
-
25
- if obj.is_a?(from) && to.ancestors.include?(target)
26
- return converter.call(obj)
27
- end
28
- end
29
-
30
- if target == Integer
31
- return obj.to_i if obj.respond_to? :to_i
32
- elsif target == Float
33
- return obj.to_f if obj.respond_to? :to_f
34
- elsif target == String
35
- return obj.to_s if obj.respond_to? :to_s
36
- end
37
-
38
- raise TypeError, "Can't convert #{obj.class} into #{target}"
39
- end
40
- end
41
- end
42
- end
43
-
44
- # Converts an object to a given type. target can be a Module or
45
- # an object registred as a type.
46
- #
47
- # If there is a converter that can directly convert from obj.class to target,
48
- # it will be used. If there isn't, it will see it will search in
49
- # obj.class.ancestors and in target's subclasses.
50
- def self.convert(obj, target)
51
- DSL::Converter.convert(obj, target)
52
- end
53
-
54
- # Adds a block telling to ray how to convert from a type to another one.
55
- # @example Converting from String to Array
56
- # Ray.describe_conversion(:string => Array) do |s| # registred type or module
57
- # s.split('')
58
- # end
59
- # @example declaring multiple conversions at once
60
- def self.describe_conversion(hash, &block)
61
- hash.each do |from, to|
62
- DSL::Converter.add_converter(from, to, &block)
63
- end
64
- end
65
- end
@@ -1,30 +0,0 @@
1
- module Ray
2
- module DSL
3
- # The module that allows you to do something when something else happened.
4
- module Listener
5
- # The args part of this method may help you to say when you want your
6
- # block to be called. Each argument applies to one argument of the
7
- # event, and they must all match or your block won't get called.
8
- #
9
- # It will use == to see if two objects match, except in two cases: if you
10
- # pass a Matcher, it will use match?, and if you pass a regex it will try
11
- # to use =~.
12
- #
13
- # Your block will be called with the same arguments as the event, just in
14
- # case you'd need them.
15
- #
16
- def on(event, *args, &block)
17
- return unless listener_runner
18
- listener_runner.add_handler(event, args, block)
19
- end
20
-
21
- def listener_runner
22
- @__listener_runner
23
- end
24
-
25
- def listener_runner=(arg)
26
- @__listener_runner = arg
27
- end
28
- end
29
- end
30
- end
data/lib/ray/dsl/type.rb DELETED
@@ -1,58 +0,0 @@
1
- module Ray
2
- module DSL
3
- # The module that knows about all the type you registred.
4
- module Type
5
- @@types = {}
6
-
7
- class << self
8
- def register_type(name, klass)
9
- @@types[name] = klass
10
- end
11
-
12
- def know_type?(name)
13
- (@@types[name] != nil)
14
- end
15
-
16
- def resolve_type(name)
17
- @@types[name]
18
- end
19
- end
20
- end
21
- end
22
-
23
- # Registers a type. It allows you to say name instead of klass
24
- # when Ray asks you for a typename.
25
- # @param [Symbol] name The object that will represent klass
26
- # @param [Module] klass The class name will be resolved as
27
- def self.register_type(name, klass)
28
- DSL::Type.register_type(name, klass)
29
- end
30
-
31
- # @return [true, false] True if name is a known type
32
- def self.know_type?(name)
33
- DSL::Type.know_type?(name)
34
- end
35
-
36
- # @param [Module, Symbol] name An object supposed to identify a module or a
37
- # class.
38
- # @return [Module, nil] The matchig module
39
- def self.resolve_type(name)
40
- if name.is_a? Module
41
- name
42
- else
43
- DSL::Type.resolve_type(name)
44
- end
45
- end
46
-
47
- register_type(:image, Ray::Image)
48
- register_type(:color, Ray::Color)
49
-
50
- register_type(:string, String)
51
- register_type(:array, Array)
52
- register_type(:hash, Hash)
53
- register_type(:integer, Integer)
54
- register_type(:float, Float)
55
- register_type(:numeric, Numeric)
56
-
57
- register_type(:anything, Object)
58
- end
@@ -1,90 +0,0 @@
1
- describe Ray::Config do
2
- describe ".config" do
3
- it "should allow to add new settings" do
4
- Ray::Config.config do |c|
5
- c[:val] = :obj
6
- c[:val].should == :obj
7
- end
8
-
9
- Ray::Config[:val].should == :obj
10
- end
11
-
12
- it "should use instance_eval if the block takes no argument" do
13
- Ray::Config.config do
14
- self.class.should == Ray::Config
15
- end
16
- end
17
- end
18
-
19
- describe "'s not set settings" do
20
- it "should be nil" do
21
- Ray::Config[:dont_put_a_value_here].should be_nil
22
- end
23
- end
24
-
25
- describe ".config!" do
26
- it "should reset every setting" do
27
- Ray::Config.config! do |c|
28
- c[:bar] = 3
29
- end
30
-
31
- Ray::Config[:bar].should == 3
32
- Ray::Config.config! {}
33
- Ray::Config[:bar].should be_nil
34
- end
35
- end
36
-
37
- describe "#set" do
38
- context "if the second argument is not a hash" do
39
- it "should set the value directly" do
40
- Ray::Config.config do |conf|
41
- conf.set :value, :val
42
- conf[:value].should == :val
43
- end
44
- end
45
- end
46
-
47
- context "if the second argument is a hash" do
48
- context "with only one element" do
49
- context "if the key is a known type" do
50
- it "should convert the object" do
51
- Ray::Config.config do |conf|
52
- conf.set :value, :string => 3
53
- conf[:value].should == "3"
54
- end
55
- end
56
- end
57
-
58
- context "if the key is not a know type" do
59
- it "should set the value directly" do
60
- Ray::Config.config do |conf|
61
- conf.set :value, 3 => "bar"
62
- conf[:value].should == {3 => "bar"}
63
- end
64
- end
65
- end
66
- end
67
-
68
- context "with more or less than one element" do
69
- it "should set the value directly" do
70
- Ray::Config.config do |conf|
71
- conf.set :value, {}
72
- conf[:value].should == {}
73
- end
74
- end
75
- end
76
- end
77
- end
78
-
79
- describe "#unset" do
80
- it "should set the value to nil" do
81
- Ray::Config.config do |conf|
82
- conf[:x] = 3
83
- conf[:x].should == 3
84
-
85
- conf.unset :x
86
- conf[:x].should be_nil
87
- end
88
- end
89
- end
90
- end
@@ -1,43 +0,0 @@
1
- class TestClass; end
2
- class SubTest < TestClass; end
3
- class OtherTest; end
4
- class YATest; end
5
-
6
- Ray.describe_conversion(TestClass => OtherTest) { OtherTest.new }
7
- Ray.describe_conversion(YATest => SubTest) { SubTest.new }
8
- Ray.describe_conversion(OtherTest => SubTest) { SubTest.new }
9
- Ray.describe_conversion(OtherTest => TestClass) { TestClass.new }
10
-
11
- describe "conversions" do
12
- context "when the requested conversion is known" do
13
- it "should convert the object" do
14
- Ray.convert(TestClass.new, OtherTest).should be_an(OtherTest)
15
- end
16
-
17
- context "even for a sub-class of the target" do
18
- it "should convert the object" do
19
- Ray.convert(YATest.new, TestClass).should be_a(TestClass)
20
- end
21
- end
22
-
23
- context "and there is a direct way to convert it" do
24
- it "should use it" do
25
- obj = Ray.convert(OtherTest.new, TestClass)
26
- obj.should be_a(TestClass)
27
- obj.should_not be_a(SubTest)
28
- end
29
- end
30
-
31
- it "should not create an instance of a superclass of the expected type" do
32
- Ray.convert(OtherTest.new, SubTest).class.should_not == TestClass
33
- end
34
- end
35
-
36
- context "when there's not available conversion" do
37
- it "should raise a type error" do
38
- lambda {
39
- Ray.convert(YATest.new, OtherTest)
40
- }.should raise_exception(TypeError)
41
- end
42
- end
43
- end
@@ -1,17 +0,0 @@
1
- describe "Ray.resolve_type" do
2
- context "when given a module" do
3
- it "should return it" do
4
- Ray.resolve_type(Class).should == Class
5
- end
6
- end
7
-
8
- context "when given something else" do
9
- it "should return nil if it's not registred" do
10
- Ray.resolve_type(3).should == nil
11
- end
12
-
13
- it "should return a module if it is registred" do
14
- Ray.resolve_type(:string).should == String
15
- end
16
- end
17
- end
data/spec_runner.rb DELETED
@@ -1,27 +0,0 @@
1
- $: << "./ext/" << "./lib/"
2
- path = File.expand_path(File.join(File.dirname(__FILE__), "ext/ray"))
3
- our_path = File.expand_path(File.join(File.dirname(__FILE__), "spec_runner.rb"))
4
-
5
- unless defined? Ray
6
- unless RUBY_PLATFORM =~ /darwin/
7
- require 'ray_ext'
8
- else
9
- if File.exist? path
10
- system "#{path} #{our_path}"
11
- exit $?.exitstatus
12
- else
13
- $stderr.puts "please build ray (rake ext)"
14
- exit 1
15
- end
16
- end
17
- end
18
-
19
- def path_of(res)
20
- File.expand_path(File.join(File.dirname(__FILE__), "spec", "res", res))
21
- end
22
-
23
- require 'ray'
24
-
25
- require 'spec'
26
- require 'spec/autorun'
27
- Dir["spec/**/*_spec.rb"].each { |file| load file }