Exemple #1
0
int world_main_loop(int argc, char **argv, world_t *w){
	init_time();
	set_fps(90);
	world_set(w);
	/*printf("coucou2\n");	*/
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGB);
	glEnable(GL_DEPTH_TEST);
	glutInitWindowPosition(250,250);
	glutInitWindowSize(600,400);
	glutCreateWindow("BASTOS 85");
	draw_init();
	glutDisplayFunc(do_world);
	glutIdleFunc(do_world);
	glutReshapeFunc(draw_reshape);
	/*glutIgnoreKeyRepeat(1);*/
	glutKeyboardFunc(keyboard_down_func);
	glutKeyboardUpFunc(keyboard_up_func);
	glutMainLoop();
	return 0;
}
Exemple #2
0
int loadWorldText(char* file, struct world* world) {
	int fd = open(file, O_RDONLY);
	if (fd < 0) return -1;

	char** lines = NULL;
	size_t lineCount = 0;
	char* line = NULL;
	size_t maxWidth = 0;
	do {
		line = NULL;
		if (readLineDynamic(fd, &line) < 0 || line == NULL) break;
		if (lines == NULL) {
			lines = malloc(sizeof(char*));
			lineCount = 0;
		} else {
			lines = realloc(lines, sizeof(char*) * (lineCount + 1));
		}
		lines[lineCount++] = line;
		size_t wid = strlen(line);
		if (wid > maxWidth) maxWidth = wid;
	} while (line != NULL);

	close(fd);

	world->height = lineCount;
	world->width = maxWidth;

	if (world->width % 4 > 0) world->width += (4 - world->width % 4);
	if (world->height % 4 > 0) world->height += (4 - world->height % 4);

	world->data = malloc((world->height * world->width / 4) + 1);
	world->newData = calloc((world->height * world->width / 4) + 1, 1);

	for (size_t i = 0; i < world->height; i++) {
		if (i < lineCount) {
			size_t sl = strlen(lines[i]);
			for (size_t x = 0; x < world->width; x++) {
				if (x < sl) {
					uint8_t value = CELL_NONE;
					if (lines[i][x] == '#') value = CELL_WIRE;
					else if (lines[i][x] == '@') value = CELL_HEAD;
					else if (lines[i][x] == '~') value = CELL_TAIL;
					world_set(world->data, world->width, x, i, value);
				} else world_set(world->data, world->width, x, i, CELL_NONE);
			}
			free(lines[i]);
		} else {
			for (size_t x = 0; x < world->width; x++) {
				world_set(world->data, world->width, x, i, CELL_NONE);
			}
		}
	}
	free(lines);
	//world->vao = calloc(sizeof(struct vao*), 1);
	//world->verts = calloc(sizeof(struct vertex) * world->width * world->height * 4, 1);
	//glGenVertexArrays(1, &world->vao->vao);
	//printf("gvao %i\n", world->vao->vao);
	//glBindVertexArray(world->vao->vao);
	//glGenBuffers(1, world->vao->vbo);
	//glBindBuffer(GL_ARRAY_BUFFER, world->vao->vbo);
	//glBufferData(GL_ARRAY_BUFFER, sizeof(struct vertex) * world->width * world->height * 4, world->verts, GL_STATIC_DRAW);
	//world->vao->vertex_count = world->width * world->height * 4;
	//glVertexPointer(2, GL_FLOAT, sizeof(struct vertex), 0);
	//glEnableClientState (GL_VERTEX_ARRAY);
	return 0;
}