Skip to content
Browse files

Wrap SDL_ttf. Start fixing wrappers

  • Loading branch information...
1 parent d64febd commit bad4bae1573a54cf1087e84e6488f9e5748a1f3c Vincent Kuyatt committed Jan 25, 2014
Showing with 767 additions and 213 deletions.
  1. +6 −7 binding.gyp
  2. +1 −96 sdl.js
  3. +651 −0 src/font.cc
  4. +56 −0 src/font.h
  5. +15 −74 src/node_sdl.cc
  6. +0 −6 src/node_sdl.h
  7. +38 −30 src/surface.cc
View
13 binding.gyp
@@ -15,17 +15,16 @@
'src/sdl_gl.cc',
'src/event.cc',
'src/key.cc',
- 'src/mouse.cc'
+ 'src/mouse.cc',
+ "src/font.cc"
],
'libraries': [
- '/Library/Frameworks/SDL2.framework/SDL2',
- '/Library/Frameworks/SDL2_ttf.framework/SDL2_ttf',
- '/Library/Frameworks/SDL2_image.framework/SDL2_image',
+ '/usr/local/lib/libSDL2.dylib',
+ '/usr/local/lib/libSDL2_ttf.dylib',
+ '/usr/local/lib/libSDL2_image.dylib',
],
'include_dirs': [
- "/Library/Frameworks/SDL2.framework/Headers",
- "/Library/Frameworks/SDL2_ttf.framework/Headers",
- "/Library/Frameworks/SDL2_image.framework/Headers",
+ "/usr/local/include/SDL2"
],
'cflags': [
"-D_THREAD_SAFE"
View
97 sdl.js
@@ -1,96 +1 @@
-function isFunction(functionToCheck) {
- var getType = {};
- return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
-}
-
-var SDL = module.exports = require('./build/Release/node_sdl.node');
-SDL.Color.White = new SDL.Color(255, 255, 255);
-SDL.Color.Red = new SDL.Color(255, 0, 0);
-SDL.Color.Green = new SDL.Color(0, 255, 0);
-SDL.Color.Blue = new SDL.Color(0, 0, 255);
-
-// 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) {
-// console.log("INITKEY: " + key + ", INITVAL: " + SDL.INIT[key]);
-// }
-
-console.log("About to init SDL. (no args, default to EVERYTHING)");
-SDL.init("FOO");
-
-console.log(SDL.Renderer.SOFTWARE);
-console.log("COLOR: " + new SDL.Color(100, 100, 100, 255).toString());
-console.log(SDL.Color.White.toString());
-
-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);
-console.log("Filling white rect to window.");
-surface.fillRect(SDL.Color.White.getColor(surface.getPixelFormat()));
-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])) {
-// try {
-// console.log("WINDOWCALLRET: " + window[key]());
-// }
-// catch(err) {
-// console.log("WINDOWCALLERR: " + err);
-// }
-// }
-// }
-
-// var mode = window.getDisplayMode();
-// for(key in mode) {
-// console.log("MODEKEY: " + key + ", " + "MODEVALUE: " + mode[key]);
-// }
-
-var p = new SDL.Point(1, 1);
-// console.log(p.x + ", " + p.y);
-console.log(p.toString());
-
-var timeout = 1000;
-console.log("Setting timeout to: " + timeout + ", to SDL quit");
-setTimeout(function() {SDL.quit()}, timeout);
-
-// Easy event emitter based event loop. Started automatically when the first
-// listener is added.
-// var events;
-// Object.defineProperty(SDL, 'events', {
-// get: function () {
-// if (events) return events;
-// events = new (require('events').EventEmitter);
-// var now = Date.now();
-// setInterval(function () {
-// var after = Date.now();
-// var delta = after - now;
-// now = after;
-// var data;
-// while (data = SDL.pollEvent()) {
-// events.emit('event', data);
-// events.emit(data.type, data);
-// }
-// events.emit('tick', delta);
-// }, 16);
-// return events;
-// }
-// });
-
+var sdl = module.exports = require('./build/Release/node_sdl.node');
View
651 src/font.cc
@@ -0,0 +1,651 @@
+#include "font.h"
+#include "helpers.h"
+#include "container.h"
+#include "surface.h"
+
+#include <iostream>
+
+using namespace v8;
+using namespace node;
+
+
+Persistent<FunctionTemplate> sdl::TTF::FontWrapper::wrap_template_;
+
+sdl::TTF::FontWrapper::FontWrapper() {
+}
+sdl::TTF::FontWrapper::~FontWrapper() {
+ if(NULL != font_) {
+ TTF_CloseFont(font_);
+ }
+}
+
+void sdl::TTF::FontWrapper::Init(Handle<Object> exports) {
+ Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
+ wrap_template_ = Persistent<FunctionTemplate>::New(tpl);
+
+ wrap_template_->InstanceTemplate()->SetInternalFieldCount(1);
+ wrap_template_->SetClassName(String::NewSymbol("FontWrapper"));
+
+ NODE_SET_PROTOTYPE_METHOD(wrap_template_, "renderTextSolid", RenderTextSolid);
+ NODE_SET_PROTOTYPE_METHOD(wrap_template_, "renderUtf8Solid", RenderUTF8Solid);
+ NODE_SET_PROTOTYPE_METHOD(wrap_template_, "renderUnicodeSolid", RenderUnicodeSolid);
+ NODE_SET_PROTOTYPE_METHOD(wrap_template_, "renderGlyphSolid", RenderGlyphSolid);
+
+ NODE_SET_PROTOTYPE_METHOD(wrap_template_, "renderTextShaded", RenderTextShaded);
+ NODE_SET_PROTOTYPE_METHOD(wrap_template_, "renderUtf8Shaded", RenderUTF8Shaded);
+ NODE_SET_PROTOTYPE_METHOD(wrap_template_, "renderUnicodeShaded", RenderUnicodeShaded);
+ NODE_SET_PROTOTYPE_METHOD(wrap_template_, "renderGlyphShaded", RenderGlyphShaded);
+
+ NODE_SET_PROTOTYPE_METHOD(wrap_template_, "renderTextBlended", RenderTextBlended);
+ NODE_SET_PROTOTYPE_METHOD(wrap_template_, "renderUtf8Blended", RenderUTF8Blended);
+ NODE_SET_PROTOTYPE_METHOD(wrap_template_, "renderUnicodeBlended", RenderUnicodeBlended);
+ NODE_SET_PROTOTYPE_METHOD(wrap_template_, "renderGlyphBlended", RenderGlyphBlended);
+
+ exports->Set(String::New("Font"), wrap_template_->GetFunction());
+}
+Handle<Value> sdl::TTF::FontWrapper::New(const Arguments& args) {
+ if(!args.IsConstructCall()) {
+ return ThrowException(Exception::TypeError(
+ String::New("A Font must be created with the new operator.")));
+ }
+
+ HandleScope scope;
+
+ if(args[0]->IsExternal()) {
+ FontWrapper* obj = new FontWrapper();
+ obj->font_ = static_cast<TTF_Font*>(Handle<External>::Cast(args[0])->Value());
+ obj->Wrap(args.This());
+ return args.This();
+ }
+ else {
+ if(!args[0]->IsString()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: First argument to new sdl.Font must be a String.")));
+ }
+ if(!args[1]->IsNumber()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: Second argument to new sdl.Font must be a Number.")));
+ }
+
+ String::Utf8Value file(args[0]);
+ int ptsize = args[1]->Int32Value();
+ TTF_Font* font = TTF_OpenFont(*file, ptsize);
+ if(NULL == font) {
+ return ThrowSDLException(__func__);
+ }
+
+ FontWrapper* obj = new FontWrapper();
+ obj->font_ = font;
+ obj->Wrap(args.This());
+ return args.This();
+ }
+}
+
+Handle<Value> sdl::TTF::FontWrapper::RenderTextSolid(const Arguments& args) {
+ HandleScope scope;
+ Context::Scope context_scope(Context::GetCurrent());
+
+ std::cout << "Unwrapping this into FontWrapper." << std::endl;
+ FontWrapper* font = ObjectWrap::Unwrap<FontWrapper>(args.This());
+ std::cout << "Checking arguments." << std::endl;
+ if(NULL == font) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap 'this' for some reason. (did you not use a Font object?)")));
+ }
+ if(!args[0]->IsString()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: First argument to renderTextSolid must be a String.")));
+ }
+ if(!args[1]->IsObject()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: Second argument to renderTextSolid must be an sdl.Color.")));
+ }
+
+ std::cout << "Creating Utf8Value from arg 0." << std::endl;
+ String::Utf8Value text(args[0]);
+ std::cout << "Unwrapping arg 1 to ColorWrapper." << std::endl;
+ ColorWrapper* color = ObjectWrap::Unwrap<ColorWrapper>(Handle<Object>::Cast(args[1]));
+ if(NULL == color) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap second argument to renderTextSolid for some reason. (is it not an sdl.Color?)")));
+ }
+
+ std::cout << "Rendering text." << std::endl;
+ SDL_Surface* surface = TTF_RenderText_Solid(font->font_, *text, *color->color_);
+ std::cout << "Done rendering text." << std::endl;
+ if(NULL == surface) {
+ return ThrowSDLException(__func__);
+ }
+
+ std::cout << "Creating new Javascript object to carry the surface wrapper." << std::endl;
+ Handle<Value> arg = External::New(surface);
+ Handle<Value> argv[] = {arg};
+ Handle<Object> ret = SurfaceWrapper::wrap_template_->GetFunction()->NewInstance(1, argv);
+ // std::cout << "Internal field count: " << ret->InternalFieldCount() << std::endl;
+ // std::cout << "Creating a surface wrapper, which will wrap the previous object." << std::endl;
+ // SurfaceWrapper* wrap = new SurfaceWrapper(ret);
+ // std::cout << "Setting the wrapper internal surface to the rendered text." << std::endl;
+ // wrap->surface_ = surface;
+ // std::cout << "Returning the Javascript surface." << std::endl;
+ return scope.Close(ret);
+}
+Handle<Value> sdl::TTF::FontWrapper::RenderUTF8Solid(const Arguments& args) {
+ HandleScope scope;
+
+ FontWrapper* font = ObjectWrap::Unwrap<FontWrapper>(args.This());
+ if(NULL == font) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap 'this' for some reason. (did you not use a Font object?)")));
+ }
+ if(!args[0]->IsString()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: First argument to renderUtf8Solid must be a String.")));
+ }
+ if(!args[1]->IsObject()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: Second argument to renderUtf8Solid must be an sdl.Color.")));
+ }
+
+ String::Utf8Value text(args[0]);
+ ColorWrapper* color = ObjectWrap::Unwrap<ColorWrapper>(Handle<Object>::Cast(args[1]));
+ if(NULL == color) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap second argument to renderUtf8Solid for some reason. (is it not an sdl.Color?)")));
+ }
+
+ SDL_Surface* surface = TTF_RenderUTF8_Solid(font->font_, *text, *color->color_);
+ if(NULL == surface) {
+ return ThrowSDLException(__func__);
+ }
+
+ Handle<Object> ret = Object::New();
+ SurfaceWrapper* wrap = new SurfaceWrapper(ret);
+ wrap->surface_ = surface;
+ return scope.Close(ret);
+}
+Handle<Value> sdl::TTF::FontWrapper::RenderUnicodeSolid(const Arguments& args) {
+ HandleScope scope;
+
+ FontWrapper* font = ObjectWrap::Unwrap<FontWrapper>(args.This());
+ if(NULL == font) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap 'this' for some reason. (did you not use a Font object?)")));
+ }
+ if(!args[1]->IsObject()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: Second argument to renderUnicodeSolid must be an sdl.Color.")));
+ }
+
+ String::Value text(args[0]);
+ ColorWrapper* color = ObjectWrap::Unwrap<ColorWrapper>(Handle<Object>::Cast(args[1]));
+ if(NULL == color) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap second argument to renderUnicodeSolid for some reason. (is it not an sdl.Color?)")));
+ }
+
+ SDL_Surface* surface = TTF_RenderUNICODE_Solid(font->font_, *text, *color->color_);
+ if(NULL == surface) {
+ return ThrowSDLException(__func__);
+ }
+
+ Handle<Object> ret = Object::New();
+ SurfaceWrapper* wrap = new SurfaceWrapper(ret);
+ wrap->surface_ = surface;
+ return scope.Close(ret);
+}
+Handle<Value> sdl::TTF::FontWrapper::RenderGlyphSolid(const Arguments& args) {
+ HandleScope scope;
+
+ FontWrapper* font = ObjectWrap::Unwrap<FontWrapper>(args.This());
+ if(NULL == font) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap 'this' for some reason. (did you not use a Font object?)")));
+ }
+ if(!args[1]->IsObject()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: Second argument to renderGlyphSolid must be an sdl.Color.")));
+ }
+
+ String::Value text(args[0]);
+ if(0 == text.length()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: First argument to renderGlyphSolid must be able to convert to a string of at least length 1.")));
+ }
+ ColorWrapper* color = ObjectWrap::Unwrap<ColorWrapper>(Handle<Object>::Cast(args[1]));
+ if(NULL == color) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap second argument to renderGlyphSolid for some reason. (is it not an sdl.Color?)")));
+ }
+
+ SDL_Surface* surface = TTF_RenderGlyph_Solid(font->font_, (*text)[0], *color->color_);
+ if(NULL == surface) {
+ return ThrowSDLException(__func__);
+ }
+
+ Handle<Object> ret = Object::New();
+ SurfaceWrapper* wrap = new SurfaceWrapper(ret);
+ wrap->surface_ = surface;
+ return scope.Close(ret);
+}
+
+Handle<Value> sdl::TTF::FontWrapper::RenderTextShaded(const Arguments& args) {
+ HandleScope scope;
+
+ FontWrapper* font = ObjectWrap::Unwrap<FontWrapper>(args.This());
+ if(NULL == font) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap 'this' for some reason. (did you not use a Font object?)")));
+ }
+ if(!args[0]->IsString()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: First argument to renderTextShaded must be a String.")));
+ }
+ if(!args[1]->IsObject()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: Second argument to renderTextShaded must be an sdl.Color.")));
+ }
+ if(!args[2]->IsObject()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: Third argument to renderTextShaded must be an sdl.Color.")));
+ }
+
+ String::Utf8Value text(args[0]);
+ ColorWrapper* fg = ObjectWrap::Unwrap<ColorWrapper>(Handle<Object>::Cast(args[1]));
+ if(NULL == fg) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap second argument to renderTextShaded for some reason. (is it not an sdl.Color?)")));
+ }
+ ColorWrapper* bg = ObjectWrap::Unwrap<ColorWrapper>(Handle<Object>::Cast(args[2]));
+ if(NULL == bg) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap third argument to renderTextShaded for some reason. (is it not an sdl.Color?)")));
+ }
+
+ SDL_Surface* surface = TTF_RenderText_Shaded(font->font_, *text, *fg->color_, *bg->color_);
+ if(NULL == surface) {
+ return ThrowSDLException(__func__);
+ }
+
+ Handle<Object> ret = Object::New();
+ SurfaceWrapper* wrap = new SurfaceWrapper(ret);
+ wrap->surface_ = surface;
+ return scope.Close(ret);
+}
+Handle<Value> sdl::TTF::FontWrapper::RenderUTF8Shaded(const Arguments& args) {
+ HandleScope scope;
+
+ FontWrapper* font = ObjectWrap::Unwrap<FontWrapper>(args.This());
+ if(NULL == font) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap 'this' for some reason. (did you not use a Font object?)")));
+ }
+ if(!args[0]->IsString()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: First argument to renderUtf8Shaded must be a String.")));
+ }
+ if(!args[1]->IsObject()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: Second argument to renderUtf8Shaded must be an sdl.Color.")));
+ }
+ if(!args[2]->IsObject()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: Third argument to renderUtf8Shaded must be an sdl.Color.")));
+ }
+
+ String::Utf8Value text(args[0]);
+ ColorWrapper* fg = ObjectWrap::Unwrap<ColorWrapper>(Handle<Object>::Cast(args[1]));
+ if(NULL == fg) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap second argument to renderUtf8Shaded for some reason. (is it not an sdl.Color?)")));
+ }
+ ColorWrapper* bg = ObjectWrap::Unwrap<ColorWrapper>(Handle<Object>::Cast(args[2]));
+ if(NULL == bg) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap third argument to renderUtf8Shaded for some reason. (is it not an sdl.Color?)")));
+ }
+
+ SDL_Surface* surface = TTF_RenderUTF8_Shaded(font->font_, *text, *fg->color_, *bg->color_);
+ if(NULL == surface) {
+ return ThrowSDLException(__func__);
+ }
+
+ Handle<Object> ret = Object::New();
+ SurfaceWrapper* wrap = new SurfaceWrapper(ret);
+ wrap->surface_ = surface;
+ return scope.Close(ret);
+}
+Handle<Value> sdl::TTF::FontWrapper::RenderUnicodeShaded(const Arguments& args) {
+ HandleScope scope;
+
+ FontWrapper* font = ObjectWrap::Unwrap<FontWrapper>(args.This());
+ if(NULL == font) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap 'this' for some reason. (did you not use a Font object?)")));
+ }
+ if(!args[0]->IsString()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: First argument to renderUnicodeShaded must be a String.")));
+ }
+ if(!args[1]->IsObject()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: Second argument to renderUnicodeShaded must be an sdl.Color.")));
+ }
+ if(!args[2]->IsObject()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: Third argument to renderUnicodeShaded must be an sdl.Color.")));
+ }
+
+ String::Value text(args[0]);
+ ColorWrapper* fg = ObjectWrap::Unwrap<ColorWrapper>(Handle<Object>::Cast(args[1]));
+ if(NULL == fg) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap second argument to renderUnicodeShaded for some reason. (is it not an sdl.Color?)")));
+ }
+ ColorWrapper* bg = ObjectWrap::Unwrap<ColorWrapper>(Handle<Object>::Cast(args[2]));
+ if(NULL == bg) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap third argument to renderUnicodeShaded for some reason. (is it not an sdl.Color?)")));
+ }
+
+ SDL_Surface* surface = TTF_RenderUNICODE_Shaded(font->font_, *text, *fg->color_, *bg->color_);
+ if(NULL == surface) {
+ return ThrowSDLException(__func__);
+ }
+
+ Handle<Object> ret = Object::New();
+ SurfaceWrapper* wrap = new SurfaceWrapper(ret);
+ wrap->surface_ = surface;
+ return scope.Close(ret);
+}
+Handle<Value> sdl::TTF::FontWrapper::RenderGlyphShaded(const Arguments& args) {
+ HandleScope scope;
+
+ FontWrapper* font = ObjectWrap::Unwrap<FontWrapper>(args.This());
+ if(NULL == font) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap 'this' for some reason. (did you not use a Font object?)")));
+ }
+ if(!args[0]->IsString()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: First argument to renderUnicodeShaded must be a String.")));
+ }
+ if(!args[1]->IsObject()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: Second argument to renderUnicodeShaded must be an sdl.Color.")));
+ }
+ if(!args[2]->IsObject()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: Third argument to renderUnicodeShaded must be an sdl.Color.")));
+ }
+
+ String::Value text(args[0]);
+ if(0 == text.length()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: First argument to renderGlyphShaded must be able to convert to a string of at least length 1.")));
+ }
+ ColorWrapper* fg = ObjectWrap::Unwrap<ColorWrapper>(Handle<Object>::Cast(args[1]));
+ if(NULL == fg) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap second argument to renderUnicodeShaded for some reason. (is it not an sdl.Color?)")));
+ }
+ ColorWrapper* bg = ObjectWrap::Unwrap<ColorWrapper>(Handle<Object>::Cast(args[2]));
+ if(NULL == bg) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap third argument to renderUnicodeShaded for some reason. (is it not an sdl.Color?)")));
+ }
+
+ SDL_Surface* surface = TTF_RenderGlyph_Shaded(font->font_, (*text)[0], *fg->color_, *bg->color_);
+ if(NULL == surface) {
+ return ThrowSDLException(__func__);
+ }
+
+ Handle<Object> ret = Object::New();
+ SurfaceWrapper* wrap = new SurfaceWrapper(ret);
+ wrap->surface_ = surface;
+ return scope.Close(ret);
+}
+
+Handle<Value> sdl::TTF::FontWrapper::RenderTextBlended(const Arguments& args) {
+ HandleScope scope;
+
+ FontWrapper* font = ObjectWrap::Unwrap<FontWrapper>(args.This());
+ if(NULL == font) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap 'this' for some reason. (did you not use a Font object?)")));
+ }
+ if(!args[0]->IsString()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: First argument to renderTextBlended must be a String.")));
+ }
+ if(!args[1]->IsObject()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: Second argument to renderTextBlended must be an sdl.Color.")));
+ }
+
+ String::Utf8Value text(args[0]);
+ ColorWrapper* color = ObjectWrap::Unwrap<ColorWrapper>(Handle<Object>::Cast(args[1]));
+ if(NULL == color) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap second argument to renderTextBlended for some reason. (is it not an sdl.Color?)")));
+ }
+
+ SDL_Surface* surface = TTF_RenderText_Blended(font->font_, *text, *color->color_);
+ if(NULL == surface) {
+ return ThrowSDLException(__func__);
+ }
+
+ Handle<Object> ret = Object::New();
+ SurfaceWrapper* wrap = new SurfaceWrapper(ret);
+ wrap->surface_ = surface;
+ return scope.Close(ret);
+}
+Handle<Value> sdl::TTF::FontWrapper::RenderUTF8Blended(const Arguments& args) {
+ HandleScope scope;
+
+ FontWrapper* font = ObjectWrap::Unwrap<FontWrapper>(args.This());
+ if(NULL == font) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap 'this' for some reason. (did you not use a Font object?)")));
+ }
+ if(!args[0]->IsString()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: First argument to renderUtf8Blended must be a String.")));
+ }
+ if(!args[1]->IsObject()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: Second argument to renderUtf8Blended must be an sdl.Color.")));
+ }
+
+ String::Utf8Value text(args[0]);
+ ColorWrapper* color = ObjectWrap::Unwrap<ColorWrapper>(Handle<Object>::Cast(args[1]));
+ if(NULL == color) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap second argument to renderUtf8Blended for some reason. (is it not an sdl.Color?)")));
+ }
+
+ SDL_Surface* surface = TTF_RenderUTF8_Blended(font->font_, *text, *color->color_);
+ if(NULL == surface) {
+ return ThrowSDLException(__func__);
+ }
+
+ Handle<Object> ret = Object::New();
+ SurfaceWrapper* wrap = new SurfaceWrapper(ret);
+ wrap->surface_ = surface;
+ return scope.Close(ret);
+}
+Handle<Value> sdl::TTF::FontWrapper::RenderUnicodeBlended(const Arguments& args) {
+ HandleScope scope;
+
+ FontWrapper* font = ObjectWrap::Unwrap<FontWrapper>(args.This());
+ if(NULL == font) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap 'this' for some reason. (did you not use a Font object?)")));
+ }
+ if(!args[1]->IsObject()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: Second argument to renderUnicodeBlended must be an sdl.Color.")));
+ }
+
+ String::Value text(args[0]);
+ ColorWrapper* color = ObjectWrap::Unwrap<ColorWrapper>(Handle<Object>::Cast(args[1]));
+ if(NULL == color) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap second argument to renderUnicodeBlended for some reason. (is it not an sdl.Color?)")));
+ }
+
+ SDL_Surface* surface = TTF_RenderUNICODE_Blended(font->font_, *text, *color->color_);
+ if(NULL == surface) {
+ return ThrowSDLException(__func__);
+ }
+
+ Handle<Object> ret = Object::New();
+ SurfaceWrapper* wrap = new SurfaceWrapper(ret);
+ wrap->surface_ = surface;
+ return scope.Close(ret);
+}
+Handle<Value> sdl::TTF::FontWrapper::RenderGlyphBlended(const Arguments& args) {
+ HandleScope scope;
+
+ FontWrapper* font = ObjectWrap::Unwrap<FontWrapper>(args.This());
+ if(NULL == font) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap 'this' for some reason. (did you not use a Font object?)")));
+ }
+ if(!args[1]->IsObject()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: Second argument to renderGlyphBlended must be an sdl.Color.")));
+ }
+
+ String::Value text(args[0]);
+ if(0 == text.length()) {
+ return ThrowException(Exception::TypeError(
+ String::New("Invalid arguments: First argument to renderGlyphBlended must be able to convert to a string of at least length 1.")));
+ }
+ ColorWrapper* color = ObjectWrap::Unwrap<ColorWrapper>(Handle<Object>::Cast(args[1]));
+ if(NULL == color) {
+ return ThrowException(Exception::TypeError(
+ String::New("Could not unwrap second argument to renderGlyphBlended for some reason. (is it not an sdl.Color?)")));
+ }
+
+ SDL_Surface* surface = TTF_RenderGlyph_Blended(font->font_, (*text)[0], *color->color_);
+ if(NULL == surface) {
+ return ThrowSDLException(__func__);
+ }
+
+ Handle<Object> ret = Object::New();
+ SurfaceWrapper* wrap = new SurfaceWrapper(ret);
+ wrap->surface_ = surface;
+ return scope.Close(ret);
+}
+
+void sdl::TTF::Initialize(Handle<Object> exports) {
+ Handle<Object> TTF = Object::New();
+ exports->Set(String::New("TTF"), TTF);
+ NODE_SET_METHOD(TTF, "init", Init);
+ NODE_SET_METHOD(TTF, "wasInit", WasInit);
+ NODE_SET_METHOD(TTF, "quit", Quit);
+ NODE_SET_METHOD(TTF, "getError", GetError);
+
+ FontWrapper::Init(TTF);
+}
+
+Handle<Value> sdl::TTF::Init(const Arguments& args) {
+ HandleScope scope;
+
+ if (!(args.Length() == 0)) {
+ return ThrowException(Exception::TypeError(String::New("Invalid arguments: Expected TTF::Init()")));
+ }
+
+ if (TTF_Init() < 0) {
+ return ThrowException(Exception::Error(String::Concat(
+ String::New("TTF::Init: "),
+ String::New(TTF_GetError())
+ )));
+ }
+
+ return Undefined();
+}
+
+Handle<Value> sdl::TTF::WasInit(const Arguments& args) {
+ HandleScope scope;
+
+ return scope.Close(Boolean::New(TTF_WasInit() ? true : false));
+}
+
+Handle<Value> sdl::TTF::Quit(const Arguments& args) {
+ HandleScope scope;
+
+ TTF_Quit();
+
+ return Undefined();
+}
+
+// Handle<Value> sdl::TTF::SetError(const Arguments& args) {
+// HandleScope scope;
+
+// return Undefined();
+// }
+Handle<Value> sdl::TTF::GetError(const Arguments& args) {
+ HandleScope scope;
+
+ const char* error = TTF_GetError();
+ // Can this happen?!
+ if(NULL == error) {
+ return ThrowSDLException(__func__);
+ }
+
+ return scope.Close(String::New(error));
+}
+
+// Handle<Value> sdl::TTF::OpenFont(const Arguments& args) {
+// HandleScope scope;
+
+// if (!(args.Length() == 2 && args[0]->IsString() && args[1]->IsNumber())) {
+// return ThrowException(Exception::TypeError(String::New("Invalid arguments: Expected TTF::OpenFont(String, Number)")));
+// }
+
+// String::Utf8Value file(args[0]);
+// int ptsize = (args[1]->Int32Value());
+
+// TTF_Font* font = TTF_OpenFont(*file, ptsize);
+// if (font == NULL) {
+// return ThrowException(Exception::Error(String::Concat(
+// String::New("TTF::OpenFont: "),
+// String::New(TTF_GetError())
+// )));
+// }
+// return Undefined();
+// // return scope.Close(WrapFont(font));
+// }
+
+// TODO: Rewrite for SDL2.
+// static Handle<Value> sdl::TTF::RenderTextBlended(const Arguments& args) {
+// HandleScope scope;
+
+// if (!(args.Length() == 3 && args[0]->IsObject() && args[1]->IsString() && args[2]->IsNumber())) {
+// return ThrowException(Exception::TypeError(String::New("Invalid arguments: Expected TTF::RenderTextBlended(Font, String, Number)")));
+// }
+
+// SDL_PixelFormat* vfmt = SDL_GetVideoInfo()->vfmt;
+// TTF_Font* font = UnwrapFont(args[0]->ToObject());
+// String::Utf8Value text(args[1]);
+// int colorCode = args[2]->Int32Value();
+
+// Uint8 r, g, b;
+// SDL_GetRGB(colorCode, vfmt, &r, &g, &b);
+
+// SDL_Color color;
+// color.r = r;
+// color.g = g;
+// color.b = b;
+
+// SDL_Surface *resulting_text;
+// resulting_text = TTF_RenderText_Blended(font, *text, color);
+// if (!resulting_text) {
+// return ThrowException(Exception::Error(String::Concat(
+// String::New("TTF::RenderTextBlended: "),
+// String::New(TTF_GetError())
+// )));
+// }
+// return scope.Close(WrapSurface(resulting_text));
+// }
View
56 src/font.h
@@ -0,0 +1,56 @@
+#ifndef NODEJS_FONT_H
+#define NODEJS_FONT_H
+
+#include <v8.h>
+#include <node.h>
+
+#include "SDL.h"
+#include "SDL_ttf.h"
+
+
+namespace sdl {
+ namespace TTF {
+ class FontWrapper : public node::ObjectWrap {
+ public:
+ static v8::Persistent<v8::FunctionTemplate> wrap_template_;
+
+ FontWrapper();
+ ~FontWrapper();
+
+ static void Init(v8::Handle<v8::Object> exports);
+ static v8::Handle<v8::Value> New(const v8::Arguments& args);
+
+ static v8::Handle<v8::Value> RenderTextSolid(const v8::Arguments& args);
+ static v8::Handle<v8::Value> RenderUTF8Solid(const v8::Arguments& args);
+ static v8::Handle<v8::Value> RenderUnicodeSolid(const v8::Arguments& args);
+ static v8::Handle<v8::Value> RenderGlyphSolid(const v8::Arguments& args);
+
+ static v8::Handle<v8::Value> RenderTextShaded(const v8::Arguments& args);
+ static v8::Handle<v8::Value> RenderUTF8Shaded(const v8::Arguments& args);
+ static v8::Handle<v8::Value> RenderUnicodeShaded(const v8::Arguments& args);
+ static v8::Handle<v8::Value> RenderGlyphShaded(const v8::Arguments& args);
+
+ static v8::Handle<v8::Value> RenderTextBlended(const v8::Arguments& args);
+ static v8::Handle<v8::Value> RenderUTF8Blended(const v8::Arguments& args);
+ static v8::Handle<v8::Value> RenderUnicodeBlended(const v8::Arguments& args);
+ static v8::Handle<v8::Value> RenderGlyphBlended(const v8::Arguments& args);
+
+ TTF_Font* font_;
+ };
+
+ void Initialize(v8::Handle<v8::Object> exports);
+
+ v8::Handle<v8::Value> Init(const v8::Arguments& args);
+ v8::Handle<v8::Value> WasInit(const v8::Arguments& args);
+ v8::Handle<v8::Value> Quit(const v8::Arguments& args);
+
+ // v8::Handle<v8::Value> SetError(const v8::Arguments& args);
+ v8::Handle<v8::Value> GetError(const v8::Arguments& args);
+
+ // v8::Handle<v8::Value> OpenFont(const v8::Arguments& args);
+ // v8::Handle<v8::Value> OpenFontIndex(const v8::Arguments& args);
+ // v8::Handle<v8::Value> CloseFont(const v8::Arguments& args);
+ }
+}
+
+#endif
View
89 src/node_sdl.cc
@@ -15,6 +15,7 @@
#include "event.h"
#include "key.h"
#include "mouse.h"
+#include "font.h"
#include <v8.h>
#include <string>
#include <iostream>
@@ -92,6 +93,7 @@ init(Handle<Object> target)
sdl::event::Init(target);
sdl::key::Init(target);
sdl::mouse::Init(target);
+ sdl::TTF::Initialize(target);
// Initialization and Shutdown.
NODE_SET_METHOD(target, "init", sdl::Init);
@@ -188,12 +190,6 @@ init(Handle<Object> target)
TEXTUREACCESS->Set(String::New("STATIC"), Number::New(SDL_TEXTUREACCESS_STATIC));
TEXTUREACCESS->Set(String::New("STREAMING"), Number::New(SDL_TEXTUREACCESS_STREAMING));
- Local<Object> TTF = Object::New();
- target->Set(String::New("TTF"), TTF);
- NODE_SET_METHOD(TTF, "init", sdl::TTF::Init);
- NODE_SET_METHOD(TTF, "openFont", sdl::TTF::OpenFont);
- // NODE_SET_METHOD(TTF, "renderTextBlended", sdl::TTF::RenderTextBlended);
-
Local<Object> IMG = Object::New();
target->Set(String::New("IMG"), IMG);
@@ -239,6 +235,19 @@ init(Handle<Object> target)
target->Set(String::New("WINDOWPOS"), WINDOWPOS);
WINDOWPOS->Set(String::NewSymbol("CENTERED"), Number::New(SDL_WINDOWPOS_CENTERED));
WINDOWPOS->Set(String::NewSymbol("UNDEFINED"), Number::New(SDL_WINDOWPOS_UNDEFINED));
+
+ Local<Object> WINDOW = Object::New();
+ target->Set(String::New("WINDOW"), WINDOW);
+ WINDOW->Set(String::New("FULLSCREEN"), Number::New(SDL_WINDOW_FULLSCREEN));
+ WINDOW->Set(String::New("FULLSCREEN_DESKTOP"), Number::New(SDL_WINDOW_FULLSCREEN_DESKTOP));
+ WINDOW->Set(String::New("OPENGL"), Number::New(SDL_WINDOW_OPENGL));
+ WINDOW->Set(String::New("HIDDEN"), Number::New(SDL_WINDOW_HIDDEN));
+ WINDOW->Set(String::New("BORDERLESS"), Number::New(SDL_WINDOW_BORDERLESS));
+ WINDOW->Set(String::New("RESIZABLE"), Number::New(SDL_WINDOW_RESIZABLE));
+ WINDOW->Set(String::New("MINIMIZED"), Number::New(SDL_WINDOW_MINIMIZED));
+ WINDOW->Set(String::New("MAXIMIZED"), Number::New(SDL_WINDOW_MAXIMIZED));
+ WINDOW->Set(String::New("INPUT_GRABBED"), Number::New(SDL_WINDOW_INPUT_GRABBED));
+ WINDOW->Set(String::New("ALLOW_HIGHDPI"), Number::New(SDL_WINDOW_ALLOW_HIGHDPI));
}
@@ -935,74 +944,6 @@ Handle<Value> sdl::SetClipboardText(const Arguments& args) {
return Undefined();
}
-Handle<Value> sdl::TTF::Init(const Arguments& args) {
- HandleScope scope;
-
- if (!(args.Length() == 0)) {
- return ThrowException(Exception::TypeError(String::New("Invalid arguments: Expected TTF::Init()")));
- }
-
- if (TTF_Init() < 0) {
- return ThrowException(Exception::Error(String::Concat(
- String::New("TTF::Init: "),
- String::New(TTF_GetError())
- )));
- }
-
- return Undefined();
-}
-
-Handle<Value> sdl::TTF::OpenFont(const Arguments& args) {
- HandleScope scope;
-
- if (!(args.Length() == 2 && args[0]->IsString() && args[1]->IsNumber())) {
- return ThrowException(Exception::TypeError(String::New("Invalid arguments: Expected TTF::OpenFont(String, Number)")));
- }
-
- String::Utf8Value file(args[0]);
- int ptsize = (args[1]->Int32Value());
-
- TTF_Font* font = TTF_OpenFont(*file, ptsize);
- if (font == NULL) {
- return ThrowException(Exception::Error(String::Concat(
- String::New("TTF::OpenFont: "),
- String::New(TTF_GetError())
- )));
- }
- return scope.Close(WrapFont(font));
-}
-
-// TODO: Rewrite for SDL2.
-// static Handle<Value> sdl::TTF::RenderTextBlended(const Arguments& args) {
-// HandleScope scope;
-
-// if (!(args.Length() == 3 && args[0]->IsObject() && args[1]->IsString() && args[2]->IsNumber())) {
-// return ThrowException(Exception::TypeError(String::New("Invalid arguments: Expected TTF::RenderTextBlended(Font, String, Number)")));
-// }
-
-// SDL_PixelFormat* vfmt = SDL_GetVideoInfo()->vfmt;
-// TTF_Font* font = UnwrapFont(args[0]->ToObject());
-// String::Utf8Value text(args[1]);
-// int colorCode = args[2]->Int32Value();
-
-// Uint8 r, g, b;
-// SDL_GetRGB(colorCode, vfmt, &r, &g, &b);
-
-// SDL_Color color;
-// color.r = r;
-// color.g = g;
-// color.b = b;
-
-// SDL_Surface *resulting_text;
-// resulting_text = TTF_RenderText_Blended(font, *text, color);
-// if (!resulting_text) {
-// return ThrowException(Exception::Error(String::Concat(
-// String::New("TTF::RenderTextBlended: "),
-// String::New(TTF_GetError())
-// )));
-// }
-// return scope.Close(WrapSurface(resulting_text));
-// }
// TODO: make an async version so this can be used in loops or parallel load images
Handle<Value> sdl::IMG::Load(const Arguments& args) {
View
6 src/node_sdl.h
@@ -85,12 +85,6 @@ namespace sdl {
v8::Handle<v8::Value> HasClipboardText(const v8::Arguments& args);
v8::Handle<v8::Value> SetClipboardText(const v8::Arguments& args);
- namespace TTF {
- v8::Handle<v8::Value> Init(const v8::Arguments& args);
- v8::Handle<v8::Value> OpenFont(const v8::Arguments& args);
- // static v8::Handle<v8::Value> RenderTextBlended(const v8::Arguments& args);
- }
-
namespace IMG {
v8::Handle<v8::Value> Load(const v8::Arguments& args);
}
View
68 src/surface.cc
@@ -75,36 +75,44 @@ Handle<Value> sdl::SurfaceWrapper::New(const Arguments& args) {
}
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();
+ if(args[0]->IsExternal()) {
+ SurfaceWrapper* obj = new SurfaceWrapper();
+ obj->surface_ = static_cast<SDL_Surface*>(Handle<External>::Cast(args[0])->Value());
+ obj->Wrap(args.This());
+ return args.This();
+ }
+ else {
+ 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<Value> sdl::SurfaceWrapper::LoadBMP(const Arguments& args) {

0 comments on commit bad4bae

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