Skip to content

Commit 4a5daba

Browse files
author
=
committed
feat: getTileRect method added to nme.display.Tilesheet
1 parent d29279b commit 4a5daba

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

nme/display/Tilesheet.hx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ class Tilesheet
4141
{
4242
return nme_tilesheet_add_rect(nmeHandle, rectangle, centerPoint);
4343
}
44+
45+
public function getTileRect(index:Int, ?result:Rectangle):Rectangle
46+
{
47+
if (result == null)
48+
{
49+
result = new Rectangle();
50+
}
51+
nme_tilesheet_get_rect(nmeHandle, index, result);
52+
return result;
53+
}
4454

4555
public function drawTiles(graphics:Graphics, tileData:nme.utils.Floats3264, smooth:Bool = false, flags:Int = 0, count:Int=-1):Void
4656
{
@@ -50,6 +60,7 @@ class Tilesheet
5060
// Native Methods
5161
private static var nme_tilesheet_create = Loader.load("nme_tilesheet_create", 1);
5262
private static var nme_tilesheet_add_rect = Loader.load("nme_tilesheet_add_rect", 3);
63+
private static var nme_tilesheet_get_rect = Loader.load("nme_tilesheet_get_rect", 3);
5364

5465
#else
5566

@@ -71,6 +82,12 @@ class Tilesheet
7182
centres.push(centerPoint);
7283
return result;
7384
}
85+
86+
public function getTileRect(index:Int):Rectangle
87+
{
88+
return tiles[index];
89+
}
90+
7491

7592
public function drawTiles(graphics:Graphics, tileData:Array<Float>, smooth:Bool = false, flags:Int = 0, count:Int=-1):Void
7693
{

project/src/common/ExternalInterface.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4869,6 +4869,20 @@ value nme_tilesheet_add_rect(value inSheet,value inRect, value inHotSpot)
48694869
}
48704870
DEFINE_PRIM(nme_tilesheet_add_rect,3);
48714871

4872+
value nme_tilesheet_get_rect(value inSheet, value inIndex, value outRect)
4873+
{
4874+
Tilesheet *sheet;
4875+
if (AbstractToObject(inSheet,sheet))
4876+
{
4877+
int index = val_int(inIndex);
4878+
Tile tile = sheet->GetTile(index);
4879+
ToValue(outRect, tile.mRect);
4880+
}
4881+
return alloc_null();
4882+
}
4883+
DEFINE_PRIM(nme_tilesheet_get_rect,3);
4884+
4885+
48724886
// --- URL ----------------------------------------------------------
48734887

48744888
value nme_curl_initialize(value inCACertFilePath)

tests/haxe/TestMain.hx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package;
22
import haxe.Timer;
33
import nme.display.TestBitmapDataCopyChannel;
4+
import nme.display.TestTilesheet;
45
class TestMain {
56

67
static function main(){
78
var r = new haxe.unit.TestRunner();
89
r.add(new TestBitmapDataCopyChannel());
10+
r.add(new TestTilesheet());
11+
912
var t0 = Timer.stamp();
1013
var success = r.run();
1114
trace(" Time : " + (Timer.stamp()-t0)*1000 );
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package nme.display;
2+
import nme.geom.Rectangle;
3+
4+
class TestTilesheet extends haxe.unit.TestCase
5+
{
6+
7+
var tilesheet:Tilesheet;
8+
9+
override public function setup()
10+
{
11+
var bd = new BitmapData(32,32,true,0xFFFFFFFF);
12+
tilesheet = new Tilesheet( bd );
13+
}
14+
15+
public function testGetTileRect() {
16+
var tileId:Int = tilesheet.addTileRect(new Rectangle (2, 4, 6, 8));
17+
var rect:Rectangle = tilesheet.getTileRect( tileId );
18+
assertEquals(rect.x, 2);
19+
assertEquals(rect.y, 4);
20+
assertEquals(rect.width, 6);
21+
assertEquals(rect.height, 8);
22+
}
23+
24+
public function testGetTileRectNoNewAlloc()
25+
{
26+
var tileId:Int = tilesheet.addTileRect(new Rectangle (2, 4, 6, 8));
27+
var rect:Rectangle = new Rectangle(0,0,10,10);
28+
var result = tilesheet.getTileRect( tileId, rect );
29+
assertEquals(rect, result);
30+
}
31+
}

0 commit comments

Comments
 (0)