Skip to content
Browse files

More complete macros with some structures move entirely to them

  • Loading branch information...
1 parent 93639f7 commit e4746d2e07ab61e4eb0a117edac113a499758208 Vincent Kuyatt committed Feb 9, 2014
Showing with 304 additions and 195 deletions.
  1. +0 −1 binding.gyp
  2. +88 −131 src/container.cc
  3. +36 −25 src/container.h
  4. +147 −3 src/helpers.h
  5. +1 −1 src/key.cc
  6. +7 −3 src/node_sdl.cc
  7. +12 −16 src/render.cc
  8. +1 −1 src/struct_wrappers.cc
  9. +10 −12 src/surface.cc
  10. +1 −1 src/texture.cc
  11. +1 −1 src/window.cc
View
1 binding.gyp
@@ -4,7 +4,6 @@
# have to specify "liblib" here since gyp will remove the first one :\
# Is the above still true?
"target_name": "libnode_sdl",
- "type": "<(library)",
"sources": [
"src/helpers.cc",
"src/node_sdl.cc",
View
219 src/container.cc
@@ -6,150 +6,107 @@
using namespace v8;
-Persistent<FunctionTemplate> sdl::RectWrapper::wrap_template_;
-
-sdl::RectWrapper::RectWrapper() {
-}
-sdl::RectWrapper::RectWrapper(Handle<Object> toWrap) {
- Wrap(toWrap);
-}
sdl::RectWrapper::~RectWrapper() {
- if(NULL != rect_) {
- delete rect_;
- }
-}
-
-void sdl::RectWrapper::Init(Handle<Object> exports) {
- Handle<FunctionTemplate> tpl = FunctionTemplate::New(New);
- wrap_template_ = Persistent<FunctionTemplate>::New(tpl);
-
- wrap_template_->InstanceTemplate()->SetInternalFieldCount(1);
- wrap_template_->SetClassName(String::New("RectWrapper"));
-
- Local<ObjectTemplate> templ = wrap_template_->PrototypeTemplate();
- templ->SetAccessor(String::NewSymbol("x"), GetX, SetX);
- templ->SetAccessor(String::NewSymbol("y"), GetY, SetY);
- templ->SetAccessor(String::NewSymbol("w"), GetW, SetW);
- templ->SetAccessor(String::NewSymbol("h"), GetH, SetH);
-
- exports->Set(String::NewSymbol("Rect"), wrap_template_->GetFunction());
-}
-Handle<Value> sdl::RectWrapper::New(const Arguments& args) {
- if(!args.IsConstructCall()) {
- return ThrowException(Exception::TypeError(
- String::New("Cannot construct a Rect without using the new operator.")));
+ if(NULL != wrapped) {
+ delete wrapped;
}
-
- HandleScope scope;
-
- if(args.Length() < 4) {
- return ThrowException(Exception::TypeError(
- String::New("Invalid arguments: Expected new sdl.Rect(Number, Number, Number, Number)")));
- }
-
- int x = args[0]->Int32Value();
- int y = args[1]->Int32Value();
- int w = args[2]->Int32Value();
- int h = args[3]->Int32Value();
- SDL_Rect* rect = new SDL_Rect;
- rect->x = x;
- rect->y = y;
- rect->w = w;
- rect->h = h;
-
- RectWrapper* wrap = new RectWrapper();
- wrap->rect_ = rect;
- wrap->Wrap(args.This());
-
- return args.This();
-}
-
-Handle<Value> sdl::RectWrapper::GetX(Local<String> name, const AccessorInfo& info) {
- HandleScope scope;
-
- RectWrapper* wrap = ObjectWrap::Unwrap<RectWrapper>(info.This());
-
- return scope.Close(Number::New(wrap->rect_->x));
-}
-Handle<Value> sdl::RectWrapper::GetY(Local<String> name, const AccessorInfo& info) {
- HandleScope scope;
-
- RectWrapper* wrap = ObjectWrap::Unwrap<RectWrapper>(info.This());
-
- return scope.Close(Number::New(wrap->rect_->y));
-}
-Handle<Value> sdl::RectWrapper::GetW(Local<String> name, const AccessorInfo& info) {
- HandleScope scope;
-
- RectWrapper* wrap = ObjectWrap::Unwrap<RectWrapper>(info.This());
-
- return scope.Close(Number::New(wrap->rect_->w));
-}
-Handle<Value> sdl::RectWrapper::GetH(Local<String> name, const AccessorInfo& info) {
- HandleScope scope;
-
- RectWrapper* wrap = ObjectWrap::Unwrap<RectWrapper>(info.This());
-
- return scope.Close(Number::New(wrap->rect_->h));
}
-void sdl::RectWrapper::SetX(Local<String> name, Local<Value> value, const AccessorInfo& info) {
- HandleScope scope;
-
- int x = value->Int32Value();
- RectWrapper* wrap = ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(info.This()));
- wrap->rect_->x = x;
-}
-void sdl::RectWrapper::SetY(Local<String> name, Local<Value> value, const AccessorInfo& info) {
- HandleScope scope;
+START_INIT(sdl, RectWrapper)
+ GETTER_SETTER(wrap_template_, "x", GetX, SetX)
+ GETTER_SETTER(wrap_template_, "y", GetY, SetY)
+ GETTER_SETTER(wrap_template_, "w", GetW, SetW)
+ GETTER_SETTER(wrap_template_, "h", GetH, SetH)
+END_INIT("Rect")
+
+START_NEW(sdl, RectWrapper, 4)
+ if(args[0]->IsExternal()) {
+ UNWRAP_EXTERNAL(SDL_Rect, rect, 0);
+ RectWrapper* wrap = new RectWrapper();
+ wrap->wrapped = rect;
+ wrap->Wrap(args.This());
+ }
+ else {
+ EXTRACT_INT32(x, 0);
+ EXTRACT_INT32(y, 1);
+ EXTRACT_INT32(w, 2);
+ EXTRACT_INT32(h, 3);
+ SDL_Rect* rect = new SDL_Rect;
+ rect->x = x;
+ rect->y = y;
+ rect->w = w;
+ rect->h = h;
+
+ RectWrapper* wrap = new RectWrapper();
+ wrap->wrapped = rect;
+ wrap->Wrap(args.This());
+ }
+END_NEW
+
+GETTER_BEGIN(sdl::RectWrapper, GetX)
+ UNWRAP_THIS(RectWrapper, info, wrap)
+GETTER_END(Number::New(wrap->wrapped->x))
+
+GETTER_BEGIN(sdl::RectWrapper, GetY)
+ UNWRAP_THIS(RectWrapper, info, wrap)
+GETTER_END(Number::New(wrap->wrapped->y))
+
+GETTER_BEGIN(sdl::RectWrapper, GetW)
+ UNWRAP_THIS(RectWrapper, info, wrap)
+GETTER_END(Number::New(wrap->wrapped->w));
+
+GETTER_BEGIN(sdl::RectWrapper, GetH)
+ UNWRAP_THIS(RectWrapper, info, wrap)
+GETTER_END(Number::New(wrap->wrapped->h))
+
+SETTER_BEGIN(sdl::RectWrapper, SetX)
+ UNWRAP_THIS_SETTER(RectWrapper, info, wrap)
+ VALUE_INT32(x);
+ wrap->wrapped->x = x;
+ END_VALUE
+ UNWRAP_END
+SETTER_END
+
+SETTER_BEGIN(sdl::RectWrapper, SetY)
+ UNWRAP_THIS_SETTER(RectWrapper, info, wrap)
+ VALUE_INT32(y);
+ wrap->wrapped->y = y;
+ END_VALUE
+ UNWRAP_END
+SETTER_END
+
+SETTER_BEGIN(sdl::RectWrapper, SetW)
+ UNWRAP_THIS_SETTER(RectWrapper, info, wrap)
+ VALUE_INT32(w);
+ wrap->wrapped->w = w;
+ END_VALUE
+ UNWRAP_END
+SETTER_END
+
+SETTER_BEGIN(sdl::RectWrapper, SetH)
+ UNWRAP_THIS_SETTER(RectWrapper, info, wrap)
+ VALUE_INT32(h);
+ wrap->wrapped->h = h;
+ END_VALUE
+ UNWRAP_END
+SETTER_END
- int y = value->Int32Value();
- RectWrapper* wrap = ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(info.This()));
- wrap->rect_->y = y;
-}
-void sdl::RectWrapper::SetW(Local<String> name, Local<Value> value, const AccessorInfo& info) {
- HandleScope scope;
-
- int w = value->Int32Value();
- RectWrapper* wrap = ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(info.This()));
- wrap->rect_->w = w;
-}
-void sdl::RectWrapper::SetH(Local<String> name, Local<Value> value, const AccessorInfo& info) {
- HandleScope scope;
-
- int h = value->Int32Value();
- RectWrapper* wrap = ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(info.This()));
- wrap->rect_->h = h;
-}
-
-Persistent<FunctionTemplate> sdl::ColorWrapper::wrap_template_;
-
-sdl::ColorWrapper::ColorWrapper() {
-}
sdl::ColorWrapper::~ColorWrapper() {
- // std::cout << "ColorWrapper destructor running." << std::endl;
if(NULL != color_) {
delete color_;
}
}
-void sdl::ColorWrapper::Init(Handle<Object> exports) {
- Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
- wrap_template_ = Persistent<FunctionTemplate>::New(tpl);
+START_INIT(sdl, ColorWrapper)
+ GETTER_SETTER(wrap_template_, "r", GetRed, SetRed);
+ GETTER_SETTER(wrap_template_, "g", GetGreen, SetGreen);
+ GETTER_SETTER(wrap_template_, "b", GetBlue, SetBlue);
+ GETTER_SETTER(wrap_template_, "a", GetAlpha, SetAlpha);
- wrap_template_->InstanceTemplate()->SetInternalFieldCount(1);
- wrap_template_->SetClassName(String::NewSymbol("ColorWrapper"));
+ PROTO_METHOD(wrap_template_, getColor, GetColor);
+ PROTO_METHOD(wrap_template_, toString, ToString);
+END_INIT("Color")
- Local<ObjectTemplate> templ = wrap_template_->PrototypeTemplate();
- templ->SetAccessor(String::NewSymbol("r"), GetRed, SetRed);
- templ->SetAccessor(String::NewSymbol("g"), GetGreen, SetGreen);
- templ->SetAccessor(String::NewSymbol("b"), GetBlue, SetBlue);
- templ->SetAccessor(String::NewSymbol("a"), GetAlpha, SetAlpha);
- NODE_SET_PROTOTYPE_METHOD(tpl, "getColor", GetColor);
- NODE_SET_PROTOTYPE_METHOD(tpl, "toString", ToString);
-
- exports->Set(String::NewSymbol("Color"), wrap_template_->GetFunction());
-}
Handle<Value> sdl::ColorWrapper::New(const Arguments& args) {
if(!args.IsConstructCall()) {
return ThrowException(Exception::TypeError(
View
61 src/container.h
@@ -5,38 +5,49 @@
#include <v8.h>
#include "SDL.h"
+#include "helpers.h"
namespace sdl {
- class RectWrapper : public node::ObjectWrap {
- public:
- static v8::Persistent<v8::FunctionTemplate> wrap_template_;
-
- RectWrapper();
- RectWrapper(v8::Handle<v8::Object> toWrap);
- ~RectWrapper();
-
- static void Init(v8::Handle<v8::Object> exports);
- static v8::Handle<v8::Value> New(const v8::Arguments& args);
-
- static v8::Handle<v8::Value> GetX(v8::Local<v8::String> name, const v8::AccessorInfo& info);
- static v8::Handle<v8::Value> GetY(v8::Local<v8::String> name, const v8::AccessorInfo& info);
- static v8::Handle<v8::Value> GetW(v8::Local<v8::String> name, const v8::AccessorInfo& info);
- static v8::Handle<v8::Value> GetH(v8::Local<v8::String> name, const v8::AccessorInfo& info);
-
- static void SetX(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info);
- static void SetY(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info);
- static void SetW(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info);
- static void SetH(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info);
-
- SDL_Rect* rect_;
- };
+ OPEN_OBJECTWRAP(RectWrapper)
+ static GETTER_DEF(GetX);
+ static GETTER_DEF(GetY);
+ static GETTER_DEF(GetW);
+ static GETTER_DEF(GetH);
+
+ static SETTER_DEF(SetX);
+ static SETTER_DEF(SetY);
+ static SETTER_DEF(SetW);
+ static SETTER_DEF(SetH);
+ CLOSE_OBJECTWRAP(SDL_Rect)
+ // class RectWrapper : public node::ObjectWrap {
+ // public:
+ // static v8::Persistent<v8::FunctionTemplate> wrap_template_;
+
+ // RectWrapper();
+ // RectWrapper(v8::Handle<v8::Object> toWrap);
+ // ~RectWrapper();
+
+ // static void Init(v8::Handle<v8::Object> exports);
+ // static v8::Handle<v8::Value> New(const v8::Arguments& args);
+
+ // static GETTER_DEF(GetX);
+ // static GETTER_DEF(GetY);
+ // static GETTER_DEF(GetW);
+ // static GETTER_DEF(GetH);
+
+ // static void SetX(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info);
+ // static void SetY(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info);
+ // static void SetW(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info);
+ // static void SetH(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info);
+
+ // SDL_Rect* wrapped;
+ // };
class ColorWrapper : public node::ObjectWrap {
public:
static v8::Persistent<v8::FunctionTemplate> wrap_template_;
- ColorWrapper();
~ColorWrapper();
static void Init(v8::Handle<v8::Object> exports);
@@ -78,4 +89,4 @@ namespace sdl {
};
}
-#endif
+#endif
View
150 src/helpers.h
@@ -5,11 +5,15 @@
#include <node.h>
#include <node_buffer.h>
#include <sstream>
+#include <iostream>
#define S(x) #x
+#define TOKENPASTE(x, y) x ## y
+#define TOKENPASTE2(x, y) TOKENPASTE(x, y)
+
#ifdef ENABLE_ARG_CHECKING
#define CHECK_ARGLEN(function_name, num_args) \
- if(args.Length() < num_args) { \
+ if(!args[0]->IsExternal() && args.Length() < num_args) { \
std::stringstream ss; \
ss << "Invalid arguments: Expected "; \
ss << num_args; \
@@ -82,6 +86,34 @@
return v8::ThrowException(v8::Exception::TypeError( \
v8::String::New(ss.str().c_str()))); \
}
+
+#define CHECK_WRAPPER(name, type) \
+ if(NULL == name) { \
+ std::stringstream ss; \
+ ss << "Invalid call: Expected this to be a "; \
+ ss << S(type); \
+ ss << "."; \
+ return v8::ThrowException(v8::Exception::TypeError( \
+ v8::String::New(ss.str().c_str()))); \
+ }
+
+#define CHECK_CONSTRUCT(class_name) \
+ if(!args.IsConstructCall()) { \
+ std::stringstream ss; \
+ ss << "Must use the new operator to create instances of class " << S(type); \
+ return v8::ThrowException(v8::Exception::TypeError( \
+ String::New(ss.str().c_str()))); \
+ }
+
+#define CHECK_EXTERNAL(arg_n) \
+ if(!args[0]->IsExternal()) { \
+ std::stringstream ss; \
+ ss << "Invalid arguments: Expected argument "; \
+ ss << arg_n; \
+ ss << " to be an External."; \
+ return v8::ThrowException(v8::Exception::TypeError( \
+ v8::String::New(ss.str().c_str()))); \
+ }
#else
#define CHECK_ARGLEN(function_name, num_args)
@@ -93,6 +125,12 @@
#define CHECK_BOOL(arg_n)
#define CHECK_BOOL_F(arg_n, function_name)
+
+#define CHECK_WRAPPER(name, type)
+
+#define CHECK_CONSTRUCT(class_name)
+
+#define CHECK_EXTERNAL(arg_n)
#endif
#define FUNCTION_DEF(name) v8::Handle<v8::Value> name(const v8::Arguments& args)
@@ -108,6 +146,21 @@
#define FUNCTION_UNDEFINED return Undefined(); \
}
+#define GETTER_DEF(func_name) v8::Handle<v8::Value> func_name(v8::Local<v8::String> name, const v8::AccessorInfo& info)
+#define GETTER_DEFP(prefix, func_name) v8::Handle<v8::Value> prefix::func_name(v8::Local<v8::String> name, const v8::AccessorInfo& info)
+#define GETTER_BEGIN(prefix, func_name) GETTER_DEFP(prefix, func_name) { \
+ v8::HandleScope scope;
+#define GETTER_END(ret) return scope.Close(ret); \
+}
+#define GETTER_UNDEFINED return Undefined(); \
+}
+
+#define SETTER_DEF(func_name) void func_name(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
+#define SETTER_DEFP(prefix, func_name) void prefix::func_name(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
+#define SETTER_BEGIN(prefix, func_name) SETTER_DEFP(prefix, func_name) { \
+ v8::HandleScope scope;
+#define SETTER_END }
+
#define EXTRACT_STRING(name, arg_n) \
CHECK_STRING(arg_n); \
v8::String::Utf8Value name(args[arg_n])
@@ -116,7 +169,7 @@
bool name = args[arg_n]->BooleanValue()
#define EXTRACT_NUMBER(name, arg_n) \
CHECK_NUMBER(arg_n); \
- float name = args[arg_n]->NumberValue()
+ double name = args[arg_n]->NumberValue()
#define EXTRACT_INT64(name, arg_n) \
CHECK_NUMBER(arg_n); \
int64_t name = args[arg_n]->IntegerValue()
@@ -127,13 +180,104 @@
CHECK_NUMBER(arg_n); \
uint32_t name = args[arg_n]->Uint32Value()
+#ifdef ENABLE_ARG_CHECKING
+#define VALUE_STRING(name) \
+ if(value->IsString()) { \
+ v8::String::Utf8Value name(value);
+#define VALUE_BOOL(name) \
+ if(value->IsBoolean()) { \
+ bool name = value->BooleanValue();
+#define VALUE_NUMBER(name) \
+ if(value->IsNumber()) { \
+ double name = value->NumberValue();
+#define VALUE_INT64(name) \
+ if(value->IsNumber()) { \
+ int64_t name = value->IntegerValue();
+#define VALUE_INT32(name) \
+ if(value->IsInt32()) { \
+ int name = value->Int32Value();
+#define VALUE_UINT32(name) \
+ if(value->IsUint32()) { \
+ uint32_t name = value->Uint32Value();
+#define UNWRAP_THIS_SETTER(type, from, name) type* name = node::ObjectWrap::Unwrap<type>(from.This()); \
+ if(NULL != name) {
+#define END_VALUE } else { \
+ std::cout << "Unable to unwrap value in '" << __func__ << ":" << __LINE__ << "'." << std::endl; \
+ }
+#define UNWRAP_END } else { \
+ std::cout << "Unable to unwrap 'this' in '" << __func__ << ":" << __LINE__ << "'." << std::endl; \
+ }
+#else
+#define VALUE_STRING(name) \
+ v8::String::Utf8Value name(value);
+#define VALUE_BOOL(name) \
+ bool name = value->BooleanValue();
+#define VALUE_NUMBER(name) \
+ double name = value->NumberValue();
+#define VALUE_INT64(name) \
+ int64_t name = value->IntegerValue();
+#define VALUE_INT32(name) \
+ int name = value->Int32Value();
+#define VALUE_UINT32(name) \
+ uint32_t name = value->Uint32Value();
+#define UNWRAP_THIS_SETTER(type, from, name) \
+ type* name = node::ObjectWrap::Unwrap<type>(from.This());
+#define END_VALUE
+#define UNWRAP_END
+#endif
+
+#define UNWRAP_THIS(type, from, name) type* name = node::ObjectWrap::Unwrap<type>(from.This()); \
+ CHECK_WRAPPER(name, type)
+
+#define OPEN_OBJECTWRAP(type) class type : public node::ObjectWrap { \
+ public: \
+ static v8::Persistent<v8::FunctionTemplate> wrap_template_; \
+ ~type(); \
+ static void Init(v8::Handle<v8::Object> target); \
+ static v8::Handle<v8::Value> New(const v8::Arguments& args);
+#define CLOSE_OBJECTWRAP(wrap_type) wrap_type* wrapped; \
+ };
+
+#define CREATE_TEMPLATE(template, type) \
+ v8::Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New(New); \
+ template = v8::Persistent<v8::FunctionTemplate>::New(tpl); \
+ template->InstanceTemplate()->SetInternalFieldCount(2); \
+ template->SetClassName(v8::String::NewSymbol(S(type)));
+#define GETTER(template, name, fun) template->PrototypeTemplate()->SetAccessor(String::NewSymbol(name), fun);
+#define GETTER_SETTER(template, name, get, set) template->PrototypeTemplate()->SetAccessor(String::NewSymbol(name), get, set);
+#define SET(target, symbol, object) target->Set(String::NewSymbol(symbol), object)
+#define START_INIT(prefix, type) \
+ v8::Persistent<v8::FunctionTemplate> prefix::type::wrap_template_; \
+ void prefix::type::Init(Handle<Object> target) { \
+ CREATE_TEMPLATE(wrap_template_, type)
+#define END_INIT(symbol) SET(target, symbol, wrap_template_->GetFunction()); \
+ }
+
+#define PROTO_METHOD(target, name, callback) \
+ v8::Local<v8::FunctionTemplate> TOKENPASTE2(template, name) = v8::FunctionTemplate::New(callback); \
+ target->PrototypeTemplate()->Set(v8::String::NewSymbol(S(name)), TOKENPASTE2(template, name));
+
+#define UNWRAP_EXTERNAL(type, name, arg) \
+ CHECK_EXTERNAL(arg) \
+ type* name = static_cast<type*>(Handle<External>::Cast(args[arg])->Value())
+#define START_NEW(prefix, type, num_args) v8::Handle<v8::Value> prefix::type::New(const v8::Arguments& args) { \
+ CHECK_CONSTRUCT(S(type)) \
+ HandleScope scope; \
+ CHECK_ARGLEN(S(type), num_args);
+#define END_NEW return args.This(); \
+ }
+
+#define NEW_WRAPPED(pointer, type, ret) \
+ v8::Handle<v8::Value> argv[] = {v8::External::New(pointer)}; \
+ v8::Handle<v8::Object> ret = type::wrap_template_->GetFunction()->NewInstance(1, argv);
+
namespace sdl {
// Error reporting helpers
v8::Handle<v8::Value> ThrowSDLException(const char* name);
v8::Local<v8::Value> MakeSDLException(const char* name);
-
+
// Helpers to work with buffers
char* BufferData(node::Buffer *b);
size_t BufferLength(node::Buffer *b);
View
2 src/key.cc
@@ -717,7 +717,7 @@ Handle<Value> sdl::SetTextInputRect(const Arguments& args) {
}
RectWrapper* rect = ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(args[0]));
- SDL_SetTextInputRect(rect->rect_);
+ SDL_SetTextInputRect(rect->wrapped);
return Undefined();
}
View
10 src/node_sdl.cc
@@ -18,6 +18,7 @@
#include "font.h"
#include "joystick.h"
#include "controller.h"
+#include "helpers.h"
#include <v8.h>
#include <string>
#include <iostream>
@@ -402,9 +403,12 @@ Handle<Value> sdl::GetDisplayBounds(const Arguments& args) {
return ThrowSDLException(__func__);
}
- Handle<Object> ret = Object::New();
- RectWrapper* wrap = new RectWrapper(ret);
- wrap->rect_ = bounds;
+ NEW_WRAPPED(bounds, RectWrapper, ret);
+// Handle<Value> argv[] = {External::New(bounds)};
+// Handle<Object> ret = RectWrapper::wrap_template_->GetFunction()->NewInstance(1, argv);
+// Handle<Object> ret = Object::New();
+// RectWrapper* wrap = new RectWrapper(ret);
+// wrap->wrapped = bounds;
return scope.Close(ret);
}
View
28 src/render.cc
@@ -248,9 +248,7 @@ Handle<Value> sdl::RendererWrapper::GetClipRect(const Arguments& args) {
SDL_Rect* rect = new SDL_Rect;
SDL_RenderGetClipRect(obj->renderer_, rect);
- Handle<Object> ret = Object::New();
- RectWrapper* wrap = new RectWrapper(ret);
- wrap->rect_ = rect;
+ NEW_WRAPPED(rect, RectWrapper, ret)
return scope.Close(ret);
}
@@ -286,9 +284,7 @@ Handle<Value> sdl::RendererWrapper::GetViewport(const Arguments& args) {
SDL_Rect* rect = new SDL_Rect;
SDL_RenderGetViewport(obj->renderer_, rect);
- Handle<Object> ret = Object::New();
- RectWrapper* wrap = new RectWrapper(ret);
- wrap->rect_ = rect;
+ NEW_WRAPPED(rect, RectWrapper, ret)
return scope.Close(ret);
}
@@ -316,7 +312,7 @@ Handle<Value> sdl::RendererWrapper::SetClipRect(const Arguments& args) {
}
else {
RectWrapper* clip = ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(args[0]));
- err = SDL_RenderSetClipRect(obj->renderer_, clip->rect_);
+ err = SDL_RenderSetClipRect(obj->renderer_, clip->wrapped);
}
if(err < 0) {
return ThrowSDLException(__func__);
@@ -366,7 +362,7 @@ Handle<Value> sdl::RendererWrapper::SetViewport(const Arguments& args) {
}
else {
RectWrapper* wrap = ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(args[0]));
- err = SDL_RenderSetViewport(obj->renderer_, wrap->rect_);
+ err = SDL_RenderSetViewport(obj->renderer_, wrap->wrapped);
}
if(err < 0) {
return ThrowSDLException(__func__);
@@ -450,8 +446,8 @@ Handle<Value> sdl::RendererWrapper::Copy(const Arguments& args) {
PointWrapper* point = args[4]->IsUndefined() ? NULL : ObjectWrap::Unwrap<PointWrapper>(Handle<Object>::Cast(args[4]));
SDL_RendererFlip flip = args[5]->IsUndefined() ? SDL_FLIP_NONE : static_cast<SDL_RendererFlip>(args[5]->Int32Value());
int err = SDL_RenderCopyEx(obj->renderer_, texture->texture_,
- src == NULL ? NULL : src->rect_,
- dst == NULL ? NULL : dst->rect_,
+ src == NULL ? NULL : src->wrapped,
+ dst == NULL ? NULL : dst->wrapped,
angle,
point == NULL ? NULL : point->point_,
flip);
@@ -464,8 +460,8 @@ Handle<Value> sdl::RendererWrapper::Copy(const Arguments& args) {
RectWrapper* src = args[1]->IsUndefined() ? NULL : ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(args[1]));
RectWrapper* dst = args[2]->IsUndefined() ? NULL : ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(args[2]));
int err = SDL_RenderCopy(obj->renderer_, texture->texture_,
- src == NULL ? NULL : src->rect_,
- dst == NULL ? NULL : dst->rect_);
+ src == NULL ? NULL : src->wrapped,
+ dst == NULL ? NULL : dst->wrapped);
if(err < 0) {
return ThrowSDLException(__func__);
}
@@ -564,7 +560,7 @@ Handle<Value> sdl::RendererWrapper::DrawRect(const Arguments& args) {
}
RectWrapper* rect = ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(args[0]));
- int err = SDL_RenderDrawRect(obj->renderer_, rect->rect_);
+ int err = SDL_RenderDrawRect(obj->renderer_, rect->wrapped);
if(err < 0) {
return ThrowSDLException(__func__);
}
@@ -584,7 +580,7 @@ Handle<Value> sdl::RendererWrapper::DrawRects(const Arguments& args) {
SDL_Rect* rects = new SDL_Rect[numRects];
for(int i = 0; i < numRects; i++) {
RectWrapper* rect = ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(arr->Get(i)));
- rects[i] = *rect->rect_;
+ rects[i] = *rect->wrapped;
}
int err = SDL_RenderDrawRects(obj->renderer_, rects, numRects);
delete rects;
@@ -603,7 +599,7 @@ Handle<Value> sdl::RendererWrapper::FillRect(const Arguments& args) {
}
RectWrapper* rect = ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(args[0]));
- int err = SDL_RenderFillRect(obj->renderer_, rect->rect_);
+ int err = SDL_RenderFillRect(obj->renderer_, rect->wrapped);
if(err < 0) {
return ThrowSDLException(__func__);
}
@@ -623,7 +619,7 @@ Handle<Value> sdl::RendererWrapper::FillRects(const Arguments& args) {
SDL_Rect* rects = new SDL_Rect[numRects];
for(int i = 0; i < numRects; i++) {
RectWrapper* rect = ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(arr->Get(i)));
- rects[i] = *rect->rect_;
+ rects[i] = *rect->wrapped;
}
int err = SDL_RenderFillRects(obj->renderer_, rects, numRects);
delete rects;
View
2 src/struct_wrappers.cc
@@ -12,7 +12,7 @@ using namespace v8;
namespace sdl {
typedef Handle<ObjectTemplate> (*TemplateMaker)();
- static Persistent<ObjectTemplate> rect_template_;
+ static Persistent<ObjectTemplate> wrappedtemplate_;
static Persistent<ObjectTemplate> point_template_;
static Persistent<ObjectTemplate> color_template_;
static Persistent<ObjectTemplate> palette_template_;
View
22 src/surface.cc
@@ -177,9 +177,9 @@ Handle<Value> sdl::SurfaceWrapper::BlitScaled(const Arguments& args) {
RectWrapper* dst = args[1]->IsUndefined() ? NULL : ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(args[1]));
RectWrapper* src = args[2]->IsUndefined() ? NULL : ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(args[2]));
int err = SDL_BlitScaled(obj->surface_,
- src == NULL ? NULL : src->rect_,
+ src == NULL ? NULL : src->wrapped,
other->surface_,
- dst == NULL ? NULL : dst->rect_);
+ dst == NULL ? NULL : dst->wrapped);
if(err < 0) {
return ThrowSDLException(__func__);
}
@@ -198,7 +198,7 @@ Handle<Value> sdl::SurfaceWrapper::BlitSurface(const Arguments& args) {
SurfaceWrapper* other = ObjectWrap::Unwrap<SurfaceWrapper>(Handle<Object>::Cast(args[0]));
RectWrapper* dst = ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(args[1]));
RectWrapper* src = args[2]->IsUndefined() ? NULL : ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(args[2]));
- int err = SDL_BlitSurface(obj->surface_, src->rect_, other->surface_, dst == NULL ? NULL : dst->rect_);
+ int err = SDL_BlitSurface(obj->surface_, src->wrapped, other->surface_, dst == NULL ? NULL : dst->wrapped);
if(err < 0) {
return ThrowSDLException(__func__);
}
@@ -217,7 +217,7 @@ Handle<Value> sdl::SurfaceWrapper::LowerBlit(const Arguments& args) {
SurfaceWrapper* other = ObjectWrap::Unwrap<SurfaceWrapper>(Handle<Object>::Cast(args[0]));
RectWrapper* dst = ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(args[1]));
RectWrapper* src = args[2]->IsUndefined() ? NULL : ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(args[2]));
- int err = SDL_LowerBlit(obj->surface_, src->rect_, other->surface_, dst == NULL ? NULL : dst->rect_);
+ int err = SDL_LowerBlit(obj->surface_, src->wrapped, other->surface_, dst == NULL ? NULL : dst->wrapped);
if(err < 0) {
return ThrowSDLException(__func__);
}
@@ -237,9 +237,9 @@ Handle<Value> sdl::SurfaceWrapper::LowerBlitScaled(const Arguments& args) {
RectWrapper* dst = args[1]->IsUndefined() ? NULL : ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(args[1]));
RectWrapper* src = args[2]->IsUndefined() ? NULL : ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(args[2]));
int err = SDL_LowerBlitScaled(obj->surface_,
- src == NULL ? NULL : src->rect_,
+ src == NULL ? NULL : src->wrapped,
other->surface_,
- dst == NULL ? NULL : dst->rect_);
+ dst == NULL ? NULL : dst->wrapped);
if(err < 0) {
return ThrowSDLException(__func__);
}
@@ -301,7 +301,7 @@ Handle<Value> sdl::SurfaceWrapper::FillRect(const Arguments& args) {
SurfaceWrapper* self = ObjectWrap::Unwrap<SurfaceWrapper>(handleObj);
int color = args[0]->Int32Value();
RectWrapper* rect = args[1]->IsUndefined() ? NULL : ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(args[1]));
- int err = SDL_FillRect(self->surface_, rect == NULL ? NULL : rect->rect_, color);
+ int err = SDL_FillRect(self->surface_, rect == NULL ? NULL : rect->wrapped, color);
if(err < 0) {
return ThrowSDLException(__func__);
}
@@ -323,7 +323,7 @@ Handle<Value> sdl::SurfaceWrapper::FillRects(const Arguments& args) {
SDL_Rect* rects = new SDL_Rect[numRects];
for(int i = 0; i < numRects; i++) {
RectWrapper* rect = ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(arr->Get(i)));
- rects[i] = *rect->rect_;
+ rects[i] = *rect->wrapped;
}
int err = SDL_FillRects(self->surface_, rects, numRects, color);
delete rects;
@@ -341,9 +341,7 @@ Handle<Value> sdl::SurfaceWrapper::GetClipRect(const Arguments& args) {
SDL_Rect* rect = new SDL_Rect;
SDL_GetClipRect(self->surface_, rect);
- Handle<Object> ret = Object::New();
- RectWrapper* wrap = new RectWrapper(ret);
- wrap->rect_ = rect;
+ NEW_WRAPPED(rect, RectWrapper, ret)
return scope.Close(ret);
}
@@ -429,7 +427,7 @@ Handle<Value> sdl::SurfaceWrapper::SetClipRect(const Arguments& args) {
}
SurfaceWrapper* self = ObjectWrap::Unwrap<SurfaceWrapper>(Handle<Object>::Cast(args.This()));
RectWrapper* clip = ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(args[0]));
- SDL_bool ret = SDL_SetClipRect(self->surface_, clip->rect_);
+ SDL_bool ret = SDL_SetClipRect(self->surface_, clip->wrapped);
return scope.Close(Boolean::New(ret));
}
View
2 src/texture.cc
@@ -289,7 +289,7 @@ Handle<Value> sdl::TextureWrapper::Update(const Arguments& args) {
String::New("Invalid arguments: Failed to unwrap first argument to a SurfaceWrapper. (did you not pass in an sdl.Surface?)")));
}
RectWrapper* rect = args[1]->IsUndefined() ? NULL : ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(args[1]));
- int err = SDL_UpdateTexture(texture->texture_, rect == NULL ? NULL : rect->rect_, surface->surface_->pixels, surface->surface_->pitch);
+ int err = SDL_UpdateTexture(texture->texture_, rect == NULL ? NULL : rect->wrapped, surface->surface_->pixels, surface->surface_->pitch);
if(err < 0) {
return ThrowSDLException(__func__);
}
View
2 src/window.cc
@@ -526,7 +526,7 @@ Handle<Value> sdl::WindowWrapper::UpdateWindowSurfaceRects(const Arguments& args
SDL_Rect* rects = new SDL_Rect[len];
for(int i = 0; i < len; i++) {
RectWrapper* wrap = ObjectWrap::Unwrap<RectWrapper>(Handle<Object>::Cast(arr->Get(i)));
- rects[i] = *wrap->rect_;
+ rects[i] = *wrap->wrapped;
}
int err = SDL_UpdateWindowSurfaceRects(obj->window_, rects, len);

0 comments on commit e4746d2

Please sign in to comment.
Something went wrong with that request. Please try again.