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 MainMenu;
class MainView; class MainView;
#define THEME_ROOT_PATH "themes"
static const char *get_theme_path(const char *dir, const char *what) static const char *get_theme_path(const char *dir, const char *what)
{ {
static char buf[255]; static char buf[255];
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
snprintf(buf, 254, "%s/%s", snprintf(buf, 254, "%s/%s/%s",
dir, what); THEME_ROOT_PATH, dir, what);
return buf; 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); 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; GuiView **views;
int n_views; int n_views;
/* Singleton */
static void init();
static Gui *gui;
}; };
#endif /* GUI_HH */ #endif /* GUI_HH */

View File

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