Skip to content

Commit

Permalink
Update Rect API and bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
creationix committed Jul 18, 2011
1 parent 98214d7 commit b2207bf
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
8 changes: 4 additions & 4 deletions examples/Chaser.js
Expand Up @@ -53,9 +53,9 @@ Player.prototype.tick = function (delta) {

var px = Math.floor(this.x);
var py = Math.floor(this.y);
SDL.fillRect(screen, px - 10, py - 10, 20, 20, colors[this.colorName][0]);
SDL.fillRect(screen, px - 8, py - 8, 16, 16, colors[this.colorName][2]);
SDL.fillRect(screen, px - 4, py - 4, 8, 8, colors[this.colorName][1]);
SDL.fillRect(screen, [px - 10, py - 10, 20, 20], colors[this.colorName][0]);
SDL.fillRect(screen, [px - 8, py - 8, 16, 16], colors[this.colorName][2]);
SDL.fillRect(screen, [px - 4, py - 4, 8, 8], colors[this.colorName][1]);
};

var rotate = 0;
Expand All @@ -74,7 +74,7 @@ Spark.prototype.tick = function (delta) {
for (var a = 0; a < Math.PI * 2; a += Math.PI / 3) {
var px = Math.floor(this.x + this.d * Math.sin(a + this.r));
var py = Math.floor(this.y + this.d * Math.cos(a + this.r));
SDL.fillRect(screen, px - 5, py - 5, 10, 10, this.color);
SDL.fillRect(screen, [px - 5, py - 5, 10, 10], this.color);
}
};
Spark.prototype.expire = function () {
Expand Down
4 changes: 3 additions & 1 deletion examples/Fonts.js
Expand Up @@ -30,7 +30,9 @@ setInterval(function () {
now = after;
SDL.fillRect(screen, null, 0);
var message = (Math.floor(100000 / delta) / 100) + "fps";
TTF.renderTextBlended(screen, font, message, 10, 10, 0xffffff);
var s = TTF.renderTextBlended(screen, font, message, 0xffffff);
SDL.blitSurface(s, null, screen, [10, 10]);
SDL.freeSurface(s);
SDL.flip(screen);
}, 15);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"author": "Tim Caswell <tim@creationix.com> (http://creationix.com/)",
"name": "sdl",
"description": "SDL bindings for node",
"version": "0.1.0",
"version": "0.1.1",
"repository": {
"type": "git",
"url": "git://github.com/creationix/node-sdl.git"
Expand Down
46 changes: 38 additions & 8 deletions src/sdl.cc
Expand Up @@ -455,15 +455,29 @@ static Handle<Value> sdl::Flip(const Arguments& args) {
static Handle<Value> sdl::FillRect(const Arguments& args) {
HandleScope scope;

if (!
((args.Length() == 3 && args[0]->IsObject() && (args[1]->IsObject() || args[1]->IsNull()) && args[2]->IsNumber()) ||
(args.Length() == 6 && args[0]->IsObject() && args[1]->IsNumber() && args[2]->IsNumber() && args[3]->IsNumber() && args[4]->IsNumber() && args[5]->IsNumber()))
) {
return ThrowException(Exception::TypeError(String::New("Invalid arguments: Expected FillRect(Surface, Rect, Number) or (Surface, Number, Number, Number, Number, Number)")));
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 = UnwrapRect(args[1]->ToObject());
SDL_Rect* rect;
if (args[1]->IsNull()) {
rect = NULL;
} else if (args[1]->IsArray()) {
SDL_Rect r;
Handle<Object> 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__);
Expand All @@ -474,12 +488,28 @@ static Handle<Value> sdl::FillRect(const Arguments& args) {
static Handle<Value> sdl::UpdateRect(const Arguments& args) {
HandleScope scope;

if (!(args.Length() == 2 && args[0]->IsObject() && args[1]->IsObject())) {
if (!(args.Length() == 2
&& args[0]->IsObject()
&& (args[1]->IsObject() || args[1]->IsNull())
)) {
return ThrowException(Exception::TypeError(String::New("Invalid arguments: Expected UpdateRect(Surface, Rect)")));
}

SDL_Surface* surface = UnwrapSurface(args[0]->ToObject());
SDL_Rect* rect = UnwrapRect(args[1]->ToObject());
SDL_Rect* rect;
if (args[1]->IsNull()) {
rect = NULL;
} else if (args[1]->IsArray()) {
SDL_Rect r;
Handle<Object> 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());
}

SDL_UpdateRect(surface, rect->x, rect->y, rect->w, rect->h);

Expand Down

0 comments on commit b2207bf

Please sign in to comment.