Ejemplo n.º 1
0
/* return true if digest calculated */
int get_file_digest(const char * filename, Uint8 digest[16])
{
	MD5 md5;
	el_file_ptr file = NULL;

	file = el_open(filename);

	memset (digest, 0, 16);

	if (file == NULL)
	{
		LOG_ERROR("MD5Digest: Unable to open %s (%d)", filename, errno);
		return 0;
	}
	
	if (el_get_pointer(file) == NULL)
	{
		el_close(file);
		return 0;
	}

	MD5Open(&md5);
	MD5Digest(&md5, el_get_pointer(file), el_get_size(file));
	MD5Close(&md5, digest);

	el_close(file);
	
	return 1;
}
Ejemplo n.º 2
0
Uint32 get_image_information(el_file_ptr file, image_t* image)
{
	Uint32 dds, result;

	if (file == 0)
	{
		LOG_ERROR("Invalid file!");

		return 0;
	}

	dds = 0;

	if (el_get_size(file) >= 4)
	{
		if (check_dds(el_get_pointer(file)))
		{
			dds = 1;
		}
	}

	if (dds == 1)
	{
		result = get_dds_information(file, image);

		el_seek(file, 0, SEEK_SET);

		return result;
	}
	else
	{
		return get_sdl_image_information(file, image);
	}
}
Ejemplo n.º 3
0
static Uint32 get_sdl_image_information(el_file_ptr file, image_t* image)
{
	SDL_Surface *image_surface;
	SDL_RWops *buffer;

	if (file == 0)
	{
		LOG_ERROR("Invalid file!");
		return 0;
	}

	buffer = SDL_RWFromMem(el_get_pointer(file), el_get_size(file));

	image_surface = IMG_Load_RW(buffer, 1);

	if (image_surface == 0)
	{
		LOG_ERROR("load_image() error: [%s] [%s]", el_file_name(file),
			IMG_GetError());
		return 0;
	}

	SDL_LockSurface(image_surface);

	memset(image, 0, sizeof(image_t));

	image->width = image_surface->w;
	image->height = image_surface->h;
	image->mipmaps = 1;
	image->format = ift_rgba8;

	if ((image_surface->format->BitsPerPixel == 8) &&
		(image_surface->format->palette != 0))
	{
		image->alpha = 0;
	}
	else
	{
		if (image_surface->format->Amask != 0)
		{
			image->alpha = 1;
		}
		else
		{
			image->alpha = 0;
		}
	}

	SDL_UnlockSurface(image_surface);
	SDL_FreeSurface(image_surface);

	return 1;
}
Ejemplo n.º 4
0
static void* read_dds(el_file_ptr file, DdsHeader *header,
	const Uint32 strip_mipmaps, const Uint32 base_level)
{
	Uint8* dst;
	Uint32 size, offset;

	size = get_dds_size(header, 0, strip_mipmaps, base_level);
	offset = get_dds_offset(header, base_level);

	dst = malloc_aligned(size, 16);

	memcpy(dst, el_get_pointer(file) + sizeof(DdsHeader) + offset + 4, size);

	return dst;
}
Ejemplo n.º 5
0
static void* unpack_dds(el_file_ptr file, DdsHeader *header,
	const Uint32 strip_mipmaps, const Uint32 base_level)
{
	Uint8* dest;
	Uint32 size, offset, bpp;

	size = get_dds_size(header, 0, strip_mipmaps, base_level);
	offset = get_dds_offset(header, base_level);
	bpp = header->m_pixel_format.m_bit_count / 8;

	dest = malloc_aligned(size, 16);

	fast_unpack(el_get_pointer(file) + sizeof(DdsHeader) + offset + 4, size / bpp,
		header->m_pixel_format.m_red_mask,
		header->m_pixel_format.m_green_mask,
		header->m_pixel_format.m_blue_mask,
		header->m_pixel_format.m_alpha_mask, dest);

	return dest;
}
Ejemplo n.º 6
0
static obj_2d_def* load_obj_2d_def(const char *file_name)
{
	int f_size;
	el_file_ptr file = NULL;
	char cur_dir[200]={0};
	obj_2d_def *cur_object;
	const char *obj_file_mem;
	const char* sep;

	sep = strrchr(file_name, '/');
	if (!sep || sep == file_name)
		*cur_dir = '\0';
	else
		my_strncp(cur_dir, file_name, sep-file_name+1);

	file = el_open(file_name);
	if (!file)
	{
		LOG_ERROR("%s: %s \"%s\": %s\n", reg_error_str, cant_open_file, file_name, strerror(errno));
		return NULL;
	}

	obj_file_mem = el_get_pointer(file);
	if (!obj_file_mem)
	{
		LOG_ERROR("%s: %s (read)\"%s\"\n", reg_error_str, cant_open_file, file_name);
		el_close(file);
		return NULL;
	}
	f_size = el_get_size(file);

	//ok, the file is loaded, so parse it
	cur_object=calloc(1, sizeof(obj_2d_def));
	my_strncp(cur_object->file_name, file_name,
		sizeof(cur_object->file_name));
	parse_2d0(obj_file_mem, f_size, cur_dir, cur_object);

	el_close(file);

	return cur_object;
}
Ejemplo n.º 7
0
int get_tile_map_sizes(const char *file_name, int *x, int *y)
{
	map_header cur_map_header;
	char* file_mem;
	el_file_ptr file;

	file = el_open(file_name);

	if (!file)
	{
		return 0;
	}

	file_mem = el_get_pointer(file);

	memcpy(&cur_map_header, file_mem, sizeof(cur_map_header));
	*x = SDL_SwapLE32(cur_map_header.tile_map_x_len);
	*y = SDL_SwapLE32(cur_map_header.tile_map_y_len);
	el_close(file);
	return 1;
}
Ejemplo n.º 8
0
static obj_2d_def* load_obj_2d_def(const char *file_name)
{
	int f_size;
	int i,k,l;
	el_file_ptr file = NULL;
	char cur_dir[200]={0};
	obj_2d_def *cur_object;
	char *obj_file_mem;
	char texture_file_name[256] = {0};
	float x_size,y_size;
	float alpha_test;
	int file_x_len;
	int file_y_len;
	int u_start,u_end,v_start,v_end;

	cur_object=calloc(1, sizeof(obj_2d_def));
	//get the current directory
	l=strlen(file_name);
	//parse the string backwards, until we find a /
	while(l>0)
		{
			if(file_name[l]=='/' || file_name[l]=='\\')break;
			l--;
		}

	i=0;
	if(l)//prevent invalid dir names
		{
			while(l>=0)
				{
					cur_dir[i]=file_name[i];
					i++;
					l--;
				}
			cur_dir[i+1]=0;
		}


	file = el_open(file_name);
	if(file == NULL){
		LOG_ERROR("%s: %s \"%s\": %s\n", reg_error_str, cant_open_file, file_name, strerror(errno));
		free(cur_object);
		return NULL;
	}

	obj_file_mem = el_get_pointer(file);
	
	if(obj_file_mem == NULL){
		LOG_ERROR("%s: %s (read)\"%s\"\n", reg_error_str, cant_open_file, file_name);
		el_close(file);
		free(cur_object);
		return NULL;
	}

	f_size = el_get_size(file);

	//ok, the file is loaded, so parse it
	file_x_len=get_integer_after_string("file_x_len:",obj_file_mem,f_size);
	file_y_len=get_integer_after_string("file_y_len:",obj_file_mem,f_size);
	u_start=get_integer_after_string("u_start:",obj_file_mem,f_size);
	u_end=get_integer_after_string("u_end:",obj_file_mem,f_size);
	v_start=get_integer_after_string("v_start:",obj_file_mem,f_size);
	v_end=get_integer_after_string("v_end:",obj_file_mem,f_size);
	x_size=get_float_after_string("x_size:",obj_file_mem,f_size);
	y_size=get_float_after_string("y_size:",obj_file_mem,f_size);
	alpha_test=get_float_after_string("alpha_test:",obj_file_mem,f_size);
	if(alpha_test<0)alpha_test=0;

	//get the proper u/v coordinates
	cur_object->u_start=(float)u_start/file_x_len;
	cur_object->u_end=(float)u_end/file_x_len;
	cur_object->v_start=1.0f-(float)v_start/file_y_len;
	cur_object->v_end=1.0f-(float)v_end/file_y_len;
	cur_object->x_size=x_size;
	cur_object->y_size=y_size;
	cur_object->alpha_test=alpha_test;

	//now  find the texture name
	i=get_string_occurance("texture:",obj_file_mem,40,0);
	obj_file_mem+=i;
	k=0;
	//find the file name
	while(k<128)
		{
			if(obj_file_mem[k]!=' ' && obj_file_mem[k]!=0x0a)break;
			k++;
		}
	//we found the beginning of the file name
	//now, copy the current directory string to the file_name string
	i=strlen(cur_dir);
	l=0;
	while(l<i)
		{
			texture_file_name[l]=cur_dir[l];
			l++;
		}
	while(l<128)
		{
			if(obj_file_mem[k]!=' ' && obj_file_mem[k]!=0x0a
			   && obj_file_mem[k]!=0x0d)
				{
					texture_file_name[l]=obj_file_mem[k];
					k++;
					l++;
				}
			else
				{
					texture_file_name[l]=0;
					break;
				}
		}

	cur_object->texture_id = load_texture_cached(texture_file_name, tt_mesh);
	//now get the object type
	i=get_string_occurance("type:",obj_file_mem,f_size,0);
	obj_file_mem+=i;
	k=0;
	for(k=0;k<10;k++)
		{
			if(obj_file_mem[k]==0x0a)
				{
					cur_object->object_type=INVALID;
					break;
				}
			if(obj_file_mem[k]==' ')continue;

			if(obj_file_mem[k]=='g' || obj_file_mem[k]=='G')
				{
					cur_object->object_type=GROUND;
					break;
				}

			if(obj_file_mem[k]=='p' || obj_file_mem[k]=='P')
				{
					cur_object->object_type=PLANT;
					break;
				}

			if(obj_file_mem[k]=='f' || obj_file_mem[k]=='F')
				{
					cur_object->object_type=FENCE;
					break;
				}
		}
	el_close(file);

	return cur_object;
}
Ejemplo n.º 9
0
void load_cursors()
{
	int cursors_colors_no, x, y, i;
	Uint8 * cursors_mem_bmp;
	Uint8 cur_color;
	el_file_ptr file;

	file = el_open("textures/cursors.bmp");

	if (file == NULL)
	{
		LOG_ERROR("%s: %s [%s]\n", reg_error_str, cursors_file_str, "textures/cursors.bmp");
		return;
	}

	if ((cursors_mem_bmp = el_get_pointer(file)) == NULL)
	{
		el_close(file);
		LOG_ERROR("%s: %s (read) [%s]\n", reg_error_str, cursors_file_str, "textures/cursors.bmp");
		return;
	}

	cursors_mem_bmp += 18;		//x length is at offset+18
	cursors_x_length = SDL_SwapLE32(*((int *) cursors_mem_bmp));
	cursors_mem_bmp += 4;		//y length is at offset+22
	cursors_y_length = SDL_SwapLE32(*((int *) cursors_mem_bmp));
	cursors_mem_bmp += 46 - 22;
	cursors_colors_no = SDL_SwapLE32(*((int *) cursors_mem_bmp));
	cursors_mem_bmp += 54 - 46 + cursors_colors_no * 4;

	//ok, now transform the bitmap in cursors info
	if(cursors_mem) free(cursors_mem);
	cursors_mem = (Uint8 *)calloc ( cursors_x_length*cursors_y_length*2, sizeof(char));

	for(y=cursors_y_length-1;y>=0;y--)
		{
			i=(cursors_y_length-y-1)*cursors_x_length;
			for(x=0;x<cursors_x_length;x++)
				{
					cur_color=*(cursors_mem_bmp+y*cursors_x_length+x);
					switch(cur_color) {
					case 0: //transparent
						*(cursors_mem+(i+x)*2)=0;
						*(cursors_mem+(i+x)*2+1)=0;
						break;
					case 1: //white
						*(cursors_mem+(i+x)*2)=0;
						*(cursors_mem+(i+x)*2+1)=1;
						break;
					case 2: //black
						*(cursors_mem+(i+x)*2)=1;
						*(cursors_mem+(i+x)*2+1)=1;
						break;
					case 3: //reverse
						*(cursors_mem+(i+x)*2)=1;
						*(cursors_mem+(i+x)*2+1)=0;
						break;
					}
				}

		}
	el_close(file);
}
Ejemplo n.º 10
0
static int do_load_map(const char *file_name, update_func *update_function)
{
	int i;
	int cur_tile, j;
	AABBOX bbox;
	map_header cur_map_header;
	char* file_mem;
#ifdef CLUSTER_INSIDES
	char* occupied = 0;
	int have_clusters;
#endif
	object3d_io* objs_3d;
	obj_2d_io* objs_2d;
	light_io* lights;
	particles_io* particles;
#ifndef FASTER_MAP_LOAD
	float progress;
#endif
	el_file_ptr file;

	file = el_open(file_name);

	if (!file)
	{
		return 0;
	}

#ifdef EXTRA_DEBUG
	ERR();
#endif
	file_mem = el_get_pointer(file);

	my_strcp(map_file_name, file_name);

	LOG_DEBUG("Loading map '%s'.", file_name);

	main_bbox_tree_items = create_bbox_items(1024);

	memcpy(&cur_map_header, file_mem, sizeof(cur_map_header));
	cur_map_header.tile_map_x_len = SDL_SwapLE32(cur_map_header.tile_map_x_len);
	cur_map_header.tile_map_y_len = SDL_SwapLE32(cur_map_header.tile_map_y_len);
	cur_map_header.tile_map_offset = SDL_SwapLE32(cur_map_header.tile_map_offset);
	cur_map_header.height_map_offset = SDL_SwapLE32(cur_map_header.height_map_offset);
	cur_map_header.obj_3d_struct_len = SDL_SwapLE32(cur_map_header.obj_3d_struct_len);
	cur_map_header.obj_3d_no = SDL_SwapLE32(cur_map_header.obj_3d_no);
	cur_map_header.obj_3d_offset = SDL_SwapLE32(cur_map_header.obj_3d_offset);
	cur_map_header.obj_2d_struct_len = SDL_SwapLE32(cur_map_header.obj_2d_struct_len);
	cur_map_header.obj_2d_no = SDL_SwapLE32(cur_map_header.obj_2d_no);
	cur_map_header.obj_2d_offset = SDL_SwapLE32(cur_map_header.obj_2d_offset);
	cur_map_header.lights_struct_len = SDL_SwapLE32(cur_map_header.lights_struct_len);
	cur_map_header.lights_no = SDL_SwapLE32(cur_map_header.lights_no);
	cur_map_header.lights_offset = SDL_SwapLE32(cur_map_header.lights_offset);

	cur_map_header.ambient_r = SwapLEFloat(cur_map_header.ambient_r);
	cur_map_header.ambient_g = SwapLEFloat(cur_map_header.ambient_g);
	cur_map_header.ambient_b = SwapLEFloat(cur_map_header.ambient_b);

	cur_map_header.particles_struct_len = SDL_SwapLE32(cur_map_header.particles_struct_len);
	cur_map_header.particles_no = SDL_SwapLE32(cur_map_header.particles_no);
	cur_map_header.particles_offset = SDL_SwapLE32(cur_map_header.particles_offset);

#ifdef CLUSTER_INSIDES
	cur_map_header.clusters_offset = SDL_SwapLE32(cur_map_header.clusters_offset);
#endif

	LOG_DEBUG("Checking map '%s' file signature.", file_name);

	//verify if we have a valid file
	if(cur_map_header.file_sig[0]!='e'||
	   cur_map_header.file_sig[1]!='l'||
	   cur_map_header.file_sig[2]!='m'||
	   cur_map_header.file_sig[3]!='f')
	{
		LOG_ERROR(invalid_map, map_file_name);
		exit_now = 1; // We might as well quit...
		el_close(file);

		return 0;
	}

	LOG_DEBUG("Checking map '%s' sizes.", file_name);

	// Check the sizes of structures. If they don't match, we have a
	// major problem, since these structures are supposed to be
	// written flat out to disk.
	if (cur_map_header.obj_3d_struct_len != sizeof (object3d_io)
	    || cur_map_header.obj_2d_struct_len != sizeof (obj_2d_io)
	    || cur_map_header.lights_struct_len != sizeof (light_io)
	    || (cur_map_header.particles_struct_len != sizeof (particles_io) && cur_map_header.particles_no > 0)
	   )
	{
		LOG_ERROR ("Invalid object size on map %s", map_file_name);
		exit_now = 1; // We might as well quit...
		el_close(file);

		return 0;
	}

	update_function(load_map_str, 0);

	//get the map size
	tile_map_size_x = cur_map_header.tile_map_x_len;
	tile_map_size_y = cur_map_header.tile_map_y_len;

	LOG_DEBUG("Map '%s' size <%d, %d>.", file_name, tile_map_size_x,
		tile_map_size_y);

	// allocate the tile map (it was destroyed), and fill it
	tile_map = calloc (tile_map_size_x*tile_map_size_y, 1);
	memcpy(tile_map, file_mem + cur_map_header.tile_map_offset, tile_map_size_x*tile_map_size_y);
	begin_managing_memchunk(tile_map);

	// allocate the height map, and fill it
	height_map = calloc (tile_map_size_x*tile_map_size_y*6*6, 1);
	memcpy(height_map, file_mem + cur_map_header.height_map_offset, tile_map_size_x*tile_map_size_y*6*6);
	begin_managing_memchunk(height_map);

#ifdef CLUSTER_INSIDES
	// check if we need to compute the clusters, or if they're stored
	// in the map
	have_clusters = (cur_map_header.clusters_offset > 0)
	                && ((cur_map_header.clusters_offset +
			tile_map_size_x * tile_map_size_y * 6 * 6 *
			sizeof(short)) <= el_get_size(file));

	LOG_DEBUG("Map '%s' has clusters: %d.", file_name, have_clusters);

	if (have_clusters)
	{
		// clusters are stored in the map, set them
		set_clusters (file_mem + cur_map_header.clusters_offset);
	}
	else
	{
		// We need to compute the clusters, allocate memory for
		// the occupation array, and initialize it with the
		// tile and height maps
		occupied = calloc (tile_map_size_x*tile_map_size_y*6*6, 1);
		update_occupied_with_tile_map (occupied, tile_map);
		update_occupied_with_height_map (occupied, height_map);
	}
#endif

	LOG_DEBUG("Map '%s' is dungeon %d and ambient <%f, %f, %f>.",
		file_name, cur_map_header.dungeon, cur_map_header.ambient_r,
		cur_map_header.ambient_g, cur_map_header.ambient_b);
	//get the type of map, and the ambient light
	dungeon = cur_map_header.dungeon;
	ambient_r = cur_map_header.ambient_r;
	ambient_g = cur_map_header.ambient_g;
	ambient_b = cur_map_header.ambient_b;

/* 	water_tiles_extension = (tile_map_size_x > tile_map_size_y ? */
/* 							 tile_map_size_x : tile_map_size_y * 1.5); */
/* 	if (water_tiles_extension < 500.0) */
/* 		water_tiles_extension = 500.0 - water_tiles_extension; */
/* 	else */
/* 		water_tiles_extension = 0.0; */

	LOG_DEBUG("Loading tiles");
	//load the tiles in this map, if not already loaded
	load_map_tiles();

	LOG_DEBUG("Initializing buffers");
	init_buffers();
#ifndef CLUSTER_INSIDES
	for(i = 0; i < tile_map_size_y; i++)
	{
		bbox.bbmin[Y] = i*3.0f;
		bbox.bbmax[Y] = (i+1)*3.0f;
		if (i == 0)
			bbox.bbmin[Y] -= water_tiles_extension;
		else if (i == tile_map_size_y-1)
			bbox.bbmax[Y] += water_tiles_extension;
		for(j = 0; j < tile_map_size_x; j++)
		{
			cur_tile = tile_map[i*tile_map_size_x+j];
			if (cur_tile != 255)
			{
				bbox.bbmin[X] = j*3.0f;
				bbox.bbmax[X] = (j+1)*3.0f;
				if (j == 0)
					bbox.bbmin[X] -= water_tiles_extension;
				else if (j == tile_map_size_x-1)
					bbox.bbmax[X] += water_tiles_extension;
				if (IS_WATER_TILE(cur_tile))
				{
					bbox.bbmin[Z] = -0.25f;
					bbox.bbmax[Z] = -0.25f;
					if (IS_REFLECTING(cur_tile)) add_water_to_list(main_bbox_tree_items, get_terrain_id(j, i), bbox, cur_tile, 1);
					else add_water_to_list(main_bbox_tree_items, get_terrain_id(j, i), bbox, cur_tile, 0);
				}
				else 
				{
					bbox.bbmin[Z] = 0.0f;
					bbox.bbmax[Z] = 0.0f;
					add_terrain_to_list(main_bbox_tree_items, get_terrain_id(j, i), bbox, cur_tile);
				}
			}
		}
	}
#endif // CLUSTER_INSIDES

#ifdef FASTER_MAP_LOAD
	update_function(load_3d_object_str, 20.0f);
#else  // FASTER_MAP_LOAD
	progress = (cur_map_header.obj_3d_no + 249) / 250;
	if (progress > 0.0f)
	{
		update_function(load_3d_object_str, 0.0f);
		progress = 20.0f / progress;
	}
	else
	{
		update_function(load_3d_object_str, 20.0f);
		progress = 0.0f;
	}
#endif // FASTER_MAP_LOAD

	LOG_DEBUG("Loading %d 3d objects.", cur_map_header.obj_3d_no);

	//read the 3d objects
#ifndef FASTER_MAP_LOAD
	clear_objects_list_placeholders();
#endif
	objs_3d = (object3d_io*) (file_mem + cur_map_header.obj_3d_offset);

	ENTER_DEBUG_MARK("load 3d objects");
	for (i = 0; i < cur_map_header.obj_3d_no; i++)
	{
		object3d_io cur_3d_obj_io = objs_3d[i];

		cur_3d_obj_io.x_pos = SwapLEFloat (cur_3d_obj_io.x_pos);
		cur_3d_obj_io.y_pos = SwapLEFloat (cur_3d_obj_io.y_pos);
		cur_3d_obj_io.z_pos = SwapLEFloat (cur_3d_obj_io.z_pos);
		cur_3d_obj_io.x_rot = SwapLEFloat (cur_3d_obj_io.x_rot);
		cur_3d_obj_io.y_rot = SwapLEFloat (cur_3d_obj_io.y_rot);
		cur_3d_obj_io.z_rot = SwapLEFloat (cur_3d_obj_io.z_rot);
		cur_3d_obj_io.r = SwapLEFloat (cur_3d_obj_io.r);
		cur_3d_obj_io.g = SwapLEFloat (cur_3d_obj_io.g);
		cur_3d_obj_io.b = SwapLEFloat (cur_3d_obj_io.b);

		LOG_DEBUG("Adding 3d object (%d) '%s' at <%d, %f, %f> with "
			"rotation <%f, %f, %f>, left lit %d, blended %d and "
			"color <%f, %f, %f>.", i, cur_3d_obj_io.file_name,
			cur_3d_obj_io.x_pos, cur_3d_obj_io.y_pos,
			cur_3d_obj_io.z_pos, cur_3d_obj_io.x_rot,
			cur_3d_obj_io.y_rot, cur_3d_obj_io.z_rot,
			cur_3d_obj_io.self_lit, cur_3d_obj_io.blended,
			cur_3d_obj_io.r, cur_3d_obj_io.g, cur_3d_obj_io.b);

		if (cur_3d_obj_io.blended != 20)
		{
#ifdef CLUSTER_INSIDES
			int id;

			if (cur_3d_obj_io.blended != 1)
				cur_3d_obj_io.blended = 0;
			id = add_e3d (cur_3d_obj_io.file_name, cur_3d_obj_io.x_pos, cur_3d_obj_io.y_pos,
				cur_3d_obj_io.z_pos, cur_3d_obj_io.x_rot, cur_3d_obj_io.y_rot,
				cur_3d_obj_io.z_rot, cur_3d_obj_io.self_lit, cur_3d_obj_io.blended,
				cur_3d_obj_io.r, cur_3d_obj_io.g, cur_3d_obj_io.b, 0);

			if (!have_clusters)
				update_occupied_with_3d (occupied, id);
#else
			if (cur_3d_obj_io.blended != 1) cur_3d_obj_io.blended = 0;
			add_e3d (cur_3d_obj_io.file_name, cur_3d_obj_io.x_pos, cur_3d_obj_io.y_pos,
				cur_3d_obj_io.z_pos, cur_3d_obj_io.x_rot, cur_3d_obj_io.y_rot,
				cur_3d_obj_io.z_rot, cur_3d_obj_io.self_lit, cur_3d_obj_io.blended,
				cur_3d_obj_io.r, cur_3d_obj_io.g, cur_3d_obj_io.b, 0);
#endif
		}
		else
		{
			inc_objects_list_placeholders();
		}

#ifndef FASTER_MAP_LOAD
		if (i % 250 == 0)
		{
			update_function(load_3d_object_str, progress);
		}
#endif
	}
	LEAVE_DEBUG_MARK("load 3d objects");

#ifdef FASTER_MAP_LOAD
	update_function(load_2d_object_str, 20.0f);
#else  // FASTER_MAP_LOAD
	progress = (cur_map_header.obj_2d_no + 249) / 250;
	if (progress > 0)
	{
		update_function(load_2d_object_str, 0.0f);
		progress = 20.0f / progress;
	}
	else
	{
		update_function(load_2d_object_str, 20.0f);
		progress = 0.0f;
	}
#endif // FASTER_MAP_LOAD

	LOG_DEBUG("Loading %d 2d objects.", cur_map_header.obj_2d_no);

	//read the 2d objects
	objs_2d = (obj_2d_io*) (file_mem + cur_map_header.obj_2d_offset);

	ENTER_DEBUG_MARK("load 2d objects");
	for (i = 0; i < cur_map_header.obj_2d_no; i++)
	{
		obj_2d_io cur_2d_obj_io = objs_2d[i];
#ifdef CLUSTER_INSIDES
		int id;
#endif

		cur_2d_obj_io.x_pos = SwapLEFloat(cur_2d_obj_io.x_pos);
		cur_2d_obj_io.y_pos = SwapLEFloat(cur_2d_obj_io.y_pos);
		cur_2d_obj_io.z_pos = SwapLEFloat(cur_2d_obj_io.z_pos);
		cur_2d_obj_io.x_rot = SwapLEFloat(cur_2d_obj_io.x_rot);
		cur_2d_obj_io.y_rot = SwapLEFloat(cur_2d_obj_io.y_rot);
		cur_2d_obj_io.z_rot = SwapLEFloat(cur_2d_obj_io.z_rot);

		LOG_DEBUG("Adding 2d object (%d) '%s' at <%d, %f, %f> with "
			"rotation <%f, %f, %f>.", i, cur_2d_obj_io.file_name,
			cur_2d_obj_io.x_pos, cur_2d_obj_io.y_pos,
			cur_2d_obj_io.z_pos, cur_2d_obj_io.x_rot,
			cur_2d_obj_io.y_rot, cur_2d_obj_io.z_rot);

#ifndef SHOW_FLICKERING
		// Add in low-order bits to prevent flicker.
		cur_2d_obj_io.z_pos += offset_2d;
		offset_2d += offset_2d_increment;
		if (offset_2d >= offset_2d_max)
			offset_2d = offset_2d_increment;
#endif

#ifdef FASTER_MAP_LOAD
#ifdef CLUSTER_INSIDES
		id = add_2d_obj(i, cur_2d_obj_io.file_name, cur_2d_obj_io.x_pos, cur_2d_obj_io.y_pos,
			cur_2d_obj_io.z_pos, cur_2d_obj_io.x_rot, cur_2d_obj_io.y_rot,
			cur_2d_obj_io.z_rot, 0);
		if (!have_clusters)
			update_occupied_with_2d (occupied, id);
#else  // CLUSTER_INSIDES
		add_2d_obj(i, cur_2d_obj_io.file_name, cur_2d_obj_io.x_pos, cur_2d_obj_io.y_pos,
			cur_2d_obj_io.z_pos, cur_2d_obj_io.x_rot, cur_2d_obj_io.y_rot,
			cur_2d_obj_io.z_rot, 0);
#endif // CLUSTER_INSIDES
#else  // FASTER_MAP_LOAD
#ifdef CLUSTER_INSIDES
		id = add_2d_obj (cur_2d_obj_io.file_name, cur_2d_obj_io.x_pos, cur_2d_obj_io.y_pos,
			cur_2d_obj_io.z_pos, cur_2d_obj_io.x_rot, cur_2d_obj_io.y_rot,
			cur_2d_obj_io.z_rot, 0);
		if (!have_clusters)
			update_occupied_with_2d (occupied, id);
#else  // CLUSTER_INSIDES
		add_2d_obj(cur_2d_obj_io.file_name, cur_2d_obj_io.x_pos, cur_2d_obj_io.y_pos,
			cur_2d_obj_io.z_pos, cur_2d_obj_io.x_rot, cur_2d_obj_io.y_rot,
			cur_2d_obj_io.z_rot, 0);
#endif // CLUSTER_INSIDES

		if (i % 250 == 0)
		{
			update_function(load_2d_object_str, progress);
		}
#endif // FASTER_MAP_LOAD
	}
	LEAVE_DEBUG_MARK("load 2d objects");

#ifdef CLUSTER_INSIDES
	// If we need to compute the clusters, do it here, so that the
	// newly added lights and particle systems get the right cluster
	// number automagically. We don't update the occupation map with
	// lights or particle systems since I don't expect them to bridge
	// any clusters, and if they do happen to hang in the void,
	// they'll be shown anyway.
	if (!have_clusters)
	{
		compute_clusters (occupied);
		free (occupied);

		// Ok, we have the clusters, now assign new IDs to each
		// object that we added.
		for (i = 0; i < MAX_OBJ_3D; i++)
		{
			if (objects_list[i])
			{
				int x = (int) (objects_list[i]->x_pos / 0.5f);
				int y = (int) (objects_list[i]->y_pos / 0.5f);
				objects_list[i]->cluster = get_cluster (x, y);
			}
		}

		for (i = 0; i < MAX_OBJ_2D; i++)
		{
			if (obj_2d_list[i])
			{
				int x = (int) (obj_2d_list[i]->x_pos / 0.5f);
				int y = (int) (obj_2d_list[i]->y_pos / 0.5f);
				obj_2d_list[i]->cluster = get_cluster (x, y);
			}
		}
	}

	// we finally add the tiles to the abt
	for(i = 0; i < tile_map_size_y; i++)
	{
		bbox.bbmin[Y] = i*3.0f;
		bbox.bbmax[Y] = (i+1)*3.0f;
		if (i == 0)
			bbox.bbmin[Y] -= water_tiles_extension;
		else if (i == tile_map_size_y-1)
			bbox.bbmax[Y] += water_tiles_extension;
		for(j = 0; j < tile_map_size_x; j++)
		{
			current_cluster = get_cluster(j*6, i*6);
			cur_tile = tile_map[i*tile_map_size_x+j];
			if (cur_tile != 255)
			{
				bbox.bbmin[X] = j*3.0f;
				bbox.bbmax[X] = (j+1)*3.0f;
				if (j == 0)
					bbox.bbmin[X] -= water_tiles_extension;
				else if (j == tile_map_size_x-1)
					bbox.bbmax[X] += water_tiles_extension;
				if (IS_WATER_TILE(cur_tile))
				{
					bbox.bbmin[Z] = -0.25f;
					bbox.bbmax[Z] = -0.25f;
					if (IS_REFLECTING(cur_tile)) add_water_to_list(main_bbox_tree_items, get_terrain_id(j, i), bbox, cur_tile, 1);
					else add_water_to_list(main_bbox_tree_items, get_terrain_id(j, i), bbox, cur_tile, 0);
				}
				else
				{
					bbox.bbmin[Z] = 0.0f;
					bbox.bbmax[Z] = 0.0f;
					add_terrain_to_list(main_bbox_tree_items, get_terrain_id(j, i), bbox, cur_tile);
				}
			}
		}
	}
#endif

#ifdef FASTER_MAP_LOAD
	update_function(load_lights_str, 20.0f);
#else  // FASTER_MAP_LOAD
	progress = (cur_map_header.lights_no + 99) / 100;
	if (progress > 0)
	{
		update_function(load_lights_str, 0.0f);
		progress = 20.0f / progress;
	}
	else
	{
		update_function(load_lights_str, 20.0f);
		progress = 0.0f;
	}
#endif // FASTER_MAP_LOAD

	LOG_DEBUG("Loading %d lights.", cur_map_header.lights_no);

	//read the lights
	lights = (light_io *) (file_mem + cur_map_header.lights_offset);

	ENTER_DEBUG_MARK("load lights");
	for (i = 0; i < cur_map_header.lights_no; i++)
	{
		light_io cur_light_io = lights[i];

		cur_light_io.pos_x = SwapLEFloat (cur_light_io.pos_x);
		cur_light_io.pos_y = SwapLEFloat (cur_light_io.pos_y);
		cur_light_io.pos_z = SwapLEFloat (cur_light_io.pos_z);
		cur_light_io.r = SwapLEFloat (cur_light_io.r);
		cur_light_io.g = SwapLEFloat (cur_light_io.g);
		cur_light_io.b = SwapLEFloat (cur_light_io.b);

		LOG_DEBUG("Adding light(%d) at <%d, %f, %f> with color "
			"<%f, %f, %f>.", i, cur_light_io.pos_x,
			cur_light_io.pos_y, cur_light_io.pos_z, cur_light_io.r,
			cur_light_io.g, cur_light_io.b);

		if (cur_light_io.pos_x < 0.0f || cur_light_io.pos_x > tile_map_size_x * 60 ||
			cur_light_io.pos_y < 0.0f || cur_light_io.pos_y > tile_map_size_y * 60 ||
			cur_light_io.pos_z < -1000.0f || cur_light_io.pos_z > 1000.0f ||
			cur_light_io.r < -1.0f || cur_light_io.r > 1000.0f ||
			cur_light_io.g < -1.0f || cur_light_io.g > 1000.0f ||
			cur_light_io.b < -1.0f || cur_light_io.b > 1000.0f)
		{
			LOG_ERROR("Bad light (number %d) when loading '%s'; co-ords [%f %f %f] "
				"colour [%f %f %f]", i, file_name, cur_light_io.pos_x,
				cur_light_io.pos_y, cur_light_io.pos_z, cur_light_io.r,
				cur_light_io.g, cur_light_io.b);
			cur_light_io.pos_x = cur_light_io.pos_y = 1.0f;
			cur_light_io.pos_z = 2.0f;
			cur_light_io.r = cur_light_io.g = cur_light_io.b = 1.0f;
			continue;
		}

		add_light (cur_light_io.pos_x, cur_light_io.pos_y, cur_light_io.pos_z,
		           cur_light_io.r, cur_light_io.g, cur_light_io.b, 1.0f, 0);

#ifndef FASTER_MAP_LOAD
		if (i % 100 == 0)
		{
			update_function(load_lights_str, progress);
		}
#endif
	}
	LEAVE_DEBUG_MARK("load lights");

#ifdef FASTER_MAP_LOAD
	update_function(load_particles_str, 20.0f);
#else  // FASTER_MAP_LOAD
	progress = (cur_map_header.particles_no + 99) / 100;
	if (progress > 0.0f)
	{
		update_function(load_particles_str, 0.0f);
		progress = 20.0f / progress;
	}
	else
	{
		update_function(load_particles_str, 20.0f);
		progress = 0.0f;
	}
#endif // FASTER_MAP_LOAD

	LOG_DEBUG("Loading %d particles.", cur_map_header.particles_no);

	//read particle systems
	particles = (particles_io *) (file_mem + cur_map_header.particles_offset);

	ENTER_DEBUG_MARK("load particles");
	for (i = 0; i < cur_map_header.particles_no; i++)
	{
		particles_io cur_particles_io = particles[i];

		cur_particles_io.x_pos = SwapLEFloat (cur_particles_io.x_pos);
		cur_particles_io.y_pos = SwapLEFloat (cur_particles_io.y_pos);
		cur_particles_io.z_pos = SwapLEFloat (cur_particles_io.z_pos);

		LOG_DEBUG("Adding particle(%d) '%s' at <%d, %f, %f>.", i,
			cur_particles_io.file_name, cur_particles_io.x_pos,
			cur_particles_io.y_pos, cur_particles_io.z_pos);

		if (!strncmp(cur_particles_io.file_name, "ec://", 5))
		{
			ec_create_effect_from_map_code(cur_particles_io.file_name + 5, cur_particles_io.x_pos, cur_particles_io.y_pos, cur_particles_io.z_pos, (poor_man ? 6 : 10));
		}
		else
		{
#ifdef NEW_SOUND
			add_map_particle_sys (cur_particles_io.file_name, cur_particles_io.x_pos, cur_particles_io.y_pos, cur_particles_io.z_pos, 0);
#else
			add_particle_sys (cur_particles_io.file_name, cur_particles_io.x_pos, cur_particles_io.y_pos, cur_particles_io.z_pos, 0);
#endif // NEW_SOUND
		}

#ifndef FASTER_MAP_LOAD
		if (i % 100 == 0)
		{
			update_function(load_particles_str, progress);
		}
#endif
	}
	LEAVE_DEBUG_MARK("load particles");

	// Everything copied, get rid of the file data
	el_close(file);

	update_function(bld_sectors_str, 0.0f);

	LOG_DEBUG("Building bbox tree for map '%s'.", file_name);

	init_bbox_tree(main_bbox_tree, main_bbox_tree_items);
	free_bbox_items(main_bbox_tree_items);
	main_bbox_tree_items = 0;
	update_function(init_done_str, 20.0f);
#ifdef EXTRA_DEBUG
	ERR();//We finished loading the new map apparently...
#endif
	return 1;
}
Ejemplo n.º 11
0
Uint32 load_image_data_file(el_file_ptr file, const Uint32 compression,
	const Uint32 unpack, const Uint32 strip_mipmaps,
	const Uint32 base_level, image_t* image)
{
	char buffer[128];
	el_file_ptr alpha_file;
	Uint32 dds, result;

	if (file == 0)
	{
		LOG_ERROR("Invalid file!");

		return 0;
	}

	dds = 0;

	if (el_get_size(file) >= 4)
	{
		if (check_dds(el_get_pointer(file)))
		{
			dds = 1;
		}
	}

	if (dds == 1)
	{
		result = load_dds(file, compression, unpack,
			strip_mipmaps, base_level, image);
	}
	else
	{
		result = load_image_SDL(file, image);
	}

	if ((result == 0) || (image->image == 0))
	{
		LOG_ERROR("Can't load file '%s'!", el_file_name(file));

		el_close(file);

		return 0;
	}

	if ((dds != 1) && (check_alpha_image_name(el_file_name(file),
		sizeof(buffer), buffer) != 0))
	{
		alpha_file = el_open_custom(buffer);

		if (alpha_file == 0)
		{
			LOG_ERROR("Can't load file '%s'!", buffer);

			el_close(file);

			return 0;
		}

		load_image_SDL_alpha(alpha_file, image);

		el_close(alpha_file);
	}

	el_close(file);

	return 1;
}
Ejemplo n.º 12
0
static Uint32 load_image_SDL_alpha(el_file_ptr file, image_t* image)
{
	SDL_Surface *image_surface;
	GLubyte* data;
	int image_width, image_height, idx;
	int pixel, temp, r, g, b, a;
	int bpp, i, j, index, x_padding;

	if (file == 0)
	{
		LOG_ERROR("Invalid file!");

		return 0;
	}

	if (image == 0)
	{
		LOG_ERROR("Invalid image for file '%s'.", el_file_name(file));

		return 0;
	}

	if (image->format != ift_rgba8)
	{
		LOG_ERROR("Alpha map '%s' can't be used for formats other than"
			" RGBA8.", el_file_name(file));

		return 0;
	}

	image_surface = IMG_Load_RW(SDL_RWFromMem(el_get_pointer(file),
		el_get_size(file)), 1);

	if (image_surface == 0)
	{
		LOG_ERROR("load_image() error: [%s] [%s]", el_file_name(file),
			IMG_GetError());
		return 0;
	}

	// at this point, the Surface contains some type of pixel data.
	// SDL requires us to lock the surface before using the pixel data:
	SDL_LockSurface(image_surface);

	image_width = image_surface->w;
	image_height = image_surface->h;

	if (image->width != image_width)
	{
		LOG_ERROR("Alpha map '%s' had wrong width %i, expected width"
			" is %i.", el_file_name(file), image_width,
			image->width);

		return 0;
	}

	if (image->height != image_height)
	{
		LOG_ERROR("Alpha map '%s' had wrong width %i, expected width"
			" is %i.", el_file_name(file), image_height,
			image->height);

		return 0;
	}

	x_padding = image_width % 4;

	if (x_padding)
	{
		x_padding = 4 - x_padding;
	}
	if (image_width <= x_padding)
	{
		x_padding = 0;
	}

	data = image->image;
	image->alpha = 1;

	idx = 0;
	pixel = 0;
	bpp = image_surface->format->BytesPerPixel;

	for (i = 0; i < image_height; i++)
	{
		for (j = 0; j < image_width; j++)
		{
			if ((image_surface->format->BitsPerPixel == 8) &&
				(image_surface->format->palette != 0))
			{
				index = ((Uint8 *)image_surface->pixels)[idx];
				r = image_surface->format->palette->colors[index].r;
				g = image_surface->format->palette->colors[index].g;
				b = image_surface->format->palette->colors[index].b;
				a = (r + g + b) / 3;
			}
			else
			{
				memcpy(&pixel, &((Uint8 *)image_surface->pixels)[idx], bpp);

				/* Get Alpha component */
				temp = pixel & image_surface->format->Amask;  /* Isolate alpha component */
				temp = temp >> image_surface->format->Ashift; /* Shift it down to 8-bit */
				temp = temp << image_surface->format->Aloss;  /* Expand to a full 8-bit number */
				a = (Uint8)temp;
			}

			idx += bpp;

			index = (image_height - i - 1) * image_width + j;

			data[index * 4 + 3] = a;
		}
		idx += bpp * x_padding;
	}

	SDL_UnlockSurface(image_surface);
	SDL_FreeSurface(image_surface);

	return 1;
}
Ejemplo n.º 13
0
static Uint32 load_image_SDL(el_file_ptr file, image_t* image)
{
	SDL_Surface *image_surface;
	SDL_RWops *buffer;
	Uint8* data;
	int image_width, image_height, idx;
	int pixel, temp, r, g, b, a;
	int bpp, i, j, index, x_padding;

	if (file == 0)
	{
		LOG_ERROR("Invalid file!");

		return 0;
	}

	if (image == 0)
	{
		LOG_ERROR("Invalid image for file '%s'!", el_file_name(file));

		return 0;
	}

	buffer = SDL_RWFromMem(el_get_pointer(file), el_get_size(file));

	image_surface = IMG_Load_RW(buffer, 1);

	if (image_surface == 0)
	{
		LOG_ERROR("load_image() error: [%s] [%s]", el_file_name(file),
			IMG_GetError());
		return 0;
	}

	// at this point, the Surface contains some type of pixel data.
	// SDL requires us to lock the surface before using the pixel data:
	SDL_LockSurface(image_surface);

	image_width = image_surface->w;
	image_height = image_surface->h;

	image->width = image_width;
	image->height = image_height;
	image->mipmaps = 1;
	image->format = ift_rgba8;
	image->sizes[0] = image_width * image_height * 4;

	if ((image_surface->format->BitsPerPixel == 8) &&
		(image_surface->format->palette != 0))
	{
		image->alpha = 0;
	}
	else
	{
		if (image_surface->format->Amask != 0)
		{
			image->alpha = 1;
		}
		else
		{
			image->alpha = 0;
		}
	}

	x_padding = image_width % 4;

	if (x_padding)
	{
		x_padding = 4 - x_padding;
	}
	if (image_width <= x_padding)
	{
		x_padding = 0;
	}

	image->image = (GLubyte*)malloc_aligned(image_width * image_height * 4,
		16);
	data = image->image;

	idx = 0;
	pixel = 0;
	bpp = image_surface->format->BytesPerPixel;

	for (i = 0; i < image_height; i++)
	{
		for (j = 0; j < image_width; j++)
		{
			if ((image_surface->format->BitsPerPixel == 8) &&
				(image_surface->format->palette != 0))
			{
				index = ((Uint8 *)image_surface->pixels)[idx];
				r = image_surface->format->palette->colors[index].r;
				g = image_surface->format->palette->colors[index].g;
				b = image_surface->format->palette->colors[index].b;
				a = 255;
			}
			else
			{
				memcpy(&pixel, &((Uint8 *)image_surface->pixels)[idx], bpp);
				/* Get Red component */
				temp = pixel & image_surface->format->Rmask;  /* Isolate red component */
				temp = temp >> image_surface->format->Rshift; /* Shift it down to 8-bit */
				temp = temp << image_surface->format->Rloss;  /* Expand to a full 8-bit number */
				r = (Uint8)temp;

				/* Get Green component */
				temp = pixel & image_surface->format->Gmask;  /* Isolate green component */
				temp = temp >> image_surface->format->Gshift; /* Shift it down to 8-bit */
				temp = temp << image_surface->format->Gloss;  /* Expand to a full 8-bit number */
				g = (Uint8)temp;

				/* Get Blue component */
				temp = pixel & image_surface->format->Bmask;  /* Isolate blue component */
				temp = temp >> image_surface->format->Bshift; /* Shift it down to 8-bit */
				temp = temp << image_surface->format->Bloss;  /* Expand to a full 8-bit number */
				b = (Uint8)temp;

				/* Get Alpha component */
				temp = pixel & image_surface->format->Amask;  /* Isolate alpha component */
				temp = temp >> image_surface->format->Ashift; /* Shift it down to 8-bit */
				temp = temp << image_surface->format->Aloss;  /* Expand to a full 8-bit number */
				a = (Uint8)temp;

				if (image_surface->format->Amask == 0)
				{
					a = 255;
				}
			}
			idx += bpp;

			index = i * image_width + j;
			data[index * 4 + 0] = r;
			data[index * 4 + 1] = g;
			data[index * 4 + 2] = b;
			data[index * 4 + 3] = a;
		}
		idx += bpp * x_padding;
	}

	SDL_UnlockSurface(image_surface);
	SDL_FreeSurface(image_surface);

	return 1;
}