Ejemplo n.º 1
0
int main(void)
{
  initscr();
  noecho();
  noecho();
  curs_set(0); 

  int grid[COLS][LINES];

  init_life (grid);
  printgrid (grid);
  
  for (;;)
    {
      run_life (grid);
      printgrid (grid);
      refresh();
      if (gameover(grid) == 0)
	break;
      usleep (50000);
    }

  refresh();
  endwin();
  return 0;
}
Ejemplo n.º 2
0
int main(int argc, char **argv) {
        int i,
            j,
            running,
            drawing,
            erasing,
            held_l,
            held_r,
            held_d,
            held_u,
            held_zoom_out,
            held_zoom_in,
            verbose;
        unsigned int w,
                     h,
                     ci,
                     cj,
                     ww,
                     wh,
                     fps_limit = 60;
        Uint16 mx,
               my;
        Uint32 begin,
               ticks,
               delay;
        int **grid;
        double aspect_ratio,
               x,
               y,
               zoom,
               min_x,
               max_x,
               min_y,
               max_y,
               min_zoom,
               max_zoom,
               pan_speed,
               zoom_speed;
        GLdouble pos3D_x,
                 pos3D_y,
                 pos3D_z;
        GLdouble model_view[16];
        GLdouble projection[16];
        GLint viewport[4];
        SDLKey key;
        SDL_Event event;
        SDL_Surface *screen;

        /* TODO: cmdline arguments:
           -v: verbose mode
           -h: usage text */

        if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) == -1) exit_error(SDL_Init);
        if (atexit(SDL_Quit) != 0) {
                fprintf(stderr, "atexit failed!\n");
                exit(EX_OSERR);
        }
        if (SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8) == -1) exit_error(SDL_GL_SetAttribute);
        if (SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8) == -1) exit_error(SDL_GL_SetAttribute);
        if (SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8) == -1) exit_error(SDL_GL_SetAttribute);
        if (SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8) == -1) exit_error(SDL_GL_SetAttribute);
        if (SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32) == -1) exit_error(SDL_GL_SetAttribute);
        if (SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24) == -1) exit_error(SDL_GL_SetAttribute);
        if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) == -1) exit_error(SDL_GL_SetAttribute);
        SDL_WM_SetCaption(argv[0], argv[0]);

        ww = 640;
        wh = 480;
        aspect_ratio = ((float)wh)/ww;
        if ((screen = SDL_SetVideoMode(ww, wh, 0, SDL_RESIZABLE|SDL_OPENGL)) == NULL) exit_error(SDL_SetVideoMode);
        if (ww != screen->w||wh != screen->h) {
                fprintf(stderr, "SDL screen dimensions don't match those requested!\nRequested %dx%d; obtained %dx%d", wh, ww, screen->h, screen->w);
                exit(EX_SOFTWARE);
        }

        glViewport(0, 0, ww, wh);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 1, 0, aspect_ratio, 0, 1);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glClearColor(0, 0, 0, 0);
        glEnable(GL_BLEND);
        glEnable(GL_POINT_SMOOTH);
        glEnable(GL_LINE_SMOOTH);
