Using Games Start-up Screen
This document has been reviewed for maemo 4.0.
Introduction
The osso-games-startup application is a generic game start-up interface, providing a common hildonized user interface view for game startup control and configuration.
Figure 1. Basic osso-games-startup application screen
Note that the older maemo-games-startup has been replaced by osso-games-startup, which this document covers.
Application Functionality
In order to execute the game, the osso-games-startup application should be called. The game configuration file should be passed as an argument. Once loaded, osso-games-startup will create a common interface for all games (see Figure 1) and, if needed, will load a specific plug-in for each game. Games are activated through an auto-activating D-BUS message, which tells the game either to start, restart or to continue. In cases where no cleanup routine within the plug-in exist, it can also start the game to clean its state data. In these cases the game usually does not open its own window but kills the state data in the background instead.
When the Play button is pressed, the D-BUS service defined in the configuration file will be executed. Games that do not use glib mainloop must integrate some functionality to their mainloop (for more information about games without a glib mainloop see Integrating Non-GTK+ Games).
The service is called every time communication between osso-games-startup and the game is needed.
Figure 2. The Games Start-up screen with a simple plug-in containing difficulty level and sound configuration
Activity Diagram
The following diagram illustrates the operational execution flow of the osso-games-startup application.
Figure 3. Execution flow of the osso-games-startup application
Integration
The integration tasks depend on the toolkit the game is based on. The following sections describe the integration tasks for:
- Games based on GTK+
- Games based on other toolkits, such as SDL
Integrating GTK+ Games
To integrate games based on GTK+, a configuration file should be created, defining the information necessary for the osso-games-startup application. The following example illustrates the contents of the configuration file:
[Startup Entry] # the name of the application Name=example # the current version of the application Version=0.2.0 # the title that will be used if not specified by the GettextPackage Title=Example # the GettextPackage to be used to locate the title string GettextPackage=example # if the TitleId is defined, it will search for it inside the gettext package TitleId=example_title # if your game has a plug-in PluginPath=/usr/share/example/example_plugin.so # the games-startup screen image Image=/usr/share/example/pixmaps/example.png # the D-BUS service, path and interface names ServiceName=com.domain.example PathName=/com/domain/example InterfaceName=com.domain.example
The game should be integrated with libosso to enable it to receive and send D-BUS messages such as "pause", "restart" and "continue". In order to receive messages from the osso-games-startup application, the game must register the service as defined in the configuration file.
When sending messages to osso-games-startup, the game must use the service, path and interface name defined in the configuration file, but with a "_startup" suffix.
Then a D-BUS service (a .service file) should be created, using the name of the service that executes the game binary, as defined in the configuration file. The following example illustrates the contents of the file:
[D-BUS Service] Name=com.domain.example Exec=/usr/games/wrapper/games/wrapper.l
Finally, the game must create a shell script that calls the osso-games-startup application executable, and passes the game configuration file as an argument. The following example illustrates the contents of the script:
/usr/bin/osso_games_startup /usr/share/example/example.conf
Integrating Non-GTK+ Games
In order to integrate non-GTK+ applications to osso-games-startup, the only thing different is the actual game implementation. The same steps should be followed to create osso desktop files and registering dbus services as for GTK+ based games, the only difference is that Nokia is providing a hildon-games-wrapper library to deal with dbus messaging without the use of libosso.
To initialize game application to receive events from osso-games-startup, the developer can use following approach:
#include <gw/hgw.h> /* snip */ HgwContext *hgw; hgw = hgw_context_init();
If and when hgw pointer is not null, the game is ready to receive dbus messages which are intended for the game application. The next step is to check what state the game was started into. This happens by calling hgw_context_get_start_command(). Return values indicate the state the game should be going into. This is necessery, for example, when the game has been paused previously, the state of the game has been saved and the player wishes to continue the current game or restart the on-going game. Again, check the header file for HgwStartCommand for possible start values.
In the game's event loop the user has to check if events are available by calling hgw_msg_check_incoming(). It will return HGW_ERR_COMMUNICATION if there are messages available and other values if there are not or if there was error. (see hgw/hgw.h for more details about HgwError enum) Example:
/* in game event polling */ HgwMessage *msg; HgwMessageFlags flags; if ( hgw_msg_check_incoming(hgw,msg,flags) == HGW_ERR_COMMUNICATION ) { /* Message Incoming, process msg */ }
Pointer *msg will then hold information about the received dbus message and the game application should then be able to process and act accordingly. Actual information about the received events is stored in msg->e_val which is an enumeration mapped against HgwCallback.
When the game ends or the game is requested to pause or quit, call to hgw_context_destroy() should be made with initialized hgw as a parameter.
Also, Hildon-games-wrapper library provides an interface to gconf. Gconf should be used to store persistent data like the level of difficulty, sounds on/off and so forth. This way is preferred because the data stored in gconf is automatically backed up and restored when user so chooses (from desktop, backup manager). However, this interface is read-only, as writing should be happening in the game-specific plug-in in the osso-games-startup screen.
Creating Game-Specific Plug-in
Each game can create a plug-in for specific settings, such as sound control or difficulty level (see Figure 2). Basically, each plug-in must implement some pre-defined functions that are executed by the osso-games-startup application.
The functions that can be implemented by each plug-in are:
-
static GtkWidget *load_plugin (void)
This function creates the game-specific plug-in.
-
static void unload_plugin (void)
This function destroys global variables, if necessary.
-
static void write_config (void)
This function saves the game configuration chosen by the player using the plug-in options.
If the game needs a submenu in the osso-games-startup screen main menu (see Figure 1), the following functions must be used:
-
static GtkWidget **load_menu (guint *)
This function creates the game-specific submenu that is added to the osso-games-startup main menu.
-
static void update_menu (void)
This function updates the game-specific menu.
The struct below must be filled and sent to the osso-games-startup application. It specifies which plug-in functions the start-up must call.
static StartupPluginInfo plugin_info = { GtkWidget * (* load) (void); void (* unload) (void); void (* write_config) (void); GtkWidget ** (* load_menu) (guint *); void (* update_menu) (void); void (* plugin_cb) (GtkWidget *menu_item, gpointer cb_data); };
The last item on the struct is necessary only if the game plug-in requires items such as "save", "save as" or "open" to be a submenu in the default Game submenu.
Each plug-in must contain a reference to the osso-games-startup info. The reference is given when STARTUP_INIT_PLUGIN is called. The following example illustrates a reference to osso-games-startup:
static GameStartupInfo gs;
The following example illustrates the STARTUP_INIT_PLUGIN that initializes the plug-in. The parameters, in the order they are shown, are:
- Pointer for plug-in information (see the struct shown above)
- GameStartupInfo
- Definition on whether the osso-games-startup should send a D-BUS message on closing
- Definition on whether the osso-games-startup menu has open/save game options
STARTUP_INIT_PLUGIN(StartupPluginInfo, GameStartupInfo, gboolean, gboolean)
In order to inform osso-games-startup that the game has a plug-in, the .conf configuration file of the game must include the PluginPath entry, such as PluginPath=@datadir@/game01/game01_plugin.so.
Building Example Plug-in for Game01
This section illustrates the plug-in for the Game01 implementation. The plug-in allows the player to set the initial level of the game, and to define whether the game uses sounds.
Figure 4. Games Start-up screen with the Game01 plug-in's level and sound configuration
The following code examples illustrate how the Games Start-up screen works, but the best way to learn to use it is to tinker with the game itself. The source code can be used as wished: as a basic skeleton for your game, or simply to gain a better understanding of the game start-up.
Since the plug-in and osso-games-startup are written with GTK+-2.0, they must include startup_plugin.h from osso-games-startup. In addition, Gconf is used to save the user settings.
#include <stdio.h> #include <stdlib.h> #include <gtk/gtk.h> #include <startup_plugin.h> #include <gconf/gconf.h> #include <gconf/gconfÂclient.h> #define MENU_SOUND 15
The following example illustrates the labels for retrieving information at GConf:
#define SETTINGS_LEVEL "/apps/osso/game01/level" #define SETTINGS_SOUND "/apps/osso/game01/sound"
The following example illustrates the functions that are implemented:
static GtkWidget *load_plugin (void); static void unload_plugin (void); static void write_config (void); static GtkWidget **load_menu (guint *); static void update_menu (void); static void difficulty_callback (GtkWidget *widget, gpointer data); static void plugin_callback (GtkWidget *menu_item, gpointer data);
The following example illustrates some global variables:
GConfClient *gcc = NULL; GtkWidget *board_box; GtkWidget *level_1_item; GtkWidget *level_2_item; GtkWidget *level_3_item; GtkWidget *level_4_item; GtkWidget *level_5_item; GtkWidget *level_6_item; GtkWidget *level_7_item; GtkWidget *level_8_item; GtkWidget *level_9_item; GtkWidget *level_10_item; GtkWidget *level_11_item; GtkWidget *level_12_item; GtkWidget *settings_item; GtkWidget *settings_menu; GtkWidget *difficulty_item; GtkWidget *difficulty_menu; static GameStartupInfo gs; GtkWidget *menu_items[2]; static int changed = FALSE; GSList * group = NULL; GtkWidget *sound_check = NULL; GtkWidget *sound_item;
The implemented functions of the plug-in must be sent to osso-games-startup: in this case, a GTK_SPIN_BUTTON and a GTK_CHECK_ITEM. If the plug-in has no specific menu, load_menu and update_menu must be NULL.
static StartupPluginInfo plugin_info = { load_plugin, unload_plugin, write_config, load_menu, update_menu, NULL };
The following example illustrates the initializing plug-in that informs the application that there is a plug-in:
STARTUP_INIT_PLUGIN(plugin_info, gs, FALSE, FALSE);
The following example illustrates the function that initializes the widgets that localize the osso-games-startup standard buttons:
static GtkWidget * load_plugin (void) { int board, enable_sound; GtkWidget *game_vbox, *game_hbox; GtkWidget *board_hbox, *board_label; GtkWidget *sound_hbox, *sound_label; g_type_init(); gcc = gconf_client_get_default(); board = gconf_client_get_int(gcc, SETTINGS_LEVEL, NULL); enable_sound = gconf_client_get_int(gcc, SETTINGS_SOUND, NULL); game_vbox = gtk_vbox_new (TRUE, 0); game_hbox = gtk_hbox_new (TRUE, 0); g_assert (game_hbox); g_assert (game_vbox); board_hbox = gtk_hbox_new (FALSE, 4); board_box = gtk_spin_button_new_with_range (1, 12, 1); g_assert (board_box); gtk_spin_button_set_value (GTK_SPIN_BUTTON (board_box), board); gtk_spin_button_set_numeric(GTK_SPIN_BUTTON (board_box), TRUE); gtk_widget_set_size_request (board_box, 200, -1); g_signal_connect(G_OBJECT(board_box), "changed", G_CALLBACK(settings_callback), NULL); board_label = gtk_label_new ("Starting level"); g_assert(board_label); sound_label = gtk_label_new ("Sound"); g_assert (sound_label); sound_check = gtk_check_button_new(); g_assert (sound_check); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(sound_check), enable_sound); gtk_box_pack_end (GTK_BOX (board_hbox), sound_check, FALSE, FALSE, 0); gtk_box_pack_end (GTK_BOX (board_hbox), sound_label, TRUE, TRUE, 0); gtk_box_pack_end (GTK_BOX (board_hbox), board_box, FALSE, FALSE, 0); gtk_box_pack_end (GTK_BOX (board_hbox), board_label, FALSE, FALSE, 0); gtk_box_pack_start (GTK_BOX (game_hbox),board_hbox, FALSE, FALSE, 2); gtk_box_pack_start (GTK_BOX (game_vbox), game_hbox, FALSE, FALSE, 2); g_signal_connect (G_OBJECT(sound_check), "clicked", G_CALLBACK(sound_callback), NULL); return game_vbox; }
The following example illustrates the function that frees global variables at the end of plug-in execution:
static void unload_plugin (void) { free(gcc); free(board_box); ... }
The following example illustrates the function that is responsible for using Gconf to store the user preferences (as is recommended, since the back-up application stores the GConf database):
static void write_config (void) { gconf_client_set_int(gcc, SETTINGS_LEVEL, gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON(board_box)), NULL); gconf_client_set_int(gcc, SETTINGS_SOUND, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(sound_check)), NULL); }
The following example illustrates the function that initializes the game-specific plug-in menu, which is between the Game and Close submenus of osso-games-startup:
static GtkWidget **load_menu (guint *nitems) { int enable_sound; //number of entries in maemo-games-startup main menu for this game *nitems = 1; settings_item = gtk_menu_item_new_with_label ("Settings"); settings_menu = gtk_menu_new (); menu_items[0] = settings_item; gtk_menu_item_set_submenu (GTK_MENU_ITEM (settings_item), settings_menu); //difficulty settings difficulty_menu = gtk_menu_new (); difficulty_item = gtk_menu_item_new_with_label ("Difficulty"); gtk_menu_item_set_submenu (GTK_MENU_ITEM (difficulty_item), difficulty_menu); gtk_menu_append (GTK_MENU (settings_menu), difficulty_item); level_1_item = gtk_radio_menu_item_new_with_label (group, "Level 1"); gtk_menu_append (GTK_MENU (difficulty_menu), level_1_item); group = gtk_radio_menu_item_group(GTK_RADIO_MENU_ITEM(level_1_item)); level_2_item = gtk_radio_menu_item_new_with_label (group, "Level 2"); gtk_menu_append (GTK_MENU (difficulty_menu), level_2_item); group = gtk_radio_menu_item_group(GTK_RADIO_MENU_ITEM(level_2_item)); level_3_item = gtk_radio_menu_item_new_with_label (group, "Level 3"); gtk_menu_append (GTK_MENU (difficulty_menu), level_3_item); group = gtk_radio_menu_item_group(GTK_RADIO_MENU_ITEM(level_3_item)); . . . level_12_item = gtk_radio_menu_item_new_with_label (group, "Level 12"); gtk_menu_append (GTK_MENU (difficulty_menu), level_12_item); group = gtk_radio_menu_item_group(GTK_RADIO_MENU_ITEM(level_12_item)); g_signal_connect (G_OBJECT (level_1_item), "toggled", G_CALLBACK (plugin_callback), (gpointer) LEVEL_1); g_signal_connect (G_OBJECT (level_2_item), "toggled", G_CALLBACK (plugin_callback), (gpointer) LEVEL_2); g_signal_connect (G_OBJECT (level_3_item), "toggled", G_CALLBACK (plugin_callback), (gpointer) LEVEL_3); ... g_signal_connect (G_OBJECT (level_12_item), "toggled", G_CALLBACK (plugin_callback), (gpointer) LEVEL_12); gtk_menu_append (GTK_MENU (settings_menu), gtk_menu_item_new()); //sound settings sound_item = gtk_check_menu_item_new_with_label("Sound"); gtk_menu_append (GTK_MENU (settings_menu), sound_item); g_signal_connect (G_OBJECT (sound_item), "toggled", G_CALLBACK (plugin_callback), (gpointer) MENU_SOUND); gtk_check_menu_item_set_state (GTK_CHECK_MENU_ITEM(sound_item), gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (sound_check))); return menu_items; }
The following example illustrates the function that is called when any configuration is performed in the menu. This function updates the GTK_SPIN_BUTTON (level change) or the GTK_CHECK_MENU_ITEM (sound change).
static void plugin_callback (GtkWidget *menu_item, gpointer data) { if (MENU_SOUND == (int) data && !changed){ changed = TRUE; gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (sound_check), gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM(sound_item))); } else if (!changed) { changed = TRUE; gtk_spin_button_set_value (GTK_SPIN_BUTTON (board_box), (int) data); } changed = FALSE; }
The following example illustrates the function that is called to update the menu level option, when the user chooses the level using the GTK_SPIN_BUTTON:
static void settings_callback (GtkWidget *widget, gpointer data) { if (!changed) { changed = TRUE; gint active = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget)); if (active == LEVEL_1) { gtk_check_menu_item_set_state(GTK_CHECK_MENU_ITEM(level_1_item), TRUE); } else if (active == LEVEL_2) { gtk_check_menu_item_set_state(GTK_CHECK_MENU_ITEM(level_2_item), TRUE); } else if (active == LEVEL_3) { gtk_check_menu_item_set_state(GTK_CHECK_MENU_ITEM(level_3_item), TRUE); } ... else if (active == LEVEL_12) { gtk_check_menu_item_set_state(GTK_CHECK_MENU_ITEM(level_12_item), TRUE); } } changed = FALSE; }
The following example illustrates the function that is called by osso-games-startup, if necessary.
static void update_menu (void) { settings_callback(board_box, NULL); sound_callback(sound_check, NULL); }
You can get the full source code in Scratchbox with following command:
apt-get source osso-games-startup-example
References
Improve this page