From c1306b32cb8292e622aeedbf9c64422cd422844d Mon Sep 17 00:00:00 2001 From: Vincent Kuyatt Date: Wed, 15 Jan 2014 10:47:26 -0500 Subject: [PATCH] Surface/texture/renderer wrapped. Can draw a white square! --- binding.gyp | 3 +- sdl.js | 24 +- src/node_sdl.cc | 192 +-------------- src/node_sdl.h | 7 - src/render.cc | 7 +- src/struct_wrappers.cc | 114 +-------- src/struct_wrappers.h | 13 - src/surface.cc | 533 +++++++++++++++++++++++++++++++++++++++++ src/surface.h | 62 +++++ src/texture.cc | 25 +- src/window.cc | 11 +- 11 files changed, 665 insertions(+), 326 deletions(-) create mode 100644 src/surface.cc create mode 100644 src/surface.h diff --git a/binding.gyp b/binding.gyp index 875f906..c7d42bf 100644 --- a/binding.gyp +++ b/binding.gyp @@ -9,7 +9,8 @@ 'src/struct_wrappers.cc', 'src/render.cc', 'src/texture.cc', - 'src/window.cc' + 'src/window.cc', + 'src/surface.cc' ], 'libraries': [ '/Library/Frameworks/SDL2.framework/SDL2', diff --git a/sdl.js b/sdl.js index 7253a89..df4db74 100755 --- a/sdl.js +++ b/sdl.js @@ -7,11 +7,11 @@ console.log("About to load node_sdl.node"); var SDL = module.exports = require('./build/Release/node_sdl.node'); console.log("Finished loading node_sdl.node"); -// console.log("Showing all keys for base SDL namespace:"); -// for(key in SDL) { -// console.log("SDLKEY: " + key + ", SDLVAL: " + SDL[key]); -// } -// console.log() +console.log("Showing all keys for base SDL namespace:"); +for(key in SDL) { + console.log("SDLKEY: " + key + ", SDLVAL: " + SDL[key]); +} +console.log() // console.log("Showing all keys for SDL.INIT:"); // for(key in SDL.INIT) { @@ -24,7 +24,21 @@ SDL.init("FOO"); console.log("About to create a window.") var window = new SDL.Window("Foo title") console.log("Finished creating window.") + +console.log("Creating a renderer."); var render = new SDL.Renderer(window); + +console.log("Creating a surface."); +var surface = new SDL.Surface(40, 40); +surface.fillRect(0xffffffff); +console.log("Surface has: w:" + surface.getWidth() + ", h:" + surface.getHeight()); + +console.log("Creating a texture."); +var texture = new SDL.Texture(render, surface); + +render.clear(); +render.copy(texture, undefined, SDL.Rect(10, 10, 100, 100)); +render.present(); // for(key in window) { // console.log("WINDOWKEY: " + key + ", " + "WINDOWVAL: " + window[key]); // if(isFunction(window[key])) { diff --git a/src/node_sdl.cc b/src/node_sdl.cc index 0bcbfd1..c64b021 100644 --- a/src/node_sdl.cc +++ b/src/node_sdl.cc @@ -8,6 +8,7 @@ #include "struct_wrappers.h" #include "window.h" #include "texture.h" +#include "surface.h" #include #include #include @@ -78,6 +79,7 @@ init(Handle target) sdl::WindowWrapper::Init(target); sdl::RendererWrapper::Init(target); sdl::TextureWrapper::Init(target); + sdl::SurfaceWrapper::Init(target); // Initialization and Shutdown. NODE_SET_METHOD(target, "init", sdl::Init); @@ -107,17 +109,10 @@ init(Handle target) NODE_SET_METHOD(target, "joystickUpdate", sdl::JoystickUpdate); NODE_SET_METHOD(target, "joystickEventState", sdl::JoystickEventState); - NODE_SET_METHOD(target, "fillRect", sdl::FillRect); - NODE_SET_METHOD(target, "createRGBSurface", sdl::CreateRGBSurface); - NODE_SET_METHOD(target, "blitSurface", sdl::BlitSurface); - NODE_SET_METHOD(target, "freeSurface", sdl::FreeSurface); - NODE_SET_METHOD(target, "setColorKey", sdl::SetColorKey); - NODE_SET_METHOD(target, "mapRGB", sdl::MapRGB); NODE_SET_METHOD(target, "mapRGBA", sdl::MapRGBA); NODE_SET_METHOD(target, "getRGB", sdl::GetRGB); NODE_SET_METHOD(target, "getRGBA", sdl::GetRGBA); - NODE_SET_METHOD(target, "setClipRect",sdl::SetClipRect); Local INIT = Object::New(); target->Set(String::New("INIT"), INIT); @@ -751,156 +746,6 @@ Handle sdl::JoystickEventState(const Arguments& args) { return Boolean::New(SDL_JoystickEventState(state)); } -Handle sdl::FillRect(const Arguments& args) { - HandleScope scope; - - if (!(args.Length() == 3 - && args[0]->IsObject() - && (args[1]->IsObject() || args[1]->IsNull()) - && args[2]->IsNumber() - )) { - return ThrowException(Exception::TypeError(String::New("Invalid arguments: Expected FillRect(Surface, Rect, Number)"))); - } - - SDL_Surface* surface = UnwrapSurface(args[0]->ToObject()); - SDL_Rect* rect; - if (args[1]->IsNull()) { - rect = NULL; - } else if (args[1]->IsArray()) { - SDL_Rect r; - Handle arr = args[1]->ToObject(); - r.x = arr->Get(String::New("0"))->Int32Value(); - r.y = arr->Get(String::New("1"))->Int32Value(); - r.w = arr->Get(String::New("2"))->Int32Value(); - r.h = arr->Get(String::New("3"))->Int32Value(); - rect = &r; - } else { - rect = UnwrapRect(args[1]->ToObject()); - } - int color = args[2]->Int32Value(); - - if (SDL_FillRect (surface, rect, color) < 0) return ThrowSDLException(__func__); - - return Undefined(); -} - -Handle sdl::CreateRGBSurface(const Arguments& args) { - HandleScope scope; - - if (!(args.Length() == 3 && args[0]->IsNumber() && args[1]->IsNumber() && args[2]->IsNumber())) { - return ThrowException(Exception::TypeError(String::New("Invalid arguments: Expected CreateRGBSurface(Number, Number, Number)"))); - } - - int flags = args[0]->Int32Value(); - int width = args[1]->Int32Value(); - int height = args[2]->Int32Value(); - - SDL_Surface *surface; - int rmask, gmask, bmask, amask; - - /* SDL interprets each pixel as a 32-bit number, so our masks must depend - on the endianness (byte order) of the machine */ -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - rmask = 0xff000000; - gmask = 0x00ff0000; - bmask = 0x0000ff00; - amask = 0x000000ff; -#else - rmask = 0x000000ff; - gmask = 0x0000ff00; - bmask = 0x00ff0000; - amask = 0xff000000; -#endif - - surface = SDL_CreateRGBSurface(flags, width, height, 32, rmask, gmask, bmask, amask); - if (surface == NULL) return ThrowSDLException(__func__); - return scope.Close(WrapSurface(surface)); -} - -Handle sdl::BlitSurface(const Arguments& args) { - HandleScope scope; - - if (!(args.Length() == 4 - && args[0]->IsObject() - && (args[1]->IsObject() || args[1]->IsNull()) - && args[2]->IsObject() - && (args[3]->IsObject() || args[3]->IsNull()) - )) { - return ThrowException(Exception::TypeError(String::New("Invalid arguments: Expected BlitSurface(Surface, Rect, Surface, Rect)"))); - } - - SDL_Surface* src = UnwrapSurface(args[0]->ToObject()); - SDL_Surface* dst = UnwrapSurface(args[2]->ToObject()); - - SDL_Rect* srcrect; - if (args[1]->IsNull()) { - srcrect = NULL; - } else if (args[1]->IsArray()) { - Handle arr1 = args[1]->ToObject(); - srcrect = new SDL_Rect(); - srcrect->x = arr1->Get(String::New("0"))->Int32Value(); - srcrect->y = arr1->Get(String::New("1"))->Int32Value(); - srcrect->w = arr1->Get(String::New("2"))->Int32Value(); - srcrect->h = arr1->Get(String::New("3"))->Int32Value(); - } else { - srcrect = UnwrapRect(args[1]->ToObject()); - } - - SDL_Rect* dstrect; - if (args[3]->IsNull()) { - dstrect = NULL; - } else if (args[3]->IsArray()) { - Handle arr2 = args[3]->ToObject(); - dstrect = new SDL_Rect(); - dstrect->x = arr2->Get(String::New("0"))->Int32Value(); - dstrect->y = arr2->Get(String::New("1"))->Int32Value(); - dstrect->w = arr2->Get(String::New("2"))->Int32Value(); - dstrect->h = arr2->Get(String::New("3"))->Int32Value(); - } else { - dstrect = UnwrapRect(args[3]->ToObject()); - } - -// if (srcrect) printf("srcrect = {x: %d, y: %d, w: %d, h: %d}\n", srcrect->x, srcrect->y, srcrect->w, srcrect->h); -// else printf("srcrect = null\n"); -// if (dstrect) printf("dstrect = {x: %d, y: %d, w: %d, h: %d}\n", dstrect->x, dstrect->y, dstrect->w, dstrect->h); -// else printf("dstrect = null\n"); - - - if (SDL_BlitSurface(src, srcrect, dst, dstrect) < 0) return ThrowSDLException(__func__); - return Undefined(); -} - -Handle sdl::FreeSurface(const Arguments& args) { - HandleScope scope; - - if (!(args.Length() == 1 && args[0]->IsObject())) { - return ThrowException(Exception::TypeError(String::New("Invalid arguments: Expected FreeSurface(Surface)"))); - } - - // TODO: find a way to do this automatically by using GC hooks. This is dangerous in JS land - SDL_FreeSurface(UnwrapSurface(args[0]->ToObject())); - args[0]->ToObject()->Set(String::New("DEAD"), Boolean::New(true)); - - return Undefined(); -} - -Handle sdl::SetColorKey(const Arguments& args) { - HandleScope scope; - - if (!(args.Length() == 3 && args[0]->IsObject() && args[1]->IsNumber() && args[2]->IsNumber())) { - return ThrowException(Exception::TypeError(String::New("Invalid arguments: Expected SetColorKey(Surface, Number, Number)"))); - } - - SDL_Surface* surface = UnwrapSurface(args[0]->ToObject()); - int flag = args[1]->Int32Value(); - int key = args[2]->Int32Value(); - - if (SDL_SetColorKey(surface, flag, key) < 0) return ThrowSDLException(__func__); - - return Undefined(); - -} - Handle sdl::MapRGB(const Arguments& args) { HandleScope scope; @@ -975,34 +820,6 @@ Handle sdl::GetRGBA(const Arguments& args) { return scope.Close(rgba); } - -Handle sdl::SetClipRect(const Arguments& args) { - HandleScope scope; - - if (!(args.Length() == 2 && args[0]->IsObject())) { - return ThrowException(Exception::TypeError(String::New("Invalid arguments: Expected SetClipRect(SDL_Surface, SDL_Rect)"))); - } - - SDL_Surface* surface = UnwrapSurface(args[0]->ToObject()); - SDL_Rect* rect; - if (args[1]->IsNull()) { - rect = NULL; - } else if (args[1]->IsArray()) { - SDL_Rect r; - Handle arr = args[1]->ToObject(); - r.x = arr->Get(String::New("0"))->Int32Value(); - r.y = arr->Get(String::New("1"))->Int32Value(); - r.w = arr->Get(String::New("2"))->Int32Value(); - r.h = arr->Get(String::New("3"))->Int32Value(); - rect = &r; - } else { - rect = UnwrapRect(args[1]->ToObject()); - } - if (SDL_SetClipRect (surface, rect ) < 0) return ThrowSDLException(__func__); - - return Undefined(); -} - Handle sdl::TTF::Init(const Arguments& args) { HandleScope scope; @@ -1091,7 +908,10 @@ Handle sdl::IMG::Load(const Arguments& args) { ))); } - return scope.Close(WrapSurface(image)); + Handle ret = Object::New(); + SurfaceWrapper* wrap = new SurfaceWrapper(ret); + wrap->surface_ = image; + return scope.Close(ret); } Handle sdl::GL::SetAttribute(const Arguments& args) { diff --git a/src/node_sdl.h b/src/node_sdl.h index 4385074..7e70d6f 100644 --- a/src/node_sdl.h +++ b/src/node_sdl.h @@ -68,17 +68,10 @@ namespace sdl { v8::Handle JoystickUpdate(const v8::Arguments& args); v8::Handle JoystickEventState(const v8::Arguments& args); - v8::Handle FillRect(const v8::Arguments& args); - v8::Handle CreateRGBSurface(const v8::Arguments& args); - v8::Handle BlitSurface(const v8::Arguments& args); - v8::Handle FreeSurface(const v8::Arguments& args); - v8::Handle SetColorKey(const v8::Arguments& args); - v8::Handle MapRGB(const v8::Arguments& args); v8::Handle MapRGBA(const v8::Arguments& args); v8::Handle GetRGB(const v8::Arguments& args); v8::Handle GetRGBA(const v8::Arguments& args); - v8::Handle SetClipRect(const v8::Arguments& args); namespace TTF { v8::Handle Init(const v8::Arguments& args); diff --git a/src/render.cc b/src/render.cc index 6f107fd..4059057 100644 --- a/src/render.cc +++ b/src/render.cc @@ -2,6 +2,7 @@ #include "SDL.h" #include "helpers.h" #include "texture.h" +#include "surface.h" #include "struct_wrappers.h" #include "window.h" @@ -22,7 +23,7 @@ sdl::RendererWrapper::~RendererWrapper() { } void sdl::RendererWrapper::Init(Handle exports) { - // Setup hardware renderer construction. + // Setup hardware renderer construction. Local tpl = FunctionTemplate::New(New); render_wrap_template_ = Persistent::New(tpl); @@ -113,8 +114,8 @@ Handle sdl::RendererWrapper::NewSoftware(const Arguments& args) { return ThrowException(Exception::TypeError(String::New("Invalid Arguments: Expected: new SoftwareRenderer(sdl.Surface)"))); } - SDL_Surface* surface = UnwrapSurface(Handle::Cast(args[0])); - SDL_Renderer* renderer = SDL_CreateSoftwareRenderer(surface); + SurfaceWrapper* wrap = ObjectWrap::Unwrap(Handle::Cast(args[0])); + SDL_Renderer* renderer = SDL_CreateSoftwareRenderer(wrap->surface_); if(NULL == renderer) { return ThrowSDLException(__func__); } diff --git a/src/struct_wrappers.cc b/src/struct_wrappers.cc index 8cf0b25..38cad77 100644 --- a/src/struct_wrappers.cc +++ b/src/struct_wrappers.cc @@ -15,7 +15,6 @@ namespace sdl { static Persistent rect_template_; static Persistent point_template_; static Persistent color_template_; - static Persistent surface_template_; static Persistent palette_template_; static Persistent pixelformat_template_; static Persistent rendererinfo_template_; @@ -30,13 +29,6 @@ namespace sdl { HandleScope scope; std::cout << "Created scope for struct wrapper initialization." << std::endl; - std::cout << "Putting together information for surface wrapper..." << std::endl; - const int numSurface = 6; - std::string surfaceSymbols[] = {"flags", "format", "w", "h", "pitch", "clip_rect"}; - AccessorGetter surfaceGetters[] = {GetSurfaceFlags, GetSurfaceFormat, GetSurfaceWidth, - GetSurfaceHeight, GetSurfacePitch, GetSurfaceRect}; - AccessorSetter surfaceSetters[] = {0, 0, 0, 0, 0, 0}; - std::cout << "Putting together information for rect wrapper..." << std::endl; const int numRect = 4; std::string rectSymbols[] = {"x", "y", "w", "h"}; @@ -82,21 +74,21 @@ namespace sdl { AccessorSetter rendererInfoSetters[] = {0, 0, 0, 0, 0, 0}; std::cout << "Putting together meta information for creating wrapping bindings..." << std::endl; - const int numberTemplates = 7; - Handle *templates[] = {&surface_template_, &rect_template_, &color_template_, + const int numberTemplates = 6; + Handle *templates[] = {&rect_template_, &color_template_, &palette_template_, &displaymode_template_, &pixelformat_template_, &rendererinfo_template_}; - int numberSymbols[] = {numSurface, numRect, numColor, numberPalette, numberDisplayMode, + int numberSymbols[] = {numRect, numColor, numberPalette, numberDisplayMode, numberPixelFormat, numberRendererInfo}; - std::string *allSymbols[] = {surfaceSymbols, rectSymbols, colorSymbols, paletteSymbols, + std::string *allSymbols[] = {rectSymbols, colorSymbols, paletteSymbols, displayModeSymbols, pixelFormatSymbols, rendererInfoSymbols}; - AccessorGetter *allGetters[] = {surfaceGetters, rectGetters, colorGetters, paletteGetters, + AccessorGetter *allGetters[] = {rectGetters, colorGetters, paletteGetters, displayModeGetters, pixelFormatGetters, rendererInfoGetters}; - AccessorSetter *allSetters[] = {surfaceSetters, rectSetters, colorSetters, paletteSetters, + AccessorSetter *allSetters[] = {rectSetters, colorSetters, paletteSetters, displayModeSetters, pixelFormatSetters, rendererInfoSetters}; - InvocationCallback allConstructors[] = {ConstructSurface, ConstructRect, ConstructColor, + InvocationCallback allConstructors[] = {ConstructRect, ConstructColor, ConstructPalette, 0, 0, 0}; - std::string constructorNames[] = {"Surface", "Rect", "Color", "Palette", + std::string constructorNames[] = {"Rect", "Color", "Palette", "DisplayMode", "PixelFormat", "RendererInfo"}; std::cout << std::endl << "About to begin loop to create wrapping bindings..." << std::endl; @@ -132,96 +124,6 @@ namespace sdl { // TODO: Joystick and Font. } - /////////////////////////////////////////////////////////////////////////////// - // SDL_Surface Wrapper/Unwrapper. - Handle ConstructSurface(const Arguments& args) { - HandleScope scope; - - if(args.Length() < 0) { - return ThrowException(Exception::TypeError(String::New("Invalid call. Expected: ConstructSurface(width, height)"))); - } - - int width = args[0]->Int32Value(); - int height = args[1]->Int32Value(); - int rmask, gmask, bmask, amask; -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - rmask = 0xff000000; - gmask = 0x00ff0000; - bmask = 0x0000ff00; - amask = 0x000000ff; -#else - rmask = 0x000000ff; - gmask = 0x0000ff00; - bmask = 0x00ff0000; - amask = 0xff000000; -#endif - SDL_Surface* surface = SDL_CreateRGBSurface(0, width, height, 32, - rmask, gmask, bmask, amask); - if(NULL == surface) { - return ThrowSDLException("ConstructSurface"); - } - - return scope.Close(WrapSurface(surface)); - } - - Handle WrapSurface(SDL_Surface* surface) { - // Handle scope for temporary handles. - HandleScope handle_scope; - - Handle templ = surface_template_; - - // Create an empty http request wrapper. - Handle result = templ->NewInstance(); - - // Wrap the raw C++ pointer in an External so it can be referenced - // from within JavaScript. - Handle request_ptr = External::New(surface); - - // Store the request pointer in the JavaScript wrapper. - result->SetInternalField(0, request_ptr); - - // Return the result through the current handle scope. Since each - // of these handles will go away when the handle scope is deleted - // we need to call Close to let one, the result, escape into the - // outer handle scope. - return handle_scope.Close(result); - } - - SDL_Surface* UnwrapSurface(Handle obj) { - Handle field = Handle::Cast(obj->GetInternalField(0)); - void* ptr = field->Value(); - return static_cast(ptr); - } - - // Property getters. - Handle GetSurfaceFlags(Local name, const AccessorInfo& info) { - SDL_Surface* surface = UnwrapSurface(info.Holder()); - return Number::New(surface->flags); - } - Handle GetSurfaceFormat(Local name, const AccessorInfo& info) { - HandleScope scope; - SDL_Surface* surface = UnwrapSurface(info.Holder()); - return scope.Close(WrapPixelFormat(surface->format)); - } - Handle GetSurfaceWidth(Local name, const AccessorInfo& info) { - SDL_Surface* surface = UnwrapSurface(info.Holder()); - return Number::New(surface->w); - } - Handle GetSurfaceHeight(Local name, const AccessorInfo& info) { - SDL_Surface* surface = UnwrapSurface(info.Holder()); - return Number::New(surface->h); - } - Handle GetSurfacePitch(Local name, const AccessorInfo& info) { - SDL_Surface* surface = UnwrapSurface(info.Holder()); - return Number::New(surface->pitch); - } - Handle GetSurfaceRect(Local name, const AccessorInfo& info) { - HandleScope scope; - SDL_Surface* surface = UnwrapSurface(info.Holder()); - return scope.Close(WrapRect(&surface->clip_rect)); - } - // Property setters. - /////////////////////////////////////////////////////////////////////////////// // SDL_Rect Wrapper/Unwrapper. Handle ConstructRect(const Arguments& args) { diff --git a/src/struct_wrappers.h b/src/struct_wrappers.h index f783b8a..c108a07 100644 --- a/src/struct_wrappers.h +++ b/src/struct_wrappers.h @@ -18,19 +18,6 @@ namespace sdl { // as defined in the SDL documentation. (excluding anything SDL uses internally // or is just plain unused) - /////////////////////////////////////////////////////////////////////////////// - // SDL_Surface Wrapper/Unwrapper. - v8::Handle ConstructSurface(const v8::Arguments& args); - v8::Handle WrapSurface(SDL_Surface* surface); - SDL_Surface* UnwrapSurface(v8::Handle obj); - // Property getters; - v8::Handle GetSurfaceFlags(v8::Local name, const v8::AccessorInfo& info); - v8::Handle GetSurfaceFormat(v8::Local name, const v8::AccessorInfo& info); - v8::Handle GetSurfaceWidth(v8::Local name, const v8::AccessorInfo& info); - v8::Handle GetSurfaceHeight(v8::Local name, const v8::AccessorInfo& info); - v8::Handle GetSurfacePitch(v8::Local name, const v8::AccessorInfo& info); - v8::Handle GetSurfaceRect(v8::Local name, const v8::AccessorInfo& info); - /////////////////////////////////////////////////////////////////////////////// // SDL_Rect Wrapper/Unwrapper. v8::Handle ConstructRect(const v8::Arguments& args); diff --git a/src/surface.cc b/src/surface.cc new file mode 100644 index 0000000..7e9a4f3 --- /dev/null +++ b/src/surface.cc @@ -0,0 +1,533 @@ +#include "surface.h" +#include "helpers.h" +#include "struct_wrappers.h" + +using namespace v8; +using namespace node; + +Persistent sdl::SurfaceWrapper::wrap_template_; + +sdl::SurfaceWrapper::SurfaceWrapper() { +} + +sdl::SurfaceWrapper::SurfaceWrapper(Handle toWrap) { + Wrap(toWrap); +} + +sdl::SurfaceWrapper::~SurfaceWrapper() { + if(NULL != surface_) { + SDL_FreeSurface(surface_); + } +} + +void sdl::SurfaceWrapper::Init(Handle exports) { + Local tpl = FunctionTemplate::New(New); + wrap_template_ = Persistent::New(tpl); + + wrap_template_->InstanceTemplate()->SetInternalFieldCount(1); + wrap_template_->SetClassName(String::NewSymbol("SurfaceWrapper")); + + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "loadBMP", LoadBMP); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "loadBMPRW", LoadBMPRW); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "saveBMP", SaveBMP); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "saveBMPRW", SaveBMPRW); + + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "blitScaled", BlitScaled); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "blitSurface", BlitSurface); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "lowerBlit", LowerBlit); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "lowerBlitScaled", LowerBlitScaled); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "convertSurface", ConvertSurface); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "convertSurfaceFormat", ConvertSurfaceFormat); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "getWidth", GetWidth); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "getHeight", GetHeight); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "getPitch", GetPitch); + + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "fillRect", FillRect); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "fillRects", FillRects); + + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "getClipRect", GetClipRect); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "getColorKey", GetColorKey); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "getAlphaMod", GetAlphaMod); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "getBlendMode", GetBlendMode); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "getColorMod", GetColorMod); + + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "setClipRect", SetClipRect); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "setColorKey", SetColorKey); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "setAlphaMod", SetAlphaMod); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "setBlendMode", SetBlendMode); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "setColorMod", SetColorMod); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "setPalette", SetPalette); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "setRLE", SetRLE); + + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "lock", Lock); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "unlock", Unlock); + NODE_SET_PROTOTYPE_METHOD(wrap_template_, "mustLock", MustLock); + + exports->Set(String::NewSymbol("Surface"), wrap_template_->GetFunction()); +} +Handle sdl::SurfaceWrapper::New(const Arguments& args) { + if(!args.IsConstructCall()) { + return ThrowException(Exception::TypeError( + String::New("Use the new operator to create instances of a Surface."))); + } + + HandleScope scope; + if(args.Length() < 2) { + return ThrowException(Exception::TypeError( + String::New("Invalid arguments: expected new sdl.Surface(Number, Number)"))); + } + + int flags = 0; + int width = args[0]->Int32Value(); + int height = args[1]->Int32Value(); + int depth = args[2]->IsUndefined() ? 32 : args[2]->Int32Value(); + int rmask, gmask, bmask, amask; +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + rmask = 0xff000000; + gmask = 0x00ff0000; + bmask = 0x0000ff00; + amask = 0x000000ff; +#else + rmask = 0x000000ff; + gmask = 0x0000ff00; + bmask = 0x00ff0000; + amask = 0xff000000; +#endif + SDL_Surface* surface = SDL_CreateRGBSurface(flags, width, height, depth, rmask, gmask, bmask, amask); + if(NULL == surface) { + return ThrowSDLException(__func__); + } + + SurfaceWrapper* obj = new SurfaceWrapper(); + obj->surface_ = surface; + obj->Wrap(args.This()); + return args.This(); +} + +Handle sdl::SurfaceWrapper::LoadBMP(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 1) { + return ThrowException(Exception::TypeError( + String::New("Invalid arguments: expected loadBMP(String)"))); + } + + String::Utf8Value file(args[0]); + SDL_Surface* surface = SDL_LoadBMP(*file); + if(NULL == surface) { + return ThrowSDLException(__func__); + } + + SurfaceWrapper* obj = new SurfaceWrapper(); + obj->surface_ = surface; + Handle ret = Object::New(); + obj->Wrap(ret); + + return scope.Close(ret); +} +Handle sdl::SurfaceWrapper::LoadBMPRW(const Arguments& args) { + // TODO: Implement LoadBMPRW. + return ThrowException(Exception::TypeError( + String::New("LoadBMPRW is not implemented yet."))); +} +Handle sdl::SurfaceWrapper::SaveBMP(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 2) { + return ThrowException(Exception::TypeError( + String::New("Invalid arguments: expected saveBMP(Surface, String)"))); + } + + SurfaceWrapper* wrap = ObjectWrap::Unwrap(Handle::Cast(args[0])); + String::Utf8Value file(args[1]); + int err = SDL_SaveBMP(wrap->surface_, *file); + if(err < 0) { + return ThrowSDLException(__func__); + } + + return Undefined(); +} +Handle sdl::SurfaceWrapper::SaveBMPRW(const Arguments& args) { + // TODO Implement SaveBMPRW. + return ThrowException(Exception::TypeError( + String::New("SaveBMPRW is not implemented yet."))); +} + +Handle sdl::SurfaceWrapper::BlitScaled(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 1) { + return ThrowException(Exception::TypeError( + String::New("Invalid arguments: expected blitScaled(Surface, Rect[, Rect])"))); + } + + SurfaceWrapper* obj = ObjectWrap::Unwrap(Handle::Cast(args.This())); + SurfaceWrapper* other = ObjectWrap::Unwrap(Handle::Cast(args[0])); + SDL_Rect* dst = args[1]->IsUndefined() ? NULL : UnwrapRect(Handle::Cast(args[1])); + SDL_Rect* src = args[2]->IsUndefined() ? NULL : UnwrapRect(Handle::Cast(args[2])); + int err = SDL_BlitScaled(obj->surface_, src, other->surface_, dst); + if(err < 0) { + return ThrowSDLException(__func__); + } + + return Undefined(); +} +Handle sdl::SurfaceWrapper::BlitSurface(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 2) { + return ThrowException(Exception::TypeError( + String::New("Invalid arguments: expected blitSurface(Surface, Rect[, Rect])"))); + } + + SurfaceWrapper* obj = ObjectWrap::Unwrap(Handle::Cast(args.This())); + SurfaceWrapper* other = ObjectWrap::Unwrap(Handle::Cast(args[0])); + SDL_Rect* dst = UnwrapRect(Handle::Cast(args[1])); + SDL_Rect* src = args[2]->IsUndefined() ? NULL : UnwrapRect(Handle::Cast(args[2])); + int err = SDL_BlitSurface(obj->surface_, src, other->surface_, dst); + if(err < 0) { + return ThrowSDLException(__func__); + } + + return Undefined(); +} +Handle sdl::SurfaceWrapper::LowerBlit(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 2) { + return ThrowException(Exception::TypeError( + String::New("Invalid arguments: expected lowerBlit(Surface, Rect[, Rect])"))); + } + + SurfaceWrapper* obj = ObjectWrap::Unwrap(Handle::Cast(args.This())); + SurfaceWrapper* other = ObjectWrap::Unwrap(Handle::Cast(args[0])); + SDL_Rect* dst = UnwrapRect(Handle::Cast(args[1])); + SDL_Rect* src = args[2]->IsUndefined() ? NULL : UnwrapRect(Handle::Cast(args[2])); + int err = SDL_LowerBlit(obj->surface_, src, other->surface_, dst); + if(err < 0) { + return ThrowSDLException(__func__); + } + + return Undefined(); +} +Handle sdl::SurfaceWrapper::LowerBlitScaled(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 1) { + return ThrowException(Exception::TypeError( + String::New("Invalid arguments: expected lowerBlitScaled(Surface, Rect[, Rect])"))); + } + + SurfaceWrapper* obj = ObjectWrap::Unwrap(Handle::Cast(args.This())); + SurfaceWrapper* other = ObjectWrap::Unwrap(Handle::Cast(args[0])); + SDL_Rect* dst = args[1]->IsUndefined() ? NULL : UnwrapRect(Handle::Cast(args[1])); + SDL_Rect* src = args[2]->IsUndefined() ? NULL : UnwrapRect(Handle::Cast(args[2])); + int err = SDL_LowerBlitScaled(obj->surface_, src, other->surface_, dst); + if(err < 0) { + return ThrowSDLException(__func__); + } + + return Undefined(); +} +Handle sdl::SurfaceWrapper::ConvertSurface(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 1) { + return ThrowException(Exception::TypeError( + String::New("Invalid arguments: expected convertSurface(PixelFormat)"))); + } + + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + SDL_PixelFormat* fmt = UnwrapPixelFormat(Handle::Cast(args[0])); + SDL_Surface* ret = SDL_ConvertSurface(self->surface_, fmt, 0); + if(NULL == ret) { + return ThrowSDLException(__func__); + } + + SurfaceWrapper* obj = new SurfaceWrapper(); + obj->surface_ = ret; + Handle objRet = Object::New(); + obj->Wrap(objRet); + return scope.Close(objRet); +} +Handle sdl::SurfaceWrapper::ConvertSurfaceFormat(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 1) { + return ThrowException(Exception::TypeError( + String::New("Invalid arguments: expected convertSurfaceFormat(Number)"))); + } + + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + int fmt = args[0]->Int32Value(); + SDL_Surface* ret = SDL_ConvertSurfaceFormat(self->surface_, fmt, 0); + if(NULL == ret) { + return ThrowSDLException(__func__); + } + + SurfaceWrapper* obj = new SurfaceWrapper(); + obj->surface_ = ret; + Handle objRet = Object::New(); + obj->Wrap(objRet); + return scope.Close(objRet); +} + +Handle sdl::SurfaceWrapper::FillRect(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 1) { + return ThrowException(Exception::TypeError( + String::New("Invalid arguments: expected fillRect(Number[, Rect])"))); + } + + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + int color = args[0]->Int32Value(); + SDL_Rect* rect = args[1]->IsUndefined() ? NULL : UnwrapRect(Handle::Cast(args[1])); + int err = SDL_FillRect(self->surface_, rect, color); + if(err < 0) { + return ThrowSDLException(__func__); + } + + return Undefined(); +} +Handle sdl::SurfaceWrapper::FillRects(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 2) { + return ThrowException(Exception::TypeError( + String::New("Invalid arguments: expected fillRect(Number, Array)"))); + } + + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + int color = args[0]->Int32Value(); + Handle arr = Handle::Cast(args[1]); + int numRects = arr->Length(); + SDL_Rect* rects = new SDL_Rect[numRects]; + for(int i = 0; i < numRects; i++) { + SDL_Rect* rect = UnwrapRect(Handle::Cast(arr->Get(i))); + rects[i] = *rect; + } + int err = SDL_FillRects(self->surface_, rects, numRects, color); + delete rects; + if(err < 0) { + return ThrowSDLException(__func__); + } + + return Undefined(); +} + +Handle sdl::SurfaceWrapper::GetClipRect(const Arguments& args) { + HandleScope scope; + + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + SDL_Rect* rect = new SDL_Rect; + SDL_GetClipRect(self->surface_, rect); + + return scope.Close(WrapRect(rect)); +} +Handle sdl::SurfaceWrapper::GetColorKey(const Arguments& args) { + HandleScope scope; + + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + uint32_t colorKey; + int err = SDL_GetColorKey(self->surface_, &colorKey); + if(err < 0) { + return ThrowSDLException(__func__); + } + + return scope.Close(Number::New(colorKey)); +} +Handle sdl::SurfaceWrapper::GetAlphaMod(const Arguments& args) { + HandleScope scope; + + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + uint8_t alphaMod; + int err = SDL_GetSurfaceAlphaMod(self->surface_, &alphaMod); + if(err < 0) { + return ThrowSDLException(__func__); + } + + return scope.Close(Number::New(alphaMod)); +} +Handle sdl::SurfaceWrapper::GetBlendMode(const Arguments& args) { + HandleScope scope; + + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + SDL_BlendMode mode; + int err = SDL_GetSurfaceBlendMode(self->surface_, &mode); + if(err < 0) { + return ThrowSDLException(__func__); + } + + return scope.Close(Number::New(mode)); +} +Handle sdl::SurfaceWrapper::GetColorMod(const Arguments& args) { + HandleScope scope; + + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + uint8_t r, g, b; + int err = SDL_GetSurfaceColorMod(self->surface_, &r, &g, &b); + if(err < 0) { + return ThrowSDLException(__func__); + } + + Handle ret = Array::New(3); + ret->Set(0, Number::New(r)); + ret->Set(1, Number::New(g)); + ret->Set(2, Number::New(b)); + return scope.Close(ret); +} +Handle sdl::SurfaceWrapper::GetWidth(const Arguments& args) { + HandleScope scope; + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + return scope.Close(Number::New(self->surface_->w)); +} +Handle sdl::SurfaceWrapper::GetHeight(const Arguments& args) { + HandleScope scope; + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + return scope.Close(Number::New(self->surface_->h)); +} +Handle sdl::SurfaceWrapper::GetPitch(const Arguments& args) { + HandleScope scope; + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + return scope.Close(Number::New(self->surface_->pitch)); +} + +Handle sdl::SurfaceWrapper::SetClipRect(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 1) { + return ThrowException(Exception::TypeError( + String::New("Invalid arguments: expected setClipRect(Rect)"))); + } + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + SDL_Rect* clip = UnwrapRect(Handle::Cast(args[0])); + SDL_bool ret = SDL_SetClipRect(self->surface_, clip); + + return scope.Close(Boolean::New(ret)); +} +Handle sdl::SurfaceWrapper::SetColorKey(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 2) { + return ThrowException(Exception::TypeError( + String::New("Invalid arguments: expected setColorKey(Boolean, Number)"))); + } + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + int flag = args[0]->BooleanValue() ? 1 : 0; + uint32_t key = args[1]->Int32Value(); + int err = SDL_SetColorKey(self->surface_, flag, key); + if(err < 0) { + return ThrowSDLException(__func__); + } + + return Undefined(); +} +Handle sdl::SurfaceWrapper::SetAlphaMod(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 1) { + return ThrowException(Exception::TypeError( + String::New("Invalid arguments: expected setAlphaMod(Number)"))); + } + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + uint8_t alpha = static_cast(args[0]->Int32Value()); + int err = SDL_SetSurfaceAlphaMod(self->surface_, alpha); + if(err < 0) { + return ThrowSDLException(__func__); + } + + return Undefined(); +} +Handle sdl::SurfaceWrapper::SetBlendMode(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 1) { + return ThrowException(Exception::TypeError( + String::New("Invalid arguments: expected setBlendMode(Number)"))); + } + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + SDL_BlendMode mode = static_cast(args[0]->Int32Value()); + int err = SDL_SetSurfaceBlendMode(self->surface_, mode); + if(err < 0) { + return ThrowSDLException(__func__); + } + + return Undefined(); +} +Handle sdl::SurfaceWrapper::SetColorMod(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 3) { + return ThrowException(Exception::TypeError( + String::New("Invalid arguments: expected setColorMod(Number, Number, Number)"))); + } + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + uint8_t r = static_cast(args[0]->Int32Value()); + uint8_t g = static_cast(args[1]->Int32Value()); + uint8_t b = static_cast(args[2]->Int32Value()); + int err = SDL_SetSurfaceColorMod(self->surface_, r, g, b); + if(err < 0) { + return ThrowSDLException(__func__); + } + + return Undefined(); +} +Handle sdl::SurfaceWrapper::SetPalette(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 1) { + return ThrowException(Exception::TypeError( + String::New("Invalid arguments: expected setPalette(Palette)"))); + } + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + SDL_Palette* palette = UnwrapPalette(Handle::Cast(args[0])); + int err = SDL_SetSurfacePalette(self->surface_, palette); + if(err < 0) { + return ThrowSDLException(__func__); + } + + return Undefined(); +} +Handle sdl::SurfaceWrapper::SetRLE(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 1) { + return ThrowException(Exception::TypeError( + String::New("Invalid arguments: expected setRLE(Number)"))); + } + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + int flags = args[0]->Int32Value(); + int err = SDL_SetSurfaceRLE(self->surface_, flags); + if(err < 0) { + return ThrowSDLException(__func__); + } + + return Undefined(); +} + +Handle sdl::SurfaceWrapper::Lock(const Arguments& args) { + HandleScope scope; + + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + int err = SDL_LockSurface(self->surface_); + if(err < 0) { + return ThrowSDLException(__func__); + } + + return Undefined(); +} +Handle sdl::SurfaceWrapper::Unlock(const Arguments& args) { + HandleScope scope; + + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + SDL_UnlockSurface(self->surface_); + + return Undefined(); +} +Handle sdl::SurfaceWrapper::MustLock(const Arguments& args) { + HandleScope scope; + + SurfaceWrapper* self = ObjectWrap::Unwrap(Handle::Cast(args.This())); + return scope.Close(Boolean::New(SDL_MUSTLOCK(self->surface_))); +} diff --git a/src/surface.h b/src/surface.h new file mode 100644 index 0000000..6c183ae --- /dev/null +++ b/src/surface.h @@ -0,0 +1,62 @@ +#ifndef NODESDL_SURFACE_H +#define NODESDL_SURFACE_H + +#include +#include + +#include "SDL.h" + + +namespace sdl { + class SurfaceWrapper : public node::ObjectWrap { + public: + static v8::Persistent wrap_template_; + + SurfaceWrapper(); + SurfaceWrapper(v8::Handle toWrap); + ~SurfaceWrapper(); + + static void Init(v8::Handle exports); + static v8::Handle New(const v8::Arguments& args); + + static v8::Handle LoadBMP(const v8::Arguments& args); + static v8::Handle LoadBMPRW(const v8::Arguments& args); + static v8::Handle SaveBMP(const v8::Arguments& args); + static v8::Handle SaveBMPRW(const v8::Arguments& args); + + static v8::Handle BlitScaled(const v8::Arguments& args); + static v8::Handle BlitSurface(const v8::Arguments& args); + static v8::Handle LowerBlit(const v8::Arguments& args); + static v8::Handle LowerBlitScaled(const v8::Arguments& args); + static v8::Handle ConvertSurface(const v8::Arguments& args); + static v8::Handle ConvertSurfaceFormat(const v8::Arguments& args); + + static v8::Handle FillRect(const v8::Arguments& args); + static v8::Handle FillRects(const v8::Arguments& args); + + static v8::Handle GetClipRect(const v8::Arguments& args); + static v8::Handle GetColorKey(const v8::Arguments& args); + static v8::Handle GetAlphaMod(const v8::Arguments& args); + static v8::Handle GetBlendMode(const v8::Arguments& args); + static v8::Handle GetColorMod(const v8::Arguments& args); + static v8::Handle GetWidth(const v8::Arguments& args); + static v8::Handle GetHeight(const v8::Arguments& args); + static v8::Handle GetPitch(const v8::Arguments& args); + + static v8::Handle SetClipRect(const v8::Arguments& args); + static v8::Handle SetColorKey(const v8::Arguments& args); + static v8::Handle SetAlphaMod(const v8::Arguments& args); + static v8::Handle SetBlendMode(const v8::Arguments& args); + static v8::Handle SetColorMod(const v8::Arguments& args); + static v8::Handle SetPalette(const v8::Arguments& args); + static v8::Handle SetRLE(const v8::Arguments& args); + + static v8::Handle Lock(const v8::Arguments& args); + static v8::Handle Unlock(const v8::Arguments& args); + static v8::Handle MustLock(const v8::Arguments& args); + + SDL_Surface* surface_; + }; +} + +#endif \ No newline at end of file diff --git a/src/texture.cc b/src/texture.cc index 75916b2..6be08b9 100644 --- a/src/texture.cc +++ b/src/texture.cc @@ -1,4 +1,7 @@ +#include + #include "texture.h" +#include "surface.h" #include "helpers.h" #include "struct_wrappers.h" #include "render.h" @@ -36,6 +39,7 @@ void sdl::TextureWrapper::Init(Handle exports) { } Handle sdl::TextureWrapper::New(const Arguments& args) { + // std::cout << "Texture::New - Checking for constructor call." << std::endl; if(!args.IsConstructCall()) { return ThrowException(Exception::TypeError( String::New("Use the new operator to create instances of a Texture."))); @@ -44,24 +48,41 @@ Handle sdl::TextureWrapper::New(const Arguments& args) { HandleScope scope; SDL_Texture* tex; + // std::cout << "Texture::New - Unwrapping RendererWrapper from first argument." << std::endl; RendererWrapper* r = ObjectWrap::Unwrap(Handle::Cast(args[0])); + // std::cout << "Texture::New - Done. Checking arguments length." << std::endl; if(args.Length() > 2) { + // std::cout << "Texture::New - Found more than 2 arguments. Pulling format, access, w, h." << std::endl; int format = args[1]->IsUndefined() ? SDL_PIXELFORMAT_UNKNOWN : args[1]->Int32Value(); int access = args[2]->IsUndefined() ? SDL_TEXTUREACCESS_STREAMING : args[2]->Int32Value(); int w = args[3]->IsUndefined() ? 1 : args[3]->Int32Value(); int h = args[4]->IsUndefined() ? 1 : args[4]->Int32Value(); + // std::cout << "Texture::New - Done. Calling SDL_CreateTexture." << std::endl; tex = SDL_CreateTexture(r->renderer_, format, access, w, h); + // std::cout << "Texture::New - Done." << std::endl; + } + else if(args.Length() == 2) { + // std::cout << "Texture::New - Found 2 arguments. Pulling SurfaceWrapper from second arg." << std::endl; + SurfaceWrapper* wrap = ObjectWrap::Unwrap(Handle::Cast(args[1])); + // std::cout << "Texture::New - Done. Calling SDL_CreateTextureFromSurface." << std::endl; + tex = SDL_CreateTextureFromSurface(r->renderer_, wrap->surface_); + // std::cout << "Texture::New - Done." << std::endl; } else { - SDL_Surface* surface = UnwrapSurface(Handle::Cast(args[1])); - tex = SDL_CreateTextureFromSurface(r->renderer_, surface); + return ThrowException(Exception::TypeError( + String::New("Invalid arguments: expecting new sdl.Texture(Renderer, Surface) or new sdl.Texture(Renderer, Number, Number, Number, Number)"))); } if(NULL == tex) { return ThrowSDLException(__func__); } + + // std::cout << "Texture::New - Creating new TextureWrapper." << std::endl; TextureWrapper* obj = new TextureWrapper(); + // std::cout << "Texture::New - Setting texture_ to created tex." << std::endl; obj->texture_ = tex; + // std::cout << "Texture::New - Wrapping args.This()." << std::endl; obj->Wrap(args.This()); + // std::cout << "Texture::New - Returning args.This()." << std::endl; return args.This(); } diff --git a/src/window.cc b/src/window.cc index 00bb623..12ed8d8 100644 --- a/src/window.cc +++ b/src/window.cc @@ -3,6 +3,7 @@ #include "window.h" #include "helpers.h" #include "struct_wrappers.h" +#include "surface.h" using namespace v8; @@ -252,7 +253,11 @@ Handle sdl::WindowWrapper::GetSurface(const Arguments& args) { if(NULL == surf) { return ThrowSDLException("Window->GetSurface"); } - return scope.Close(WrapSurface(surf)); + + Handle ret = Object::New(); + SurfaceWrapper* wrap = new SurfaceWrapper(ret); + wrap->surface_ = surf; + return scope.Close(ret); } Handle sdl::WindowWrapper::GetTitle(const Arguments& args) { @@ -414,8 +419,8 @@ Handle sdl::WindowWrapper::SetIcon(const Arguments& args) { if(args.Length() < 1) { return ThrowException(Exception::TypeError(String::New("Invalid arguments: Expected SetIcon(Surface)"))); } - SDL_Surface* surface = UnwrapSurface(Handle::Cast(args[0])); - SDL_SetWindowIcon(obj->window_, surface); + SurfaceWrapper* wrap = ObjectWrap::Unwrap(Handle::Cast(args[0])); + SDL_SetWindowIcon(obj->window_, wrap->surface_); return Undefined(); }