//      glEnable(GL_POLYGON_SMOOTH);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

        w = 100;
        h = 100;
        if ((grid = init_life(w, h)) == NULL) {
                perror("malloc");
                exit(EX_OSERR);
        }

        min_zoom = 1.0;
        max_zoom = 15.0;
        zoom = 10.0;

        min_x = -0.5;
        min_y = -0.5;
        max_x = w + 0.5 - (((float)ww)/zoom);
        max_y = h + 0.5 - (((float)wh)/zoom);
        x = (min_x + max_x)/2;
        y = (min_y + max_y)/2;

        zoom_speed = 0.025;
        pan_speed = 0.5;

        ci = w/2;
        cj = h/2;

        held_l = 0;
        held_r = 0;
        held_d = 0;
        held_u = 0;
        running = 0;
        drawing = 0;
        erasing = 0;
        held_zoom_out = 0;
        held_zoom_in = 0;
        ticks = SDL_GetTicks();
        while (1) {
                while (SDL_PollEvent(&event)) switch (event.type) {
                        case SDL_MOUSEBUTTONDOWN:
                                if (event.button.button == SDL_BUTTON_LEFT) {
                                        drawing = 1;
                                        erasing = 0;
                                } else if (event.button.button == SDL_BUTTON_RIGHT) {
                                        erasing = 1;
                                        drawing = 0;
                                }
                        case SDL_MOUSEMOTION:
                                glGetDoublev(GL_MODELVIEW_MATRIX, model_view);
                                glGetDoublev(GL_PROJECTION_MATRIX, projection);
                                glGetIntegerv(GL_VIEWPORT, viewport);
                                gluUnProject(event.button.x, wh-event.button.y, 0.01, model_view, projection, viewport, &pos3D_x, &pos3D_y, &pos3D_z);
                                ci = floorf(pos3D_x);
                                cj = floorf(pos3D_y);
                                if (ci <  0) ci = 0;
                                if (ci >= w) ci = w-1;
                                if (cj <  0) cj = 0;
                                if (cj >= h) cj = h-1;
                                if (drawing) set_life(grid, ci, cj);
                                else if (erasing) unset_life(grid, ci, cj);
                                break;
                        case SDL_MOUSEBUTTONUP:
                                if (event.button.button == SDL_BUTTON_LEFT) drawing = 0;
                                else if (event.button.button == SDL_BUTTON_RIGHT) erasing = 0;
                                break;
                        case SDL_KEYDOWN:
                                key = event.key.keysym.sym;
                                if      (key == SDLK_q       ) exit(EX_OK);
                                else if (key == SDLK_LEFT    ) held_l        = 1    ;
                                else if (key == SDLK_RIGHT   ) held_r        = 1    ;
                                else if (key == SDLK_DOWN    ) held_d        = 1    ;
                                else if (key == SDLK_UP      ) held_u        = 1    ;
                                else if (key == SDLK_MINUS   ) held_zoom_out = 1    ;
                                else if (key == SDLK_EQUALS  ) held_zoom_in  = 1    ;
                                else if (key == SDLK_HOME    ) x             = min_x;
                                else if (key == SDLK_END     ) x             = max_x;
                                else if (key == SDLK_PAGEDOWN) y             = min_y;
                                else if (key == SDLK_PAGEUP  ) y             = max_y;
                                else if (key == SDLK_SPACE   ) {
                                        begin = SDL_GetTicks();
                                        running ^= 1;
                                }
                                break;
                        case SDL_KEYUP:
                                key = event.key.keysym.sym;
                                if      (key == SDLK_LEFT  ) held_l        = 0;
                                else if (key == SDLK_RIGHT ) held_r        = 0;
                                else if (key == SDLK_DOWN  ) held_d        = 0;
                                else if (key == SDLK_UP    ) held_u        = 0;
                                else if (key == SDLK_MINUS ) held_zoom_out = 0;
                                else if (key == SDLK_EQUALS) held_zoom_in  = 0;
                                break;
                        case SDL_VIDEORESIZE:
                                if ((event.resize.w > 0)&&(event.resize.h > 0)) {
                                        ww = event.resize.w;
                                        wh = event.resize.h;
                                        aspect_ratio = ((float)wh)/ww;
                                        screen = SDL_SetVideoMode(ww, wh, 32, SDL_RESIZABLE|SDL_OPENGL);
                                        glViewport(0, 0, ww, wh);
                                        glMatrixMode(GL_PROJECTION);
                                        glLoadIdentity();
                                        glOrtho(0, 1, 0, aspect_ratio, 0, 1);
                                        glClearColor(0, 0, 0, 0);
                                        glEnable(GL_BLEND);
                                        glEnable(GL_POINT_SMOOTH);
                                        glEnable(GL_LINE_SMOOTH);
                                        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
                                        max_x = w + 0.5 - (((float)ww)/zoom);
                                        max_y = h + 0.5 - (((float)wh)/zoom);
                                }
                                break;
                        case SDL_QUIT:
                                exit(EX_OK);
                        default:
                                break;
                }

                if (held_l&&((x -= pan_speed) < min_x)) x = min_x;
                if (held_r&&((x += pan_speed) > max_x)) x = max_x;
                if (held_d&&((y -= pan_speed) < min_y)) y = min_y;
                if (held_u&&((y += pan_speed) > max_y)) y = max_y;
                if (held_zoom_out) {
                        if ((zoom -= zoom*zoom_speed) < min_zoom) zoom = min_zoom;
                        max_x = w + 0.5 - (((float)ww)/zoom);
                        max_y = h + 0.5 - (((float)wh)/zoom);
                }
                if (held_zoom_in) {
                        if ((zoom += zoom*zoom_speed) > max_zoom) zoom = max_zoom;
                        max_x = w + 0.5 - (((float)ww)/zoom);
                        max_y = h + 0.5 - (((float)wh)/zoom);
                }

                glClear(GL_COLOR_BUFFER_BIT);

                glMatrixMode(GL_MODELVIEW);
                glLoadIdentity();
                glScalef(zoom/ww, zoom/ww, 1);
                glTranslatef(-x, -y, 0);

                if (running && (((SDL_GetTicks() - begin) > 250) == 0)) {
                        advance_life(grid, w, h);
                        begin = SDL_GetTicks();
                }

                glBegin(GL_LINES);
                glColor3f(1, 1, 1);
                glVertex3f(0, 0, 0);
                glVertex3f(0, h, 0);
                glVertex3f(0, h, 0);
                glVertex3f(w, h, 0);
                glVertex3f(w, h, 0);
                glVertex3f(w, 0, 0);
                glVertex3f(w, 0, 0);
                glVertex3f(0, 0, 0);
                glColor3f(0.5F, 0.5F, 0.5F);
                for (i = 1; i < w; ++i) {
                        glVertex3f(i, 0, 0);
                        glVertex3f(i, h, 0);
                }
                for (j = 1; j < h; ++j) {
                        glVertex3f(0, j, 0);
                        glVertex3f(w, j, 0);
                }
                glEnd();

                glBegin(GL_QUADS);
                glColor3f(1, 1, 1);
                for (j = 0; j < h; ++j) for (i = 0; i < w; i++) if (get_life(grid, i, j)) {
                        glVertex3f(i, j, 0);
                        glVertex3f(i+1, j, 0);
                        glVertex3f(i+1, j+1, 0);
                        glVertex3f(i, j+1, 0);
                }
                glColor4f(0, 1, 0, 0.3F);
                glVertex3f(ci, cj, 0);
                glVertex3f(ci+1, cj, 0);
                glVertex3f(ci+1, cj+1, 0);
                glVertex3f(ci, cj+1, 0);
                glEnd();

                SDL_GL_SwapBuffers();

                if (glGetError() != GL_NO_ERROR) {
                        /* NOTE: many of these can probably be safely ignored. */
                        fprintf(stderr, "OpenGL error!\n");
                        exit(EX_SOFTWARE);
                }

                /* TODO: fix this. */
                if (fps_limit != 0) {
                        fprintf(stderr, "%f\n", (1000.0F/fps_limit) - (1.0F/(SDL_GetTicks() - ticks)));
                        delay = (1000.0F/fps_limit) - (1.0F/(SDL_GetTicks() - ticks));
                        if (verbose) fprintf(stderr, "delay = %dms: ", delay);
                        if (delay > 0) {
                                SDL_Delay(delay);
                        }
                }
                fprintf(stderr, "%f fps\n", 1000.0F/(SDL_GetTicks() - ticks));
                ticks = SDL_GetTicks();
        }

        exit(EX_OK);
}
Ejemplo n.º 3
0
int main(int argc, char *argv[]){
	
	
	
	// for debugging only
	rand_test(100);
	
	
	// random seed based on what time it is.
	sgenrand(time(NULL));
	
	
	// initialize the necessary globals
	init_life();
	init_stats();
	
	
	
	// make a test lifeform
	struct lifeformData *test1, *test2, *child;
	// generate two parents
	test1 = lifeform_generate();
	
	//printf("test1 pointer = %d\n",(int)test1);
	test2 = lifeform_generate();
	//printf("test2 pointer = %d\n",(int)test2);
	child = lifeform_mate(test1, test2, false);
	
	
	
	
	
	FILE *printfile;
	
	printfile = fopen("test1.txt", "w");
	print_lifeformData(printfile, test1);
	//if(test1 != NULL)free(test1);//lifeform_kill(test1);
	fclose(printfile);
	
	printfile = fopen("test2.txt", "w");
	print_lifeformData(printfile, test2);
	//if(test2 != NULL)free(test2);//lifeform_kill(test2);
	fclose(printfile);
	
	printfile = fopen("child.txt", "w");
	print_lifeformData(printfile, child);
	//if(child != NULL)free(child);//lifeform_kill(child);
	fclose(printfile);
	
	/*
	struct lifeformData *genLife = lifeform_generate();
	FILE *printfile;
	printfile = fopen("new.txt", "w");
	print_lifeformData(printfile, genLife);
	fclose(printfile);
	
	free(genLife);
	
	printf("sucess!");
	char dummy;
	scanf("%c",&dummy);
	*/
	
	
	
	// quit
	return 0;
}
Ejemplo n.º 4
0
//------------------------------------------------------------------------------
// main ------------------------------------------------------------------------
void main(void)
{
	RBX430_init(_16MHZ);				// init board
	ERROR2(lcd_init());					// init LCD
	//lcd_volume(376);					// increase LCD brightness
	watchdog_init();					// init watchdog
	port1_init();						// init P1.0-3 switches
	__bis_SR_register(GIE);				// enable interrupts

	lcd_clear();
	memset(life, 0, sizeof(life));		// clear life array
	lcd_backlight(ON);
	lcd_wordImage(life_image, (HD_X_MAX - 126) / 2, 50, 1);
	lcd_cursor(10, 20);
	printf("\b\tPress Any Key");
	switches = 0;						// clear switches flag

	life_pr = life_prev;
	life_cr = life_cur;
	life_nr = life_nex;

	while (!switches);					// wait for any switch

	while (1)							// new pattern seed
	{

		uint16 generation;				// generation counter
		uint16 row, col;

		WDT_Sec_Cnt = WDT_1SEC_CNT;		// reset WD 1 second counter
		seconds = 0;					// clear second counter
		generation = 0;					// start generation counter
		int loop;
		int neighbors;
		int left;
		int middle;
		int current;
		int right;
		loop = 1;
		memset(life, 0, sizeof(life));				// clear life array
		memset(life_pr, 0, 10 * sizeof(uint8));		// clear slider
		memset(life_cr, 0, 10 * sizeof(uint8));		// clear slider
		memset(life_nr, 0, 10 * sizeof(uint8));		// clear slider
		init_life(switches);						// load seed based on switch
		switches = 0;								// reset switches



		while (loop)								// next generation
		{
			RED_TOGGLE;
			memcpy(life_pr, life[79], 10 * sizeof(uint8));
			memcpy(life_cr, life[78], 10 * sizeof(uint8));
			memcpy(life_nr, life[77], 10 * sizeof(uint8));
			// for each life row (78 down to 1)
			for (row = NUM_ROWS-2; row > 0; row--)
			{
				left = 0;
				neighbors = 0;
				current = TEST_CELL(life_cr, 1);
				right = TEST_CELL(life_pr, 2) + TEST_CELL(life_cr, 2) + TEST_CELL(life_nr, 2);
				middle = TEST_CELL(life_pr, 1) + current + TEST_CELL(life_nr, 1);

				// for each life column (78 down to 1)
				for (col = 1; col < 79; col++)
				{
					neighbors = left + (middle - current) + right;

					//neighbors += (TEST_CELL(life_pr, (col - 1)) + TEST_CELL(life_pr, col) + TEST_CELL(life_pr, (col + 1)));		// add number of neighbors on row above
					//neighbors += (TEST_CELL(life_cr, (col - 1)) + TEST_CELL(life_cr, (col + 1)));												// add number of neighbors on current row
					//neighbors += (TEST_CELL(life_nr, (col - 1)) + TEST_CELL(life_nr, col) + TEST_CELL(life_nr, (col + 1)));		// add number of neighbors on row below

					if(current == 1)												// if the cell is currently alive
					{
						if(neighbors == 2 || neighbors == 3)							// the cell has 2 or 3 neighbors
						{
																						// do nothing, the cell remains alive
						}

						else															// the cell doesn't have necessary neighbors
						{
							CELL_DEATH(row, col);										// clear cell bit in life array
							CELL_DELETE(row, col);										// clear LCD 2x2 pixel point
						}
					}

					else																// the cell is currently dead
					{
						if(neighbors == 3)												// the cell has 3 live neighbors
						{
							CELL_BIRTH(row, col);										// set cell bit in life array
							CELL_DRAW(row, col);										// set LCD 2x2 pixel point
						}
					}

					neighbors = 0;														// reset neighbors
					left = middle;
					current = TEST_CELL(life_cr, col + 1);
					middle = right;
					right = TEST_CELL(life_pr, col + 2) + TEST_CELL(life_cr, col + 2) + TEST_CELL(life_nr, col + 2);
				}

				temp = life_pr;
				life_pr = life_cr;
				life_cr = life_nr;
				life_nr = temp;
				//memcpy(life_pr, life_cr, 10 * sizeof(uint8));			// sets next row
				//memcpy(life_cr, life_nr, 10 * sizeof(uint8));			// sets next row
				memcpy(life_nr, life[row - 2], 10 * sizeof(uint8));			// sets next row


			}

			// display life generation and generations/second on LCD
			if (display_results(++generation)) break;
			if(switches)
			{
				loop = 0;					// when a switch is pressed, exit the while loop
			}
		}

	}
} // end main()