Module: Ray

Defined in:
lib/ray/dsl.rb,
lib/ray/font.rb,
lib/ray/game.rb,
lib/ray/rect.rb,
lib/ray/audio.rb,
lib/ray/color.rb,
lib/ray/image.rb,
lib/ray/scene.rb,
lib/ray/helper.rb,
lib/ray/sprite.rb,
lib/ray/font_set.rb,
lib/ray/joystick.rb,
lib/ray/dsl/event.rb,
lib/ray/image_set.rb,
lib/ray/music_set.rb,
lib/ray/sound_set.rb,
lib/ray/dsl/handler.rb,
lib/ray/dsl/matcher.rb,
lib/ray/resource_set.rb,
lib/ray/dsl/event_raiser.rb,
lib/ray/dsl/event_runner.rb,
lib/ray/dsl/event_listener.rb,
lib/ray/dsl/event_translator.rb,
ext/ray.c

Defined Under Namespace

Modules: Audio, DSL, FontSet, Helper, ImageSet, Matchers, MusicSet, ResourceSet, SoundSet Classes: Channel, Color, Event, Font, Game, Image, Joystick, Music, NoPatternError, Rect, Scene, Sound, Sprite

Constant Summary collapse

KEYS =
Ray::Event.constants.inject({}) do |hash, const|
  if const =~ /^KEY_(.+)$/
    hash[$1.downcase.to_sym] = [Ray::Event.const_get(const)]
  elsif const =~ /^PSP_BUTTON_(.+)$/
    hash["psp_#{$1.downcase.to_sym}".to_sym] = [Ray::Event.const_get(const)]
  end

  hash
end
MOD =
Ray::Event.constants.inject({}) do |hash, const|
  if const =~ /^KMOD_(.+)$/
    hash[$1.downcase.to_sym] = [Ray::Event.const_get(const)]
  end

  hash
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.can_use_mode?(hash) ⇒ true, false

Returns True if the video mode described by hash can be used.

Parameters:

  • hash (Hash)

    Same options as in create_window

Returns:

  • (true, false)

    True if the video mode described by hash can be used.



196
197
198
199
200
201
# File 'ext/ray.c', line 196

VALUE ray_can_use_mode(VALUE self, VALUE hash) {
   ray_video_mode mode = ray_parse_video_mode(hash);
   
   int res = SDL_VideoModeOK(mode.width, mode.height, mode.bpp, mode.flags);
   return res ? Qtrue : Qfalse;
}

.create_window(hash) ⇒ Ray::Image

Creates a new window.

If both hw_surface and sws_urface are false, hw_surface will be considered as true. :sw_surface should be true if you want to acess

Options Hash (hash):

  • :width (Integer)

    Width of the window

  • :height (Integer)

    Height of the window

  • :w (Integer)

    Alias for width

  • :h (Integer)

    Alias for height

  • :bits_per_pixel (Integer)

    Bits per pixel. Valid values are 8, 15, 16, 24, and 32.

  • :bpp (Integer)

    Alias for bits_per_pixel

  • :hw_surface (true, false)

    Creates the surface in video memory (default)

  • :sw_surface (true, false)

    Creates the surface in system memory

  • :async_blit (true, false)

    Enables asynchronous updates of the the surface

  • :double_buf (true, false)

    Enables double buffering. Ignored if sw_surface is set.

  • :fullscreen (true, false)

    Creates a full screen window.

  • :resizable (true, false)

    Creates a resizable window.

  • :no_frame (true, false)

    Disables window decoration if possible.

Returns:

  • (Ray::Image)

    An image representing the window



177
178
179
180
181
182
183
184
185
186
187
188
# File 'ext/ray.c', line 177

VALUE ray_create_window(VALUE self, VALUE hash) {
   ray_video_mode mode = ray_parse_video_mode(hash);
   SDL_Surface *screen = SDL_SetVideoMode(mode.width, mode.height,
                                          mode.bpp, mode.flags);
   
   if (!screen) {
      rb_raise(rb_eRuntimeError, "Could not create the window (%s)",
               SDL_GetError());
   }

   return ray_create_image(screen);
}

.describe_matcher(name, &create_block) ⇒ Object

Describes a new matcher.

Examples:

Ray.describe_matcher(:match, :string) do |regex|
  lambda { |str| str =~ regex }
end

Parameters:

  • name (Symbol)

    The name you’ll use to call your matcher

  • create_block (Proc)

    a block called with the arguments of your matcher method, and returning the block that will be used to check if the condition is matched.



43
44
45
46
47
48
49
# File 'lib/ray/dsl/matcher.rb', line 43

def self.describe_matcher(name, &create_block)
  Matchers.module_eval do
    define_method(name) do |*args|
      DSL::Matcher.new(&create_block.call(*args))
    end
  end
