From f8b5feb36180d866f7bf77e2133e780a976e9178 Mon Sep 17 00:00:00 2001 From: Gergely POLONKAI Date: Sun, 25 Mar 2012 14:32:28 +0200 Subject: [PATCH] Moved the game thread to game.c Signed-off-by: Gergely POLONKAI --- src/Makefile.am | 2 +- src/game-networking.c | 2 +- src/game-networking.h | 2 +- src/game.c | 86 +++++++++++++++++++++++++++++++++++++++ src/game.h | 7 ++++ src/main.c | 95 +++++-------------------------------------- 6 files changed, 107 insertions(+), 87 deletions(-) create mode 100644 src/game.c create mode 100644 src/game.h diff --git a/src/Makefile.am b/src/Makefile.am index 2b58d81..f8553e0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,5 +1,5 @@ bin_PROGRAMS = wmud AM_CPPFLAGS = -DWMUD_STATEDIR=\""$(localstatedir)"\" -DWMUD_CONFDIR=\""$(sysconfdir)"\" $(MEMCACHED_CFLAGS) $(GLIB_CFLAGS) $(GIO_CFLAGS) $(GTHREAD_CFLAGS) $(SQLITE3_CFLAGS) -wmud_SOURCES = main.c game-networking.c interpreter.c db.c players.c maintenance.c +wmud_SOURCES = main.c game-networking.c interpreter.c db.c players.c maintenance.c game.c wmud_LDADD = $(MEMCACHED_LIBS) $(GLIB_LIBS) $(GIO_LIBS) $(GTHREAD_LIBS) $(SQLITE3_LIBS) diff --git a/src/game-networking.c b/src/game-networking.c index a51c95d..acbc411 100644 --- a/src/game-networking.c +++ b/src/game-networking.c @@ -268,7 +268,7 @@ game_source_callback(GSocket *socket, GIOCondition condition, struct AcceptData * err is set accordingly (if not NULL) */ gboolean -wmud_networking_init(guint port_number, GError **err) +wmud_networking_init(guint port_number, GMainContext *game_context, GError **err) { struct AcceptData *accept_data; GSocketListener *game_listener; diff --git a/src/game-networking.h b/src/game-networking.h index dfdda0b..c577540 100644 --- a/src/game-networking.h +++ b/src/game-networking.h @@ -50,7 +50,7 @@ extern GSList *clients; -gboolean wmud_networking_init(guint port_number, GError **err); +gboolean wmud_networking_init(guint port_number, GMainContext *game_context, GError **err); void wmud_client_send(wmudClient *client, const gchar *fmt, ...); void wmud_client_start_login(wmudClient *client); void wmud_client_interpret_newplayer_answer(wmudClient *client); diff --git a/src/game.c b/src/game.c new file mode 100644 index 0000000..3d0014b --- /dev/null +++ b/src/game.c @@ -0,0 +1,86 @@ +#include + +#include "main.h" +#include "game.h" + +/** + * elapsed_seconds: + * + * the number of seconds elapsed since game boot. May be inaccurate, as it + * simply gets updated by a timeout function which should run every second + */ +guint32 elapsed_seconds = 0; +/** + * elapsed_cycle: + * + * yes, I'm optimistic. This counter is increased if, for some reason, + * #elapsed_seconds reaches the maximum value + */ +guint32 elapsed_cycle = 0; +/** + * + * rl_sec_elapsed: + * @user_data: non-used pointer to callback's user data + * + * Keeps track of elapsed real-world time. It is inaccurate by design, but it + * doesn't actually matter. + */ +gboolean +rl_sec_elapsed(gpointer user_data) +{ + elapsed_seconds++; + if (elapsed_seconds == G_MAXUINT32) + { + elapsed_seconds = 0; + elapsed_cycle++; + } + + if (elapsed_seconds % 30 == 0) + { + g_log(G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Heartbeat"); + } + + return TRUE; +} + +/** + * game_thread_func: + * @game_loop: the main loop to be associated with the game thread + * + * The game thread's main function. + * + * Return value: This function always returns %NULL. + */ +gpointer +game_thread_func(GMainLoop *game_loop) +{ + /* Run the game loop */ + g_main_loop_run(game_loop); + + return NULL; +} + +gboolean +wmud_game_init(GThread **game_thread, GMainContext **game_context) +{ + GMainLoop *game_loop; + GSource *timeout_source; + GError *err = NULL; + + /* Create the game context and main loop */ + *game_context = g_main_context_new(); + game_loop = g_main_loop_new(*game_context, FALSE); + + /* Create the timeout source which keeps track of elapsed real-world + * time */ + timeout_source = g_timeout_source_new(1000); + g_source_set_callback(timeout_source, rl_sec_elapsed, NULL, NULL); + g_source_attach(timeout_source, *game_context); + g_source_unref(timeout_source); + + g_clear_error(&err); + *game_thread = g_thread_create((GThreadFunc)game_thread_func, game_loop, TRUE, &err); + + return TRUE; +} + diff --git a/src/game.h b/src/game.h new file mode 100644 index 0000000..e2e991e --- /dev/null +++ b/src/game.h @@ -0,0 +1,7 @@ +#ifndef __WMUD_GAME_H__ +#define __WMUD_GAME_H__ + +gboolean wmud_game_init(GThread **game_thread, GMainContext **game_context); + +#endif /* __WMUD_GAME_H__ */ + diff --git a/src/main.c b/src/main.c index 9f40b78..a66542a 100644 --- a/src/main.c +++ b/src/main.c @@ -33,6 +33,7 @@ #include "db.h" #include "players.h" #include "maintenance.h" +#include "game.h" /** * SECTION:utils @@ -51,26 +52,6 @@ struct { int line; } debug_context_loc = {NULL, 0}; -/** - * game_context: - * - * the game thread's main context - */ -GMainContext *game_context; -/** - * elapsed_seconds: - * - * the number of seconds elapsed since game boot. May be inaccurate, as it - * simply gets updated by a timeout function which should run every second - */ -guint32 elapsed_seconds = 0; -/** - * elapsed_cycle: - * - * yes, I'm optimistic. This counter is increased if, for some reason, - * #elapsed_seconds reaches the maximum value - */ -guint32 elapsed_cycle = 0; /** * main_rand: * @@ -108,31 +89,6 @@ gchar *database_file = NULL; */ gchar *admin_email = NULL; -/** - * rl_sec_elapsed: - * @user_data: non-used pointer to callback's user data - * - * Keeps track of elapsed real-world time. It is inaccurate by design, but it - * doesn't actually matter. - */ -gboolean -rl_sec_elapsed(gpointer user_data) -{ - elapsed_seconds++; - if (elapsed_seconds == G_MAXUINT32) - { - elapsed_seconds = 0; - elapsed_cycle++; - } - - if (elapsed_seconds % 30 == 0) - { - g_log(G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Heartbeat"); - } - - return TRUE; -} - /** * wmud_random_string: * @len: the desired length of the generated random string @@ -291,23 +247,6 @@ wmud_config_init(GError **err) return TRUE; } -/** - * game_thread_func: - * @game_loop: the main loop to be associated with the game thread - * - * The game thread's main function. - * - * Return value: This function always returns %NULL. - */ -gpointer -game_thread_func(GMainLoop *game_loop) -{ - /* Run the game loop */ - g_main_loop_run(game_loop); - - return NULL; -} - /** * main: * @argc: The number of arguments on the command line @@ -318,10 +257,9 @@ game_thread_func(GMainLoop *game_loop) int main(int argc, char **argv) { - GMainLoop *game_loop; - GSource *timeout_source; GError *err = NULL; GThread *game_thread; + GMainContext *game_context; /* Initialize the thread and type system */ g_thread_init(NULL); @@ -333,17 +271,6 @@ main(int argc, char **argv) /* Initialize random number generator */ main_rand = g_rand_new(); - /* Create the game context and main loop */ - game_context = g_main_context_new(); - game_loop = g_main_loop_new(game_context, FALSE); - - /* Create the timeout source which keeps track of elapsed real-world - * time */ - timeout_source = g_timeout_source_new(1000); - g_source_set_callback(timeout_source, rl_sec_elapsed, NULL, NULL); - g_source_attach(timeout_source, game_context); - g_source_unref(timeout_source); - /* TODO: Create signal handlers! */ if (!wmud_config_init(&err)) @@ -377,8 +304,16 @@ main(int argc, char **argv) return 1; } + g_clear_error(&err); - if (!wmud_networking_init(port, &err)) + wmud_db_players_load(&err); + + /* Initialization ends here */ + + wmud_game_init(&game_thread, &game_context); + + g_clear_error(&err); + if (!wmud_networking_init(port, game_context, &err)) { if (err) { @@ -392,14 +327,6 @@ main(int argc, char **argv) return 1; } - g_clear_error(&err); - wmud_db_players_load(&err); - - /* Initialization ends here */ - - g_clear_error(&err); - game_thread = g_thread_create((GThreadFunc)game_thread_func, game_loop, TRUE, &err); - wmud_maintenance_init(); /* Initialize other threads here */