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
- .can_use_mode?(hash) ⇒ true, false
-
.create_window(hash) ⇒ Ray::Image
Creates a new window.
-
.describe_matcher(name, &create_block) ⇒ Object
Describes a new matcher.
-
.font_set(regex) {|*args, size| ... } ⇒ Object
Creates a new font set.
- .game(title, opts = {}, &block) ⇒ Object
-
.grab_input ⇒ 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.
-
.grab_input=(grab) ⇒ Object
Sets the grab input to true or false.
-
.has_audio_support? ⇒ true, false
True if Ray supports audio playback.
-
.has_font_support? ⇒ true, false
True if Ray supports ttf fonts.
-
.has_gfx_support? ⇒ true, false
True if Ray supports graphical effect like rotations.
-
.has_image_support? ⇒ true, false
True if Ray supports other image formats than BMP.
-
.icon=(icon) ⇒ Object
Sets the window icon.
-
.image_set(regex) {|*args| ... } ⇒ Object
Creates a new image set.
-
.init(opts = {}) ⇒ Object
Inits Ray.
-
.music_set(regex) {|*args| ... } ⇒ Object
Creates a new music set.
-
.screen ⇒ Ray::Image?
The current screen, created by create_window.
-
.sound_set(regex) {|*args| ... } ⇒ Object
Creates a new sound set.
-
.stop ⇒ Object
Stops ray.
-
.text_icon ⇒ String?
The window text icon.
-
.text_icon=(val) ⇒ Object
Sets the window text icon.
-
.window_title ⇒ String?
The window title.
-
.window_title=(title) ⇒ Object
Sets the window title.
Instance Method Summary collapse
-
#almost ⇒ DSL::Matcher
A matcher matching a value close of x.
-
#anything ⇒ DSL::Matcher
A matcher matching anything (always true).
-
#colliding_with ⇒ DSL::Matcher
A matching matching any rect colliding with the argument.
-
#inside ⇒ DSL::Matcher
A matching matching any rect inside the argument.
-
#key ⇒ DSL::Matcher
A matcher matching the given key, which is a symbol like :space, :a, :b, :number, :letter, :arrow, …
-
#key_mod ⇒ DSL::Matcher
A matcher matching the given modifier key (:rctrl, :lctrl, :rmeta, :lmeta, …).
-
#less_than ⇒ DSL::Matcher
A matcher matching anything that is less than x (comparaison using <).
-
#more_than ⇒ DSL::Matcher
A matcher matching anything greater than x (comparaison using >).
-
#outside ⇒ DSL::Matcher
A matching matching any rect outside the argument.
Class Method Details
.can_use_mode?(hash) ⇒ true, false
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
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.
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.
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_input ⇒ true, 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.
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
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.
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.
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.
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.
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
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.
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
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.
23 24 25 |
# File 'lib/ray/music_set.rb', line 23 def self.music_set(regex, &block) Ray::MusicSet.add_set(regex, &block) end |
.screen ⇒ Ray::Image?
Returns 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.
23 24 25 |
# File 'lib/ray/sound_set.rb', line 23 def self.sound_set(regex, &block) Ray::SoundSet.add_set(regex, &block) end |
.stop ⇒ Object
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_icon ⇒ String?
Returns 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
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_title ⇒ String?
Returns 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
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
#almost ⇒ DSL::Matcher
the maximum and the minimum will only be computed once.
Returns A matcher matching a value close of x.
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 |
#anything ⇒ DSL::Matcher
Returns 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.
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.
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 |
#key ⇒ DSL::Matcher
Returns 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_mod ⇒ DSL::Matcher
Returns 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_than ⇒ DSL::Matcher
Returns 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_than ⇒ DSL::Matcher
Returns 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.
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 |