[1944] | 1 | /* |
---|
[2821] | 2 | * libcaca Ruby bindings |
---|
[1944] | 3 | * Copyright (c) 2007 Pascal Terjan <pterjan@linuxfr.org> |
---|
| 4 | * |
---|
| 5 | * This library is free software. It comes without any warranty, to |
---|
| 6 | * the extent permitted by applicable law. You can redistribute it |
---|
| 7 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
| 8 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
| 9 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | #include <ruby.h> |
---|
[2821] | 13 | #include <caca.h> |
---|
[1944] | 14 | #include <errno.h> |
---|
[2821] | 15 | #include "caca-dither.h" |
---|
| 16 | #include "caca-font.h" |
---|
[1995] | 17 | #include "common.h" |
---|
[1944] | 18 | |
---|
[1995] | 19 | VALUE cCanvas; |
---|
[1944] | 20 | |
---|
| 21 | #define simple_func(x) \ |
---|
| 22 | static VALUE x (VALUE self) \ |
---|
| 23 | { \ |
---|
[3495] | 24 | if( caca_##x (_SELF) <0) \ |
---|
[1944] | 25 | rb_raise(rb_eRuntimeError, strerror(errno)); \ |
---|
| 26 | \ |
---|
| 27 | return self; \ |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | #define get_int(x) \ |
---|
| 31 | static VALUE get_##x (VALUE self) \ |
---|
| 32 | { \ |
---|
[3495] | 33 | return INT2NUM(caca_get_##x (_SELF)); \ |
---|
[1944] | 34 | } |
---|
| 35 | |
---|
| 36 | static void canvas_free(void * p) |
---|
| 37 | { |
---|
[2821] | 38 | caca_free_canvas((caca_canvas_t *)p); |
---|
[1944] | 39 | } |
---|
| 40 | |
---|
| 41 | static VALUE canvas_alloc(VALUE klass) |
---|
| 42 | { |
---|
[3495] | 43 | VALUE obj; |
---|
[2295] | 44 | obj = Data_Wrap_Struct(klass, NULL, canvas_free, NULL); |
---|
[1944] | 45 | return obj; |
---|
| 46 | } |
---|
| 47 | |
---|
[2821] | 48 | VALUE canvas_create(caca_canvas_t *canvas) |
---|
[2093] | 49 | { |
---|
[2295] | 50 | return Data_Wrap_Struct(cCanvas, NULL, NULL, canvas); |
---|
[2093] | 51 | } |
---|
| 52 | |
---|
[1944] | 53 | static VALUE canvas_initialize(VALUE self, VALUE width, VALUE height) |
---|
| 54 | { |
---|
[2821] | 55 | caca_canvas_t *canvas; |
---|
[1944] | 56 | |
---|
[2821] | 57 | canvas = caca_create_canvas(NUM2INT(width), NUM2INT(height)); |
---|
[2094] | 58 | |
---|
| 59 | if(canvas == NULL) |
---|
| 60 | { |
---|
| 61 | rb_raise(rb_eRuntimeError, strerror(errno)); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | _SELF = canvas; |
---|
| 65 | |
---|
[1944] | 66 | return self; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | get_int(canvas_height) |
---|
| 70 | get_int(canvas_width) |
---|
| 71 | |
---|
| 72 | static VALUE set_canvas_width(VALUE self, VALUE width) |
---|
| 73 | { |
---|
[2821] | 74 | caca_set_canvas_size(_SELF, NUM2INT(width), caca_get_canvas_height(_SELF)); |
---|
[1944] | 75 | return width; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | static VALUE set_canvas_width2(VALUE self, VALUE width) |
---|
| 79 | { |
---|
| 80 | set_canvas_width(self, width); |
---|
| 81 | return self; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | static VALUE set_canvas_height(VALUE self, VALUE height) |
---|
| 85 | { |
---|
[2821] | 86 | caca_set_canvas_size(_SELF, caca_get_canvas_width(_SELF), NUM2INT(height)); |
---|
[1944] | 87 | return height; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | static VALUE set_canvas_height2(VALUE self, VALUE height) |
---|
| 91 | { |
---|
| 92 | set_canvas_height(self, height); |
---|
| 93 | return self; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | static VALUE set_canvas_size(VALUE self, VALUE height, VALUE width) |
---|
| 97 | { |
---|
[2821] | 98 | caca_set_canvas_size(_SELF, NUM2INT(width), NUM2INT(height)); |
---|
[1944] | 99 | return self; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | /****/ |
---|
| 103 | |
---|
| 104 | static VALUE gotoxy(VALUE self, VALUE x, VALUE y) |
---|
| 105 | { |
---|
[2821] | 106 | if( caca_gotoxy(_SELF, NUM2INT(x), NUM2INT(y)) <0) { |
---|
[1944] | 107 | rb_raise(rb_eRuntimeError, strerror(errno)); |
---|
| 108 | } |
---|
| 109 | return self; |
---|
| 110 | } |
---|
| 111 | |
---|
[3587] | 112 | static VALUE wherex(VALUE self) |
---|
| 113 | { |
---|
| 114 | return INT2NUM(caca_wherex(_SELF)); |
---|
| 115 | } |
---|
[1944] | 116 | |
---|
[3587] | 117 | static VALUE wherey(VALUE self) |
---|
| 118 | { |
---|
| 119 | return INT2NUM(caca_wherey(_SELF)); |
---|
| 120 | } |
---|
| 121 | |
---|
[1944] | 122 | simple_func(clear_canvas) |
---|
| 123 | |
---|
| 124 | static VALUE put_char(VALUE self, VALUE x, VALUE y, VALUE ch) |
---|
| 125 | { |
---|
[2821] | 126 | caca_put_char(_SELF, NUM2INT(x), NUM2INT(y), NUM2ULONG(ch)); |
---|
[1944] | 127 | return self; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | static VALUE get_char(VALUE self, VALUE x, VALUE y) |
---|
| 131 | { |
---|
| 132 | unsigned long int ch; |
---|
[2821] | 133 | ch = caca_get_char(_SELF, NUM2INT(x), NUM2INT(y)); |
---|
[1944] | 134 | return INT2NUM(ch); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | static VALUE put_str(VALUE self, VALUE x, VALUE y, VALUE str) |
---|
| 138 | { |
---|
[2821] | 139 | caca_put_str(_SELF, NUM2INT(x), NUM2INT(y), StringValuePtr(str)); |
---|
[1944] | 140 | return self; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | static VALUE get_attr(VALUE self, VALUE x, VALUE y) |
---|
| 144 | { |
---|
| 145 | unsigned long int ch; |
---|
[2821] | 146 | ch = caca_get_attr(_SELF, NUM2INT(x), NUM2INT(y)); |
---|
[1944] | 147 | return INT2NUM(ch); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | static VALUE set_attr(VALUE self, VALUE attr) |
---|
| 151 | { |
---|
[2821] | 152 | if(caca_set_attr(_SELF, NUM2ULONG(attr)) <0) |
---|
[1944] | 153 | rb_raise(rb_eRuntimeError, strerror(errno)); |
---|
| 154 | |
---|
| 155 | return self; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | static VALUE set_attr2(VALUE self, VALUE attr) |
---|
| 159 | { |
---|
| 160 | set_attr(self, attr); |
---|
| 161 | return self; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | static VALUE put_attr(VALUE self, VALUE x, VALUE y, VALUE attr) |
---|
| 165 | { |
---|
[2821] | 166 | if(caca_put_attr(_SELF, NUM2INT(x), NUM2INT(y), NUM2ULONG(attr)) <0) |
---|
[1944] | 167 | rb_raise(rb_eRuntimeError, strerror(errno)); |
---|
| 168 | |
---|
| 169 | return self; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | static VALUE set_color_ansi(VALUE self, VALUE fg, VALUE bg) |
---|
| 173 | { |
---|
[2821] | 174 | if(caca_set_color_ansi(_SELF, NUM2INT(fg), NUM2INT(bg)) <0) |
---|
[1944] | 175 | rb_raise(rb_eRuntimeError, strerror(errno)); |
---|
| 176 | |
---|
| 177 | return self; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | static VALUE set_color_argb(VALUE self, VALUE fg, VALUE bg) |
---|
| 181 | { |
---|
[2821] | 182 | if(caca_set_color_argb(_SELF, NUM2UINT(fg), NUM2UINT(bg)) <0) { |
---|
[1944] | 183 | rb_raise(rb_eRuntimeError, strerror(errno)); |
---|
| 184 | } |
---|
| 185 | return self; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | static VALUE cprintf(int argc, VALUE* argv, VALUE self) |
---|
| 189 | { |
---|
| 190 | int x, y; |
---|
| 191 | VALUE rx, ry, format, rest, string; |
---|
| 192 | rb_scan_args(argc, argv, "3*", &rx, &ry, &format, &rest); |
---|
| 193 | x = NUM2INT(rx); |
---|
| 194 | y = NUM2INT(ry); |
---|
| 195 | string = rb_funcall2(rb_mKernel, rb_intern("sprintf"), argc-2, argv+2); |
---|
[2821] | 196 | caca_put_str(_SELF, x, y, StringValuePtr(string)); |
---|
[1944] | 197 | return self; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | |
---|
| 201 | get_int(canvas_handle_x) |
---|
| 202 | get_int(canvas_handle_y) |
---|
| 203 | |
---|
| 204 | static VALUE set_canvas_handle(VALUE self, VALUE x, VALUE y) |
---|
| 205 | { |
---|
[2821] | 206 | caca_set_canvas_handle(_SELF, NUM2INT(x), NUM2INT(y)); |
---|
[1944] | 207 | return self; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | static VALUE blit(int argc, VALUE* argv, VALUE self) { |
---|
| 211 | VALUE x, y, src, mask; |
---|
[2821] | 212 | caca_canvas_t *csrc, *cmask; |
---|
[1944] | 213 | |
---|
| 214 | rb_scan_args(argc, argv, "31", &x, &y, &src, &mask); |
---|
| 215 | |
---|
| 216 | Check_Type(x, T_FIXNUM); |
---|
| 217 | Check_Type(y, T_FIXNUM); |
---|
[2004] | 218 | |
---|
| 219 | if(CLASS_OF(src) != cCanvas) |
---|
| 220 | { |
---|
[2822] | 221 | rb_raise(rb_eArgError, "src is not a Caca::Canvas"); |
---|
[2004] | 222 | } |
---|
[2821] | 223 | Data_Get_Struct(src, caca_canvas_t, csrc); |
---|
[2004] | 224 | |
---|
[1944] | 225 | if(!NIL_P(mask)) |
---|
| 226 | { |
---|
[2004] | 227 | if(CLASS_OF(mask) != cCanvas) |
---|
| 228 | { |
---|
[2822] | 229 | rb_raise(rb_eArgError, "mask is not a Caca::Canvas"); |
---|
[2004] | 230 | } |
---|
[2821] | 231 | Data_Get_Struct(mask, caca_canvas_t, cmask); |
---|
[1944] | 232 | } |
---|
| 233 | else |
---|
| 234 | cmask = NULL; |
---|
[3582] | 235 | |
---|
[2821] | 236 | if(caca_blit(_SELF, NUM2INT(x), NUM2INT(y), csrc, cmask)<0) |
---|
[1944] | 237 | rb_raise(rb_eRuntimeError, strerror(errno)); |
---|
| 238 | |
---|
| 239 | return self; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | static VALUE set_canvas_boundaries(VALUE self, VALUE x, VALUE y, VALUE w, VALUE h) |
---|
| 243 | { |
---|
[2821] | 244 | if(caca_set_canvas_boundaries(_SELF, NUM2INT(x), NUM2INT(y), NUM2UINT(w), NUM2UINT(h))<0) |
---|
[1944] | 245 | { |
---|
| 246 | rb_raise(rb_eRuntimeError, strerror(errno)); |
---|
| 247 | } |
---|
| 248 | return self; |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | /****/ |
---|
| 252 | |
---|
| 253 | simple_func(invert) |
---|
| 254 | simple_func(flip) |
---|
| 255 | simple_func(flop) |
---|
| 256 | simple_func(rotate_180) |
---|
| 257 | simple_func(rotate_left) |
---|
| 258 | simple_func(rotate_right) |
---|
| 259 | simple_func(stretch_left) |
---|
| 260 | simple_func(stretch_right) |
---|
| 261 | |
---|
| 262 | /****/ |
---|
| 263 | |
---|
| 264 | static VALUE draw_line(VALUE self, VALUE x1, VALUE y1, VALUE x2, VALUE y2, VALUE ch) |
---|
| 265 | { |
---|
[2821] | 266 | caca_draw_line(_SELF, NUM2INT(x1), NUM2INT(y1), NUM2INT(x2), NUM2INT(y2),NUM2ULONG(ch)); |
---|
[1944] | 267 | return self; |
---|
| 268 | } |
---|
| 269 | |
---|
[1948] | 270 | static VALUE draw_polyline(VALUE self, VALUE points, VALUE ch) |
---|
[1944] | 271 | { |
---|
| 272 | int i, n; |
---|
| 273 | int *ax, *ay; |
---|
[1948] | 274 | int error = 0; |
---|
| 275 | VALUE v, x, y; |
---|
[1944] | 276 | |
---|
[1948] | 277 | n = RARRAY(points)->len; |
---|
[1944] | 278 | |
---|
| 279 | ax = (int*)malloc(n*sizeof(int)); |
---|
| 280 | if(!ax) |
---|
| 281 | rb_raise(rb_eNoMemError,"Out of memory"); |
---|
| 282 | |
---|
| 283 | ay = (int*)malloc(n*sizeof(int)); |
---|
| 284 | if(!ay) |
---|
| 285 | { |
---|
| 286 | free(ax); |
---|
| 287 | rb_raise(rb_eNoMemError,"Out of memory"); |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | for(i=0; i<n; i++) |
---|
| 291 | { |
---|
[1948] | 292 | v = rb_ary_entry(points, i); |
---|
| 293 | if((TYPE(v) == T_ARRAY) && (RARRAY(v)->len == 2)) |
---|
| 294 | { |
---|
| 295 | x = rb_ary_entry(v,0); |
---|
| 296 | y = rb_ary_entry(v,1); |
---|
| 297 | if(rb_obj_is_kind_of(x, rb_cInteger) && |
---|
| 298 | rb_obj_is_kind_of(y, rb_cInteger)) |
---|
| 299 | { |
---|
| 300 | ax[i] = NUM2INT(x); |
---|
| 301 | ay[i] = NUM2INT(y); |
---|
| 302 | } else |
---|
| 303 | error = 1; |
---|
| 304 | } |
---|
[3582] | 305 | else |
---|
[1948] | 306 | error = 1; |
---|
[1944] | 307 | } |
---|
| 308 | |
---|
[1948] | 309 | if(error) |
---|
| 310 | { |
---|
| 311 | free(ax); |
---|
| 312 | free(ay); |
---|
| 313 | rb_raise(rb_eArgError, "Invalid list of points"); |
---|
| 314 | } |
---|
| 315 | |
---|
[1944] | 316 | n--; |
---|
| 317 | |
---|
[2821] | 318 | caca_draw_polyline(_SELF, ax, ay, n, NUM2ULONG(ch)); |
---|
[1944] | 319 | |
---|
| 320 | free(ax); |
---|
| 321 | free(ay); |
---|
| 322 | |
---|
| 323 | return self; |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | static VALUE draw_thin_line(VALUE self, VALUE x1, VALUE y1, VALUE x2, VALUE y2) |
---|
| 327 | { |
---|
[2821] | 328 | caca_draw_thin_line(_SELF, NUM2INT(x1), NUM2INT(y1), NUM2INT(x2), NUM2INT(y2)); |
---|
[1944] | 329 | return self; |
---|
| 330 | } |
---|
| 331 | |
---|
[1948] | 332 | static VALUE draw_thin_polyline(VALUE self, VALUE points) |
---|
[1944] | 333 | { |
---|
| 334 | int i, n; |
---|
| 335 | int *ax, *ay; |
---|
[1948] | 336 | int error = 0; |
---|
| 337 | VALUE v, x, y; |
---|
[1944] | 338 | |
---|
[1948] | 339 | n = RARRAY(points)->len; |
---|
[1944] | 340 | |
---|
| 341 | ax = (int*)malloc(n*sizeof(int)); |
---|
| 342 | if(!ax) |
---|
| 343 | rb_raise(rb_eNoMemError,"Out of memory"); |
---|
| 344 | |
---|
| 345 | ay = (int*)malloc(n*sizeof(int)); |
---|
| 346 | if(!ay) |
---|
| 347 | { |
---|
| 348 | free(ax); |
---|
| 349 | rb_raise(rb_eNoMemError,"Out of memory"); |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | for(i=0; i<n; i++) |
---|
| 353 | { |
---|
[1948] | 354 | v = rb_ary_entry(points, i); |
---|
| 355 | if((TYPE(v) == T_ARRAY) && (RARRAY(v)->len == 2)) |
---|
| 356 | { |
---|
| 357 | x = rb_ary_entry(v,0); |
---|
| 358 | y = rb_ary_entry(v,1); |
---|
| 359 | if(rb_obj_is_kind_of(x, rb_cInteger) && |
---|
| 360 | rb_obj_is_kind_of(y, rb_cInteger)) |
---|
| 361 | { |
---|
| 362 | ax[i] = NUM2INT(x); |
---|
| 363 | ay[i] = NUM2INT(y); |
---|
| 364 | } else |
---|
| 365 | error = 1; |
---|
| 366 | } |
---|
[3582] | 367 | else |
---|
[1948] | 368 | error = 1; |
---|
[1944] | 369 | } |
---|
| 370 | |
---|
[1948] | 371 | if(error) |
---|
| 372 | { |
---|
| 373 | free(ax); |
---|
| 374 | free(ay); |
---|
| 375 | rb_raise(rb_eArgError, "Invalid list of points"); |
---|
| 376 | } |
---|
| 377 | |
---|
[1944] | 378 | n--; |
---|
| 379 | |
---|
[2821] | 380 | caca_draw_thin_polyline(_SELF, ax, ay, n); |
---|
[1944] | 381 | |
---|
| 382 | free(ax); |
---|
| 383 | free(ay); |
---|
| 384 | |
---|
| 385 | return self; |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | static VALUE draw_circle(VALUE self, VALUE x, VALUE y, VALUE r, VALUE ch) |
---|
| 389 | { |
---|
[2821] | 390 | caca_draw_circle(_SELF, NUM2INT(x), NUM2INT(y), NUM2INT(r), NUM2ULONG(ch)); |
---|
[1944] | 391 | return self; |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | static VALUE draw_ellipse(VALUE self, VALUE x, VALUE y, VALUE a, VALUE b, VALUE ch) |
---|
| 395 | { |
---|
[2821] | 396 | caca_draw_ellipse(_SELF, NUM2INT(x), NUM2INT(y), NUM2INT(a), NUM2INT(b), NUM2ULONG(ch)); |
---|
[1944] | 397 | return self; |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | static VALUE draw_thin_ellipse(VALUE self, VALUE x, VALUE y, VALUE a, VALUE b) |
---|
| 401 | { |
---|
[2821] | 402 | caca_draw_thin_ellipse(_SELF, NUM2INT(x), NUM2INT(y), NUM2INT(a), NUM2INT(b)); |
---|
[1944] | 403 | return self; |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | static VALUE fill_ellipse(VALUE self, VALUE x, VALUE y, VALUE a, VALUE b, VALUE ch) |
---|
| 407 | { |
---|
[2821] | 408 | caca_fill_ellipse(_SELF, NUM2INT(x), NUM2INT(y), NUM2INT(a), NUM2INT(b), NUM2ULONG(ch)); |
---|
[1944] | 409 | return self; |
---|
| 410 | } |
---|
| 411 | |
---|
[2086] | 412 | static VALUE draw_box(VALUE self, VALUE x, VALUE y, VALUE w, VALUE h, VALUE ch) |
---|
[1944] | 413 | { |
---|
[2821] | 414 | caca_draw_box(_SELF, NUM2INT(x), NUM2INT(y), NUM2INT(w), NUM2INT(h), NUM2ULONG(ch)); |
---|
[1944] | 415 | return self; |
---|
| 416 | } |
---|
| 417 | |
---|
[2086] | 418 | static VALUE draw_thin_box(VALUE self, VALUE x, VALUE y, VALUE w, VALUE h) |
---|
[1944] | 419 | { |
---|
[2821] | 420 | caca_draw_thin_box(_SELF, NUM2INT(x), NUM2INT(y), NUM2INT(w), NUM2INT(h)); |
---|
[1944] | 421 | return self; |
---|
| 422 | } |
---|
| 423 | |
---|
[2086] | 424 | static VALUE draw_cp437_box(VALUE self, VALUE x, VALUE y, VALUE w, VALUE h) |
---|
[1944] | 425 | { |
---|
[2821] | 426 | caca_draw_cp437_box(_SELF, NUM2INT(x), NUM2INT(y), NUM2INT(w), NUM2INT(h)); |
---|
[1944] | 427 | return self; |
---|
| 428 | } |
---|
| 429 | |
---|
[2086] | 430 | static VALUE fill_box(VALUE self, VALUE x, VALUE y, VALUE w, VALUE h, VALUE ch) |
---|
[1944] | 431 | { |
---|
[2821] | 432 | caca_fill_box(_SELF, NUM2INT(x), NUM2INT(y), NUM2INT(w), NUM2INT(h), NUM2ULONG(ch)); |
---|
[1944] | 433 | return self; |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | static VALUE draw_triangle(VALUE self, VALUE x1, VALUE y1, VALUE x2, VALUE y2, VALUE x3, VALUE y3, VALUE ch) |
---|
| 437 | { |
---|
[2821] | 438 | caca_draw_triangle(_SELF, NUM2INT(x1), NUM2INT(y1), NUM2INT(x2), NUM2INT(y2), NUM2INT(x3), NUM2INT(y3), NUM2ULONG(ch)); |
---|
[1944] | 439 | return self; |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | static VALUE draw_thin_triangle(VALUE self, VALUE x1, VALUE y1, VALUE x2, VALUE y2, VALUE x3, VALUE y3) |
---|
| 443 | { |
---|
[2821] | 444 | caca_draw_thin_triangle(_SELF, NUM2INT(x1), NUM2INT(y1), NUM2INT(x2), NUM2INT(y2), NUM2INT(x3), NUM2INT(y3)); |
---|
[1944] | 445 | return self; |
---|
| 446 | } |
---|
| 447 | |
---|
| 448 | static VALUE fill_triangle(VALUE self, VALUE x1, VALUE y1, VALUE x2, VALUE y2, VALUE x3, VALUE y3, VALUE ch) |
---|
| 449 | { |
---|
[2821] | 450 | caca_fill_triangle(_SELF, NUM2INT(x1), NUM2INT(y1), NUM2INT(x2), NUM2INT(y2), NUM2INT(x3), NUM2INT(y3), NUM2ULONG(ch)); |
---|
[1944] | 451 | return self; |
---|
| 452 | } |
---|
| 453 | |
---|
[2009] | 454 | static VALUE dither_bitmap(VALUE self, VALUE x, VALUE y, VALUE w, VALUE h, VALUE d, VALUE pixels) |
---|
| 455 | { |
---|
| 456 | if(CLASS_OF(d) != cDither) |
---|
[2822] | 457 | rb_raise(rb_eArgError, "d is not a Caca::Dither"); |
---|
[2009] | 458 | Check_Type(pixels, T_STRING); |
---|
| 459 | |
---|
[2821] | 460 | caca_dither_bitmap(_SELF, NUM2INT(x), NUM2INT(y), NUM2INT(w), NUM2INT(h), DATA_PTR(d), StringValuePtr(pixels)); |
---|
[2009] | 461 | return self; |
---|
| 462 | } |
---|
| 463 | |
---|
[1944] | 464 | /****/ |
---|
| 465 | |
---|
| 466 | get_int(frame_count) |
---|
| 467 | |
---|
| 468 | static VALUE set_frame(VALUE self, VALUE id) |
---|
| 469 | { |
---|
[2821] | 470 | if(caca_set_frame(_SELF, NUM2INT(id))<0) |
---|
[1944] | 471 | rb_raise(rb_eArgError, strerror(errno)); |
---|
| 472 | |
---|
| 473 | return self; |
---|
| 474 | } |
---|
| 475 | |
---|
| 476 | static VALUE set_frame2(VALUE self, VALUE id) |
---|
| 477 | { |
---|
| 478 | set_frame(self, id); |
---|
| 479 | return self; |
---|
| 480 | } |
---|
| 481 | |
---|
| 482 | static VALUE get_frame_name(VALUE self) |
---|
| 483 | { |
---|
[2821] | 484 | return rb_str_new2(caca_get_frame_name(_SELF)); |
---|
[1944] | 485 | } |
---|
| 486 | |
---|
| 487 | static VALUE set_frame_name(VALUE self, VALUE name) |
---|
| 488 | { |
---|
[2821] | 489 | if(caca_set_frame_name(_SELF, StringValuePtr(name))<0) |
---|
[1944] | 490 | rb_raise(rb_eRuntimeError, strerror(errno)); |
---|
| 491 | |
---|
| 492 | return self; |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | static VALUE set_frame_name2(VALUE self, VALUE name) |
---|
| 496 | { |
---|
| 497 | set_frame_name(self, name); |
---|
| 498 | return self; |
---|
| 499 | } |
---|
| 500 | |
---|
| 501 | static VALUE create_frame(VALUE self, VALUE id) |
---|
| 502 | { |
---|
[2821] | 503 | if(caca_create_frame(_SELF, NUM2INT(id))<0) { |
---|
[1944] | 504 | rb_raise(rb_eRuntimeError, strerror(errno)); |
---|
| 505 | } |
---|
| 506 | return self; |
---|
| 507 | } |
---|
| 508 | |
---|
| 509 | static VALUE free_frame(VALUE self, VALUE id) |
---|
| 510 | { |
---|
[2821] | 511 | if(caca_free_frame(_SELF, NUM2INT(id))<0) { |
---|
[1944] | 512 | rb_raise(rb_eArgError, strerror(errno)); |
---|
| 513 | } |
---|
| 514 | return self; |
---|
| 515 | } |
---|
| 516 | |
---|
| 517 | /****/ |
---|
| 518 | |
---|
[1995] | 519 | static VALUE render_canvas(VALUE self, VALUE font, VALUE width, VALUE height, VALUE pitch) |
---|
| 520 | { |
---|
| 521 | void *buf; |
---|
[2821] | 522 | caca_font_t *f; |
---|
[1995] | 523 | VALUE b; |
---|
| 524 | |
---|
[2004] | 525 | if(CLASS_OF(font) != cFont) |
---|
| 526 | { |
---|
[2822] | 527 | rb_raise(rb_eArgError, "First argument is not a Caca::Font"); |
---|
[2004] | 528 | } |
---|
[1995] | 529 | |
---|
| 530 | buf = malloc(width*height*4); |
---|
| 531 | if(buf == NULL) |
---|
| 532 | { |
---|
| 533 | rb_raise(rb_eNoMemError, "Out of memory"); |
---|
| 534 | } |
---|
| 535 | |
---|
| 536 | f = DATA_PTR(font); |
---|
[2821] | 537 | caca_render_canvas(_SELF, f, buf, NUM2UINT(width), NUM2UINT(height), NUM2UINT(pitch)); |
---|
[1995] | 538 | |
---|
| 539 | b = rb_str_new(buf, width*height*4); |
---|
| 540 | free(buf); |
---|
| 541 | return b; |
---|
| 542 | } |
---|
| 543 | |
---|
[3495] | 544 | static VALUE import_from_memory(VALUE self, VALUE data, VALUE format) |
---|
[1944] | 545 | { |
---|
| 546 | long int bytes; |
---|
[3495] | 547 | bytes = caca_import_canvas_from_memory (_SELF, StringValuePtr(data), RSTRING(StringValue(data))->len, StringValuePtr(format)); |
---|
[1944] | 548 | if(bytes <= 0) |
---|
| 549 | rb_raise(rb_eRuntimeError, strerror(errno)); |
---|
| 550 | |
---|
| 551 | return self; |
---|
| 552 | } |
---|
| 553 | |
---|
[3495] | 554 | static VALUE import_from_file(VALUE self, VALUE filename, VALUE format) |
---|
[1944] | 555 | { |
---|
| 556 | long int bytes; |
---|
[3495] | 557 | bytes = caca_import_canvas_from_file (_SELF, StringValuePtr(filename), StringValuePtr(format)); |
---|
[1944] | 558 | if(bytes <= 0) |
---|
| 559 | rb_raise(rb_eRuntimeError, strerror(errno)); |
---|
| 560 | |
---|
| 561 | return self; |
---|
| 562 | } |
---|
| 563 | |
---|
[3495] | 564 | static VALUE export_to_memory(VALUE self, VALUE format) |
---|
[1944] | 565 | { |
---|
[2829] | 566 | size_t bytes; |
---|
[1944] | 567 | void *result; |
---|
| 568 | VALUE ret; |
---|
[3495] | 569 | result = caca_export_canvas_to_memory (_SELF, StringValuePtr(format), &bytes); |
---|
[1944] | 570 | ret = rb_str_new(result, bytes); |
---|
| 571 | free(result); |
---|
| 572 | return ret; |
---|
| 573 | } |
---|
| 574 | |
---|
[2009] | 575 | get_singleton_double_list(export) |
---|
| 576 | get_singleton_double_list(import) |
---|
[1944] | 577 | |
---|
| 578 | /****/ |
---|
| 579 | |
---|
[2822] | 580 | void Init_caca_canvas(VALUE mCaca) |
---|
[1944] | 581 | { |
---|
[2822] | 582 | cCanvas = rb_define_class_under(mCaca, "Canvas", rb_cObject); |
---|
[1944] | 583 | rb_define_alloc_func(cCanvas, canvas_alloc); |
---|
| 584 | |
---|
| 585 | rb_define_method(cCanvas, "initialize", canvas_initialize, 2); |
---|
| 586 | rb_define_method(cCanvas, "width", get_canvas_width, 0); |
---|
| 587 | rb_define_method(cCanvas, "width=", set_canvas_width, 1); |
---|
| 588 | rb_define_method(cCanvas, "set_width", set_canvas_width2, 1); |
---|
| 589 | rb_define_method(cCanvas, "height", get_canvas_height, 0); |
---|
[1964] | 590 | rb_define_method(cCanvas, "height=", set_canvas_height, 1); |
---|
| 591 | rb_define_method(cCanvas, "set_height", set_canvas_height2, 1); |
---|
[1944] | 592 | rb_define_method(cCanvas, "set_size", set_canvas_size, 2); |
---|
[3582] | 593 | |
---|
[1944] | 594 | rb_define_method(cCanvas, "gotoxy", gotoxy, 2); |
---|
[3587] | 595 | rb_define_method(cCanvas, "wherex", wherex, 0); |
---|
| 596 | rb_define_method(cCanvas, "wherey", wherey, 0); |
---|
[1944] | 597 | rb_define_method(cCanvas, "handle_x", get_canvas_handle_x, 0); |
---|
| 598 | rb_define_method(cCanvas, "handle_y", get_canvas_handle_y, 0); |
---|
| 599 | rb_define_method(cCanvas, "set_handle", set_canvas_handle, 2); |
---|
| 600 | rb_define_method(cCanvas, "blit", blit, -1); |
---|
| 601 | rb_define_method(cCanvas, "set_boundaries", set_canvas_boundaries, 4); |
---|
| 602 | |
---|
| 603 | rb_define_method(cCanvas, "clear", clear_canvas, 0); |
---|
[3582] | 604 | |
---|
[1944] | 605 | rb_define_method(cCanvas, "put_char", put_char, 3); |
---|
| 606 | rb_define_method(cCanvas, "get_char", get_char, 2); |
---|
| 607 | rb_define_method(cCanvas, "put_str", put_str, 3); |
---|
| 608 | rb_define_method(cCanvas, "printf", cprintf, -1); |
---|
| 609 | |
---|
| 610 | rb_define_method(cCanvas, "get_attr", get_attr, 3); |
---|
| 611 | rb_define_method(cCanvas, "attr=", set_attr, 1); |
---|
| 612 | rb_define_method(cCanvas, "set_attr", set_attr2, 1); |
---|
| 613 | rb_define_method(cCanvas, "put_attr", put_attr, 3); |
---|
| 614 | rb_define_method(cCanvas, "set_color_ansi", set_color_ansi, 2); |
---|
| 615 | rb_define_method(cCanvas, "set_color_argb", set_color_argb, 2); |
---|
| 616 | |
---|
| 617 | rb_define_method(cCanvas, "invert", invert, 0); |
---|
| 618 | rb_define_method(cCanvas, "flip", flip, 0); |
---|
| 619 | rb_define_method(cCanvas, "flop", flop, 0); |
---|
| 620 | rb_define_method(cCanvas, "rotate_180", rotate_180, 0); |
---|
| 621 | rb_define_method(cCanvas, "rotate_left", rotate_left, 0); |
---|
| 622 | rb_define_method(cCanvas, "rotate_right", rotate_right, 0); |
---|
| 623 | rb_define_method(cCanvas, "stretch_left", stretch_left, 0); |
---|
| 624 | rb_define_method(cCanvas, "stretch_right", stretch_right, 0); |
---|
| 625 | |
---|
| 626 | rb_define_method(cCanvas, "draw_line", draw_line, 5); |
---|
[1948] | 627 | rb_define_method(cCanvas, "draw_polyline", draw_polyline, 2); |
---|
[1944] | 628 | rb_define_method(cCanvas, "draw_thin_line", draw_thin_line, 4); |
---|
[1948] | 629 | rb_define_method(cCanvas, "draw_thin_polyline", draw_thin_polyline, 1); |
---|
[1944] | 630 | rb_define_method(cCanvas, "draw_circle", draw_circle, 4); |
---|
| 631 | rb_define_method(cCanvas, "draw_ellipse", draw_ellipse, 5); |
---|
| 632 | rb_define_method(cCanvas, "draw_thin_ellipse", draw_thin_ellipse, 4); |
---|
| 633 | rb_define_method(cCanvas, "fill_ellipse", fill_ellipse, 5); |
---|
| 634 | rb_define_method(cCanvas, "draw_box", draw_box, 5); |
---|
| 635 | rb_define_method(cCanvas, "draw_thin_box", draw_thin_box, 4); |
---|
| 636 | rb_define_method(cCanvas, "draw_cp437_box", draw_cp437_box, 4); |
---|
| 637 | rb_define_method(cCanvas, "fill_box", fill_box, 5); |
---|
| 638 | rb_define_method(cCanvas, "draw_triangle", draw_triangle, 7); |
---|
| 639 | rb_define_method(cCanvas, "draw_thin_triangle", draw_thin_triangle, 6); |
---|
| 640 | rb_define_method(cCanvas, "fill_triangle", fill_triangle, 7); |
---|
[2009] | 641 | rb_define_method(cCanvas, "dither_bitmap", dither_bitmap, 6); |
---|
[1944] | 642 | |
---|
| 643 | rb_define_method(cCanvas, "frame_count", get_frame_count, 0); |
---|
| 644 | rb_define_method(cCanvas, "frame=", set_frame, 1); |
---|
| 645 | rb_define_method(cCanvas, "set_frame", set_frame2, 1); |
---|
| 646 | rb_define_method(cCanvas, "frame_name", get_frame_name, 0); |
---|
| 647 | rb_define_method(cCanvas, "frame_name=", set_frame_name, 1); |
---|
| 648 | rb_define_method(cCanvas, "set_frame_name", set_frame_name2, 1); |
---|
| 649 | rb_define_method(cCanvas, "create_frame", create_frame, 1); |
---|
| 650 | rb_define_method(cCanvas, "free_frame", free_frame, 1); |
---|
| 651 | |
---|
[1995] | 652 | rb_define_method(cCanvas, "render", render_canvas, 4); |
---|
[3495] | 653 | rb_define_method(cCanvas, "import_from_memory", import_from_memory, 2); |
---|
| 654 | rb_define_method(cCanvas, "import_from_file", import_from_file, 2); |
---|
| 655 | rb_define_method(cCanvas, "export_to_memory", export_to_memory, 1); |
---|
[1944] | 656 | rb_define_singleton_method(cCanvas, "export_list", export_list, 0); |
---|
| 657 | rb_define_singleton_method(cCanvas, "import_list", import_list, 0); |
---|
| 658 | } |
---|
| 659 | |
---|