Add a game info menu, only halfly implemented as of now

This commit is contained in:
simon.kagstrom 2010-01-16 14:08:39 +00:00
parent f1e9694bb3
commit baa43d1cab
12 changed files with 228 additions and 47 deletions

View File

@ -14,7 +14,9 @@ widget.oo: widget.cpp widget.hh
gui.oo: gui.cpp gui.hh Makefile font.hh menu.hh sdl_ttf_font.hh \ gui.oo: gui.cpp gui.hh Makefile font.hh menu.hh sdl_ttf_font.hh \
dialogue_box.hh help_box.hh main_menu.cpp disc_menu.cpp \ dialogue_box.hh help_box.hh main_menu.cpp disc_menu.cpp \
file_browser.hh timer.hh game_info.hh widget.hh options_menu.cpp \ file_browser.hh timer.hh game_info.hh widget.hh options_menu.cpp \
network_menu.cpp theme_menu.cpp bind_keys_menu.cpp mocks/Prefs.h mocks/C64.h network_menu.cpp theme_menu.cpp bind_keys_menu.cpp game_info_menu.cpp \
game_info_box.hh \
mocks/Prefs.h mocks/C64.h \
virtual_keyboard.oo: virtual_keyboard.hh virtual_keyboard.cpp widget.hh listener.hh virtual_keyboard.oo: virtual_keyboard.hh virtual_keyboard.cpp widget.hh listener.hh

View File

