2012-03-08 16:39:45 +00:00
|
|
|
#include <glib.h>
|
|
|
|
#include <gio/gio.h>
|
2012-03-10 17:24:23 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2012-03-08 16:39:45 +00:00
|
|
|
|
2012-03-12 12:55:53 +00:00
|
|
|
#include "main.h"
|
|
|
|
#include "networking.h"
|
2012-03-13 01:22:22 +00:00
|
|
|
#include "interpreter.h"
|
2012-03-12 12:55:53 +00:00
|
|
|
|
2012-03-11 22:44:29 +00:00
|
|
|
#define MAX_RECV_LEN 1024
|
|
|
|
|
2012-03-08 16:39:45 +00:00
|
|
|
struct {
|
|
|
|
char *file;
|
|
|
|
int line;
|
|
|
|
} debug_context_loc = {NULL, 0};
|
|
|
|
|
2012-03-12 12:55:53 +00:00
|
|
|
GMainContext *game_context;
|
2012-03-12 15:42:13 +00:00
|
|
|
guint32 elapsed_seconds = 0;
|
|
|
|
guint32 elapsed_cycle = 0;
|
2012-03-14 19:19:05 +00:00
|
|
|
GRand *main_rand = NULL;
|
2012-03-08 16:39:45 +00:00
|
|
|
|
2012-03-22 10:00:59 +00:00
|
|
|
/* rl_sec_elapsed()
|
|
|
|
*
|
|
|
|
* This function keeps track of elapsed real-world time. It is inaccurate by
|
|
|
|
* design, but it doesn't actually matter.
|
|
|
|
*/
|
2012-03-08 16:39:45 +00:00
|
|
|
gboolean
|
|
|
|
rl_sec_elapsed(gpointer user_data)
|
|
|
|
{
|
2012-03-12 15:42:13 +00:00
|
|
|
elapsed_seconds++;
|
|
|
|
if (elapsed_seconds == G_MAXUINT32)
|
|
|
|
{
|
|
|
|
elapsed_seconds = 0;
|
|
|
|
elapsed_cycle++;
|
|
|
|
}
|
2012-03-08 16:39:45 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-03-10 17:24:23 +00:00
|
|
|
#ifdef DEBUG
|
2012-03-08 16:39:45 +00:00
|
|
|
void
|
2012-03-22 10:00:59 +00:00
|
|
|
/* 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.
|
|
|
|
*/
|
2012-03-08 16:39:45 +00:00
|
|
|
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__)
|
2012-03-10 17:24:23 +00:00
|
|
|
#else
|
|
|
|
#define DebugContext
|
|
|
|
#endif
|
2012-03-08 16:39:45 +00:00
|
|
|
|
2012-03-07 22:00:58 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2012-03-08 16:39:45 +00:00
|
|
|
GMainLoop *game_loop;
|
2012-03-12 12:55:53 +00:00
|
|
|
GSource *timeout_source;
|
2012-03-08 16:39:45 +00:00
|
|
|
guint timeout_id;
|
2012-03-11 15:50:45 +00:00
|
|
|
GSocketListener *game_listener;
|
2012-03-08 16:39:45 +00:00
|
|
|
|
2012-03-22 10:00:59 +00:00
|
|
|
/* Initialize the thread and type system */
|
2012-03-12 15:42:13 +00:00
|
|
|
g_thread_init(NULL);
|
2012-03-08 16:39:45 +00:00
|
|
|
g_type_init();
|
|
|
|
|
2012-03-22 10:00:59 +00:00
|
|
|
/* Initialize random number generator */
|
2012-03-14 19:19:05 +00:00
|
|
|
main_rand = g_rand_new();
|
|
|
|
|
2012-03-22 10:00:59 +00:00
|
|
|
/* Create the game context and main loop */
|
2012-03-08 16:39:45 +00:00
|
|
|
game_context = g_main_context_new();
|
|
|
|
game_loop = g_main_loop_new(game_context, FALSE);
|
|
|
|
|
2012-03-22 10:00:59 +00:00
|
|
|
/* Create the timeout source which keeps track of elapsed real-world
|
|
|
|
* time */
|
2012-03-08 16:39:45 +00:00
|
|
|
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);
|
|
|
|
|
2012-03-22 10:00:59 +00:00
|
|
|
/* TODO: Create signal handlers! */
|
|
|
|
|
2012-03-08 16:39:45 +00:00
|
|
|
|
2012-03-13 01:22:22 +00:00
|
|
|
wmud_interpreter_init();
|
2012-03-22 10:00:59 +00:00
|
|
|
/* TODO: Change 4000 to the port number coming from configuration */
|
2012-03-12 12:55:53 +00:00
|
|
|
wmud_networking_init(4000);
|
2012-03-08 16:39:45 +00:00
|
|
|
|
2012-03-22 10:00:59 +00:00
|
|
|
/* Run the game loop */
|
2012-03-08 16:39:45 +00:00
|
|
|
g_main_loop_run(game_loop);
|
2012-03-07 22:00:58 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|