Skip to content

Commit

Permalink
Tiles.xform and Polygon.resize, key sdl events
Browse files Browse the repository at this point in the history
  • Loading branch information
dustmop committed Mar 24, 2023
1 parent 401c72f commit ff8a3e5
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 11 deletions.
26 changes: 23 additions & 3 deletions src/addon/sdl_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,13 @@ Napi::Value SDLBackend::HandleEvent(const Napi::CallbackInfo& info) {
// TODO: handle click, click-with-region
if (eventName.Utf8Value() == std::string("keypress")) {
Napi::Function handleFunc = info[2].As<Napi::Function>();
this->keyHandleFunc = Napi::Persistent(handleFunc);
this->keyDownHandleFunc = Napi::Persistent(handleFunc);
} else if (eventName.Utf8Value() == std::string("keyup")) {
Napi::Function handleFunc = info[2].As<Napi::Function>();
this->keyUpHandleFunc = Napi::Persistent(handleFunc);
} else if (eventName.Utf8Value() == std::string("keydown")) {
Napi::Function handleFunc = info[2].As<Napi::Function>();
this->keyDownHandleFunc = Napi::Persistent(handleFunc);
}
return Napi::Number::New(env, 0);
}
Expand Down Expand Up @@ -421,14 +427,28 @@ void SDLBackend::execOneFrame(const CallbackInfo& info) {
if (event.key.keysym.sym == SDLK_ESCAPE) {
this->isRunning = false;
return;
} else if (!this->keyHandleFunc.IsEmpty()) {
} else if (!this->keyDownHandleFunc.IsEmpty()) {
int code = event.key.keysym.sym;
std::string s(1, char(code));
Napi::String str = Napi::String::New(env, s);
Napi::Object obj = Napi::Object::New(env);
obj["key"] = str;
napi_value val = obj;
this->keyDownHandleFunc.Call({val});
if (env.IsExceptionPending()) {
return;
}
}
break;
case SDL_KEYUP:
if (!this->keyUpHandleFunc.IsEmpty()) {
int code = event.key.keysym.sym;
std::string s(1, char(code));
Napi::String str = Napi::String::New(env, s);
Napi::Object obj = Napi::Object::New(env);
obj["key"] = str;
napi_value val = obj;
this->keyHandleFunc.Call({val});
this->keyUpHandleFunc.Call({val});
if (env.IsExceptionPending()) {
return;
}
Expand Down
3 changes: 2 additions & 1 deletion src/addon/sdl_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class SDLBackend : public Napi::ObjectWrap<SDLBackend> {
bool hasWriteBuffer;
Napi::Reference<Napi::Value> writeBuffer;

Napi::FunctionReference keyHandleFunc;
Napi::FunctionReference keyUpHandleFunc;
Napi::FunctionReference keyDownHandleFunc;
int sdlInitialized;
int zoomLevel;

Expand Down
15 changes: 15 additions & 0 deletions src/geometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ class Polygon {
result.push([rot_x + this._centerX, rot_y + this._centerY]);
}
this._points = result;
return this;
}

resize(factor) {
let newPoints = [];
for (let p of this._points) {
var x = p[0] * factor;
var y = p[1] * factor;
newPoints.push([x, y]);
}
let newCenterAxis = {
x: this._centerX * factor,
y: this._centerY * factor,
};
return new Polygon(newPoints, newCenterAxis);
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,15 +528,12 @@ class Scene {
'save', spec, arguments, null);
if (component) {
// render component
let res = component.visualize();
let res = component.visualize({palette: this.palette});
this._fsacc.saveTo(savepath, res);
return;
}
// render the main field
let surfs = this.renderPrimaryField();
if (!this._fsacc) {
throw new Error('cannot save field without filesys access');
}
let comp = new compositor.Compositor();
let combined = comp.combine(surfs, surfs[0].width, surfs[0].height,
this.config.zoomScale);
Expand Down Expand Up @@ -637,8 +634,8 @@ class Scene {
eventName = optField;
}

let allowed = ['keypress', 'click', 'ready', 'render', 'message',
'dipchange'];
let allowed = ['keypress', 'keydown', 'keyup', 'click', 'ready',
'render', 'message', 'dipchange'];
if (!allowed.includes(eventName)) {
let expect = allowed.map((n)=>`"${n}"`).join(', ');
throw new Error(`unknown event "${eventName}", only ${expect} supported`);
Expand Down
58 changes: 57 additions & 1 deletion src/tiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ class Tileset extends component.Component {
let detail = {tile_width: this.tileWidth, tile_height: this.tileHeight};
let make = new Tileset(detail);
make.data = this.data.slice();
make._palette = this._palette;
make._fillContents();
return make;
}
Expand Down Expand Up @@ -368,6 +367,63 @@ class Tile {
return make;
}

xform(kind) {
if (kind == 'vhflip') {
let make = this.xform('vflip');
return make.xform('hflip');
} else if (kind == 'hflip') {
// TODO: get pitch from the env
let newPitch = this.width;
let numPixels = this.height * newPitch;
let buff = new Uint8Array(numPixels);
for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) {
let j = y * newPitch + x;
buff[j] = this.get(this.width - x - 1, y);
}
}
let make = new field.Field();
make.data = buff;
make.pitch = newPitch;
make.width = this.width;
make.height = this.height;
return make;
} else if (kind == 'vflip') {
// TODO: get pitch from the env
let newPitch = this.width;
let numPixels = this.height * newPitch;
let buff = new Uint8Array(numPixels);
for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) {
let j = y * newPitch + x;
buff[j] = this.get(x, this.height - y - 1);
}
}
let make = new field.Field();
make.data = buff;
make.pitch = newPitch;
make.width = this.width;
make.height = this.height;
return make;
}
throw new Error(`unknown xform: "${kind}"`);
}