@ -21,9 +21,18 @@ GameInfo::GameInfo(const char *filename,
GameInfo::GameInfo(GameInfo *gi) GameInfo::GameInfo(GameInfo *gi)
{ {
this->name = xstrdup(gi->name); if (!gi)
this->author = xstrdup(gi->author); {
this->filename = xstrdup(gi->filename); this->filename = NULL;
this->name = NULL;
this->author = NULL;
this->screenshot = NULL;
return;
}
this->name = gi->name ? xstrdup(gi->name) : NULL;
this->author = gi->author ? xstrdup(gi->author) : NULL;
this->filename = gi->filename ? xstrdup(gi->filename) : NULL;
this->screenshot = NULL; this->screenshot = NULL;
if (gi->screenshot) if (gi->screenshot)
@ -164,3 +173,15 @@ GameInfo *GameInfo::loadFromFile(const char *fileName)
return out; return out;
} }
void GameInfo::setAuthor(const char *author)
{
free((void*)this->author);
this->author = xstrdup(author);
}
void GameInfo::setName(const char *name)
{
free((void*)this->name);
this->name = xstrdup(name);
}

View File

@ -26,6 +26,10 @@ public:
~GameInfo(); ~GameInfo();
void setAuthor(const char *author);
void setName(const char *name);
void resetDefaults(); void resetDefaults();
/** Returns an allocated dump structure */ /** Returns an allocated dump structure */

View File

@ -1,3 +1,6 @@
#ifndef _GAME_INFO_BOX_H_
#define _GAME_INFO_BOX_H_
#include "menu.hh" #include "menu.hh"
#include "game_info.hh" #include "game_info.hh"
@ -14,14 +17,14 @@ public:
void setGameInfo(GameInfo *gi) void setGameInfo(GameInfo *gi)
{ {
/* Make a copy */ /* Make a copy */
if (this->gi)
delete this->gi;
this->gi = new GameInfo(gi); this->gi = new GameInfo(gi);
this->updateMessages();
} }
void loadGameInfo(const char *what) void loadGameInfo(const char *what)
{ {
this->setText(NULL);
memset(this->gi_messages, 0, sizeof(this->gi_messages));
/* Reset the current game info */ /* Reset the current game info */
if (this->gi) if (this->gi)
{ {
@ -30,27 +33,20 @@ public:
} }
/* No need to do this for directories or the special "None" field */ /* No need to do this for directories or the special "None" field */
if (strcmp(what, "None") == 0 || if ( !(strcmp(what, "None") == 0 ||
what[0] == '[') what[0] == '[') )
return; {
size_t len = strlen(Gui::gui->metadata_base_path) + strlen(what) + 6; size_t len = strlen(Gui::gui->metadata_base_path) + strlen(what) + 6;
char *tmp = (char*)xmalloc(len); char *tmp = (char*)xmalloc(len);
sprintf(tmp, "%s/%s.lra", Gui::gui->metadata_base_path, what); sprintf(tmp, "%s/%s.lra", Gui::gui->metadata_base_path, what);
/* Might return NULL, but that's OK */ /* Might return NULL, but that's OK */
this->gi = GameInfo::loadFromFile(tmp); this->gi = GameInfo::loadFromFile(tmp);
if (this->gi)
{
this->gi_messages[0] = "Game:";
this->gi_messages[1] = this->gi->name;
this->gi_messages[2] = "Author:";
this->gi_messages[3] = this->gi->author;
this->setText(this->gi_messages);
}
free(tmp); free(tmp);
} }
this->updateMessages();
}
virtual void selectCallback(int which) virtual void selectCallback(int which)
{ {
@ -66,20 +62,41 @@ public:
{ {
if (!this->gi) if (!this->gi)
return; return;
if (!this->gi->screenshot) if (this->gi->screenshot)
return; {
SDL_Rect dst; SDL_Rect dst;
/* Blit the backgrounds */ /* Blit the screenshot */
dst = (SDL_Rect){x + w / 2 - this->gi->screenshot->w / 2, y, w, h}; dst = (SDL_Rect){x + w / 2 - this->gi->screenshot->w / 2, y, w, h};
SDL_BlitSurface(this->gi->screenshot, NULL, where, &dst); SDL_BlitSurface(this->gi->screenshot, NULL, where, &dst);
Menu::draw(where, x, y + this->gi->screenshot->h + 10, Menu::draw(where, x, y + this->gi->screenshot->h + 10,
w, h - this->gi->screenshot->h - 10); w, h - this->gi->screenshot->h - 10);
} }
else
Menu::draw(where, x, y + 10, w, h - 10);
}
void updateMessages()
{
this->setText(NULL);
memset(this->gi_messages, 0, sizeof(this->gi_messages));
this->gi_messages[0] = "Game:";
this->gi_messages[1] = " ";
this->gi_messages[2] = "Author:";
this->gi_messages[3] = " ";
if (this->gi)
{
this->gi_messages[1] = this->gi->name ? this->gi->name : " ";
this->gi_messages[3] = this->gi->author ? this->gi->author : " ";
}
this->setText(this->gi_messages);
}
private:
const char *gi_messages[6]; const char *gi_messages[6];
GameInfo *gi; GameInfo *gi;
}; };
#endif /* _GAME_INFO_BOX_H_ */

115
game_info_menu.cpp Normal file
View File

@ -0,0 +1,115 @@
#include "gui.hh"
#include "menu.hh"
#include "game_info_box.hh"
#include "virtual_keyboard.hh"
class GameInfoView;
class GameInfoMenu : public Menu, public KeyboardListener
{
friend class GameInfoView;
public:
GameInfoMenu(Font *font, GameInfoBox *box) : Menu(font)
{
this->box = box;
this->setText(game_info_menu_messages);
}
virtual void stringCallback(const char *str)
{
switch (this->cur_sel)
{
case 2:
this->box->gi->setName(str);
break;
case 3:
this->box->gi->setAuthor(str);
break;
default:
panic("Cur sel is %d, not possible!\n", this->cur_sel);
break;
}
this->box->updateMessages();
}
virtual void selectCallback(int which)
{
switch (which)
{
case 0:
break;
case 2:
case 3:
VirtualKeyboard::kbd->activate();
VirtualKeyboard::kbd->registerListener(this);
break;
default:
panic("Impossible menu option\n");
break;
}
}
virtual void hoverCallback(int which)
{
}
virtual void escapeCallback(int which)
{
Gui::gui->popView();
}
private:
GameInfoBox *box;
};
class GameInfoView : public GuiView
{
public:
GameInfoView() : GuiView()
{
this->gameInfo = new GameInfoBox(Gui::gui->default_font);;
this->menu = new GameInfoMenu(Gui::gui->default_font, this->gameInfo);
}
~GameInfoView()
{
delete this->gameInfo;
delete this->menu;
}
void runLogic()
{
this->menu->runLogic();
}
void pushEvent(SDL_Event *ev)
{
this->menu->pushEvent(ev);
}
void viewPushCallback()
{
this->gameInfo->setGameInfo(Gui::gui->cur_gameInfo);
}
void draw(SDL_Surface *where)
{
SDL_Rect dst;
/* Blit the backgrounds */
dst = (SDL_Rect){20,45,300,400};
SDL_BlitSurface(Gui::gui->main_menu_bg, NULL, where, &dst);
dst = (SDL_Rect){350,13,0,0};
SDL_BlitSurface(Gui::gui->disc_info, NULL, where, &dst);
this->menu->draw(where, 50, 70, 280, 375);
this->gameInfo->draw(where, 360, 55, 262, 447);
}
protected:
GameInfoMenu *menu;
GameInfoBox *gameInfo;
};

21
gui.cpp
View File

@ -34,6 +34,7 @@ static const char *get_theme_path(const char *dir, const char *what)
#include "theme_menu.cpp" #include "theme_menu.cpp"
#include "options_menu.cpp" #include "options_menu.cpp"
#include "network_menu.cpp" #include "network_menu.cpp"
#include "game_info_menu.cpp"
#include "main_menu.cpp" #include "main_menu.cpp"
GuiView::GuiView() GuiView::GuiView()
@ -83,6 +84,8 @@ Gui::Gui()
this->metadata_base_path = METADATA_ROOT_PATH; this->metadata_base_path = METADATA_ROOT_PATH;
this->game_base_path = GAME_ROOT_PATH; this->game_base_path = GAME_ROOT_PATH;
this->cur_gameInfo = new GameInfo();
this->dlg = NULL; this->dlg = NULL;
this->kbd = NULL; this->kbd = NULL;
@ -91,6 +94,7 @@ Gui::Gui()
this->ov = NULL; this->ov = NULL;
this->nv = NULL; this->nv = NULL;
this->tv = NULL; this->tv = NULL;
this->giv = NULL;
this->bkv = NULL; this->bkv = NULL;
} }
@ -162,17 +166,9 @@ bool Gui::setTheme(const char *path)
this->nv = new NetworkView(); this->nv = new NetworkView();
this->tv = new ThemeView(); this->tv = new ThemeView();
this->bkv = new BindKeysView(); this->bkv = new BindKeysView();
this->giv = new GameInfoView();
this->pushView(mv); this->pushView(mv);
} }
else
{
this->mv->updateTheme();
this->dv->updateTheme();
this->ov->updateTheme();
this->nv->updateTheme();
this->tv->updateTheme();
this->bkv->updateTheme();
}
VirtualKeyboard::kbd->updateTheme(); VirtualKeyboard::kbd->updateTheme();
@ -317,6 +313,13 @@ 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);
} }
void Gui::updateGameInfo(GameInfo *gi)
{
panic_if(!gi, "gi must be set\n");
delete this->cur_gameInfo;
this->cur_gameInfo = gi;
}
/* The singleton/factory stuff */ /* The singleton/factory stuff */
Gui *Gui::gui; Gui *Gui::gui;

