wmud/src/main.c

101 lines
2.1 KiB
C
Raw Normal View History

#include <glib.h>
#include <gio/gio.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "main.h"
#include "networking.h"
#include "interpreter.h"
#define MAX_RECV_LEN 1024
struct {
char *file;
int line;
} debug_context_loc = {NULL, 0};
GMainContext *game_context;
guint32 elapsed_seconds = 0;
guint32 elapsed_cycle = 0;
GRand *main_rand = NULL;
/* rl_sec_elapsed()
*
* This function 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++;
}
return TRUE;
}
#ifdef DEBUG
void
/* debug_context()
*
* This function keeps track of the code flow in some way. It can help with
* debugging, as during a SIGSEGV or such signal this will print out the last
* place of DebugContext in the code.
*/
debug_context(char *file, int line)
{
if (debug_context_loc.file != NULL)
g_free(debug_context_loc.file);
debug_context_loc.file = g_strdup(file);
debug_context_loc.line = line;
}
#define DebugContext debug_context(__FILE__, __LINE__)
#else
#define DebugContext
#endif
int
main(int argc, char **argv)
{
GMainLoop *game_loop;
GSource *timeout_source;
guint timeout_id;
GSocketListener *game_listener;
/* Initialize the thread and type system */
g_thread_init(NULL);
g_type_init();
/* 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);
timeout_id = g_source_attach(timeout_source, game_context);
g_source_unref(timeout_source);
/* TODO: Create signal handlers! */
wmud_interpreter_init();
/* TODO: Change 4000 to the port number coming from configuration */
wmud_networking_init(4000);
/* Run the game loop */
g_main_loop_run(game_loop);
return 0;
}