Exemplo n.º 1
0
hashmap *hm_create(hm_compare compare, hm_hash hash){
	hashmap *map = calloc(1, sizeof(hashmap));
	check_mem(map);

	map->compare = (compare == NULL ? default_compare : compare);
	map->hash = (hash == NULL ? default_hash : hash);
	map->slots = DynArray_create(sizeof(DynArray *), DEFAULT_SLOT_NUM);
	map->slots->end = map->slots->max;
	check_mem(map->slots);

	return map;

error:
	if(map)
		hm_destroy(map);

	return NULL;
}
Exemplo n.º 2
0
int main(int ac, char **av) {
	////
	// Width, height
	////
	int width = 600;
	int height = 600;
	
	////
	// Random generator init
	////
	srand((unsigned) time(0));
	
	////
	// Alloc
	////
	vec3_field
		*vf0 = v3f_random_unit(3, 4, 2),
		*vf1 = v3f_random_unit(6, 5, 2),
		*vf2 = v3f_random_unit(8, 11, 2),
		*vf3 = v3f_random_unit(19, 21, 2);
	heightmap
		*hm0 = hm_perlin_noise(width, height, vf0, 0.5),
		*hm1 = hm_perlin_noise(width, height, vf1, 0.5),
		*hm2 = hm_perlin_noise(width, height, vf2, 0.5),
		*hm3 = hm_perlin_noise(width, height, vf3, 0.5);
	heightmap
		*hm = hm_new(width, height);
	
	////
	// Process
	////
	hm_add_scale(hm, hm0, 0.50);
	hm_add_scale(hm, hm1, 0.25);
	hm_add_scale(hm, hm2, 0.15);
	hm_add_scale(hm, hm3, 0.10);
	
	////
	// SDL TIME! FREE BEERS FOR EVERYONE AND THEIR DOG!
	// (though, you're the one paying, okay?)
	////
	if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
		printf("Error: SDL can't init.");
		return -1;
	}
	
	SDL_Surface *surface;
	int running = 1;
	SDL_Event evt;
	
	surface = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
	if(surface == NULL) {
		printf("Error: Can't set video mode.");
		return -1;
	}
	
	SDL_WM_SetCaption("Perlin noise test", NULL);
	
	while(running) {
		while(SDL_PollEvent(&evt)) {
			if(evt.type == SDL_QUIT) {
				running = 0;
			}
		}
		for(int y = 0; y < height; y++) for(int x = 0; x < width; x++) {
			Uint8 *pixel = (Uint8 *) surface->pixels + y * surface->pitch + x * 4;
			*(Uint32 *) pixel = to_pixel(surface->format, hm_get(hm, x, y));
		}
		SDL_Flip(surface);
	}
	
	SDL_Quit();
	
	////
	// Dealloc
	////
	v3f_destroy(vf0);
	v3f_destroy(vf1);
	v3f_destroy(vf2);
	v3f_destroy(vf3);
	hm_destroy(hm0);
	hm_destroy(hm1);
	hm_destroy(hm2);
	hm_destroy(hm3);
	hm_destroy(hm);
}