Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tile map support #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ Init_Fluency(void)
INIT_MODULE(Layer)
INIT_MODULE(Menu)
INIT_MODULE(Sprite)
INIT_MODULE(TileMap)
INIT_MODULE(Action)
INIT_MODULE(Particle)
INIT_MODULE(Audio)
Expand Down
2 changes: 2 additions & 0 deletions src/motion-game.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extern VALUE rb_cScene;
extern VALUE rb_cMenu;
extern VALUE rb_cLabel;
extern VALUE rb_cSprite;
extern VALUE rb_cTileMap;
extern VALUE rb_cParticle;
extern VALUE rb_cAudio;
extern VALUE rb_cPoint;
Expand All @@ -29,6 +30,7 @@ extern VALUE rb_cColor;
#define GLVIEW(obj) _COCOS_WRAP_GET(obj, cocos2d::GLView)
#define NODE(obj) _COCOS_WRAP_GET(obj, cocos2d::Node)
#define SPRITE(obj) _COCOS_WRAP_GET(obj, cocos2d::Sprite)
#define TILEMAP(obj) _COCOS_WRAP_GET(obj, cocos2d::TMXTiledMap)
#define MENU(obj) _COCOS_WRAP_GET(obj, cocos2d::Menu)
#define AUDIO(obj) _COCOS_WRAP_GET(obj, CocosDenshion::SimpleAudioEngine)
#define VEC2(obj) _COCOS_WRAP_GET(obj, cocos2d::Vec2)
Expand Down
51 changes: 51 additions & 0 deletions src/tilemap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "rubymotion.h"
#include "motion-game.h"

/// @class TileMap < Node

VALUE rb_cTileMap = Qnil;

/// @method .load(tmx_filename)
/// Load the specfied TMX file and return a TileMap object.
/// @param tmx_filename [String] the filename of the TMX file.
/// @return [TileMap]

static VALUE
tilemap_load(VALUE rcv, SEL sel, VALUE tmx_filename)
{
std::string filename_str = RSTRING_PTR(StringValue(tmx_filename));
auto map = cocos2d::TMXTiledMap::create(filename_str);
if (map == NULL) {
rb_raise(rb_eRuntimeError, "Can't create TileMap with `%s'. Need a proper TMX file.", filename_str.c_str());
}
return rb_cocos2d_object_new(map, rcv);
}

/// @method #layer(name)
/// Return the layer for the given name.
/// @return [TMXLayer] TMX layer.

static cocos2d::TMXLayer *
tilemap_layer(VALUE rcv, SEL sel, VALUE name)
{
return TILEMAP(rcv)->getLayer(RSTRING_PTR(StringValue(name)));
}

/// @property #tile_size
/// @return [Size] the tile size.

static VALUE
tilemap_tile_size(VALUE rcv, SEL sel)
{
return rb_ccsize_to_obj(TILEMAP(rcv)->getTileSize());
}

extern "C"
void
Init_TileMap(void)
{
rb_cTileMap = rb_define_class_under(rb_mMC, "TileMap", rb_cNode);
rb_define_singleton_method(rb_cTileMap, "load", tilemap_load, 1);
rb_define_method(rb_cTileMap, "layer", tilemap_layer, 1);
rb_define_method(rb_cTileMap, "tile_size", tilemap_tile_size, 0);
}