end

.font_set(regex) {|*args, size| ... } ⇒ Object

Creates a new font set.

Parameters:

  • regex (Regexp)

    Regular expression used to match file

Yields:

  • (*args, size)

    Block returning the font

Yield Parameters:

  • args

    Regex captures

  • size

    Size of the font



25
26
27
# File 'lib/ray/font_set.rb', line 25

def self.font_set(regex, &block)
  Ray::FontSet.add_set(regex, &block)
end

.game(title, opts = {}, &block) ⇒ Object



241
242
243
# File 'lib/ray/game.rb', line 241

def self.game(title, opts = {}, &block)
  Ray::Game.new(title, opts, &block)
end

.grab_inputtrue, false

Returns True if the input is grabbed, which means the mouse is confined in the window, and keyboard input is sent directly to the window.

Returns:

  • (true, false)

    True if the input is grabbed, which means the mouse is confined in the window, and keyboard input is sent directly to the window.



268
269
270
271
# File 'ext/ray.c', line 268

VALUE ray_grab_input(VALUE self) {
   SDL_GrabMode mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
   return (mode == SDL_GRAB_ON) ? Qtrue : Qfalse;
}

.grab_input=(grab) ⇒ Object

Sets the grab input to true or false



277
278
279
280
# File 'ext/ray.c', line 277

VALUE ray_set_grab_input(VALUE self, VALUE grab) {
   SDL_WM_GrabInput(RTEST(grab) ? SDL_GRAB_ON : SDL_GRAB_OFF);
   return grab;
}

.has_audio_support?true, false

Returns true if Ray supports audio playback.

Returns:

  • (true, false)

    true if Ray supports audio playback



319
320
321
322
323
324
325
# File 'ext/ray.c', line 319

VALUE ray_has_audio_support(VALUE self) {
#ifdef HAVE_SDL_MIXER
   return Qtrue;
#else
   return Qfalse;
#endif
}

.has_font_support?true, false

Returns true if Ray supports ttf fonts.

Returns:

  • (true, false)

    true if Ray supports ttf fonts



310
311
312
313
314
315
316
# File 'ext/ray.c', line 310

VALUE ray_has_font_support(VALUE self) {
#ifdef HAVE_SDL_TTF
   return Qtrue;
#else
   return Qfalse;
#endif
}

.has_gfx_support?true, false

Returns true if Ray supports graphical effect like rotations.

Returns:

  • (true, false)

    true if Ray supports graphical effect like rotations



301
302
303
304
305
306
307
# File 'ext/ray.c', line 301

VALUE ray_has_gfx_support(VALUE self) {
#ifdef HAVE_SDL_GFX
   return Qtrue;
#else
   return Qfalse;
#endif
}

.has_image_support?true, false

Returns true if Ray supports other image formats than BMP.

Returns:

  • (true, false)

    true if Ray supports other image formats than BMP



292
293
294
295
296
297
298
# File 'ext/ray.c', line 292

VALUE ray_has_image_support(VALUE self) {
#ifdef HAVE_SDL_IMAGE
   return Qtrue;
#else
   return Qfalse;
#endif
}

.icon=(icon) ⇒ Object

Sets the window icon

Parameters:



208
209
210
211
# File 'ext/ray.c', line 208

VALUE ray_set_icon(VALUE self, VALUE icon) {
   SDL_WM_SetIcon(ray_rb2surface(icon), NULL);
   return icon;
}

.image_set(regex) {|*args| ... } ⇒ Object

Creates a new image set.

Parameters:

  • regex (Regexp)

    Regular expression used to match file

Yields:

  • (*args)

    Block returning the image

Yield Parameters:

  • args

    Regex captures



22
23
24
# File 'lib/ray/image_set.rb', line 22

def self.image_set(regex, &block)
  Ray::ImageSet.add_set(regex, &block)
end

.init(opts = {}) ⇒ Object

Inits Ray.

Options Hash (opts):

  • :frequency (Integer)

    Audio frequency (defaults to 22050)

  • :format (Integer)

    A constant declared in Ray::Audio

  • :mono (true)

    or :stereo Set :mono or :stereo to true to enable one of them.

  • :chunk_size (Integer)

    Size of a chunk (defaults to 1024)



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'ext/ray.c', line 20

VALUE ray_init(int argc, VALUE *argv, VALUE self) {
   VALUE hash = Qnil;
   rb_scan_args(argc, argv, "01", &hash);

#ifdef HAVE_COCOA
   ray_osx_init();
#endif

   if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) == -1) {
      rb_raise(rb_eRuntimeError, "Couldn't init the SDL (%s)",
               SDL_GetError());
   }

