ray 0.0.0.pre1

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.
data/ext/color.c ADDED
@@ -0,0 +1,147 @@
1
+ #include "ray.h"
2
+
3
+ VALUE ray_cColor = Qnil;
4
+
5
+ ray_color ray_rb2col(VALUE object) {
6
+ if (!RAY_IS_A(object, ray_cColor)) {
7
+ rb_raise(rb_eTypeError, "Can't convert %s into Ray::Color",
8
+ RAY_OBJ_CLASSNAME(object));
9
+ }
10
+
11
+ ray_color *ret = NULL;
12
+ Data_Get_Struct(object, ray_color, ret);
13
+
14
+ return *ret;
15
+ }
16
+
17
+ VALUE ray_col2rb(ray_color color) {
18
+ VALUE ret = rb_funcall(ray_cColor, RAY_METH("new"), 4,
19
+ INT2FIX(color.r),
20
+ INT2FIX(color.g),
21
+ INT2FIX(color.b),
22
+ INT2FIX(color.a));
23
+ return ret;
24
+ }
25
+
26
+ void ray_free_color(ray_color *color) {
27
+ free(color);
28
+ }
29
+
30
+ VALUE ray_alloc_color(VALUE self) {
31
+ ray_color *ptr = malloc(sizeof(ray_color));
32
+ VALUE ret = Data_Wrap_Struct(self, 0, ray_free_color, ptr);
33
+
34
+ return ret;
35
+ }
36
+
37
+ /*
38
+ @overload initialize(red, green, blue, alpha = 255)
39
+ Creates a new color. All the parameters must be integers between
40
+ 0 and 255. Alpha is the transparency (255: opaque, 0: invisible)
41
+ */
42
+ VALUE ray_init_color(int argc, VALUE *argv, VALUE self) {
43
+ VALUE r, g, b, a;
44
+ rb_scan_args(argc, argv, "31", &r, &g, &b, &a);
45
+ if (a == Qnil) a = INT2FIX(255);
46
+
47
+ ray_color *ret = NULL;
48
+ Data_Get_Struct(self, ray_color, ret);
49
+
50
+ ret->r = NUM2INT(r);
51
+ ret->g = NUM2INT(g);
52
+ ret->b = NUM2INT(b);
53
+ ret->a = NUM2INT(a);
54
+
55
+ return Qnil;
56
+ }
57
+
58
+ /* @return [Integer] red level */
59
+ VALUE ray_color_r(VALUE self) {
60
+ ray_color *ret;
61
+ Data_Get_Struct(self, ray_color, ret);
62
+
63
+ return INT2FIX(ret->r);
64
+ }
65
+
66
+ /* @return [Integer] green level */
67
+ VALUE ray_color_g(VALUE self) {
68
+ ray_color *ret;
69
+ Data_Get_Struct(self, ray_color, ret);
70
+
71
+ return INT2FIX(ret->g);
72
+ }
73
+
74
+ /* @return [Integer] blue level */
75
+ VALUE ray_color_b(VALUE self) {
76
+ ray_color *ret;
77
+ Data_Get_Struct(self, ray_color, ret);
78
+
79
+ return INT2FIX(ret->b);
80
+ }
81
+
82
+ /* @return [Integer] alpha level */
83
+ VALUE ray_color_a(VALUE self) {
84
+ ray_color *ret;
85
+ Data_Get_Struct(self, ray_color, ret);
86
+
87
+ return INT2FIX(ret->a);
88
+ }
89
+
90
+ /* Sets the red intensity */
91
+ VALUE ray_color_set_r(VALUE self, VALUE val) {
92
+ ray_color *ret;
93
+ Data_Get_Struct(self, ray_color, ret);
94
+
95
+ ret->r = NUM2INT(val);
96
+ return val;
97
+ }
98
+
99
+ /* Sets the green intensity */
100
+ VALUE ray_color_set_g(VALUE self, VALUE val) {
101
+ ray_color *ret;
102
+ Data_Get_Struct(self, ray_color, ret);
103
+
104
+ ret->g = NUM2INT(val);
105
+ return val;
106
+ }
107
+
108
+ /* Sets the blue intensity */
109
+ VALUE ray_color_set_b(VALUE self, VALUE val) {
110
+ ray_color *ret;
111
+ Data_Get_Struct(self, ray_color, ret);
112
+
113
+ ret->b = NUM2INT(val);
114
+ return val;
115
+ }
116
+
117
+ /* Sets the alpha intensity */
118
+ VALUE ray_color_set_a(VALUE self, VALUE val) {
119
+ ray_color *ret;
120
+ Data_Get_Struct(self, ray_color, ret);
121
+
122
+ ret->a = NUM2INT(val);
123
+ return val;
124
+ }
125
+
126
+ /*
127
+ Document-class: Ray::Color
128
+
129
+ Represents a color, which does not rely on the pixel format of any
130
+ surfaces.
131
+ */
132
+ void Init_ray_color() {
133
+ ray_cColor = rb_define_class_under(ray_mRay, "Color", rb_cObject);
134
+
135
+ rb_define_alloc_func(ray_cColor, ray_alloc_color);
136
+ rb_define_method(ray_cColor, "initialize", ray_init_color, -1);
137
+
138
+ rb_define_method(ray_cColor, "r", ray_color_r, 0);
139
+ rb_define_method(ray_cColor, "g", ray_color_g, 0);
140
+ rb_define_method(ray_cColor, "b", ray_color_b, 0);
141
+ rb_define_method(ray_cColor, "a", ray_color_a, 0);
142
+
143
+ rb_define_method(ray_cColor, "r=", ray_color_set_r, 1);
144
+ rb_define_method(ray_cColor, "g=", ray_color_set_g, 1);
145
+ rb_define_method(ray_cColor, "b=", ray_color_set_b, 1);
146
+ rb_define_method(ray_cColor, "a=", ray_color_set_a, 1);
147
+ }
data/ext/extconf.rb ADDED
@@ -0,0 +1,92 @@
1
+ require 'mkmf'
2
+
3
+ def find_macosx_framework(name)
4
+ paths = [
5
+ "/opt/local/Library/Frameworks",
6
+ "/Library/Frameworks",
7
+ "/Network/Library/Frameworks",
8
+ "/System/Library/Frameworks"
9
+ ]
10
+ paths.reverse!
11
+
12
+ paths.each do |dir|
13
+ dir = File.join(dir, "#{name}.framework")
14
+ next unless File.directory? dir
15
+
16
+ return dir
17
+ end
18
+
19
+ nil
20
+ end
21
+
22
+ def have_framework(name)
23
+ print "checking for framweork #{name}... "
24
+ ret = find_macosx_framework(name)
25
+
26
+ if ret
27
+ puts "yes"
28
+
29
+ $LDFLAGS << " -F#{File.dirname(ret)}"
30
+ $LDFLAGS << " -framework #{name}"
31
+ $CFLAGS << " -I#{File.join(ret, "Headers")}"
32
+ $CFLAGS << " -DHAVE_FRAMEWORK_#{name.upcase}"
33
+ else
34
+ puts "no"
35
+ end
36
+ ret
37
+ end
38
+
39
+ has_run_node = have_func("ruby_run_node")
40
+ has_run = have_func("ruby_run")
41
+
42
+ $CFLAGS << " -pedantic -Wall -std=c99 -Wno-unused-parameter"
43
+
44
+ unless RUBY_PLATFORM =~ /darwin/
45
+ unless have_library("SDL", "SDL_Init")
46
+ $stdder.puts "SDL not found"
47
+ end
48
+
49
+ unless (have_header("SDL/SDL.h") or have_header("SDL.h"))
50
+ $stderr.puts "SDL.h not found."
51
+ exit 1
52
+ end
53
+
54
+ unless have_library("SDLmain")
55
+ $stderr.puts "SDL_main not found."
56
+ exit 1
57
+ end
58
+
59
+ create_makefile("ray_ext")
60
+
61
+ data = File.read("Makefile").gsub("SDLMain.o", "")
62
+ open("Makefile", 'w') { |f| f.write(data) }
63
+ else
64
+ if !has_run and !has_run_node
65
+ $stderr.puts "Unsupported configuration: Mac OS X and a ruby " +
66
+ "implementation that does not implement ruby_run"
67
+ exit 1
68
+ end
69
+
70
+ $CFLAGS << " -DRAY_USE_FRAMEWORK"
71
+
72
+ unless have_framework("SDL") and have_framework("Cocoa") and
73
+ have_framework("Foundation")
74
+ $stderr.puts "missing frameworks"
75
+ exit 1
76
+ end
77
+
78
+ have_library("ruby", "ruby_init")
79
+
80
+ create_makefile("ray_ext")
81
+ end
82
+
83
+ if has_run or has_run_node
84
+ open("Makefile", 'a') do |file|
85
+ file.puts "ray: $(OBJS)"
86
+ file.puts "\t$(CC) -o ray $(OBJS) $(LIBPATH) $(DLDFLAGS) " +
87
+ "$(LOCAL_LIBS) $(LIBS) -lruby"
88
+ end
89
+
90
+ data = File.read("Makefile").gsub("all:", "all: ray")
91
+ open("Makefile", 'w') { |f| f.write(data) }
92
+ end
data/ext/image.c ADDED
@@ -0,0 +1,192 @@
1
+ #include "ray.h"
2
+
3
+ VALUE ray_cImage = Qnil;
4
+
5
+ ray_image *ray_rb2image(VALUE object) {
6
+ if (!RAY_IS_A(object, ray_cImage)) {
7
+ rb_raise(rb_eTypeError, "Can't convert %s into Ray::Image",
8
+ RAY_OBJ_CLASSNAME(object));
9
+ }
10
+
11
+ ray_image *ptr = NULL;
12
+ Data_Get_Struct(object, ray_image, ptr);
13
+
14
+ return ptr;
15
+ }
16
+
17
+ SDL_Surface *ray_rb2surface(VALUE object) {
18
+ return ray_rb2image(object)->surface;
19
+ }
20
+
21
+ /*
22
+ Creates a new image. You must call Ray.create_window before
23
+ this function.
24
+
25
+ @overload initialize(hash)
26
+ @option hash [Integer] :width Width of the surface
27
+ @option hash [Integer] :height Height of the surface
28
+
29
+ @option hash [Integer] :w Alias for width
30
+ @option hash [Integer] :h Alias for height
31
+
32
+ @option hash [Integer] :bits_per_pixel See Ray.create_window
33
+ @option hash [Integer] :pp Alias for bits_per_pixel
34
+
35
+ @option hash [true, false] :hw_surface See Ray.create_window
36
+ @option hash [true, false] :sw_surface See Ray.create_window
37
+ */
38
+ VALUE ray_init_image(VALUE self, VALUE arg) {
39
+ VALUE width = rb_hash_aref(arg, RAY_SYM("width"));
40
+ if (NIL_P(width)) width = rb_hash_aref(arg, RAY_SYM("w"));
41
+
42
+ VALUE height = rb_hash_aref(arg, RAY_SYM("height"));
43
+ if (NIL_P(height)) height = rb_hash_aref(arg, RAY_SYM("h"));
44
+
45
+ VALUE bitsperpixel = rb_hash_aref(arg, RAY_SYM("bits_per_pixel"));
46
+ if (NIL_P(bitsperpixel)) bitsperpixel = rb_hash_aref(arg, RAY_SYM("bpp"));
47
+ if (NIL_P(bitsperpixel)) bitsperpixel = INT2FIX(32);
48
+
49
+ uint32_t flags = 0;
50
+ if (RTEST(rb_hash_aref(arg, RAY_SYM("sw_surface"))))
51
+ flags = SDL_SWSURFACE;
52
+ else
53
+ flags = SDL_HWSURFACE;
54
+
55
+ ray_image *image = ray_rb2image(self);
56
+ image->surface = SDL_CreateRGBSurface(flags,
57
+ NUM2INT(width),
58
+ NUM2INT(height),
59
+ NUM2INT(bitsperpixel),
60
+ 0, 0, 0, 0);
61
+
62
+ if (!image->surface) {
63
+ rb_raise(rb_eRuntimeError, "Could not create the image (%s)",
64
+ SDL_GetError());
65
+ }
66
+
67
+ return Qnil;
68
+ }
69
+
70
+ void ray_free_image(ray_image *ptr) {
71
+ if (ptr->mustFree && ptr->surface) SDL_FreeSurface(ptr->surface);
72
+ free(ptr);
73
+ }
74
+
75
+ VALUE ray_create_image(SDL_Surface *surface) {
76
+ ray_image *ptr = malloc(sizeof(ray_image));
77
+ VALUE ret = Data_Wrap_Struct(ray_cImage, 0, ray_free_image, ptr);
78
+
79
+ ptr->self = ret;
80
+ ptr->surface = surface;
81
+
82
+ ptr->mustFree = 0;
83
+
84
+ return ret;
85
+ }
86
+
87
+ VALUE ray_alloc_image(VALUE self) {
88
+ ray_image *ptr = malloc(sizeof(ray_image));
89
+ VALUE ret = Data_Wrap_Struct(self, 0, ray_free_image, ptr);
90
+
91
+ ptr->self = ret;
92
+ ptr->surface = NULL;
93
+
94
+ ptr->mustFree = 1;
95
+
96
+ return ret;
97
+ }
98
+
99
+ /*
100
+ Fills the image with a given color.
101
+ @param [Ray::Color] col The color used to fill the image.
102
+ */
103
+ VALUE ray_image_fill(VALUE self, VALUE col) {
104
+ ray_color rcol = ray_rb2col(col);
105
+
106
+ SDL_Surface *surf = ray_rb2surface(self);
107
+ uint32_t specific_col = SDL_MapRGBA(surf->format,
108
+ rcol.r, rcol.g, rcol.b,
109
+ rcol.a);
110
+
111
+ SDL_FillRect(surf, NULL, specific_col);
112
+ return self;
113
+ }
114
+
115
+ /* Updates the image. */
116
+ VALUE ray_image_flip(VALUE self) {
117
+ SDL_Flip(ray_rb2surface(self));
118
+ return self;
119
+ }
120
+
121
+ /*
122
+ Blits the receiver on another image.
123
+
124
+ @option hash [Ray::Rect, Array] :at Rects in which the image will be
125
+ drawn. If an array is given, it
126
+ passed to Ray::Rect.new. Only the
127
+ position is read.
128
+ @option hash [Ray::Rect, Array] :rect Rects that will be copied.
129
+ If an array is given, it
130
+ passed to Ray::Rect.new.
131
+ If the size is (0, 0), it will
132
+ be reset to the image's size.
133
+ @option hash [Ray::Rect, Array] :from Alias for rect
134
+
135
+ @option hash [Ray::Image, required] :on The image on which the receiver should
136
+ be drawn.
137
+
138
+ @option hash [Ray::Image, required] :to Alias for on.
139
+ */
140
+ VALUE ray_image_blit(VALUE self, VALUE hash) {
141
+ SDL_Surface *origin = ray_rb2surface(self);
142
+
143
+ SDL_Rect from_rect = {0, 0, origin->w, origin->h};
144
+ SDL_Rect to_rect = {0, 0, 0, 0};
145
+
146
+ VALUE rect = rb_hash_aref(hash, RAY_SYM("at"));
147
+
148
+ if (RTEST(rb_obj_is_kind_of(rect, ray_cRect)))
149
+ to_rect = ray_rb2rect(rect);
150
+ else if (RTEST(rb_obj_is_kind_of(rect, rb_cArray)))
151
+ to_rect = ray_rb2rect(rb_apply(ray_cRect, RAY_METH("new"), rect));
152
+ else if (rect != Qnil) {
153
+ rb_raise(rb_eTypeError, "Can't convert %s into Ray::Rect",
154
+ RAY_OBJ_CLASSNAME(rect));
155
+ }
156
+
157
+ rect = Qnil;
158
+ rect = rb_hash_aref(hash, RAY_SYM("rect"));
159
+ if (rect == Qnil) rect = rb_hash_aref(hash, RAY_SYM("from"));
160
+
161
+ if (RAY_IS_A(rect, ray_cRect))
162
+ from_rect = ray_rb2rect(rect);
163
+ else if (RAY_IS_A(rect, rb_cArray))
164
+ from_rect = ray_rb2rect(rb_apply(ray_cRect, RAY_METH("new"), rect));
165
+ else if (!NIL_P(rect)) {
166
+ rb_raise(rb_eTypeError, "Can't convert %s into Ray::Rect",
167
+ RAY_OBJ_CLASSNAME(rect));
168
+ }
169
+
170
+ if (from_rect.w == 0 && from_rect.h == 0) {
171
+ from_rect.w = origin->w;
172
+ from_rect.h = origin->h;
173
+ }
174
+
175
+ VALUE surf = rb_hash_aref(hash, RAY_SYM("on"));
176
+ if (surf == Qnil) surf = rb_hash_aref(hash, RAY_SYM("to"));
177
+ SDL_BlitSurface(origin, &from_rect, ray_rb2surface(surf), &to_rect);
178
+
179
+ return surf;
180
+ }
181
+
182
+ void Init_ray_image() {
183
+ ray_cImage = rb_define_class_under(ray_mRay, "Image", rb_cObject);
184
+
185
+ rb_define_alloc_func(ray_cImage, ray_alloc_image);
186
+ rb_define_method(ray_cImage, "initialize", ray_init_image, 1);
187
+
188
+ rb_define_method(ray_cImage, "fill", ray_image_fill, 1);
189
+ rb_define_method(ray_cImage, "flip", ray_image_flip, 0);
190
+
191
+ rb_define_method(ray_cImage, "blit", ray_image_blit, 1);
192
+ }
data/ext/ray ADDED
Binary file
data/ext/ray.bundle ADDED
Binary file
data/ext/ray.c ADDED
@@ -0,0 +1,125 @@
1
+ #include "ray.h"
2
+
3
+ VALUE ray_mRay = Qnil;
4
+
5
+ /* Inits ray */
6
+ VALUE ray_init(VALUE self) {
7
+ SDL_Init(SDL_INIT_VIDEO);
8
+ return Qnil;
9
+ }
10
+
11
+ /* Stops ray */
12
+ VALUE ray_stop(VALUE self) {
13
+ SDL_Quit();
14
+ return Qnil;
15
+ }
16
+
17
+ /*
18
+ Creates a new window.
19
+
20
+ @note If both hw_surface and sws_urface are false, hw_surface
21
+ will be considered as true. :sw_surface should be true
22
+ if you want to acess
23
+
24
+ @return [Ray::Image] An image representing the window
25
+
26
+ @option hash [Integer] :width Width of the window
27
+ @option hash [Integer] :height Height of the window
28
+
29
+ @option hash [Integer] :w Alias for width
30
+ @option hash [Integer] :h Alias for height
31
+
32
+ @option hash [Integer] :bits_per_pixel Bits per pixel. Valid values are
33
+ 8, 15, 16, 24, and 32.
34
+ @option hash [Integer] :bpp Alias for bits_per_pixel
35
+
36
+ @option hash [true, false] :hw_surface Creates the surface in video memory
37
+ (default)
38
+ @option hash [true, false] :sw_surface Creates the surface in system memory
39
+ @option hash [true, false] :async_blit Enables asynchronous updates of the
40
+ the surface
41
+ @option hash [true, false] :double_buf Enables double buffering. Ignored
42
+ if sw_surface is set.
43
+ @option hash [true, false] :fullscreen Creates a full screen window.
44
+ @option hash [true, false] :resizable Creates a resizable window.
45
+ @option hash [true, false] :no_frame Disables window decoration if
46
+ possible.
47
+ */
48
+ VALUE ray_create_window(VALUE self, VALUE hash) {
49
+ VALUE width = rb_hash_aref(hash, RAY_SYM("width"));
50
+ VALUE height = rb_hash_aref(hash, RAY_SYM("height"));
51
+
52
+ if (NIL_P(width)) width = rb_hash_aref(hash, RAY_SYM("h"));
53
+ if (NIL_P(height)) height = rb_hash_aref(hash, RAY_SYM("w"));
54
+
55
+ if (NIL_P(width) || NIL_P(height))
56
+ rb_raise(rb_eArgError, "Missing parameter: width or height");
57
+
58
+ VALUE bitsperpixel = rb_hash_aref(hash, RAY_SYM("bits_per_pixel"));
59
+ if (NIL_P(bitsperpixel)) bitsperpixel = rb_hash_aref(hash, RAY_SYM("bpp"));
60
+
61
+ if (NIL_P(bitsperpixel))
62
+ bitsperpixel = INT2FIX(32);
63
+
64
+ uint32_t flags = 0;
65
+ if (RTEST(rb_hash_aref(hash, RAY_SYM("sw_surface"))))
66
+ flags |= SDL_SWSURFACE;
67
+ else
68
+ flags |= SDL_HWSURFACE; /* even if hwsurface was false and not nil */
69
+
70
+ if (RTEST(rb_hash_aref(hash, RAY_SYM("async_blit"))))
71
+ flags |= SDL_ASYNCBLIT;
72
+
73
+ if (!(flags & SDL_SWSURFACE) &&
74
+ RTEST(rb_hash_aref(hash, RAY_SYM("double_buf")))) {
75
+ flags |= SDL_DOUBLEBUF;
76
+ }
77
+
78
+ if (RTEST(rb_hash_aref(hash, RAY_SYM("fullscreen"))))
79
+ flags |= SDL_FULLSCREEN;
80
+
81
+ if (RTEST(rb_hash_aref(hash, RAY_SYM("no_frame"))))
82
+ flags |= SDL_NOFRAME;
83
+
84
+ SDL_Surface *screen = SDL_SetVideoMode(NUM2INT(width),
85
+ NUM2INT(height),
86
+ NUM2INT(bitsperpixel),
87
+ flags);
88
+ if (!screen) {
89
+ rb_raise(rb_eRuntimeError, "Could not create the window (%s)",
90
+ SDL_GetError());
91
+ }
92
+
93
+ return ray_create_image(screen);
94
+ }
95
+
96
+ void Init_ray_ext() {
97
+ ray_mRay = rb_define_module("Ray");
98
+
99
+ rb_define_module_function(ray_mRay, "init", ray_init, 0);
100
+ rb_define_module_function(ray_mRay, "stop", ray_stop, 0);
101
+ rb_define_module_function(ray_mRay, "create_window", ray_create_window, 1);
102
+
103
+ Init_ray_image();
104
+ Init_ray_color();
105
+ Init_ray_rect();
106
+ }
107
+
108
+ int main(int argc, char *argv[]) {
109
+ #if defined(HAVE_RUBY_RUN_NODE)
110
+ ruby_init();
111
+ Init_ray_ext();
112
+ ruby_run_node(ruby_options(argc, argv));
113
+ #elif defined(HAVE_RUBY_RUN)
114
+ ruby_init();
115
+ Init_ray_ext();
116
+ ruby_init_loadpath();
117
+ ruby_options(argc, argv);
118
+ ruby_run();
119
+ #else
120
+ fprintf(stderr, "Please use \"require 'ray'\" on this platform\n");
121
+ return 1;
122
+ #endif
123
+
124
+ return 0;
125
+ }