1.1 --- a/CMakeLists.txt Sun Nov 01 19:55:42 2009 +0700
1.2 +++ b/CMakeLists.txt Fri Nov 06 00:24:26 2009 +0700
1.3 @@ -8,6 +8,8 @@
1.4 SET(MJ_SRC
1.5 src/lf/mj/TileType.cpp
1.6 src/lf/mj/Layout.cpp
1.7 + src/lf/mj/Settings.cpp
1.8 + src/lf/mj/GUI.cpp
1.9 src/lf/mj/Core.cpp
1.10 src/main.cpp
1.11 )
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/README Fri Nov 06 00:24:26 2009 +0700
2.3 @@ -0,0 +1,14 @@
2.4 +Dependencies.
2.5 +
2.6 +Linux and Windows
2.7 +-------------------------------------------------
2.8 +
2.9 +CMake >= 2.6
2.10 +Lightfeather >= 2009-11-05
2.11 +Perl5
2.12 +
2.13 +Windows
2.14 +-------------------------------------------------
2.15 +
2.16 +MinGW >= 5.1.6
2.17 +
3.1 --- a/include/lf/mj/Core.h Sun Nov 01 19:55:42 2009 +0700
3.2 +++ b/include/lf/mj/Core.h Fri Nov 06 00:24:26 2009 +0700
3.3 @@ -2,7 +2,10 @@
3.4 #ifndef LF_MJ_CORE_H
3.5 #define LF_MJ_CORE_H
3.6
3.7 +#include <lf/mj/GUI.h>
3.8 +#include <lf/mj/Settings.h>
3.9 #include <lf/mj/TileType.h>
3.10 +
3.11 #include <lf/Lightfeather.h>
3.12 #include <map>
3.13 #include <vector>
3.14 @@ -13,7 +16,10 @@
3.15 class TileTemplate;
3.16
3.17 class Core : public input::IKeyListener,
3.18 - public input::IMouseListener {
3.19 + public input::IMouseListener,
3.20 + public gui::IGUIActionListener,
3.21 + public core::CRTTI,
3.22 + public core::CSingleton<Core> {
3.23 public:
3.24 Core();
3.25 ~Core();
3.26 @@ -23,9 +29,9 @@
3.27 void lighting();
3.28 bool loadField(const core::stringc &path);
3.29 void mousePressed(input::CMouseEvent &e);
3.30 + void performAction(gui::CGUIActionEvent &e);
3.31 void run();
3.32 u32 turnsLeft() const;
3.33 - void ui();
3.34
3.35 private:
3.36 render::IRenderWindow *mWin;
3.37 @@ -45,8 +51,8 @@
3.38 typedef Tiles::const_iterator TIt;
3.39 Tiles mTiles;
3.40 TileTemplate *mTmpl;
3.41 - gui::CGUILabel *mLbFPSPolys;
3.42 - gui::CGUILabel *mLbTurnsLeft;
3.43 + GUI *mGUI;
3.44 + Settings *mSet;
3.45 };
3.46
3.47 } // namespace lf
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/include/lf/mj/GUI.h Fri Nov 06 00:24:26 2009 +0700
4.3 @@ -0,0 +1,56 @@
4.4 +
4.5 +#ifndef LF_MJ_GUI_H
4.6 +#define LF_MJ_GUI_H
4.7 +
4.8 +#include <lf/mj/Settings.h>
4.9 +
4.10 +#include <lf/Lightfeather.h>
4.11 +
4.12 +namespace lf {
4.13 +namespace mj {
4.14 +
4.15 +class Core;
4.16 +
4.17 +enum E_GUI_COMMAND_ID {
4.18 + EGCI_SETTINGS = 0,
4.19 + EGCI_ABOUT,
4.20 + EGCI_QUIT
4.21 +};
4.22 +
4.23 +const u32 GUI_CELL_HEIGHT = 50;
4.24 +const u32 GUI_CELL_WIDTH = 200;
4.25 +const u32 GUI_CELL_BORDER = 10;
4.26 +
4.27 +class GUI : public core::CRTTI,
4.28 + public CRefCounted {
4.29 + public:
4.30 + GUI(const core::stringc &theme);
4.31 + ~GUI();
4.32 +
4.33 + bool isMenuVisible() const {
4.34 + return mMenuVisible;
4.35 + }
4.36 + void setFPSPolys(u32 fps, u32 polys);
4.37 + void setMenuVisible(bool state);
4.38 + void setTurnsLeft(u32 turns);
4.39 +
4.40 + private:
4.41 + Core *mCore;
4.42 + gui::CGUIManager *mGMgrGame;
4.43 + gui::CGUIManager *mGMgrMenu;
4.44 + Settings *mSet;
4.45 + gui::CGUILabel *mLbFPSPolys;
4.46 + gui::CGUILabel *mLbTurnsLeft;
4.47 + // Menu.
4.48 + gui::CGUISizer *mSzMenu;
4.49 + gui::CGUIButton *mBtnSettings;
4.50 + gui::CGUIButton *mBtnAbout;
4.51 + gui::CGUIButton *mBtnQuit;
4.52 + bool mMenuVisible;
4.53 +};
4.54 +
4.55 +} // namespace lf
4.56 +} // namespace mj
4.57 +
4.58 +#endif // LF_MJ_GUI_H
4.59 +
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/include/lf/mj/Settings.h Fri Nov 06 00:24:26 2009 +0700
5.3 @@ -0,0 +1,28 @@
5.4 +
5.5 +#ifndef LF_MJ_SETTINGS_H
5.6 +#define LF_MJ_SETTINGS_H
5.7 +
5.8 +#include <lf/Lightfeather.h>
5.9 +
5.10 +namespace lf {
5.11 +namespace mj {
5.12 +
5.13 +class Settings : public core::CSingleton<Settings>,
5.14 + public core::CRTTI {
5.15 + public:
5.16 + Settings();
5.17 + ~Settings();
5.18 + core::vector2di getScreenResolution() const;
5.19 + void load();
5.20 + void save();
5.21 + void setScreenResolution(const core::vector2di &res);
5.22 +
5.23 + private:
5.24 + mutable core::CProperties mProp;
5.25 +};
5.26 +
5.27 +} // namespace lf
5.28 +} // namespace mj
5.29 +
5.30 +#endif // LF_MJ_SETTINGS_H
5.31 +
6.1 --- a/src/lf/mj/Core.cpp Sun Nov 01 19:55:42 2009 +0700
6.2 +++ b/src/lf/mj/Core.cpp Fri Nov 06 00:24:26 2009 +0700
6.3 @@ -8,19 +8,23 @@
6.4 namespace lf {
6.5 namespace mj {
6.6
6.7 +template<> Core *core::CSingleton<Core>::p_Singleton = 0;
6.8 +
6.9 Core::Core() :
6.10 mWin(0), mRMgr(0), mScn(0), mCam(0),
6.11 mSelectedTile(0),
6.12 mFPS(0), mPolys(0),
6.13 - mTmpl(0) {
6.14 + mTmpl(0),
6.15 + mGUI(0) {
6.16 + mSet = new Settings;
6.17 mWin = CLFRender::getInstance().createRenderWindow(
6.18 core::vector2di(0, 0),
6.19 - core::vector2d<s32>(1024, 768),
6.20 + mSet->getScreenResolution(),
6.21 32,
6.22 32,
6.23 render::EWCF_AUTOCLOSE | render::EWCF_OPPL_PIPELINE,
6.24 render::EAAF_NONE);
6.25 - mWin->setWindowCaption(L"Mahjongg by Opensource game studio");
6.26 + mWin->setWindowCaption(L"OGS Mahjong 0.2");
6.27 mWin->setVisible(true);
6.28 mWin->addKeyListener(this);
6.29 mWin->addMouseListener(this);
6.30 @@ -61,12 +65,15 @@
6.31 mTexs.push_back("tile03.png");
6.32 mTexs.push_back("tile04.png");
6.33 mRMgr->loadResources("res/lfm/tile.lfm");
6.34 - ui();
6.35 + mGUI = new GUI("res/gui/clearlooks.lfgt");
6.36 }
6.37
6.38 Core::~Core() {
6.39 if (mTmpl)
6.40 mTmpl->drop();
6.41 + mGUI->drop();
6.42 + // Should be the last.
6.43 + delete mSet;
6.44 }
6.45
6.46 bool Core::canSelect(const TileType *tile) const {
6.47 @@ -135,67 +142,13 @@
6.48 } // switch(cControllerType)
6.49 break;
6.50 case input::KEY_ESCAPE:
6.51 - mWin->closeWindow();
6.52 + mGUI->setMenuVisible(!mGUI->isMenuVisible());
6.53 break;
6.54 default:
6.55 break;
6.56 }
6.57 }
6.58
6.59 -void Core::mousePressed(input::CMouseEvent &e) {
6.60 - if (e.getButton() != 1)
6.61 - return;
6.62 - // Deselect.
6.63 - if (mSelectedTile)
6.64 - mSelectedTile->setSelected(false);
6.65 - // Select
6.66 - scene::APickingResults pickResults;
6.67 - core::line3df line = mCam->getPickingLine(
6.68 - mWin->getCursorControl()->getRelativePosition().X,
6.69 - mWin->getCursorControl()->getRelativePosition().Y);
6.70 - mScn->getRootSceneNode()->pick(
6.71 - line, true, true, mCam, false, pickResults);
6.72 - TileType *newTile = 0;
6.73 - // There's always only one selected node.
6.74 - if (!pickResults.empty() && mHNT.find(pickResults[0]->node) != mHNT.end())
6.75 - newTile = mHNT[pickResults[0]->node];
6.76 - // Do not select, if it was previously selected.
6.77 - if (mSelectedTile && newTile == mSelectedTile) {
6.78 - mSelectedTile = 0;
6.79 - return;
6.80 - }
6.81 - // If the tiles are in the same group, remove them from scene.
6.82 - // Otherwise just select new one.
6.83 - if (newTile && canSelect(newTile)) {
6.84 - if (mSelectedTile &&
6.85 - newTile->getGroup() == mSelectedTile->getGroup()) {
6.86 - mSelectedTile->setVisible(false);
6.87 - newTile->setVisible(false);
6.88 - mSelectedTile = 0;
6.89 - // Turn.
6.90 - u32 turns = turnsLeft();
6.91 - if (turns > 0)
6.92 - mLbTurnsLeft->setText(core::stringw("Turns left: ") +
6.93 - core::stringw(turns));
6.94 - else {
6.95 - u32 count = 0;
6.96 - for (u32 i = 0; i < mTiles.size(); i++)
6.97 - if (mTiles[i]->getVisible())
6.98 - count++;
6.99 - if (count)
6.100 - mLbTurnsLeft->setText(L"You lose");
6.101 - else
6.102 - mLbTurnsLeft->setText(L"You win");
6.103 - }
6.104 - return;
6.105 - }
6.106 - newTile->setSelected(true);
6.107 - mSelectedTile = newTile;
6.108 - }
6.109 - else
6.110 - mSelectedTile = 0;
6.111 -}
6.112 -
6.113 void Core::lighting() {
6.114 // Lighting state.
6.115 render::CRenderStateLighting *rsl =
6.116 @@ -306,20 +259,85 @@
6.117 mTiles.push_back(inst);
6.118 }
6.119 mTmpl->setRot(core::vector3df(0, 180, 0));
6.120 - mLbTurnsLeft->setText(core::stringw("Turns left: ") +
6.121 - core::stringw(turnsLeft()));
6.122 + mGUI->setTurnsLeft(turnsLeft());
6.123 return true;
6.124 }
6.125
6.126 +void Core::mousePressed(input::CMouseEvent &e) {
6.127 + if (mGUI->isMenuVisible() || e.getButton() != 1)
6.128 + return;
6.129 + e.consume();
6.130 + // Deselect.
6.131 + if (mSelectedTile)
6.132 + mSelectedTile->setSelected(false);
6.133 + // Select
6.134 + scene::APickingResults pickResults;
6.135 + core::line3df line = mCam->getPickingLine(
6.136 + mWin->getCursorControl()->getRelativePosition().X,
6.137 + mWin->getCursorControl()->getRelativePosition().Y);
6.138 + mScn->getRootSceneNode()->pick(
6.139 + line, true, true, mCam, false, pickResults);
6.140 + TileType *newTile = 0;
6.141 + // There's always only one selected node.
6.142 + if (!pickResults.empty() && mHNT.find(pickResults[0]->node) != mHNT.end())
6.143 + newTile = mHNT[pickResults[0]->node];
6.144 + // Do not select, if it was previously selected.
6.145 + if (mSelectedTile && newTile == mSelectedTile) {
6.146 + mSelectedTile = 0;
6.147 + return;
6.148 + }
6.149 + // If the tiles are in the same group, remove them from scene.
6.150 + // Otherwise just select new one.
6.151 + if (newTile && canSelect(newTile)) {
6.152 + if (mSelectedTile &&
6.153 + newTile->getGroup() == mSelectedTile->getGroup()) {
6.154 + mSelectedTile->setVisible(false);
6.155 + newTile->setVisible(false);
6.156 + mSelectedTile = 0;
6.157 + // Turn.
6.158 + u32 turns = turnsLeft();
6.159 + mGUI->setTurnsLeft(turns);
6.160 + if (!turns) {
6.161 + u32 count = 0;
6.162 + for (u32 i = 0; i < mTiles.size(); i++)
6.163 + if (mTiles[i]->getVisible())
6.164 + count++;
6.165 + if (count)
6.166 + std::cout << "You lose\n";
6.167 + //mLbTurnsLeft->setText(L"You lose");
6.168 + else
6.169 + std::cout << "You win\n";
6.170 + //mLbTurnsLeft->setText(L"You win");
6.171 + }
6.172 + return;
6.173 + }
6.174 + newTile->setSelected(true);
6.175 + mSelectedTile = newTile;
6.176 + }
6.177 + else
6.178 + mSelectedTile = 0;
6.179 +}
6.180 +
6.181 +void Core::performAction(gui::CGUIActionEvent &e) {
6.182 + switch (e.getCommandID()) {
6.183 + case EGCI_SETTINGS:
6.184 + case EGCI_ABOUT:
6.185 + case EGCI_QUIT:
6.186 + e.consume();
6.187 + mWin->closeWindow();
6.188 + break;
6.189 + default:
6.190 + break;
6.191 + }
6.192 +}
6.193 +
6.194 void Core::run() {
6.195 bool quit = !loadField("res/layout/pirates.layout");
6.196 while (!quit) {
6.197 u32 fps = mWin->getOneSecondFPS();
6.198 u32 polys = mWin->getTotalPolygonCount();
6.199 if (fps != mFPS || polys != mPolys) {
6.200 - wchar_t tmp[256];
6.201 - snwprintf(tmp, 256, L"FPS: %u Polys: %u", fps, polys);
6.202 - mLbFPSPolys->setText(tmp);
6.203 + mGUI->setFPSPolys(fps, polys);
6.204 mFPS = fps;
6.205 mPolys = polys;
6.206 }
6.207 @@ -340,26 +358,6 @@
6.208 return count;
6.209 }
6.210
6.211 -void Core::ui() {
6.212 - mRMgr->loadResources("res/gui/clearlooks.lfgt");
6.213 - gui::CGUIManager *gmgr = new gui::CGUIManager;
6.214 - gmgr->getMousePointer()->setVisible(false);
6.215 - mWin->addGUIManager(gmgr);
6.216 - gmgr->drop();
6.217 -
6.218 - mLbFPSPolys = new gui::CGUILabel;
6.219 - mLbFPSPolys->setPosition(5, 5);
6.220 - mLbFPSPolys->setSize(200, 30);
6.221 - gmgr->addChild(mLbFPSPolys);
6.222 - mLbFPSPolys->drop();
6.223 -
6.224 - mLbTurnsLeft = new gui::CGUILabel;
6.225 - mLbTurnsLeft->setPosition(5, 40);
6.226 - mLbTurnsLeft->setSize(200, 30);
6.227 - gmgr->addChild(mLbTurnsLeft);
6.228 - mLbTurnsLeft->drop();
6.229 -}
6.230 -
6.231 } // namespace lf
6.232 } // namespace mj
6.233
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/src/lf/mj/GUI.cpp Fri Nov 06 00:24:26 2009 +0700
7.3 @@ -0,0 +1,90 @@
7.4 +
7.5 +#include <lf/mj/GUI.h>
7.6 +#include <lf/mj/Core.h>
7.7 +
7.8 +namespace lf {
7.9 +namespace mj {
7.10 +
7.11 +GUI::GUI(const core::stringc &theme) :
7.12 + mCore(Core::getInstancePtr()),
7.13 + mSet(Settings::getInstancePtr()),
7.14 + mMenuVisible(false) {
7.15 + setObjectName("mjGUI");
7.16 + CResourceManager::getInstance().loadResources(theme.c_str());
7.17 + mGMgrGame = new gui::CGUIManager;
7.18 + mGMgrGame->getMousePointer()->setVisible(false);
7.19 + CLFRender::getInstance().getRenderWindow(0)->addGUIManager(mGMgrGame);
7.20 + mGMgrGame->drop();
7.21 + mGMgrMenu = new gui::CGUIManager;
7.22 + mGMgrMenu->getMousePointer()->setVisible(false);
7.23 + setMenuVisible(true);
7.24 +
7.25 + mLbFPSPolys = new gui::CGUILabel;
7.26 + mLbFPSPolys->setPosition(5, 5);
7.27 + mLbFPSPolys->setSize(200, 30);
7.28 + mGMgrGame->addChild(mLbFPSPolys);
7.29 + mLbFPSPolys->drop();
7.30 +
7.31 + mLbTurnsLeft = new gui::CGUILabel;
7.32 + mLbTurnsLeft->setPosition(5, 40);
7.33 + mLbTurnsLeft->setSize(200, 30);
7.34 + mGMgrGame->addChild(mLbTurnsLeft);
7.35 + mLbTurnsLeft->drop();
7.36 + // Menu.
7.37 + mSzMenu = new gui::CGUISizer(gui::EGSO_VERTICAL,
7.38 + GUI_CELL_WIDTH,
7.39 + GUI_CELL_BORDER);
7.40 + core::vector2di res = mSet->getScreenResolution();
7.41 + mSzMenu->setSize(GUI_CELL_WIDTH, int(GUI_CELL_HEIGHT * 3 + 20));
7.42 + mSzMenu->setPosition(int(res.X / 2 - GUI_CELL_WIDTH / 2),
7.43 + int(res.Y / 2 - (GUI_CELL_HEIGHT * 3 + 20) / 2));
7.44 + mGMgrMenu->addChild(mSzMenu);
7.45 + mSzMenu->drop();
7.46 + mBtnSettings = new gui::CGUIButton(L"Settings", EGCI_SETTINGS);
7.47 + mBtnSettings->addActionListener(mCore);
7.48 + mSzMenu->addChild(mBtnSettings, 2);
7.49 + mBtnSettings->drop();
7.50 + mBtnAbout = new gui::CGUIButton(L"About", EGCI_ABOUT);
7.51 + mBtnAbout->addActionListener(mCore);
7.52 + mSzMenu->addChild(mBtnAbout, 1);
7.53 + mBtnAbout->drop();
7.54 + mBtnQuit = new gui::CGUIButton(L"Quit", EGCI_QUIT);
7.55 + mBtnQuit->addActionListener(mCore);
7.56 + mSzMenu->addChild(mBtnQuit, 1);
7.57 + mBtnQuit->drop();
7.58 +}
7.59 +
7.60 +GUI::~GUI() {
7.61 + /*
7.62 + Doesn't matter when window is closed.
7.63 + if (mMenuVisible)
7.64 + setMenuVisible(false);
7.65 + */
7.66 + mGMgrMenu->drop();
7.67 +}
7.68 +
7.69 +void GUI::setFPSPolys(u32 fps, u32 polys) {
7.70 + static wchar_t buf[128];
7.71 + snwprintf(buf, 128, L"FPS: %u Polys: %u", fps, polys);
7.72 + mLbFPSPolys->setText(buf);
7.73 +}
7.74 +
7.75 +void GUI::setMenuVisible(bool state) {
7.76 + if (mMenuVisible == state)
7.77 + return;
7.78 + mMenuVisible = state;
7.79 + if (state)
7.80 + CLFRender::getInstance().getRenderWindow(0)->addGUIManager(mGMgrMenu);
7.81 + else
7.82 + CLFRender::getInstance().getRenderWindow(0)->removeGUIManager(
7.83 + mGMgrMenu);
7.84 +}
7.85 +
7.86 +void GUI::setTurnsLeft(u32 turns) {
7.87 + mLbTurnsLeft->setText(core::stringw("Turns left: ") +
7.88 + core::stringw(turns));
7.89 +}
7.90 +
7.91 +} // namespace lf
7.92 +} // namespace mj
7.93 +
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/src/lf/mj/Settings.cpp Fri Nov 06 00:24:26 2009 +0700
8.3 @@ -0,0 +1,38 @@
8.4 +
8.5 +#include <lf/mj/Settings.h>
8.6 +
8.7 +namespace lf {
8.8 +namespace mj {
8.9 +
8.10 +template<> Settings *core::CSingleton<Settings>::p_Singleton = 0;
8.11 +
8.12 +Settings::Settings() {
8.13 + setObjectName("mjSettings");
8.14 + load();
8.15 +}
8.16 +Settings::~Settings() {
8.17 + save();
8.18 +}
8.19 +
8.20 +core::vector2di Settings::getScreenResolution() const {
8.21 + return core::vector2di(mProp.getProperty("screenWidth", 640),
8.22 + mProp.getProperty("screenHeight", 480));
8.23 +}
8.24 +
8.25 +void Settings::load() {
8.26 + // File may not exist, LF handles that fine.
8.27 + mProp.readFromFile("mjrc");
8.28 +}
8.29 +
8.30 +void Settings::save() {
8.31 + mProp.writeToFile("mjrc");
8.32 +}
8.33 +
8.34 +void Settings::setScreenResolution(const core::vector2di &res) {
8.35 + mProp.setProperty("screenWidth", res.X);
8.36 + mProp.setProperty("screenHeight", res.Y);
8.37 +}
8.38 +
8.39 +} // namespace lf
8.40 +} // namespace mj
8.41 +