#ifdef HAVE_SDL_TTF
   if (!TTF_WasInit()) {
      if (TTF_Init() == -1) {
         rb_raise(rb_eRuntimeError, "Couldn't init SDL_TTF (%s)",
                  TTF_GetError());
      }
   }
#endif

#ifdef HAVE_SDL_MIXER
   int frequency   = MIX_DEFAULT_FREQUENCY;
   uint16_t format = MIX_DEFAULT_FORMAT;
   int channels    = MIX_DEFAULT_CHANNELS;
   int chunk_size = 1024;

   if (!NIL_P(hash)) {
      VALUE rb_freq, rb_format, rb_chunk_size;
      if (!NIL_P(rb_freq = rb_hash_aref(hash, RAY_SYM("frequency"))))
         frequency = NUM2INT(rb_freq);

      if (!NIL_P(rb_format = rb_hash_aref(hash, RAY_SYM("format"))))
         format = NUM2INT(rb_format);
      
      if (RTEST(rb_hash_aref(hash, RAY_SYM("mono"))))
         channels = 1;
      else if (RTEST(rb_hash_aref(hash, RAY_SYM("stereo"))))
         channels = 2;

      if (!NIL_P(rb_chunk_size = rb_hash_aref(hash, RAY_SYM("chunk_size"))))
         chunk_size = NUM2INT(rb_chunk_size);
   }
   
   if (Mix_OpenAudio(frequency, format, channels, chunk_size) == -1) {
      rb_raise(rb_eRuntimeError, "Couldn't open audio (%s)",
               Mix_GetError());
   }
#endif

   return Qnil;
}

.music_set(regex) {|*args| ... } ⇒ Object

Creates a new music set.

Parameters:

  • regex (Regexp)

    Regular expression used to match file

Yields:

  • (*args)

    Block returning the music

Yield Parameters:

  • args

    Regex captures



23
24
25
# File 'lib/ray/music_set.rb', line 23

def self.music_set(regex, &block)
  Ray::MusicSet.add_set(regex, &block)
end

.screenRay::Image?

Returns The current screen, created by create_window.

Returns:

  • (Ray::Image, nil)

    The current screen, created by create_window



283
284
285
286
287
288
289
# File 'ext/ray.c', line 283

VALUE ray_screen(VALUE self) {
   SDL_Surface *surf = SDL_GetVideoSurface();
   
   if (!surf)
      return Qnil;
   return ray_create_image(surf);
}

.sound_set(regex) {|*args| ... } ⇒ Object

Creates a new sound set.

Parameters:

  • regex (Regexp)

    Regular expression used to match file

Yields:

  • (*args)

    Block returning the sound

Yield Parameters:

  • args

    Regex captures



23
24
25
# File 'lib/ray/sound_set.rb', line 23

def self.sound_set(regex, &block)
  Ray::SoundSet.add_set(regex, &block)
end

.stopObject

Stops ray



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'ext/ray.c', line 75

VALUE ray_stop(VALUE self) {
#ifdef HAVE_SDL_MIXER
   Mix_CloseAudio();
#endif

#ifdef HAVE_SDL_TTF
   /*
     Freeing a font after TTF_Quit causes a segfault, but, Ruby being
     garbage called, we can't be sure a font won't be freed later.
   */

   /* TTF_Quit(); */
#endif

   SDL_Quit();
   /* The pool is never drained on OSX */
   return Qnil;
}

.text_iconString?

Returns The window text icon.

Returns:

  • (String, nil)

    The window text icon



239
240
241
242
243
244
245
246
# File 'ext/ray.c', line 239

VALUE ray_text_icon(VALUE self) {
   char *icon = NULL;
   SDL_WM_GetCaption(NULL, &icon);

   if (!icon)
      return Qnil;
   return rb_str_new2(icon);   
}

.text_icon=(val) ⇒ Object

Sets the window text icon



252
253
254
255
256
257
258
259
260
261
# File 'ext/ray.c', line 252

VALUE ray_set_text_icon(VALUE self, VALUE icon) {
   char *title;
   
   if (!NIL_P(icon)) icon = rb_String(icon);

   SDL_WM_GetCaption(&title, NULL);
   SDL_WM_SetCaption(title, NIL_P(icon) ? NULL : StringValuePtr(icon));

   return icon;
}

.window_titleString?

Returns The window title.

Returns:

  • (String, nil)

    The window title



214
215
216
217
218
219
220
221
# File 'ext/ray.c', line 214

VALUE ray_window_title(VALUE self) {
   char *title = NULL;
   SDL_WM_GetCaption(&title, NULL);

   if (!title)
      return Qnil;
   return rb_str_new2(title);
}

.window_title=(title) ⇒ Object

Sets the window title



