00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023
00024 #include "bulcommn.h"
00025 #include "xcubtxtr.h"
00026 #include "memfile.h"
00027
00028 short alloc_cubedata(struct CUBES_DATA *cubes,long count)
00029 {
00030 cubes->count=count;
00031 cubes->data=malloc(cubes->count*sizeof(struct CUBE_TEXTURES));
00032 if (cubes->data==NULL)
00033 {
00034 cubes->count=0;
00035 return XCUTX_MALLOC_ERR;
00036 }
00037 return ERR_NONE;
00038 }
00039
00040 short free_cubedata(struct CUBES_DATA *cubes)
00041 {
00042 free(cubes->data);
00043 cubes->count=0;
00044 cubes->data=NULL;
00045 return ERR_NONE;
00046 }
00047
00048 short load_cubedata(struct CUBES_DATA *cubes,const char *fname)
00049 {
00050
00051 struct MEMORY_FILE *mem;
00052 short result;
00053 result = memfile_readnew(&mem,fname,MAX_FILE_SIZE);
00054 if (result != MFILE_OK)
00055 return result;
00056
00057 if (mem->len<22)
00058 {
00059 memfile_free(&mem);
00060 return XCUTX_FILE_BADDATA;
00061 }
00062 unsigned long count=read_int32_le_buf(mem->content+0);
00063 if ((mem->len!=count*SIZEOF_DK_CUBE_REC+SIZEOF_DK_CUBE_HEADER))
00064 {
00065 memfile_free(&mem);
00066 return XCUTX_FILE_BADDATA;
00067 }
00068
00069 if (alloc_cubedata(cubes,count)!=ERR_NONE)
00070 {
00071 memfile_free(&mem);
00072 return XCUTX_MALLOC_ERR;
00073 }
00074 int i;
00075 for (i=0; i<cubes->count; i++)
00076 {
00077 unsigned int item_pos=i*SIZEOF_DK_CUBE_REC+SIZEOF_DK_CUBE_HEADER;
00078 unsigned int val;
00079 val=read_int16_le_buf(mem->content+item_pos+ 0);
00080 cubes->data[i].n=val;
00081 val=read_int16_le_buf(mem->content+item_pos+ 2);
00082 cubes->data[i].s=val;
00083 val=read_int16_le_buf(mem->content+item_pos+ 4);
00084 cubes->data[i].w=val;
00085 val=read_int16_le_buf(mem->content+item_pos+ 6);
00086 cubes->data[i].e=val;
00087 val=read_int16_le_buf(mem->content+item_pos+ 8);
00088 cubes->data[i].t=val;
00089 val=read_int16_le_buf(mem->content+item_pos+10);
00090 cubes->data[i].b=val;
00091 val=read_int16_le_buf(mem->content+item_pos+12);
00092 cubes->data[i].u[0]=val;
00093 val=read_int16_le_buf(mem->content+item_pos+14);
00094 cubes->data[i].u[1]=val;
00095 val=read_int16_le_buf(mem->content+item_pos+16);
00096 cubes->data[i].u[2]=val;
00097 }
00098 memfile_free(&mem);
00099 return ERR_NONE;
00100 }
00101
00102 short write_cubedata(struct CUBES_DATA *cubes,const char *fname)
00103 {
00104
00105 FILE *fp;
00106 int i, k;
00107 fp = fopen (fname, "wb");
00108 if (fp==NULL)
00109 return XCUTX_CANT_OPEN;
00110
00111 write_int32_le_file(fp,cubes->count);
00112 for (i=0; i<cubes->count; i++)
00113 {
00114 struct CUBE_TEXTURES *data = &(cubes->data[i]);
00115 write_int16_le_file(fp,data->n);
00116 write_int16_le_file(fp,data->s);
00117 write_int16_le_file(fp,data->w);
00118 write_int16_le_file(fp,data->e);
00119 write_int16_le_file(fp,data->t);
00120 write_int16_le_file(fp,data->b);
00121 write_int16_le_file(fp,data->u[0]);
00122 write_int16_le_file(fp,data->u[1]);
00123 write_int16_le_file(fp,data->u[2]);
00124 }
00125 return ERR_NONE;
00126 }
00127
00128 short switch_cubedata_entries(struct CUBES_DATA *cubes,unsigned long idx1,unsigned long idx2)
00129 {
00130 if ((idx1>=cubes->count)||(idx2>=cubes->count))
00131 return XCUTX_INTERNAL;
00132 struct CUBE_TEXTURES data;
00133 memcpy(&data,&(cubes->data[idx1]),sizeof(struct CUBE_TEXTURES));
00134 memcpy(&(cubes->data[idx1]),&(cubes->data[idx2]),sizeof(struct CUBE_TEXTURES));
00135 memcpy(&(cubes->data[idx2]),&data,sizeof(struct CUBE_TEXTURES));
00136 return ERR_NONE;
00137 }
00138
00139 short load_textureanim(struct CUBES_DATA *cubes,const char *fname)
00140 {
00141
00142 struct MEMORY_FILE *mem;
00143 short result;
00144 result = memfile_readnew(&mem,fname,MAX_FILE_SIZE);
00145 if (result != MFILE_OK)
00146 return result;
00147
00148 cubes->anitxcount=(mem->len>>4);
00149 if ((mem->len!=(cubes->anitxcount<<4)))
00150 {
00151 memfile_free(&mem);
00152 return XCUTX_FILE_BADDATA;
00153 }
00154
00155 cubes->anitx=malloc(cubes->anitxcount*sizeof(struct CUBE_TXTRANIM));
00156 int i,k;
00157 for (k=0; k<cubes->anitxcount; k++)
00158 {
00159 unsigned char *addr=mem->content + (k<<4);
00160 for (i=0; i<8; i++)
00161 {
00162 unsigned int val;
00163 val=read_int16_le_buf(addr+(i<<1));
00164 cubes->anitx[k].data[i]=val;
00165 }
00166 }
00167 memfile_free(&mem);
00168 return ERR_NONE;
00169 }
00170
00171 short load_texture(unsigned char **texture,const char *fname)
00172 {
00173 unsigned long texture_file_len = (TEXTURE_SIZE_X*TEXTURE_COUNT_X) * (TEXTURE_SIZE_Y*TEXTURE_COUNT_Y);
00174
00175 struct MEMORY_FILE *mem;
00176 short result;
00177 result = memfile_readnew(&mem,fname,MAX_FILE_SIZE);
00178 if (result != MFILE_OK)
00179 return result;
00180
00181 if ((mem->len!=texture_file_len))
00182 {
00183 memfile_free(&mem);
00184 return XCUTX_FILE_BADDATA;
00185 }
00186
00187 (*texture)=malloc(texture_file_len);
00188 if ((*texture)==NULL)
00189 {
00190 memfile_free(&mem);
00191 return XCUTX_MALLOC_ERR;
00192 }
00193
00194 int i;
00195 for (i=0; i<(TEXTURE_SIZE_Y*TEXTURE_COUNT_Y); i++)
00196 {
00197 unsigned long pos = (TEXTURE_SIZE_X*TEXTURE_COUNT_X) * i;
00198 memcpy((*texture)+pos,mem->content+pos,(TEXTURE_SIZE_X*TEXTURE_COUNT_X));
00199 }
00200 memfile_free(&mem);
00201 return ERR_NONE;
00202 }
00203