示例#1
0
int main()
{
  std::cout << "Enet Server" << std::endl;

  setup_network();
  setup_as_server();

  while(true)
  {
    poll_events();
  }

  destroy_network();

  return 0;
}
int main(int argc, char *argv[])
{
	unsigned int moment;
	unsigned int node_index;
	unsigned int moment_index;
	node_t *node;

	Display *display;
	GC gc;
	int screen_number;
	int window_x;
	int window_y;
	unsigned int each_x;
	unsigned int each_y;
	unsigned int window_border_width;
	unsigned int window_height;
	unsigned int window_width;
	unsigned long gc_value_mask;
	unsigned long window_background_color;
	unsigned long window_border_color;
	Window root_window;
	Window window;
	XGCValues gc_values;
	Visual* default_visual;
	Colormap colormap;
	XColor system_color;
	XColor exact_color;
	unsigned int window_display_x_modulus;
	unsigned int window_display_y_modulus;
	unsigned int window_x_pixel;
	unsigned int window_y_pixel;

	display = XOpenDisplay(NULL);
	screen_number = DefaultScreen(display);
	root_window = RootWindow(display, screen_number);
	window_x = 0;
	window_y = 0;
	window_width = XWINDOW_WIDTH;
	window_height = XWINDOW_HEIGHT;
	window_border_width = 0;
	window_border_color = BlackPixel(display, screen_number);
	window_background_color = WhitePixel(display, screen_number);
	window = XCreateSimpleWindow(display, root_window, window_x, window_y,
			window_width, window_height, window_border_width,
			window_border_color, window_background_color);
	XMapWindow(display, window);
	XFlush(display);
	gc_value_mask = 0;
	gc = XCreateGC(display, window, gc_value_mask, &gc_values);
	default_visual = DefaultVisual(display, DefaultScreen(display));
	colormap = XCreateColormap(display, window, default_visual, AllocNone);
	XSync(display, False);
	window_display_x_modulus = NODES / XWINDOW_WIDTH;
	window_display_y_modulus = TIME / XWINDOW_HEIGHT;

	srandom(SEED);
	/*  init_action_tables();  */
	network = create_network();

	window_y_pixel = 0;
	for (moment = 0; moment < TIME; moment++) {
		if (0 == (moment % window_display_y_modulus)) {
			window_x_pixel = 0;
			for (node_index = 0; node_index < NODES; node_index++) {
				if (0 != (node_index % window_display_x_modulus)) {
					continue;
				}

				node = network->nodes_display_order[node_index];

				system_color.red = 0;

				if (0 == node->value) {
					system_color.green = USHRT_MAX / 4;
					system_color.blue = USHRT_MAX;
				}
				else if (1 == node->value) {
					system_color.green = USHRT_MAX;
					system_color.blue = USHRT_MAX / 4;
				}
				else {
					exit(100);
				}
				/*
				  system_color.blue = 0;
				*/

				if (2 == node->link_count) {
					system_color.green /= 2;
					system_color.blue /= 2;
				}
				/*
				  system_color.green = 0;
				*/

				XAllocColor(display, colormap, &system_color);
				XSetForeground(display, gc, system_color.pixel);

				XDrawPoint(display, window, gc, window_x_pixel,
						window_y_pixel);
				window_x_pixel++;
			}
			XFlush(display);
			window_y_pixel++;
		}
		iterate_network(network);
	}

	while (1) {
		usleep(SLEEP);
	}

	destroy_network(network);

	XUnmapWindow(display, window);
	XDestroyWindow(display, window);
	XCloseDisplay(display);

	return 0;
}