227
228
229
230
231
232
233
234
235
236
# File 'ext/ray.c', line 227

VALUE ray_set_window_title(VALUE self, VALUE title) {
   char *icon = NULL;

   if (!NIL_P(title)) title = rb_String(title);

   SDL_WM_GetCaption(NULL, &icon);
   SDL_WM_SetCaption(NIL_P(title) ? NULL : StringValuePtr(title), icon);

   return title;
}

Instance Method Details

#almostDSL::Matcher

Note:

the maximum and the minimum will only be computed once.

Returns A matcher matching a value close of x.

Examples:

on :win, almost(10_000, 500) do ... end
on :win, where { |x| x <= 10_000 + 500 && x >= 10_000 - 500 } do ... end

Returns:



74
75
76
77
# File 'lib/ray/dsl/matcher.rb', line 74

describe_matcher(:almost) do |x, precision|
  min, max = (x - precision), (x + precision)
  lambda { |o| (o <= max) && (o >= min) }
end

#anythingDSL::Matcher

Returns A matcher matching anything (always true).

Returns:

  • (DSL::Matcher)

    A matcher matching anything (always true)



52
53
54
# File 'lib/ray/dsl/matcher.rb', line 52

describe_matcher(:anything) do
  lambda { |o| true }
end

#colliding_with(x, y[, w, h]) ⇒ DSL::Matcher #colliding_with(rect) ⇒ DSL::Matcher #colliding_with(array) ⇒ DSL::Matcher

Returns A matching matching any rect colliding with the argument.

Returns:

  • (DSL::Matcher)

    A matching matching any rect colliding with the argument.



105
106
107
108
# File 'lib/ray/dsl/matcher.rb', line 105

describe_matcher(:colliding_with) do |*args|
  rect = args.size > 1 ? Ray::Rect.new(*args) : args.first.to_rect
  lambda { |o| o.collide? rect }
end

#inside(x, y[, w, h]) ⇒ DSL::Matcher #inside(rect) ⇒ DSL::Matcher #inside(array) ⇒ DSL::Matcher

Returns A matching matching any rect inside the argument.

Returns:

  • (DSL::Matcher)

    A matching matching any rect inside the argument.



84
85
86
87
# File 'lib/ray/dsl/matcher.rb', line 84

describe_matcher(:inside) do |*args|
  rect = args.size > 1 ? Ray::Rect.new(*args) : args.first.to_rect
  lambda { |o| o.inside? rect }
end

#keyDSL::Matcher

Returns A matcher matching the given key, which is a symbol like :space, :a, :b, :number, :letter, :arrow, …

Returns:

  • (DSL::Matcher)

    A matcher matching the given key, which is a symbol like :space, :a, :b, :number, :letter, :arrow, …



155
156
157
158
# File 'lib/ray/dsl/matcher.rb', line 155

describe_matcher(:key) do |sym|
  ary = KEYS[sym.to_sym]
  lambda { |o| ary.include? o }
end

#key_modDSL::Matcher

Returns A matcher matching the given modifier key (:rctrl, :lctrl, :rmeta, :lmeta, …).

Returns:

  • (DSL::Matcher)

    A matcher matching the given modifier key (:rctrl, :lctrl, :rmeta, :lmeta, …)



162
163
164
165
# File 'lib/ray/dsl/matcher.rb', line 162

describe_matcher(:key_mod) do |sym|
  ary = MOD[sym.to_sym]
  lambda { |o| ary.detect { |const| o & const } }
end

#less_thanDSL::Matcher

Returns A matcher matching anything that is less than x (comparaison using <).

Returns:

  • (DSL::Matcher)

    A matcher matching anything that is less than x (comparaison using <)



64
65
66
# File 'lib/ray/dsl/matcher.rb', line 64

describe_matcher(:less_than) do |x|
  lambda { |o| o < x }
end

#more_thanDSL::Matcher

Returns A matcher matching anything greater than x (comparaison using >).

Returns:

  • (DSL::Matcher)

    A matcher matching anything greater than x (comparaison using >)



58
59
60
# File 'lib/ray/dsl/matcher.rb', line 58

describe_matcher(:more_than) do |x|
  lambda { |o| o > x }
end

#outside(x, y[, w, h]) ⇒ DSL::Matcher #outside(rect) ⇒ DSL::Matcher #outside(array) ⇒ DSL::Matcher

Returns A matching matching any rect outside the argument.

Returns:

  • (DSL::Matcher)

    A matching matching any rect outside the argument.



94
95
96
97
# File 'lib/ray/dsl/matcher.rb', line 94

describe_matcher(:outside) do |*args|
  rect = args.size > 1 ? Ray::Rect.new(*args) : args.first.to_rect
  lambda { |o| o.outside? rect }
end