1.1 --- a/include/lf/mj/Core.h Sun Nov 01 10:12:35 2009 +0700
1.2 +++ b/include/lf/mj/Core.h Sun Nov 01 17:59:23 2009 +0700
1.3 @@ -21,6 +21,7 @@
1.4 bool canSelect(const TileType *tile) const;
1.5 void keyPressed(input::CKeyEvent &e);
1.6 void lighting();
1.7 + bool loadField(const core::stringc &path);
1.8 void mousePressed(input::CMouseEvent &e);
1.9 void run();
1.10
1.11 @@ -35,13 +36,13 @@
1.12 u32 mFPS;
1.13 u32 mPolys;
1.14 std::vector<core::stringc> mTexs;
1.15 - TileTemplate *tmpl;
1.16 typedef std::map<scene::CSceneNode*, TileType*> HashNodeTile;
1.17 // Scene node <-> Tile correspondence
1.18 HashNodeTile mHNT;
1.19 typedef std::vector<TileType*> Tiles;
1.20 typedef Tiles::const_iterator TIt;
1.21 Tiles mTiles;
1.22 + TileTemplate *mTmpl;
1.23 };
1.24
1.25 } // namespace lf
2.1 --- a/include/lf/mj/TileType.h Sun Nov 01 10:12:35 2009 +0700
2.2 +++ b/include/lf/mj/TileType.h Sun Nov 01 17:59:23 2009 +0700
2.3 @@ -11,7 +11,7 @@
2.4 public:
2.5 TileType(const TileType *other = 0);
2.6 TileType(const TileType &other);
2.7 - virtual ~TileType() { }
2.8 + virtual ~TileType();
2.9
2.10 void create();
2.11
2.12 @@ -37,29 +37,25 @@
2.13 core::vector3df getScale() const {
2.14 return mScale;
2.15 }
2.16 - bool getSelectability() const {
2.17 - return mSelectability;
2.18 - }
2.19 bool getSelected() const {
2.20 return mSelected;
2.21 }
2.22 core::stringc getTex() const {
2.23 return mTex;
2.24 }
2.25 - bool getVisibility() const {
2.26 - return mVisibility;
2.27 + bool getVisible() const {
2.28 + return mVisible;
2.29 }
2.30 // Must be references
2.31 - virtual void setGroup(const u32 group);
2.32 + void setGroup(const u32 group);
2.33 void setID(const u32 &id);
2.34 - virtual void setTex(const core::stringc &mat);
2.35 + void setTex(const core::stringc &mat);
2.36 virtual void setMesh(const core::stringc &mesh);
2.37 void setPos(const core::vector3di &pos);
2.38 virtual void setRot(const core::vector3df &rot);
2.39 virtual void setScale(const core::vector3df &scale);
2.40 - virtual void setSelectability(const bool &selectability);
2.41 - virtual void setSelected(const bool selected);
2.42 - virtual void setVisibility(const bool &visibility);
2.43 + void setSelected(const bool selected);
2.44 + void setVisible(const bool visible);
2.45
2.46 protected:
2.47 TileType& operator=(const TileType &other);
2.48 @@ -71,9 +67,8 @@
2.49 core::vector3di mPos;
2.50 core::vector3df mRot;
2.51 core::vector3df mScale;
2.52 - bool mSelectability;
2.53 bool mSelected;
2.54 - bool mVisibility;
2.55 + bool mVisible;
2.56
2.57 CResourceManager *mRMgr;
2.58 scene::CScene *mScn;
3.1 --- a/src/lf/mj/Core.cpp Sun Nov 01 10:12:35 2009 +0700
3.2 +++ b/src/lf/mj/Core.cpp Sun Nov 01 17:59:23 2009 +0700
3.3 @@ -3,13 +3,16 @@
3.4 #include <lf/mj/Layout.h>
3.5 #include <lf/mj/TileTI.h>
3.6
3.7 +#include <ctime>
3.8 +
3.9 namespace lf {
3.10 namespace mj {
3.11
3.12 Core::Core() :
3.13 mWin(0), mRMgr(0), mScn(0), mCam(0),
3.14 mSelectedTile(0),
3.15 - mFPS(0), mPolys(0) {
3.16 + mFPS(0), mPolys(0),
3.17 + mTmpl(0) {
3.18 mWin = CLFRender::getInstance().createRenderWindow(
3.19 core::vector2di(0, 0),
3.20 core::vector2d<s32>(1024, 768),
3.21 @@ -58,42 +61,37 @@
3.22 mTexs.push_back("tile03.png");
3.23 mTexs.push_back("tile04.png");
3.24 mRMgr->loadResources("res/lfm/tile.lfm");
3.25 - tmpl = new TileTemplate;
3.26 - tmpl->setMesh("Tile");
3.27 }
3.28
3.29 Core::~Core() {
3.30 - tmpl->drop();
3.31 + if (mTmpl)
3.32 + mTmpl->drop();
3.33 }
3.34
3.35 bool Core::canSelect(const TileType *tile) const {
3.36 const core::vector3di pos = tile->getPos();
3.37 - u8 adjTilesNb = 0;
3.38 + bool adjTileLeft = false;
3.39 + bool adjTileRight = false;
3.40 for (TIt it = mTiles.begin(); it != mTiles.end(); it++)
3.41 // It must not be us and must be visible.
3.42 - if (*it != tile && (*it)->getVisibility()) {
3.43 + if (*it != tile && (*it)->getVisible()) {
3.44 const core::vector3di posNew = (*it)->getPos();
3.45 // Find out if this tile is above us.
3.46 if (posNew.Y - pos.Y == 1 &&
3.47 fabs(posNew.X - pos.X) <= 1 &&
3.48 - fabs(posNew.Z - pos.Z) <= 1) {
3.49 - std::cout << "above\n";
3.50 + fabs(posNew.Z - pos.Z) <= 1)
3.51 return false;
3.52 - }
3.53 - // Find out if this tile is adjacent to our side.
3.54 - if (posNew.Y == pos.Y &&
3.55 - fabs(posNew.Z - pos.Z) <= 1 &&
3.56 - fabs(posNew.X - pos.X) == 2) {
3.57 - adjTilesNb++;
3.58 - std::cout << "adj side\n";
3.59 + // Find out if this tile is adjacent to one the two sides.
3.60 + if (posNew.Y == pos.Y && fabs(posNew.Z - pos.Z) <= 1) {
3.61 + if (posNew.X - pos.X == 2)
3.62 + adjTileLeft = true;
3.63 + else if (posNew.X - pos.X == -2)
3.64 + adjTileRight = true;
3.65 }
3.66 // We are between two tiles.
3.67 - if (adjTilesNb == 2) {
3.68 - std::cout << "2 adj sides\n";
3.69 + if (adjTileLeft && adjTileRight)
3.70 return false;
3.71 - }
3.72 }
3.73 - std::cout << "return true\n";
3.74 return true;
3.75 }
3.76
3.77 @@ -144,11 +142,8 @@
3.78 }
3.79
3.80 void Core::mousePressed(input::CMouseEvent &e) {
3.81 - std::cout << "mouse pressed\n";
3.82 - if (e.getButton() != 1) {
3.83 - std::cout << "not button1\n";
3.84 + if (e.getButton() != 1)
3.85 return;
3.86 - }
3.87 // Deselect.
3.88 if (mSelectedTile)
3.89 mSelectedTile->setSelected(false);
3.90 @@ -163,47 +158,27 @@
3.91 // There's always only one selected node.
3.92 if (!pickResults.empty() && mHNT.find(pickResults[0]->node) != mHNT.end())
3.93 newTile = mHNT[pickResults[0]->node];
3.94 - else
3.95 - std::cout << "no pick results\n";
3.96 // Do not select, if it was previously selected.
3.97 if (mSelectedTile && newTile == mSelectedTile) {
3.98 - std::cout << "we toggle\n";
3.99 mSelectedTile = 0;
3.100 return;
3.101 }
3.102 // If the tiles are in the same group, remove them from scene.
3.103 // Otherwise just select new one.
3.104 - if (newTile) {
3.105 - core::vector3di pos = newTile->getPos();
3.106 - std::cout << "pos(" << pos.X << ", "
3.107 - << pos.Y << ", "
3.108 - << pos.Z << ")\n";
3.109 - }
3.110 if (newTile && canSelect(newTile)) {
3.111 if (mSelectedTile &&
3.112 newTile->getGroup() == mSelectedTile->getGroup()) {
3.113
3.114 - std::cout << "the same group\n";
3.115 - mSelectedTile->setVisibility(false);
3.116 - newTile->setVisibility(false);
3.117 + mSelectedTile->setVisible(false);
3.118 + newTile->setVisible(false);
3.119 mSelectedTile = 0;
3.120 return;
3.121 }
3.122 - if (mSelectedTile &&
3.123 - newTile->getGroup() != mSelectedTile->getGroup()) {
3.124 -
3.125 - std::cout << "different groups: "
3.126 - << mSelectedTile->getGroup() << ", "
3.127 - << newTile->getGroup() << std::endl;
3.128 - }
3.129 - std::cout << "here\nid:" << newTile->getID() << std::endl;
3.130 newTile->setSelected(true);
3.131 mSelectedTile = newTile;
3.132 }
3.133 - else {
3.134 - std::cout << "other\n";
3.135 + else
3.136 mSelectedTile = 0;
3.137 - }
3.138 }
3.139
3.140 void Core::lighting() {
3.141 @@ -281,31 +256,59 @@
3.142 sssr->drop();
3.143 }
3.144
3.145 +bool Core::loadField(const core::stringc &path) {
3.146 + // Read in field layout.
3.147 + Layout ly;
3.148 + if (!ly.read(path))
3.149 + return false;
3.150 + mHNT.clear();
3.151 + mTiles.clear();
3.152 + if (mTmpl)
3.153 + mTmpl->drop();
3.154 + mTmpl = new TileTemplate;
3.155 + mTmpl->setMesh("Tile");
3.156 + srand(time(0));
3.157 + Layout::Positions positions = ly.positions();
3.158 + // Get random positions.
3.159 + u32 count = 0;
3.160 + u32 groupID = 0;
3.161 + bool changeGroupID = true;
3.162 + while (u32 size = positions.size()) {
3.163 + u32 id = rand() % size;
3.164 + core::vector3di pos = positions[id];
3.165 + positions.erase(positions.begin() + id);
3.166 + TileInstance *inst = mTmpl->instantiate();
3.167 + inst->setPos(pos);
3.168 + inst->setID(count++);
3.169 + inst->setTex(mTexs[groupID]);
3.170 + inst->setGroup(groupID);
3.171 + changeGroupID = !changeGroupID;
3.172 + // Group IDs are taken in order. Two tiles from each group.
3.173 + if (changeGroupID && ++groupID > mTexs.size() - 1)
3.174 + groupID = 0;
3.175 + // Insert into hash.
3.176 + mHNT[inst->getNode()] = inst;
3.177 + mTiles.push_back(inst);
3.178 + }
3.179 + /*
3.180 + for (u32 i = 0; i < ly.positions().size(); i++) {
3.181 + TileInstance *inst = mTmpl->instantiate();
3.182 + inst->setPos(ly.positions()[i]);
3.183 + inst->setID(i);
3.184 + u32 id = i % 4;
3.185 + inst->setTex(mTexs[id]);
3.186 + // Group is equal to texture ID.
3.187 + inst->setGroup(id);
3.188 + mHNT[inst->getNode()] = inst;
3.189 + mTiles.push_back(inst);
3.190 + }
3.191 + */
3.192 + mTmpl->setRot(core::vector3df(0, 180, 0));
3.193 + return true;
3.194 +}
3.195 +
3.196 void Core::run() {
3.197 - bool quit = false;
3.198 - // Read in layout.
3.199 - Layout ly;
3.200 - if (ly.read("res/layout/pirates.layout")) {
3.201 - for (u32 i = 0; i < ly.positions().size(); i++) {
3.202 - TileInstance *inst = tmpl->instantiate();
3.203 - inst->setPos(ly.positions()[i]);
3.204 - inst->setID(i);
3.205 - u32 id = i % 4;
3.206 - std::cout << "id: " << id << std::endl;
3.207 - inst->setTex(mTexs[id]);
3.208 - // Group is equal to texture ID.
3.209 - inst->setGroup(id);
3.210 - mHNT[inst->getNode()] = inst;
3.211 - mTiles.push_back(inst);
3.212 - }
3.213 - tmpl->setRot(core::vector3df(0, 180, 0));
3.214 - }
3.215 - else {
3.216 - CLFLog::getInstance().log(lf::log::ELS_USER1,
3.217 - lf::log::ELL_ERROR,
3.218 - "Could not open layout");
3.219 - quit = true;
3.220 - }
3.221 + bool quit = !loadField("res/layout/pirates.layout");
3.222 // Loop.
3.223 while (!quit) {
3.224 u32 fps = mWin->getOneSecondFPS();
4.1 --- a/src/lf/mj/Layout.cpp Sun Nov 01 10:12:35 2009 +0700
4.2 +++ b/src/lf/mj/Layout.cpp Sun Nov 01 17:59:23 2009 +0700
4.3 @@ -7,12 +7,22 @@
4.4 bool Layout::read(const core::stringc &fileName) {
4.5 mPositions.clear();
4.6 std::ifstream in(fileName.c_str());
4.7 - if (!in.is_open())
4.8 + if (!in.is_open()) {
4.9 + CLFLog::getInstance().log(log::ELS_USER1,
4.10 + log::ELL_ERROR,
4.11 + "Layout file '%s' could not be open",
4.12 + fileName.c_str());
4.13 return false;
4.14 + }
4.15 std::string s;
4.16 readLine(in, s);
4.17 - if (s != "kmahjongg-layout-v1.0")
4.18 + if (s != "kmahjongg-layout-v1.0") {
4.19 + CLFLog::getInstance().log(log::ELS_USER1,
4.20 + log::ELL_ERROR,
4.21 + "Layout file '%s' is not of KMahjongg v1.0",
4.22 + fileName.c_str());
4.23 return false;
4.24 + }
4.25 // Field by field
4.26 u8 fieldID = 0;
4.27 while (readLine(in, s)) {
4.28 @@ -39,6 +49,14 @@
4.29 fieldID++;
4.30 }
4.31 }
4.32 + if (mPositions.size() % 2 != 0) {
4.33 + CLFLog::getInstance().log(log::ELS_USER1,
4.34 + log::ELL_ERROR,
4.35 + "Layout file '%s' number of positions "
4.36 + "is not aliquot to 2",
4.37 + fileName.c_str());
4.38 + return false;
4.39 + }
4.40 return true;
4.41 }
4.42
5.1 --- a/src/lf/mj/TileType.cpp Sun Nov 01 10:12:35 2009 +0700
5.2 +++ b/src/lf/mj/TileType.cpp Sun Nov 01 17:59:23 2009 +0700
5.3 @@ -13,9 +13,8 @@
5.4 mPos(core::vector3di(0, 0, 0)),
5.5 mRot(core::vector3df(0, 0, 0)),
5.6 mScale(core::vector3df(1, 1, 1)),
5.7 - mSelectability(true),
5.8 mSelected(false),
5.9 - mVisibility(true),
5.10 + mVisible(false),
5.11 mRMgr(CResourceManager::getInstancePtr()),
5.12 mScn(CLFRender::getInstance().getRenderWindow(0)->getRenderLayer3D()->getScene()),
5.13 mNode(0)
5.14 @@ -28,9 +27,8 @@
5.15 mPos = other->mPos;
5.16 mRot = other->mRot;
5.17 mScale = other->mScale;
5.18 - mSelectability = other->mSelectability;
5.19 mSelected = other->mSelected;
5.20 - mVisibility = other->mVisibility;
5.21 + mVisible = other->mVisible;
5.22 }
5.23 }
5.24
5.25 @@ -42,13 +40,17 @@
5.26 mPos(core::vector3di(0, 0, 0)),
5.27 mRot(other.mRot),
5.28 mScale(other.mScale),
5.29 - mSelectability(other.mSelectability),
5.30 - mSelected(other.mSelected),
5.31 - mVisibility(other.mVisibility),
5.32 + mSelected(false),
5.33 + mVisible(false),
5.34 mRMgr(other.mRMgr),
5.35 mScn(other.mScn),
5.36 mNode(0) { }
5.37
5.38 +TileType::~TileType() {
5.39 + if (mNode)
5.40 + mNode->drop();
5.41 +}
5.42 +
5.43 void TileType::create() {
5.44 res::CModel *model = new res::CModel;
5.45 res::CMaterial *mat = new res::CMaterial;
5.46 @@ -57,8 +59,7 @@
5.47 mat->setTexture(0, stub->getMaterial(0)->getTexture(0));
5.48 model->addMesh(stub->getMesh(0), mat);
5.49 mNode = new scene::CModelSceneNode(model);
5.50 - mScn->addSceneNode(mNode);
5.51 - mNode->drop();
5.52 + setVisible(true);
5.53 if (mMesh.size())
5.54 setMesh(mMesh);
5.55 if (mTex.size())
5.56 @@ -110,24 +111,16 @@
5.57
5.58 }
5.59
5.60 -void TileType::setSelectability(const bool &selectability) {
5.61 -
5.62 -}
5.63 -
5.64 void TileType::setSelected(const bool selected) {
5.65 mSelected = selected;
5.66 - if (!mNode) {
5.67 - std::cout << "no node\n";
5.68 + if (!mNode)
5.69 return;
5.70 - }
5.71 - if (mSelected) {
5.72 - std::cout << "translucent\n";
5.73 - mNode->getModel()->getMaterial(0)->setRenderFeature(render::ERPF_TRANSLUCENT_ADD);
5.74 - }
5.75 - else {
5.76 - std::cout << "diffuse map\n";
5.77 - mNode->getModel()->getMaterial(0)->setRenderFeature(render::ERPF_DIFFUSEMAP);
5.78 - }
5.79 + if (mSelected)
5.80 + mNode->getModel()->getMaterial(0)->setRenderFeature(
5.81 + render::ERPF_TRANSLUCENT_ADD);
5.82 + else
5.83 + mNode->getModel()->getMaterial(0)->setRenderFeature(
5.84 + render::ERPF_DIFFUSEMAP);
5.85 }
5.86
5.87 void TileType::setTex(const core::stringc &tex) {
5.88 @@ -147,9 +140,12 @@
5.89 }
5.90 }
5.91
5.92 -void TileType::setVisibility(const bool &visibility) {
5.93 - mVisibility = visibility;
5.94 - mNode->setVisible(mVisibility);
5.95 +void TileType::setVisible(const bool visible) {
5.96 + mVisible = visible;
5.97 + if (visible)
5.98 + mScn->addSceneNode(mNode);
5.99 + else
5.100 + mScn->removeSceneNode(mNode);
5.101 }
5.102
5.103 } // namespace mj