serialize() {
let data = [];
for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) {
data.push(this.get(x, y));
}
}
let obj = {
"width": this.width,
"height": this.height,
"data": data,
};
return JSON.stringify(obj);
}

display() {
for (let j = 0; j < this.height; j++) {
for (let i = 0; i < this.width; i++) {
Expand Down
13 changes: 13 additions & 0 deletions test/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,17 @@ describe('Polygon', function() {

util.renderCompareTo(ra, 'test/testdata/polygon_float.png');
});

it('resize polygon', function() {
ra.resetState();

ra.setSize({w: 36, h: 34});
ra.fillColor(0);

let polygon = new ra.Polygon([[2, 3], [14, 3], [10, 7], [6, 7]]);
let bigger = polygon.resize(2);
ra.drawPolygon(bigger, 1, 4);

util.renderCompareTo(ra, 'test/testdata/polygon_resize.png');
});
});
Binary file added test/testdata/polygon_resize.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions test/tileset.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,5 +467,55 @@ describe('Tileset', function() {
}, /put: y is null/);
});

it('xform', function() {
let input = [0,4,6,0,
5,7,2,9,
0,0,0,3,
0,1,8,0];

let t = new ra.Tile(4, 4);
t.fill(input);
let actual = t.serialize();
let expect = {
width: 4,
height: 4,
data: input,
};
assert.deepEqual(actual, JSON.stringify(expect));

actual = t.xform('hflip').serialize();
expect = {
width: 4,
height: 4,
data: [0,6,4,0,
9,2,7,5,
3,0,0,0,
0,8,1,0],
};
assert.deepEqual(actual, JSON.stringify(expect));

actual = t.xform('vflip').serialize();
expect = {
width: 4,
height: 4,
data: [0,1,8,0,
0,0,0,3,
5,7,2,9,
0,4,6,0],
};
assert.deepEqual(actual, JSON.stringify(expect));

actual = t.xform('vhflip').serialize();
expect = {
width: 4,
height: 4,
data: [0,8,1,0,
3,0,0,0,
9,2,7,5,
0,6,4,0],
};
assert.deepEqual(actual, JSON.stringify(expect));

});

});

0 comments on commit ff8a3e5

Please sign in to comment.