Refactor gui to singleton factory, stage 1

This commit is contained in:
simon.kagstrom 2009-12-13 08:54:25 +00:00
parent e50b1a1045
commit 337921d2ab
3 changed files with 27 additions and 10 deletions

19
gui.cpp
View File

@ -15,13 +15,15 @@ class Gui;
class MainMenu;
class MainView;
#define THEME_ROOT_PATH "themes"
static const char *get_theme_path(const char *dir, const char *what)
{
static char buf[255];
memset(buf, 0, sizeof(buf));
snprintf(buf, 254, "%s/%s",
dir, what);
snprintf(buf, 254, "%s/%s/%s",
THEME_ROOT_PATH, dir, what);
return buf;
}
@ -202,3 +204,16 @@ Font *Gui::loadThemeFont(const char *dir, const char *what, int size)
return new Font_TTF(fnt, 255,255,255);
}
/* The singleton/factory stuff */
Gui *Gui::gui;
void Gui::init()
{
Gui *p = new Gui();
/* Set the default theme */
panic_if(!p->setTheme("default"),
"Setting default theme failed\n");
Gui::gui = p;
}

5
gui.hh
View File

@ -83,6 +83,11 @@ public:
GuiView **views;
int n_views;
/* Singleton */
static void init();
static Gui *gui;
};
#endif /* GUI_HH */

View File

@ -5,7 +5,6 @@
#include "utils.hh"
SDL_Surface *screen;
static Gui *g_gui;
static void run(void)
{
@ -17,10 +16,10 @@ static void run(void)
if (ev.type == SDL_QUIT)
exit(1);
g_gui->pushEvent(&ev);
Gui::gui->pushEvent(&ev);
}
g_gui->runLogic();
g_gui->draw(screen);
Gui::gui->runLogic();
Gui::gui->draw(screen);
SDL_Flip(screen);
SDL_Delay(50);
@ -34,10 +33,8 @@ static void init(void)
panic_if(!screen, "Cannot initialize video: %s\n", SDL_GetError());
TTF_Init();
g_gui = new Gui();
panic_if(!g_gui->setTheme("themes/default"),
"Setting theme failed\n");
g_gui->activate();
Gui::init();
Gui::gui->activate();
}
int main(int argc, char *argv[])