rays 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,28 @@
1
+ #include <rucy.h>
2
+ #include "defs.h"
3
+
4
+
5
+ using namespace Rucy;
6
+
7
+
8
+ void Init_rays ();
9
+ void Init_bitmap ();
10
+ void Init_texture ();
11
+ void Init_image ();
12
+ void Init_font ();
13
+ void Init_painter ();
14
+
15
+
16
+ extern "C" void
17
+ Init_native ()
18
+ {
19
+ if (!Rucy::init())
20
+ raise(rb_eLoadError, "Rucy::init() failed.");
21
+
22
+ Init_rays();
23
+ Init_bitmap();
24
+ Init_texture();
25
+ Init_image();
26
+ Init_font();
27
+ Init_painter();
28
+ }
@@ -0,0 +1,554 @@
1
+ #include "rays/ruby/painter.h"
2
+
3
+
4
+ #include <rucy.h>
5
+ #include <rays/ruby/image.h>
6
+ #include <rays/ruby/font.h>
7
+ #include "defs.h"
8
+
9
+
10
+ using namespace Rucy;
11
+
12
+ using Rays::coord;
13
+
14
+
15
+ namespace Rays
16
+ {
17
+
18
+
19
+ static Class cPainter;
20
+
21
+ Class
22
+ painter_class ()
23
+ {
24
+ return cPainter;
25
+ }
26
+
27
+
28
+ }// Rays
29
+
30
+
31
+ namespace Rucy
32
+ {
33
+
34
+
35
+ Value
36
+ value (const Rays::Painter& painter)
37
+ {
38
+ return new_type<Rays::Painter>(
39
+ Rays::painter_class(), new Rays::Painter(painter));
40
+ }
41
+
42
+
43
+ }// Rucy
44
+
45
+
46
+ #define this to<Rays::Painter*>(self)
47
+
48
+ #define CHECK CHECK_OBJECT(self, Rays::Painter, Rays::painter_class())
49
+
50
+
51
+ static
52
+ VALUE alloc(VALUE klass)
53
+ {
54
+ return new_type<Rays::Painter>(klass, new Rays::Painter);
55
+ }
56
+
57
+ static
58
+ VALUE canvas(VALUE self, VALUE x, VALUE y, VALUE width, VALUE height)
59
+ {
60
+ CHECK;
61
+
62
+ coord xx = to<coord>(x);
63
+ coord yy = to<coord>(y);
64
+ coord ww = to<coord>(width);
65
+ coord hh = to<coord>(height);
66
+ if (!this->canvas(xx, yy, ww, hh))
67
+ error("Painter#canvas(%f, %f, %f, %f) failed.", xx, yy, ww, hh);
68
+
69
+ return self;
70
+ }
71
+
72
+ static
73
+ VALUE begin(VALUE self)
74
+ {
75
+ CHECK;
76
+
77
+ if (!this->begin())
78
+ error("Painter#begin() failed.");
79
+
80
+ return self;
81
+ }
82
+
83
+ static
84
+ VALUE end(VALUE self)
85
+ {
86
+ CHECK;
87
+
88
+ if (!this->end())
89
+ error("Painter#end() failed.");
90
+
91
+ return self;
92
+ }
93
+
94
+ static
95
+ VALUE push_matrix(VALUE self)
96
+ {
97
+ CHECK;
98
+
99
+ if (!this->push_matrix())
100
+ error("Painter#push_matrix() failed.");
101
+
102
+ return self;
103
+ }
104
+
105
+ static
106
+ VALUE pop_matrix(VALUE self)
107
+ {
108
+ CHECK;
109
+
110
+ if (!this->pop_matrix())
111
+ error("Painter#pop_matrix() failed.");
112
+
113
+ return self;
114
+ }
115
+
116
+
117
+ static
118
+ VALUE line(VALUE self, VALUE x1_, VALUE y1_, VALUE x2_, VALUE y2_)
119
+ {
120
+ CHECK;
121
+
122
+ coord x1 = to<coord>(x1_);
123
+ coord y1 = to<coord>(y1_);
124
+ coord x2 = to<coord>(x2_);
125
+ coord y2 = to<coord>(y2_);
126
+ if (!this->line(x1, y1, x2, y2))
127
+ error("Painter#line(%f, %f, %f, %f) failed.", x1, y1, x2, y2);
128
+
129
+ return self;
130
+ }
131
+
132
+ static
133
+ VALUE rect(VALUE self, VALUE x, VALUE y, VALUE width, VALUE height)
134
+ {
135
+ CHECK;
136
+
137
+ coord xx = to<coord>(x);
138
+ coord yy = to<coord>(y);
139
+ coord ww = to<coord>(width);
140
+ coord hh = to<coord>(height);
141
+ if (!this->rect(xx, yy, ww, hh))
142
+ error("Painter#rect(%f, %f, %f, %f) failed.", xx, yy, ww, hh);
143
+
144
+ return self;
145
+ }
146
+
147
+ static
148
+ VALUE ellipse(VALUE self)
149
+ {
150
+ CHECK;
151
+ if (argc < 3 || 6 < argc)
152
+ arg_count_error("Painter#ellipse", argc, 4, 5, 6);
153
+
154
+ coord xx = to<coord>(argv[0]);
155
+ coord yy = to<coord>(argv[1]);
156
+ coord ww = to<coord>(argv[2]);
157
+ coord hh = (argc >= 4) ? to<coord>(argv[3]) : 0;
158
+ coord min_ = (argc >= 5) ? to<coord>(argv[4]) : 0;
159
+ uint nseg = (argc >= 6) ? to<uint>(argv[5]) : 0;
160
+ if (!this->ellipse(xx, yy, ww, hh, min_, nseg))
161
+ {
162
+ error(
163
+ "Painter#ellipse(%f, %f, %f, %f, %f, %d) failed.",
164
+ xx, yy, ww, hh, min_, nseg);
165
+ }
166
+
167
+ return self;
168
+ }
169
+
170
+ static
171
+ VALUE arc(VALUE self)
172
+ {
173
+ CHECK;
174
+ if (argc < 3 || 8 < argc)
175
+ arg_count_error("Painter#ellipse", argc, 4, 5, 6, 7, 8);
176
+
177
+ coord xx = to<coord>(argv[0]);
178
+ coord yy = to<coord>(argv[1]);
179
+ coord ww = to<coord>(argv[2]);
180
+ coord hh = (argc >= 4) ? to<coord>(argv[3]) : 0;
181
+ float from = (argc >= 5) ? to<float>(argv[4]) : 0;
182
+ float to_ = (argc >= 6) ? to<float>(argv[5]) : 360;
183
+ coord min_ = (argc >= 7) ? to<coord>(argv[6]) : 0;
184
+ uint nseg = (argc >= 8) ? to<uint>(argv[7]) : 0;
185
+ if (!this->arc(xx, yy, ww, hh, from, to_, min_, nseg))
186
+ {
187
+ error(
188
+ "Painter#arc(%f, %f, %f, %f, %f, %f, %f, %d) failed.",
189
+ xx, yy, ww, hh, from, to_, min_, nseg);
190
+ }
191
+
192
+ return self;
193
+ }
194
+
195
+ static
196
+ VALUE image(VALUE self)
197
+ {
198
+ CHECK;
199
+ if (argc != 1 && argc != 3 && argc != 5 && argc != 7 && argc != 9)
200
+ arg_count_error("Painter#image", argc, 1, 3, 5, 7, 9);
201
+
202
+ const Rays::Image* image = to<Rays::Image*>(argv[0]);
203
+ if (!image)
204
+ argument_error("%s is not an image.", argv[0].inspect().c_str());
205
+
206
+ if (argc == 1)
207
+ {
208
+ if (!this->image(*image))
209
+ error("Painter#image(%s) failed.", argv[0].inspect().c_str());
210
+ }
211
+ else if (argc == 3)
212
+ {
213
+ coord x = to<coord>(argv[1]), y = to<coord>(argv[2]);
214
+ if (!this->image(*image, x, y))
215
+ {
216
+ error(
217
+ "Painter#image(%s, %f, %f) failed.",
218
+ argv[0].inspect().c_str(), x, y);
219
+ }
220
+ }
221
+ else if (argc == 5)
222
+ {
223
+ coord x = to<coord>(argv[1]), w = to<coord>(argv[3]);
224
+ coord y = to<coord>(argv[2]), h = to<coord>(argv[4]);
225
+ if (!this->image(*image, x, y, w, h))
226
+ {
227
+ error(
228
+ "Painter#image(%s, %f, %f, %f, %f) failed.",
229
+ argv[0].inspect().c_str(), x, y, w, h);
230
+ }
231
+ }
232
+ else if (argc == 7)
233
+ {
234
+ coord sx = to<coord>(argv[1]), dx = to<coord>(argv[5]);
235
+ coord sy = to<coord>(argv[2]), dy = to<coord>(argv[6]);
236
+ coord sw = to<coord>(argv[3]);
237
+ coord sh = to<coord>(argv[4]);
238
+ if (!this->image(*image, sx, sy, sw, sh, dx, dy))
239
+ {
240
+ error(
241
+ "Painter#image(%s, %f, %f, %f, %f, %f, %f) failed.",
242
+ argv[0].inspect().c_str(), sx, sy, sw, sh, dx, dy);
243
+ }
244
+ }
245
+ else if (argc == 9)
246
+ {
247
+ coord sx = to<coord>(argv[1]), dx = to<coord>(argv[5]);
248
+ coord sy = to<coord>(argv[2]), dy = to<coord>(argv[6]);
249
+ coord sw = to<coord>(argv[3]), dw = to<coord>(argv[7]);
250
+ coord sh = to<coord>(argv[4]), dh = to<coord>(argv[8]);
251
+ if (!this->image(*image, sx, sy, sw, sh, dx, dy, dw, dh))
252
+ {
253
+ error(
254
+ "Painter#image(%s, %f, %f, %f, %f, %f, %f, %f, %f) failed.",
255
+ argv[0].inspect().c_str(), sx, sy, sw, sh, dx, dy, dw, dh);
256
+ }
257
+ }
258
+
259
+ return self;
260
+ }
261
+
262
+ static
263
+ VALUE text(VALUE self)
264
+ {
265
+ CHECK;
266
+ if (argc != 1 && argc != 3 && argc != 4 && argc != 5 && argc != 7)
267
+ arg_count_error("Painter#text", argc, 1, 3, 4, 5, 7);
268
+
269
+ if (argc == 1)
270
+ {
271
+ if (!this->text(argv[0].c_str()))
272
+ error("Painter#text(%s) failed.", argv[0].inspect().c_str());
273
+ }
274
+ else if (argc == 3)
275
+ {
276
+ coord x = to<coord>(argv[1]), y = to<coord>(argv[2]);
277
+ if (!this->text(argv[0].c_str(), x, y))
278
+ {
279
+ error(
280
+ "Painter#text(%s, %f, %f) failed.",
281
+ argv[0].inspect().c_str(), x, y);
282
+ }
283
+ }
284
+ else if (argc == 4)
285
+ {
286
+ const Rays::Font* font = to<Rays::Font*>(argv[3]);
287
+ if (!font || !*font)
288
+ error("Painter#text: invalid font.");
289
+
290
+ coord x = to<coord>(argv[1]), y = to<coord>(argv[2]);
291
+ if (!this->text(argv[0].c_str(), x, y, *font))
292
+ {
293
+ error(
294
+ "Painter#image(%s, %f, %f, %s) failed.",
295
+ argv[0].inspect().c_str(), x, y, argv[3].inspect().c_str());
296
+ }
297
+ }
298
+ else if (argc == 5)
299
+ {
300
+ coord x = to<coord>(argv[1]), w = to<coord>(argv[3]);
301
+ coord y = to<coord>(argv[2]), h = to<coord>(argv[4]);
302
+ if (!this->text(argv[0].c_str(), x, y, w, h))
303
+ {
304
+ error(
305
+ "Painter#text(%s, %f, %f, %f, %f) failed.",
306
+ argv[0].inspect().c_str(), x, y, w, h);
307
+ }
308
+ }
309
+ else if (argc == 7)
310
+ {
311
+ const Rays::Font* font = to<Rays::Font*>(argv[3]);
312
+ if (!font || !*font)
313
+ error("Painter#text: invalid font.");
314
+
315
+ coord x = to<coord>(argv[1]), w = to<coord>(argv[3]);
316
+ coord y = to<coord>(argv[2]), h = to<coord>(argv[4]);
317
+ if (!this->text(argv[0].c_str(), x, y, w, h, *font))
318
+ {
319
+ error(
320
+ "Painter#image(%s, %f, %f, %f, %f, %s) failed.",
321
+ argv[0].inspect().c_str(), x, y, w, h, argv[3].inspect().c_str());
322
+ }
323
+ }
324
+
325
+ return self;
326
+ }
327
+
328
+ static
329
+ VALUE get_clear(VALUE self)
330
+ {
331
+ CHECK;
332
+
333
+ coord red = 0, green = 0, blue = 0, alpha = 0;
334
+ if (!this->get_clear(&red, &green, &blue, &alpha))
335
+ error("failed to get color for clear.");
336
+
337
+ Value ret[] = {red, green, blue, alpha};
338
+ return value(4, ret);
339
+ }
340
+
341
+ static
342
+ VALUE set_clear(VALUE self)
343
+ {
344
+ CHECK;
345
+ if (argc < 1 || 4 < argc)
346
+ arg_count_error("Painter#set_clear", argc, 1, 2, 3, 4);
347
+
348
+ if (argc == 1 || argc == 2)
349
+ {
350
+ float v = to<float>(argv[0]);
351
+ float a = (argc >= 2) ? to<float>(argv[1]) : 1;
352
+ if (!this->set_clear(v, v, v, a))
353
+ error("Painter#set_clear(%f, %f) failed.", v, a);
354
+ }
355
+ else
356
+ {
357
+ float r = to<float>(argv[0]);
358
+ float g = to<float>(argv[1]);
359
+ float b = to<float>(argv[2]);
360
+ float a = (argc == 4) ? to<float>(argv[3]) : 1;
361
+ if (!this->set_clear(r, g, b, a))
362
+ error("Painter#set_clear(%f, %f, %f, %f) failed.", r, g, b, a);
363
+ }
364
+
365
+ return self;
366
+ }
367
+
368
+ static
369
+ VALUE no_clear(VALUE self)
370
+ {
371
+ CHECK;
372
+
373
+ if (!this->no_clear())
374
+ error("Painter#no_clear() failed.");
375
+
376
+ return self;
377
+ }
378
+
379
+ static
380
+ VALUE get_fill(VALUE self)
381
+ {
382
+ CHECK;
383
+
384
+ coord red = 0, green = 0, blue = 0, alpha = 0;
385
+ if (!this->get_fill(&red, &green, &blue, &alpha))
386
+ error("failed to get color for fill.");
387
+
388
+ Value ret[] = {red, green, blue, alpha};
389
+ return value(4, ret);
390
+ }
391
+
392
+ static
393
+ VALUE set_fill(VALUE self)
394
+ {
395
+ CHECK;
396
+ if (argc < 1 || 4 < argc)
397
+ arg_count_error("Painter#set_fill", argc, 1, 2, 3, 4);
398
+
399
+ if (argc == 1 || argc == 2)
400
+ {
401
+ float v = to<float>(argv[0]);
402
+ float a = (argc >= 2) ? to<float>(argv[1]) : 1;
403
+ if (!this->set_fill(v, v, v, a))
404
+ error("Painter#set_fill(%f, %f) failed.", v, a);
405
+ }
406
+ else
407
+ {
408
+ float r = to<float>(argv[0]);
409
+ float g = to<float>(argv[1]);
410
+ float b = to<float>(argv[2]);
411
+ float a = (argc == 4) ? to<float>(argv[3]) : 1;
412
+ if (!this->set_fill(r, g, b, a))
413
+ error("Painter#set_fill(%f, %f, %f, %f) failed.", r, g, b, a);
414
+ }
415
+
416
+ return self;
417
+ }
418
+
419
+ static
420
+ VALUE no_fill(VALUE self)
421
+ {
422
+ CHECK;
423
+
424
+ if (!this->no_fill())
425
+ error("Painter#no_fill() failed.");
426
+
427
+ return self;
428
+ }
429
+
430
+ static
431
+ VALUE get_stroke(VALUE self)
432
+ {
433
+ CHECK;
434
+
435
+ coord red = 0, green = 0, blue = 0, alpha = 0;
436
+ if (!this->get_stroke(&red, &green, &blue, &alpha))
437
+ error("failed to get color for stroke.");
438
+
439
+ Value ret[] = {red, green, blue, alpha};
440
+ return value(4, ret);
441
+ }
442
+
443
+ static
444
+ VALUE set_stroke(VALUE self)
445
+ {
446
+ CHECK;
447
+ if (argc < 1 || 4 < argc)
448
+ arg_count_error("Painter#set_stroke", argc, 1, 2, 3, 4);
449
+
450
+ if (argc == 1 || argc == 2)
451
+ {
452
+ float v = to<float>(argv[0]);
453
+ float a = (argc >= 2) ? to<float>(argv[1]) : 1;
454
+ if (!this->set_stroke(v, v, v, a))
455
+ error("Painter#set_stroke(%f, %f) failed.", v, a);
456
+ }
457
+ else
458
+ {
459
+ float r = to<float>(argv[0]);
460
+ float g = to<float>(argv[1]);
461
+ float b = to<float>(argv[2]);
462
+ float a = (argc == 4) ? to<float>(argv[3]) : 1;
463
+ if (!this->set_stroke(r, g, b, a))
464
+ error("Painter#set_stroke(%f, %f, %f, %f) failed.", r, g, b, a);
465
+ }
466
+
467
+ return self;
468
+ }
469
+
470
+ static
471
+ VALUE no_stroke(VALUE self)
472
+ {
473
+ CHECK;
474
+
475
+ if (!this->no_stroke())
476
+ error("Painter#no_stroke() failed.");
477
+
478
+ return self;
479
+ }
480
+
481
+ static
482
+ VALUE get_clip(VALUE self)
483
+ {
484
+ CHECK;
485
+
486
+ coord x = 0, y = 0, width = 0, height = 0;
487
+ if (!this->get_clip(&x, &y, &width, &height))
488
+ error("failed to get clip rect.");
489
+
490
+ Value ret[] = {x, y, width, height};
491
+ return value(4, ret);
492
+ }
493
+
494
+ static
495
+ VALUE set_clip(VALUE self, VALUE x, VALUE y, VALUE width, VALUE height)
496
+ {
497
+ CHECK;
498
+
499
+ coord xx = to<coord>(x), ww = to<coord>(width);
500
+ coord yy = to<coord>(y), hh = to<coord>(height);
501
+ if (!this->set_clip(xx, yy, ww, hh))
502
+ error("Painter#set_clip(%f, %f, %f, %f) failed.", xx, yy, ww, hh);
503
+
504
+ return self;
505
+ }
506
+
507
+ static
508
+ VALUE no_clip(VALUE self)
509
+ {
510
+ CHECK;
511
+
512
+ if (!this->no_clip())
513
+ error("Painter#no_clip() failed.");
514
+
515
+ return self;
516
+ }
517
+
518
+
519
+ void
520
+ Init_painter ()
521
+ {
522
+ Module m = rb_define_module("Rays");
523
+
524
+ Class c = rb_define_class_under(m, "Painter", rb_cObject);
525
+ Rays::cPainter = c;
526
+
527
+ rb_define_alloc_func(c, alloc);
528
+
529
+ rb_define_method(c, "canvas", RUBY_METHOD_FUNC(canvas), 4);
530
+ rb_define_method(c, "begin", RUBY_METHOD_FUNC(begin), 0);
531
+ rb_define_method(c, "end", RUBY_METHOD_FUNC(end), 0);
532
+ rb_define_method(c, "push_matrix", RUBY_METHOD_FUNC(push_matrix), 0);
533
+ rb_define_method(c, "pop_matrix", RUBY_METHOD_FUNC(pop_matrix), 0);
534
+
535
+ rb_define_private_method(c, "draw_line", RUBY_METHOD_FUNC(line), 4);
536
+ rb_define_private_method(c, "draw_rect", RUBY_METHOD_FUNC(rect), 4);
537
+ rb_define_private_method(c, "draw_ellipse", RUBY_METHOD_FUNC(ellipse), -1);
538
+ rb_define_private_method(c, "draw_arc", RUBY_METHOD_FUNC(arc), -1);
539
+ rb_define_private_method(c, "draw_image", RUBY_METHOD_FUNC(image), -1);
540
+ rb_define_private_method(c, "draw_text", RUBY_METHOD_FUNC(text), -1);
541
+
542
+ rb_define_method(c, "get_fill", RUBY_METHOD_FUNC(get_fill), 0);
543
+ rb_define_method(c, "set_fill", RUBY_METHOD_FUNC(set_fill), -1);
544
+ rb_define_method(c, "no_fill", RUBY_METHOD_FUNC(no_fill), 0);
545
+ rb_define_method(c, "get_stroke", RUBY_METHOD_FUNC(get_stroke), 0);
546
+ rb_define_method(c, "set_stroke", RUBY_METHOD_FUNC(set_stroke), -1);
547
+ rb_define_method(c, "no_stroke", RUBY_METHOD_FUNC(no_stroke), 0);
548
+ rb_define_method(c, "get_clear", RUBY_METHOD_FUNC(get_clear), 0);
549
+ rb_define_method(c, "set_clear", RUBY_METHOD_FUNC(set_clear), -1);
550
+ rb_define_method(c, "no_clear", RUBY_METHOD_FUNC(no_clear), 0);
551
+ rb_define_method(c, "get_clip", RUBY_METHOD_FUNC(get_clip), 0);
552
+ rb_define_method(c, "set_clip", RUBY_METHOD_FUNC(set_clip), 4);
553
+ rb_define_method(c, "no_clip", RUBY_METHOD_FUNC(no_clip), 0);
554
+ }