7
gui.hh
View File

@ -13,6 +13,7 @@
class DialogueBox; class DialogueBox;
class StatusBar; class StatusBar;
class GameInfo;
class MainView; class MainView;
class BindKeysView; class BindKeysView;
@ -20,6 +21,7 @@ class DiscView;
class OptionsView; class OptionsView;
class NetworkView; class NetworkView;
class ThemeView; class ThemeView;
class GameInfoView;
class VirtualKeyboard; class VirtualKeyboard;
@ -59,6 +61,8 @@ public:
return this->views[this->n_views-1]; return this->views[this->n_views-1];
} }
void updateGameInfo(GameInfo *gi);
void exitMenu(); void exitMenu();
/* These are private, keep off! */ /* These are private, keep off! */
@ -97,6 +101,7 @@ public:
DiscView *dv; DiscView *dv;
OptionsView *ov; OptionsView *ov;
NetworkView *nv; NetworkView *nv;
GameInfoView *giv;
ThemeView *tv; ThemeView *tv;
BindKeysView *bkv; BindKeysView *bkv;
@ -107,6 +112,8 @@ public:
const char *theme_base_path; const char *theme_base_path;
const char *game_base_path; const char *game_base_path;
GameInfo *cur_gameInfo;
/* New preferences */ /* New preferences */
Prefs *np; Prefs *np;

View File

@ -81,6 +81,7 @@ public:
} }
break; break;
case 9: /* Game info */ case 9: /* Game info */
Gui::gui->pushView(Gui::gui->giv);
break; break;
case 10: /* Networking */ case 10: /* Networking */
Gui::gui->pushView(Gui::gui->nv); Gui::gui->pushView(Gui::gui->nv);

View File

@ -246,3 +246,11 @@ const char **network_menu_help[] = {
NULL, NULL,
}; };
const char **game_info_menu_messages = (const char*[]){
/*00*/ "Capture game screenshot",
/*01*/ " ",
/*02*/ "Set game name",
/*02*/ "Set game author",
NULL
};

View File

@ -9,6 +9,7 @@ extern const char **bind_key_menu_messages;
extern const char **options_menu_messages; extern const char **options_menu_messages;
extern const char **options_menu_help[]; extern const char **options_menu_help[];
extern const char **game_info_menu_messages;
/* The menu messages are dynamically generated */ /* The menu messages are dynamically generated */
extern const char **network_menu_help[]; extern const char **network_menu_help[];

View File

@ -47,7 +47,7 @@ public:
SDL_Rect dst; SDL_Rect dst;
p = TTF_RenderText_Blended(this->font, msg, this->clr); p = TTF_RenderText_Blended(this->font, msg, this->clr);
panic_if(!p, "TTF error for %s: %s\n", msg, TTF_GetError()); panic_if(!p, "TTF error for '%s': %s\n", msg, TTF_GetError());
dst = (SDL_Rect){x, y, w, h}; dst = (SDL_Rect){x, y, w, h};

View File

@ -4,6 +4,7 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h>
#include <SDL.h> #include <SDL.h>
#include <SDL_ttf.h> #include <SDL_ttf.h>
@ -19,6 +20,7 @@ class Font;
fprintf(stderr, "=============PANIC PANIC PANIC===========\n"); \ fprintf(stderr, "=============PANIC PANIC PANIC===========\n"); \
fprintf(stderr, "%s:%d: ", __FILE__, __LINE__); fprintf(stderr, x); \ fprintf(stderr, "%s:%d: ", __FILE__, __LINE__); fprintf(stderr, x); \
fprintf(stderr, "=========================================\n"); \ fprintf(stderr, "=========================================\n"); \
assert(0); \
exit(1); \ exit(1); \
} while(0) } while(0)