int main(int argc, char* argv[])
{
	// our demo variables
	World* demoworld = 0;
	Camera* democamera = 0;
	Tileset* demotileset = 0;
	Renderer* demorenderer = 0;

	// init SDL
	if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
	{
		fprintf(stderr, "SDL Library Initialization Failed!\n\tSDL Error: %s\n", SDL_GetError());
		exit(1);
	}
	atexit(SDL_Quit);

	// create the window
	if (!SDL_SetVideoMode(SCREEN_W, SCREEN_H, SCREEN_BPP, SDL_HWSURFACE | SDL_DOUBLEBUF))
	{
		fprintf(stderr, "SDL Screen Initialization Failed! %dx%d@%dbpp\nSDL Error: %s\n",
			SCREEN_W,
			SCREEN_H,
			SCREEN_BPP,
			SDL_GetError());
		exit(1);
	}

	// set the window caption
	SDL_WM_SetCaption("Basic SDL Map Editor Example Demo -- by Richard Marks <*****@*****.**>", 0);

	// create our keyboard handler
	Keyboard* keyboard = keyboard_create();

	// load the world
	demoworld = world_load("example_demo.map");
	if (!demoworld)
	{
		fprintf(stderr, "unable to load the world file!\n");
		exit(1);
	}

	// load the tileset
	demotileset = tileset_load("example_demo.png", 32, 32);
	if (!demotileset)
	{
		fprintf(stderr, "unable to load the tileset file!\n");
		exit(1);
	}

	// create the camera
	democamera = camera_create(
		// anchor position of the camera is at 32,32 on the screen
		32, 32,
		// the camera view is 30 x 22 tiles
		30, 22,
		// need to tell the camera the size of the tiles
		demotileset->width, demotileset->height);

	// create the renderer
	demorenderer = renderer_create(demoworld, demotileset, democamera);

	// pre-render the scene
	renderer_pre_render(demorenderer);

	fprintf(stderr, "\nstarting main loop...\n\n");
	// start the main loop
	SDL_Event event;
	bool running = true;
	int startticks = SDL_GetTicks();
	int mainlooptimer = SDL_GetTicks();
	while(running)
	{
		// check for our window being closed
		while(SDL_PollEvent(&event))
		{
			if (SDL_QUIT == event.type)
			{
				running = false;
			}
		}

		// update our keyboard handler
		keyboard_update(keyboard);

		// start timing our code
		startticks = SDL_GetTicks();

		// check for our controls

		// ESC and Q quits
		if (keyboard->key[SDLK_ESCAPE] || keyboard->key[SDLK_q])
		{
			running = false;
		}

		const unsigned int CAMERASPEED = 16;

		// W and up arrow
		if (keyboard->key[SDLK_w] || keyboard->key[SDLK_UP])
		{
			if (democamera->inpixels->y > 0)
			{
				democamera->inpixels->y -= CAMERASPEED;
			}
		}

		// S and down arrow
		if (keyboard->key[SDLK_s] || keyboard->key[SDLK_DOWN])
		{
			if (democamera->inpixels->y < demorenderer->scene->h - democamera->inpixels->h)
			{
				democamera->inpixels->y += CAMERASPEED;
			}
		}

		// A and left arrow
		if (keyboard->key[SDLK_a] || keyboard->key[SDLK_LEFT])
		{
			if (democamera->inpixels->x > 0)
			{
				democamera->inpixels->x -= CAMERASPEED;
			}
		}

		// D and right arrow
		if (keyboard->key[SDLK_d] || keyboard->key[SDLK_RIGHT])
		{
			if (democamera->inpixels->x < demorenderer->scene->w - democamera->inpixels->w)
			{
				democamera->inpixels->x += CAMERASPEED;
			}
		}

		// render our scene
		renderer_render(demorenderer);

		// flip our double buffer
		SDL_Flip(SDL_GetVideoSurface());

		// lock our framerate to roughly 30 FPS
		int tickdiff = SDL_GetTicks() - startticks;
		if (tickdiff < 33)
		{
			SDL_Delay(33 - tickdiff);
		}
	}

	int tickdiff = SDL_GetTicks() - mainlooptimer;

	fprintf(stderr, "finished after %d ticks!\n\n", tickdiff);

	// destroy our pointers
	tileset_destroy(demotileset);
	renderer_destroy(demorenderer);
	world_destroy(demoworld);
	camera_destroy(democamera);
	keyboard_destroy(keyboard);

	return 0;
}
示例#2
0
int main(int argc, char** args)
{
	srand(time(NULL));

	int error;
	c_cpu_handle cpu;
	
	printf("Cuilien cell simulation.\n");

	printf("Initializing VM...\n");
	cpu = c_cpu_init();
	if(c_error_last)
	{
		c_error_print(c_error_last);
		return 1;
	}
	
	printf("Disabling a few opcodes...\n");
	c_instruction_vector[C_INSTR_PUTC] = NULL;
	c_instruction_vector[C_INSTR_GETC] = NULL;
	c_instruction_vector[C_INSTR_SHOW] = NULL;

	printf("Initializing IVT...\n");
	cpu->ivt = build_ivt();

	printf("Loading seed...\n");
	c_mem_handle seed = c_mem_init(MAX_CELL_MEMORY);
	c_mem_load_file(seed, "seed2.cx", 12);

	printf("Setting up graphics...\n");
	if((error = graphics_init()))
	{
		// TODO: handle error
		return 1;
	}
	
	printf("Creating world...\n");
	world_load(&world, "terrain3.bmp");
	graphics_init_world_image(world);

	printf("Spawning inital cells...\n");
	cells_init();

	champion_cell = cell_spawn(c_mem_copy(seed), 1, 0x00ff00, 5, 180, 200);
	champion_cell->save = true;

	cell_spawn(c_mem_copy(seed), 1, 0xff0000, 5, 1000, 200);
	// cell_spawn(c_mem_copy(seed), 0x0000ff, 5, 180, 600);
	// cell_spawn(c_mem_copy(seed), 0xffff00, 5, 1000, 600);
	
	printf("Ready.\n");
	
	while(!graphics_update() && cells_get_count())
	{
		world_update_waste(world);

		// simulate and render cells
		/* this turned out messier than I had envisioned it */
		int i = 0;
		while((current_cell = cell_next(&i)))
		{
			cpu->context = &current_cell->process.context;

			assert(cpu->context != NULL);
			assert(cpu->context->memory != NULL);

			int step;
			for(step = 0; step < 100 && current_cell->alive; ++step)
			{
				c_cpu_step(cpu);
			}

			if(current_cell->mass)
			{
				/* mass decay */
				--current_cell->mass;
			
				/* produce waste */
				tile_t* tile = world_get_tile(world, current_cell->x, current_cell->y);
				if(tile->waste != 255)
				{
					++tile->waste;
					graphics_update_world_image(current_cell->x, current_cell->y, tile);
				}
			}

			graphics_render_cell(current_cell);

			if(current_cell != champion_cell &&
					cell_lifetime(current_cell) >= cell_lifetime(champion_cell))
			{
				champion_cell->save = false;
				if(!champion_cell->alive)
					cell_kill(champion_cell);

				current_cell->save = true;
				champion_cell = current_cell;
			}

			if(current_cell->mass == 0 ||
					cell_lifetime(current_cell) > 1000) /* test death by age */
				cell_kill(current_cell);
		}

		++cycle;
	}

	printf("Champion: \n");
	printf(" * Color: %06X\n", champion_cell->color);
	printf(" * Generation: %d\n", champion_cell->generation);
	printf(" * Times split: %d\n", champion_cell->times_split);
	printf(" * Cycles lived: %d\n", cell_lifetime(champion_cell));
	printf(" * Time of birth: %d\n", champion_cell->birth);
	if(champion_cell->alive)
		printf(" * Time of death: ALIVE\n");
	else
		printf(" * Time of death: %d\n", champion_cell->death);
	printf(" * Place of death: %d, %d\n", champion_cell->x, champion_cell->y);
	c_mem_dump_to_file(champion_cell->process.context.memory, "champion.dump");
	printf("Memory dumped to champion.dump.\n");
	
	printf("Freeing memory...\n");
	cells_free();
	c_mem_free(seed);
	
	printf("Destroying graphics...\n");
	if((error = graphics_destroy()))
	{
		// TODO: handle error
		return 1;
	}

	printf("Freeing VM...\n");
	c_cpu_free(cpu);
	
	printf("Good bye!\n");
}