ext/rays/bitmap.cpp in rays-0.1.3 vs ext/rays/bitmap.cpp in rays-0.1.4
- old
+ new
@@ -1,9 +1,10 @@
#include "rays/ruby/bitmap.h"
#include <rucy.h>
+#include "rays/ruby/font.h"
#include "defs.h"
using namespace Rucy;
@@ -11,16 +12,17 @@
using Rays::ushort;
using Rays::ulong;
using Rays::coord;
+static Class cBitmap;
+
+
namespace Rays
{
- static Class cBitmap;
-
Class
bitmap_class ()
{
return cBitmap;
}
@@ -32,91 +34,113 @@
namespace Rucy
{
Value
- value (const Rays::Bitmap& bitmap)
+ value (const Rays::Bitmap& obj)
{
- return new_type<Rays::Bitmap>(
- Rays::bitmap_class(), new Rays::Bitmap(bitmap));
+ return new_type(cBitmap, new Rays::Bitmap(obj));
}
+ Value
+ value (const Rays::Bitmap* obj)
+ {
+ return obj ? value(*obj) : nil();
+ }
+
}// Rucy
-#define this to<Rays::Bitmap*>(self)
+#define THIS to<Rays::Bitmap*>(self)
-#define CHECK CHECK_OBJECT(self, Rays::Bitmap, Rays::bitmap_class())
+#define CHECK RUCY_CHECK_OBJECT(self, Rays::Bitmap, cBitmap)
static
RUBY_DEF_ALLOC(alloc, klass)
{
- return new_type<Rays::Bitmap>(klass, new Rays::Bitmap);
+ return new_type<Rays::Bitmap>(klass);
}
RUBY_END
static
-RUBY_DEFN(initialize)
+RUBY_DEF3(setup, width, height, colorspace)
{
- CHECK_OBJ(self, Rays::Bitmap, Rays::bitmap_class());
- if (argc != 0 && argc != 2 && argc != 3)
- arg_count_error("Bitmap#initialize", argc, 0, 2, 3);
+ RUCY_CHECK_OBJ(self, Rays::Bitmap, cBitmap);
+ *THIS = Rays::Bitmap(
+ to<int>(width), to<int>(height),
+ (Rays::ColorSpaceType) to<uint>(colorspace));
+ return self;
+}
+RUBY_END
- if (argc == 0) return self;
+static
+RUBY_DEFN(draw_string)
+{
+ CHECK;
+ if (argc < 1 || 4 < argc)
+ arg_count_error("Bitmap#draw_string", argc, 1, 2, 3, 4);
- int width = to<int>(argv[0]);
- int height = to<int>(argv[1]);
- uint colorspace = (argc == 3) ? to<uint>(argv[2]) : (uint) Rays::RGBA;
+ const char* str = to<const char*>(argv[0]);
+ coord x = argc >= 2 ? to<coord>(argv[1]) : 0;
+ coord y = argc >= 3 ? to<coord>(argv[2]) : 0;
+ const Rays::Font* font = argc >= 4
+ ? to<Rays::Font*>(argv[3]) : &Rays::default_font();
- *this = Rays::Bitmap(width, height, (Rays::ColorSpaceType) colorspace);
+ if (!Rays::draw_string(THIS, str, x, y, *font))
+ {
+ rays_error(
+ "Bitmap#draw_string('%s', %f, %f, %s) failed.",
+ str, x, y, font->name().c_str());
+ }
+
return self;
}
RUBY_END
static
RUBY_DEF0(width)
{
CHECK;
- return value(this->width());
+ return value(THIS->width());
}
RUBY_END
static
RUBY_DEF0(height)
{
CHECK;
- return value(this->height());
+ return value(THIS->height());
}
RUBY_END
static
RUBY_DEF0(color_space)
{
CHECK;
- return value(this->color_space().type());
+ return value(THIS->color_space().type());
}
RUBY_END
static
RUBY_DEF2(at, x, y)
{
CHECK;
int xx = x.as_i(), yy = y.as_i();
- void* pixel = this->at<void*>(xx, yy);
- if (!pixel) error("Bitmap#at(%d, %d) failed.", xx, yy);
+ void* pixel = THIS->at<void*>(xx, yy);
+ if (!pixel) rays_error("Bitmap#at(%d, %d) failed.", xx, yy);
- Value array(this->color_space().Bpp() / this->color_space().Bpc(), NULL);
- if (this->color_space().is_float())
+ Value array(THIS->color_space().Bpp() / THIS->color_space().Bpc(), NULL);
+ if (THIS->color_space().is_float())
{
float* p = (float*) pixel;
- switch (this->color_space().type())
+ switch (THIS->color_space().type())
{
case Rays::GRAY_float:
array.push(p[0]);
break;
case Rays::RGB_float:
@@ -136,17 +160,17 @@
break;
case Rays::ABGR_float:
array.push(p[3]).push(p[2]).push(p[1]).push(p[0]);
break;
default:
- error("Bitmap#at: unknown color space");
+ rays_error("Bitmap#at: unknown color space");
}
}
else
{
uchar* p = (uchar*) pixel;
- switch (this->color_space().type())
+ switch (THIS->color_space().type())
{
case Rays::GRAY_8:
array.push(*(uchar*) pixel);
break;
case Rays::GRAY_16:
@@ -184,11 +208,11 @@
break;
case Rays::XBGR_8888:
array.push(p[3]).push(p[2]).push(p[1]);
break;
default:
- error("Bitmap#at: unknown color space");
+ rays_error("Bitmap#at: unknown color space");
}
}
return array;
}
@@ -198,18 +222,18 @@
RUBY_DEF3(assign_at, x, y, color)
{
CHECK;
int xx = x.as_i(), yy = y.as_i();
- void* pixel = this->at<void*>(xx, yy);
- if (!pixel) error("Bitmap#assign_at(%d, %d) failed.", xx, yy);
+ void* pixel = THIS->at<void*>(xx, yy);
+ if (!pixel) rays_error("Bitmap#assign_at(%d, %d) failed.", xx, yy);
- Value array(this->color_space().Bpp() / this->color_space().Bpc(), NULL);
- if (this->color_space().is_float())
+ Value array(THIS->color_space().Bpp() / THIS->color_space().Bpc(), NULL);
+ if (THIS->color_space().is_float())
{
float* p = (float*) pixel;
- switch (this->color_space().type())
+ switch (THIS->color_space().type())
{
#define C(n) ((float) color[n].as_f())
case Rays::GRAY_float:
p[0] = C(0);
break;
@@ -231,17 +255,17 @@
case Rays::ABGR_float:
p[0] = C(3); p[1] = C(2); p[2] = C(1); p[3] = C(0);
break;
#undef C
default:
- error("Bitmap#at: unknown color space");
+ rays_error("Bitmap#at: unknown color space");
}
}
else
{
uchar* p = (uchar*) pixel;
- switch (this->color_space().type())
+ switch (THIS->color_space().type())
{
#define C(n) ((uchar) color[n].as_i())
case Rays::GRAY_8:
*(uchar*) pixel = C(0);
break;
@@ -281,11 +305,11 @@
case Rays::XBGR_8888:
p[0] = C(3); p[1] = C(2); p[2] = C(1);
break;
#undef C
default:
- error("Bitmap#at: unknown color space");
+ rays_error("Bitmap#at: unknown color space");
}
}
return color;
}
@@ -295,32 +319,32 @@
static
RUBY_DEF1(load, path)
{
Rays::Bitmap bmp;
if (!Rays::load_bitmap(&bmp, path.c_str()))
- error("Bitmap.load('%s') failed.", path.c_str());
+ rays_error("Bitmap.load('%s') failed.", path.c_str());
return value(bmp);
}
RUBY_END
void
Init_bitmap ()
{
- Module m = define_module("Rays");
+ Module mRays = define_module("Rays");
- m.define_const("RGB", Rays::RGB);
- m.define_const("RGBA", Rays::RGBA);
+ mRays.define_const("GRAY", Rays::GRAY);
+ mRays.define_const("RGB", Rays::RGB);
+ mRays.define_const("RGBA", Rays::RGBA);
- Class c = m.define_class("Bitmap");
- Rays::cBitmap = c;
-
- c.define_alloc_func(alloc);
- c.define_method("initialize", initialize);
- c.define_method("width", width);
- c.define_method("height", height);
- c.define_method("color_space", color_space);
- c.define_method("[]", at);
- c.define_method("[]=", assign_at);
- c.define_function("load", load);
+ cBitmap = mRays.define_class("Bitmap");
+ cBitmap.define_alloc_func(alloc);
+ cBitmap.define_private_method("setup", setup);
+ cBitmap.define_method("draw_string", draw_string);
+ cBitmap.define_method("width", width);
+ cBitmap.define_method("height", height);
+ cBitmap.define_method("color_space", color_space);
+ cBitmap.define_method("[]", at);
+ cBitmap.define_method("[]=", assign_at);
+ cBitmap.define_function("load", load);
}