Documentation/Maemo 5 Developer Guide/Using Multimedia Components/Using Games Start-up Screen

Contents

Using Games Start-up Screen

The osso-games-startup application is a generic game start-up interface, providing a common hildonized user interface view for game start-up control and configuration.


Application Functionality

To execute the game, the osso-games-startup application needs to be called. The game configuration file must be passed as an argument. Once loaded, osso-games-startup creates a common interface for all games and, if needed, loads 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 clean-up routine within the plug-in exists, 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 is executed. Games that do not use the glib mainloop must integrate some functionality to their mainloop (for more information about games without a glib mainloop see section #Integrating non-GTK+ Games).

The service is called every time any communication between osso-games-startup and the game is needed.

The following diagram illustrates the operational execution flow of the osso-games-startup application.

Image:diagram_chinook.png

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 must 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 is 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 searches for it inside the gettext package
 TitleId=example_title
 # if the 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 must 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.

Next, a D-BUS service (a .service file) must 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, differences in the actual game implementation must be dealt with. The same steps must be followed to create osso desktop files and registering dbus services as for the 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 snippets/games_integrating_non-gtk+-hgw__init approach can be used.

If and when hgw pointer is not null, the game is ready to receive dbus messages that are intended for the game application. The next step is to check which state the game was started in. This is done by calling hgw_context_get_start_command(). The return values indicate the state the game must 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 returns HGW_ERR_COMMUNICATION if there are messages available and other values if messages are not available or if there was error. (see hgw/hgw.h for more details about HgwError enum) Example: snippets/games_integrating_non-gtk+-HGW_ERR_

Pointer *msg then holds information about the received dbus message and the game application is 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, a call to hgw_context_destroy() is made with initialized hgw as a parameter.

Also, hildon-games-wrapper library provides an interface to gconf. Gconf is used to store persistent data such as the level of difficulty and sounds on/off. This method is preferred because the data stored in gconf is automatically backed up and restored when the user chooses to do so (from desktop, back-up manager). However, this interface is read-only, because writing happens 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. 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 [localhost#fig:basic_osso-games-startup_app_screen 8.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. snippets/games_integrating_non-gtk+-StartupPluginInfo

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: snippets/games_integrating_non-gtk+-GameStartupInfo

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

snippets/games_integrating_non-gtk+-STARTUP_INIT_PLUGIN

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 CrazyParking

This section illustrates the plug-in for CrazyParking implementation. The plug-in allows the player to set the initial level of the game, and to define whether the game uses sounds.

Games Start-up screen with CrazyParking plug-in's level and sound configuration:

Source code is available here.

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 the 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. crazyparking/src/plugin/plugin.c

#include <stdio.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: crazyparking/src/plugin/plugin.c

#define SETTINGS_LEVEL "/apps/osso/crazyparking/level"
#define SETTINGS_SOUND "/apps/osso/crazyparking/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        plugin_callback      (GtkWidget *menu_item, gpointer data);
static void        settings_callback    (GtkWidget *widget, gpointer data);
static void        sound_callback       (GtkWidget *widget, gpointer data);

The following example illustrates some global variables: crazyparking/src/plugin/plugin.c

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. crazyparking/src/plugin/plugin.c

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: crazyparking/src/plugin/plugin.c

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: crazyparking/src/plugin/plugin.c

static GtkWidget *load_plugin (void)
{
  int board, enable_sound;
  GtkWidget *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_hbox = gtk_hbox_new (TRUE, 0);
  g_assert (game_hbox);

  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);
  g_signal_connect(G_OBJECT(board_box), "value-changed", G_CALLBACK(settings_callback), NULL);
  gtk_box_pack_end (GTK_BOX (board_hbox), board_box, FALSE, FALSE, 0);

  board_label = gtk_label_new ("Starting level");
  g_assert(board_label);

  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);

  sound_hbox = gtk_hbox_new (FALSE, 4);

  sound_check = gtk_check_button_new();
  g_assert (sound_check);

  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(sound_check), enable_sound);
  g_signal_connect (G_OBJECT(sound_check), "clicked",
                    G_CALLBACK(sound_callback), NULL);

  gtk_box_pack_end (GTK_BOX (sound_hbox), sound_check, FALSE, FALSE, 0);

  sound_label = gtk_label_new ("Sound");
  g_assert (sound_label);

  gtk_box_pack_end (GTK_BOX (sound_hbox), sound_label, TRUE, TRUE, 0);

  gtk_box_pack_start (GTK_BOX (game_hbox), sound_hbox, FALSE, FALSE, 2);

  printf ("%s : %s : %d\n", __FILE__, __FUNCTION__, __LINE__);

  return game_hbox;
}

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): crazyparking/src/plugin/plugin.c

static void write_config (void)
{
  int value;
  value = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON(board_box));
  if (value < 1) value = 1;
  else if (value > 12) value = 12;
  gconf_client_set_int(gcc, SETTINGS_LEVEL, value, 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: crazyparking/src/plugin/plugin.c

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));
  /* ... Listing cut for brevity ...*/
  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);
  /* ... Listing cut for brevity ...*/
  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). crazyparking/src/plugin/plugin.c

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: crazyparking/src/plugin/plugin.c

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);
    }
    /*... Listing cut for brevity ...*/
    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 handles changes to the sound setup: crazyparking/src/plugin/plugin.c

static void sound_callback (GtkWidget *widget, gpointer data)
{
  if (!changed) {
    changed = TRUE;
    gtk_check_menu_item_set_state (GTK_CHECK_MENU_ITEM(sound_item),
                                   gtk_toggle_button_get_active(
                                   GTK_TOGGLE_BUTTON (widget)));
  }
  changed = FALSE;
}

The following example illustrates the function that is called by osso-games-startup, if necessary. crazyparking/src/plugin/plugin.c

static void update_menu (void)
{
  settings_callback(board_box, NULL);
  sound_callback(sound_check, NULL);
}