rubygame 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -49,15 +49,11 @@ In order to use Rubygame applications on Mac OS X, you must use a special wrappe
49
49
 
50
50
  == Installing Rubygame
51
51
  1. {Download and unpack Rubygame}[https://sourceforge.net/project/showfiles.php?group_id=172781].
52
- 2. (For Rubygame-1.1.0) From the rubygame directory:
53
-
54
- sudo ruby setup.rb all
55
-
56
- 3. (For Rubygame-2.0.0) From the rubygame directory:
52
+ 2. From within the rubygame directory:
57
53
 
58
54
  sudo rake install
59
55
 
60
- 4. Please refer to the README file for options that you can pass to setup.rb
56
+ 3. Please refer to the README file for options that you can pass to setup.rb
61
57
  or rake (depending on your version of Rubygame)
62
58
 
63
59
  == Running an application
@@ -96,19 +96,9 @@ the libraries.
96
96
  set LINK_FLAGS=-L \windows\system32 -lSDL
97
97
  set LIBRUBYARG_SHARED=-L \ruby\bin -lmsvcrt-ruby18
98
98
 
99
- set RUBYGAME_CONFIG=--no-sdl-config
100
-
101
99
  The -L parameter for LIBRUBYARG_SHARED may be different if you installed
102
100
  Ruby to a path other than C:/ruby.
103
101
 
104
- IMPORTANT: If you are missing an optional library (such as SDL_gfx), you need
105
- to add some extra flags on to the 'RUBYGAME_CONFIG=...' line. The flags you
106
- must add are of the style '--no-????' (without quotes), where ???? is the
107
- name of the missing library, dropping the 'SDL_' from in front. So, if you
108
- don't have SDL_gfx, the last line of your envsetup.bat should be this:
109
-
110
- set RUBYGAME_CONFIG=--no-sdl-config --no-gfx
111
-
112
102
  == Step 4: Compile and install Rubygame
113
103
 
114
104
  Open a command prompt and navigate to the root of your Rubygame source
@@ -117,8 +107,17 @@ directory. Type:
117
107
  envsetup.bat
118
108
  rake install
119
109
 
120
- Congratulations! If all goes well, you have built and installed Rubygame.
110
+ IMPORTANT: If you are missing an optional library (such as SDL_gfx), you must
111
+ disable optional features by passing a "no-???" command to rake before the word
112
+ "install". For example, to disable features that depend on SDL_gfx, you would
113
+ type this instead of the above:
114
+
115
+ envsetup.bat
116
+ rake no-gfx install
117
+
118
+ If all goes well, you have built and installed Rubygame.
121
119
  Try to execute <code>require 'rubygame'</code> in an irb session and run
122
120
  the provided samples to ensure that everything is acceptable.
123
121
 
124
- (Thanks to Ash Wilson (smashwilson) for contributing these instructions.)
122
+ (Thanks to Ash Wilson (smashwilson) for contributing the original version of
123
+ these instructions.)
@@ -59,21 +59,6 @@ VALUE rbgm_draw_fillpolygon(VALUE, VALUE, VALUE);
59
59
  * Clean up this ugly mess of code!
60
60
  */
61
61
 
62
- void extract_color(VALUE rgba, Uint8* r, Uint8* g, Uint8* b, Uint8* a)
63
- {
64
- rgba = convert_to_array(rgba);
65
- if(RARRAY(rgba)->len < 3)
66
- rb_raise(rb_eArgError,"color must be [r,g,b] or [r,g,b,a] form");
67
- *r = NUM2UINT(rb_ary_entry(rgba,0));
68
- *g = NUM2UINT(rb_ary_entry(rgba,1));
69
- *b = NUM2UINT(rb_ary_entry(rgba,2));
70
-
71
- if(RARRAY(rgba)->len > 3)
72
- *a = NUM2UINT(rb_ary_entry(rgba,3));
73
- else
74
- *a = 255;
75
- }
76
-
77
62
  void extract_xy(VALUE point, Sint16* x, Sint16* y)
78
63
  {
79
64
  point = convert_to_array(point);
@@ -97,7 +82,8 @@ void draw_line(VALUE target, VALUE pt1, VALUE pt2, VALUE rgba, int aa)
97
82
  extract_xy(pt1, &x1, &y1);
98
83
  extract_xy(pt2, &x2, &y2);
99
84
 
100
- extract_color(rgba, &r, &g, &b, &a);
85
+ rgba = convert_color( rgba );
86
+ extract_rgba_u8_as_u8(rgba, &r, &g, &b, &a);
101
87
 
102
88
  Data_Get_Struct(target,SDL_Surface,dest);
103
89
  //printf("dest: %dx%d\n",dest->w,dest->h);
@@ -171,7 +157,8 @@ void draw_rect(VALUE target, VALUE pt1, VALUE pt2, VALUE rgba, int fill)
171
157
  extract_xy(pt1, &x1, &y1);
172
158
  extract_xy(pt2, &x2, &y2);
173
159
 
174
- extract_color(rgba, &r, &g, &b, &a);
160
+ rgba = convert_color( rgba );
161
+ extract_rgba_u8_as_u8(rgba, &r, &g, &b, &a);
175
162
 
176
163
  Data_Get_Struct(target,SDL_Surface,dest);
177
164
  //printf("dest: %dx%d\n",dest->w,dest->h);
@@ -235,7 +222,8 @@ void draw_circle(VALUE target, VALUE center, VALUE radius, VALUE rgba, int aa, i
235
222
  extract_xy(center, &x, &y);
236
223
  rad = NUM2INT(radius);
237
224
 
238
- extract_color(rgba, &r, &g, &b, &a);
225
+ rgba = convert_color( rgba );
226
+ extract_rgba_u8_as_u8(rgba, &r, &g, &b, &a);
239
227
 
240
228
  Data_Get_Struct(target,SDL_Surface,dest);
241
229
  //printf("dest: %dx%d\n",dest->w,dest->h);
@@ -318,7 +306,8 @@ void draw_ellipse(VALUE target, VALUE center, VALUE radii, VALUE rgba, int aa, i
318
306
  extract_xy(center, &x, &y);
319
307
  extract_xy(radii, &radx, &rady);
320
308
 
321
- extract_color(rgba, &r, &g, &b, &a);
309
+ rgba = convert_color( rgba );
310
+ extract_rgba_u8_as_u8(rgba, &r, &g, &b, &a);
322
311
 
323
312
  Data_Get_Struct(target,SDL_Surface,dest);
324
313
 
@@ -403,7 +392,8 @@ void draw_pie(VALUE target, VALUE center, VALUE radius, VALUE angles, VALUE rgba
403
392
  extract_xy(angles, &start, &end);
404
393
  rad = NUM2INT(radius);
405
394
 
406
- extract_color(rgba, &r, &g, &b, &a);
395
+ rgba = convert_color( rgba );
396
+ extract_rgba_u8_as_u8(rgba, &r, &g, &b, &a);
407
397
 
408
398
  Data_Get_Struct(target,SDL_Surface,dest);
409
399
 
@@ -431,7 +421,7 @@ void draw_pie(VALUE target, VALUE center, VALUE radius, VALUE angles, VALUE rgba
431
421
 
432
422
  /*
433
423
  * call-seq:
434
- * draw_pie(center, radius, angles, color)
424
+ * draw_arc(center, radius, angles, color)
435
425
  *
436
426
  * Draw a non-solid arc (part of a circle), given the coordinates of
437
427
  * its center, radius, and starting/ending angles.
@@ -499,7 +489,8 @@ void draw_polygon(VALUE target, VALUE points, VALUE rgba, int aa, int fill)
499
489
  extract_xy(each_point, &(x[loop]), &(y[loop]));
500
490
  }
501
491
 
502
- extract_color(rgba, &r, &g, &b, &a);
492
+ rgba = convert_color( rgba );
493
+ extract_rgba_u8_as_u8(rgba, &r, &g, &b, &a);
503
494
 
504
495
  Data_Get_Struct(target,SDL_Surface,dest);
505
496
 
@@ -63,7 +63,6 @@
63
63
  #endif
64
64
 
65
65
  extern void Init_rubygame_gfx();
66
- extern void extract_color(VALUE, Uint8*, Uint8*, Uint8*, Uint8*);
67
66
  extern void extract_xy(VALUE, Sint16*, Sint16*);
68
67
 
69
68
  extern void draw_line(VALUE, VALUE, VALUE, VALUE, int);
@@ -35,6 +35,8 @@ VALUE rbgm_screen_getsurface(VALUE);
35
35
  VALUE rbgm_screen_getcaption(VALUE);
36
36
  VALUE rbgm_screen_setcaption(VALUE, VALUE);
37
37
 
38
+ VALUE rbgm_screen_seticon(VALUE, VALUE);
39
+
38
40
  VALUE rbgm_screen_update(int, VALUE*, VALUE);
39
41
  VALUE rbgm_screen_updaterects(VALUE, VALUE);
40
42
  VALUE rbgm_screen_flip(VALUE);
@@ -190,6 +192,31 @@ VALUE rbgm_screen_setcaption(VALUE self, VALUE title)
190
192
  return self;
191
193
  }
192
194
 
195
+ /* call-seq:
196
+ * icon = icon
197
+ *
198
+ * Sets the window icon for the Screen.
199
+ *
200
+ * icon:: a Rubygame::Surface to be displayed at the top of the Rubygame
201
+ * window (when not in fullscreen mode), and in other OS-specific
202
+ * areas (like the taskbar entry). If omitted or +nil+, no icon
203
+ * will be shown at all.
204
+ *
205
+ * NOTE: The SDL docs state that icons on Win32 systems must be 32x32 pixels.
206
+ * That may or may not be true anymore, but you might want to consider it
207
+ * when creating games to run on Windows.
208
+ *
209
+ */
210
+ VALUE rbgm_screen_seticon(VALUE self, VALUE data)
211
+ {
212
+ SDL_Surface *icon;
213
+
214
+ Data_Get_Struct(data, SDL_Surface, icon);
215
+ SDL_WM_SetIcon(icon, NULL);
216
+
217
+ return self;
218
+ }
219
+
193
220
  /* call-seq:
194
221
  * update()
195
222
  * update(rect)
@@ -290,7 +317,7 @@ VALUE rbgm_screen_updaterects(VALUE self, VALUE array_rects)
290
317
  /* initialize the array of Rects from array_rects */
291
318
  for( i=0; i < num_rects; i++ )
292
319
  {
293
- each_rect = rb_ary_entry(array_rects,i);
320
+ each_rect = convert_to_array(rb_ary_entry(array_rects,i));
294
321
 
295
322
  Sint16 x,y,left,top,right,bottom;
296
323
  Uint16 w,h;
@@ -404,6 +431,7 @@ void Rubygame_Init_Screen()
404
431
  /* Screen methods */
405
432
  rb_define_method(cScreen,"title",rbgm_screen_getcaption,0);
406
433
  rb_define_method(cScreen,"title=",rbgm_screen_setcaption,1);
434
+ rb_define_method(cScreen,"icon=",rbgm_screen_seticon,1);
407
435
  rb_define_method(cScreen,"update",rbgm_screen_update,-1);
408
436
  rb_define_method(cScreen,"update_rects",rbgm_screen_updaterects,1);
409
437
  rb_define_method(cScreen,"flip",rbgm_screen_flip,0);
@@ -31,6 +31,8 @@ extern VALUE rbgm_screen_getsurface(VALUE);
31
31
  extern VALUE rbgm_screen_getcaption(VALUE);
32
32
  extern VALUE rbgm_screen_setcaption(VALUE, VALUE);
33
33
 
34
+ extern VALUE rbgm_screen_seticon(VALUE, VALUE);
35
+
34
36
  extern VALUE rbgm_screen_update(int, VALUE*, VALUE);
35
37
  extern VALUE rbgm_screen_updaterects(VALUE, VALUE);
36
38
  extern VALUE rbgm_screen_flip(VALUE);
@@ -27,6 +27,7 @@ VALUE cSurface;
27
27
  VALUE cRect;
28
28
  VALUE eSDLError;
29
29
  SDL_Rect *make_rect(int, int, int, int);
30
+ SDL_Color make_sdl_color(VALUE);
30
31
  int init_video_system();
31
32
  void Init_rubygame_shared();
32
33
 
@@ -91,6 +92,62 @@ VALUE convert_to_array(VALUE val)
91
92
  return v;
92
93
  }
93
94
 
95
+ /* Takes a Color, Array, or color name (Symbol or String).
96
+ * Returns an RGBA Array, or raises eTypeError if it can't.
97
+ */
98
+ VALUE convert_color(VALUE color)
99
+ {
100
+ if( rb_respond_to(color, rb_intern("to_sdl_rgba_ary")) )
101
+ {
102
+ return rb_funcall( color, rb_intern("to_sdl_rgba_ary"), 0 );
103
+ }
104
+ else if( rb_respond_to(color, rb_intern("to_ary")) )
105
+ {
106
+ return convert_to_array( color );
107
+ }
108
+ else if( TYPE(color) == T_SYMBOL || TYPE(color) == T_STRING )
109
+ {
110
+ VALUE mColor = rb_const_get( mRubygame, rb_intern("Color") );
111
+ return convert_color( rb_funcall( mColor, rb_intern("[]"), 1, color) );
112
+ }
113
+ else
114
+ {
115
+ rb_raise(rb_eTypeError, "unsupported type %s for color",
116
+ rb_obj_classname(color));
117
+ }
118
+ }
119
+
120
+ SDL_Color make_sdl_color(VALUE vcolor)
121
+ {
122
+ SDL_Color color;
123
+ vcolor = convert_color(vcolor);
124
+ extract_rgb_u8_as_u8(vcolor, &(color.r), &(color.g), &(color.b));
125
+ return color;
126
+ }
127
+
128
+ void extract_rgb_u8_as_u8(VALUE color, Uint8 *r, Uint8 *g, Uint8 *b)
129
+ {
130
+ *r = NUM2UINT(rb_ary_entry(color, 0));
131
+ *g = NUM2UINT(rb_ary_entry(color, 1));
132
+ *b = NUM2UINT(rb_ary_entry(color, 2));
133
+ }
134
+
135
+ void extract_rgba_u8_as_u8(VALUE color, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a)
136
+ {
137
+ *r = NUM2UINT(rb_ary_entry(color, 0));
138
+ *g = NUM2UINT(rb_ary_entry(color, 1));
139
+ *b = NUM2UINT(rb_ary_entry(color, 2));
140
+
141
+ if( RARRAY(color)->len > 3 )
142
+ {
143
+ *a = NUM2UINT(rb_ary_entry(color, 3));
144
+ }
145
+ else
146
+ {
147
+ *a = 255;
148
+ }
149
+ }
150
+
94
151
  /* --
95
152
  *
96
153
  * call-seq:
@@ -30,10 +30,16 @@ extern VALUE mRubygame;
30
30
  extern VALUE eSDLError;
31
31
  extern VALUE cSurface;
32
32
  extern VALUE cRect;
33
+
33
34
  extern SDL_Rect *make_rect(int, int, int, int);
34
35
  extern VALUE make_symbol(char *);
35
36
  extern Uint32 collapse_flags(VALUE);
36
37
  extern VALUE convert_to_array(VALUE);
38
+
39
+ extern SDL_Color make_sdl_color(VALUE);
40
+ extern void extract_rgb_u8_as_u8(VALUE, Uint8*, Uint8*, Uint8*);
41
+ extern void extract_rgba_u8_as_u8(VALUE, Uint8*, Uint8*, Uint8*, Uint8*);
42
+
37
43
  extern int init_video_system();
38
44
  extern void Init_rubygame_shared();
39
45
 
@@ -379,10 +379,8 @@ VALUE rbgm_surface_set_colorkey( int argc, VALUE *argv, VALUE self)
379
379
 
380
380
  if( RTEST(vcolor) )
381
381
  {
382
- vcolor = convert_to_array(vcolor);
383
- r = NUM2UINT(rb_ary_entry(vcolor,0));
384
- g = NUM2UINT(rb_ary_entry(vcolor,1));
385
- b = NUM2UINT(rb_ary_entry(vcolor,2));
382
+ vcolor = convert_color(vcolor);
383
+ extract_rgb_u8_as_u8(vcolor, &r, &g, &b);
386
384
  color = SDL_MapRGB(surf->format, r,g,b);
387
385
  }
388
386
  else
@@ -508,20 +506,9 @@ VALUE rbgm_surface_fill( int argc, VALUE *argv, VALUE self )
508
506
 
509
507
  rb_scan_args(argc, argv, "11", &vcolor, &vrect);
510
508
 
511
- vcolor = convert_to_array(vcolor);
512
- r = NUM2UINT(rb_ary_entry(vcolor,0));
513
- g = NUM2UINT(rb_ary_entry(vcolor,1));
514
- b = NUM2UINT(rb_ary_entry(vcolor,2));
515
- /* if the array is larger than [R,G,B], it should be [R,G,B,A] */
516
- if(RARRAY(vcolor)->len > 3)
517
- {
518
- a = NUM2UINT(rb_ary_entry(vcolor,3));
519
- color = SDL_MapRGBA(surf->format, r,g,b,a);
520
- }
521
- else
522
- {
523
- color = SDL_MapRGB(surf->format, r,g,b);
524
- }
509
+ vcolor = convert_color(vcolor);
510
+ extract_rgba_u8_as_u8(vcolor, &r, &g, &b, &a);
511
+ color = SDL_MapRGBA(surf->format, r,g,b,a);
525
512
 
526
513
  if( NIL_P(vrect) )
527
514
  {
@@ -641,6 +628,57 @@ VALUE rbgm_surface_getat( int argc, VALUE *argv, VALUE self )
641
628
  return rb_ary_new3(4,UINT2NUM(r),UINT2NUM(g),UINT2NUM(b),UINT2NUM(a));
642
629
  }
643
630
 
631
+
632
+ /*
633
+ * call-seq:
634
+ * set_at([x,y], color)
635
+ *
636
+ * Set the color of the pixel at the given coordinate.
637
+ *
638
+ * color can be one of:
639
+ * * an Array, [r,g,b] or [r,g,b,a] with each component in 0-255
640
+ * * an instance of ColorRGB, ColorHSV, etc.
641
+ * * the name of a color in Rubygame::Color, as a Symbol or String
642
+ *
643
+ * Raises IndexError if the coordinates are out of bounds.
644
+ *
645
+ *--
646
+ *
647
+ * I'm lazy and just using SDL_FillRect. ;-P
648
+ * It's easier and less bug-prone this way.
649
+ * I have no idea about speed comparisons.
650
+ *
651
+ */
652
+ VALUE rbgm_surface_setat( int argc, VALUE *argv, VALUE self )
653
+ {
654
+ SDL_Surface *surf;
655
+ SDL_Rect *rect;
656
+ Uint32 color;
657
+ Uint8 r,g,b,a;
658
+ VALUE vpos, vcolor;
659
+
660
+ Data_Get_Struct(self, SDL_Surface, surf);
661
+
662
+ rb_scan_args(argc, argv, "2", &vpos, &vcolor);
663
+
664
+ vcolor = convert_color(vcolor);
665
+ extract_rgba_u8_as_u8(vcolor, &r, &g, &b, &a);
666
+ color = SDL_MapRGBA(surf->format, r,g,b,a);
667
+
668
+ vpos = convert_to_array(vpos);
669
+ rect = make_rect( NUM2INT(rb_ary_entry(vpos,0)),
670
+ NUM2INT(rb_ary_entry(vpos,1)),
671
+ 1, 1 );
672
+
673
+ SDL_FillRect(surf,rect,color);
674
+
675
+ free(rect);
676
+
677
+ return self;
678
+ }
679
+
680
+
681
+
644
682
  /*
645
683
  * call-seq:
646
684
  * pixels -> String
@@ -721,6 +759,7 @@ VALUE rbgm_surface_set_clip( VALUE self, VALUE clip )
721
759
  );
722
760
 
723
761
  SDL_SetClipRect(surf,rect);
762
+ free(rect);
724
763
  }
725
764
 
726
765
  return self;
@@ -1081,6 +1120,7 @@ void Rubygame_Init_Surface()
1081
1120
  rb_define_method(cSurface,"blit",rbgm_surface_blit,-1);
1082
1121
  rb_define_method(cSurface,"fill",rbgm_surface_fill,-1);
1083
1122
  rb_define_method(cSurface,"get_at",rbgm_surface_getat,-1);
1123
+ rb_define_method(cSurface,"set_at",rbgm_surface_setat,-1);
1084
1124
  rb_define_method(cSurface,"pixels",rbgm_surface_pixels,0);
1085
1125
  rb_define_method(cSurface,"clip",rbgm_surface_get_clip,0);
1086
1126
  rb_define_method(cSurface,"clip=",rbgm_surface_set_clip,1);
@@ -353,19 +353,6 @@ VALUE rbgm_ttf_size_unicode(VALUE self, VALUE string)
353
353
  return result;
354
354
  }
355
355
 
356
- /*
357
- * Helper function for color handling for the rendering functions.
358
- */
359
-
360
- static void RBGM_array_to_color(SDL_Color * color, VALUE arr) {
361
- if( RTEST(arr) )
362
- {
363
- arr = convert_to_array(arr);
364
- color->r = NUM2UINT(rb_ary_entry(arr, 0));
365
- color->g = NUM2UINT(rb_ary_entry(arr, 1));
366
- color->b = NUM2UINT(rb_ary_entry(arr, 2));
367
- }
368
- }
369
356
 
370
357
  /*
371
358
  *--
@@ -397,8 +384,12 @@ VALUE rbgm_ttf_render(int argc, VALUE *argv, VALUE self)
397
384
 
398
385
  Data_Get_Struct(self,TTF_Font,font);
399
386
 
400
- RBGM_array_to_color(&fore, vfg);
401
- RBGM_array_to_color(&back, vbg);
387
+ fore = make_sdl_color(vfg);
388
+
389
+ if( RTEST(vbg) )
390
+ {
391
+ back = make_sdl_color(vbg);
392
+ }
402
393
 
403
394
  if( RTEST(vaa) ) /* anti-aliasing enabled */
404
395
  {
@@ -452,8 +443,12 @@ VALUE rbgm_ttf_render_utf8(int argc, VALUE *argv, VALUE self)
452
443
 
453
444
  Data_Get_Struct(self,TTF_Font,font);
454
445
 
455
- RBGM_array_to_color(&fore, vfg);
456
- RBGM_array_to_color(&back, vbg);
446
+ fore = make_sdl_color(vfg);
447
+
448
+ if( RTEST(vbg) )
449
+ {
450
+ back = make_sdl_color(vbg);
451
+ }
457
452
 
458
453
  if( RTEST(vaa) ) /* anti-aliasing enabled */
459
454
  {
@@ -507,8 +502,12 @@ VALUE rbgm_ttf_render_unicode(int argc, VALUE *argv, VALUE self)
507
502
 
508
503
  Data_Get_Struct(self,TTF_Font,font);
509
504
 
510
- RBGM_array_to_color(&fore, vfg);
511
- RBGM_array_to_color(&back, vbg);
505
+ fore = make_sdl_color(vfg);
506
+
507
+ if( RTEST(vbg) )
508
+ {
509
+ back = make_sdl_color(vbg);
510
+ }
512
511
 
513
512
  if( RTEST(vaa) ) /* anti-aliasing enabled */
514
513
  {