makepak.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* quick and dirty utility to create gzipped stoneage.pak */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <dirent.h>
  5. #include <string.h>
  6. #include <fcntl.h>
  7. #include <zlib.h>
  8. typedef unsigned char byte;
  9. gzFile pakfile;
  10. int loadmap(char* filename)
  11. {
  12. int f,i;
  13. unsigned char buffer[768];
  14. printf("MAP: %s... ",filename);
  15. f=open(filename,O_RDONLY);
  16. i=read(f,&buffer,768);
  17. close(f);
  18. gzwrite(pakfile,&buffer,i);
  19. printf("OK\n");
  20. }
  21. int loadtga(char* filename)
  22. {
  23. FILE *f;
  24. unsigned char header[18];
  25. unsigned char c,c2,*d;
  26. int i,j;
  27. int picx,picy;
  28. long newpixval;
  29. printf("PIC: %s... ",filename);
  30. //read tga file
  31. if((f=fopen(filename,"r"))){
  32. for(i=0;i<18;i++){header[i]=fgetc(f);};
  33. gzwrite(pakfile,&header,18);
  34. if((header[1]!=1 && header[1]!=0) || //color map type
  35. (header[2]!=1 && header[2]!=9) || //raw or RLE color mapped image
  36. header[7]!=24 || //number of bits/color (8*3)
  37. header[16]!=8){ //bits per pixel
  38. fclose(f);
  39. printf("Unsupported tga format.\n");
  40. return(1);
  41. }
  42. picx=header[12]+header[13]*256;
  43. picy=header[14]+header[15]*256;
  44. for(i=0;i<header[0];i++){ c=fgetc(f); gzwrite(pakfile,&c,1); } //file info
  45. //read palette
  46. for(i=0;i<header[5]+header[6]*256;i++){
  47. j=header[3]+header[4]*256+i;
  48. j=j*3;
  49. c=fgetc(f); gzwrite(pakfile,&c,1);
  50. c=fgetc(f); gzwrite(pakfile,&c,1);
  51. c=fgetc(f); gzwrite(pakfile,&c,1);
  52. }
  53. if(header[2]==1){
  54. //raw image
  55. for(i=0;i<picy*picx;i++){
  56. c=fgetc(f); gzwrite(pakfile,&c,1);
  57. }
  58. } else {
  59. //rle compressed
  60. i=0;
  61. while(i<picy*picx){
  62. c2=fgetc(f); gzwrite(pakfile,&c2,1);
  63. if(c2 & 0x80){
  64. //compressed package
  65. c2&=0x7f; c2++; i+=c2;
  66. c=fgetc(f); gzwrite(pakfile,&c,1);
  67. } else {
  68. //raw
  69. c2++;
  70. while(c2>0){
  71. c=fgetc(f); gzwrite(pakfile,&c,1);
  72. i++;
  73. c2--;
  74. }
  75. }
  76. }
  77. }
  78. fclose(f);
  79. printf("OK\n");
  80. } else {
  81. printf("%s not found.\n",filename);
  82. return(1);
  83. }
  84. return(0);
  85. }
  86. int loadsnd(char *filename)
  87. {
  88. unsigned int size=0;
  89. int f;
  90. byte *data;
  91. printf("SND: %s...",filename);
  92. f=open(filename,O_RDONLY);
  93. read(f,&size,2);
  94. data=(byte *)malloc(size+4);
  95. bcopy(&size,data,4);
  96. read(f,data+4,size);
  97. close(f);
  98. gzwrite(pakfile,data,size+2);
  99. free(data);
  100. printf("OK\n");
  101. return(0);
  102. }
  103. void readmaps(char* path)
  104. {
  105. DIR *dh;
  106. struct dirent *de;
  107. unsigned char str[256];
  108. dh=opendir(path);
  109. de=readdir(dh);
  110. while(de!=NULL){
  111. if(!strncmp(de->d_name + strlen(de->d_name)-4,".map",4)){
  112. strcpy(str,path); strcat(str,de->d_name);
  113. loadmap(str);
  114. }
  115. de=readdir(dh);
  116. };
  117. closedir(dh);
  118. }
  119. int main(void)
  120. {
  121. unlink("../stoneage.pak");
  122. //umask(0x124);
  123. pakfile=gzopen("../stoneage.pak","wb9");
  124. loadtga("tiles.tga");
  125. loadtga("sidebars.tga");
  126. loadtga("back.tga");
  127. loadtga("eyes.tga");
  128. loadtga("stoneage.tga");
  129. loadtga("main.tga");
  130. loadtga("light.tga");
  131. loadtga("pass.tga");
  132. loadtga("hiscore.tga");
  133. loadtga("endseq.tga");
  134. loadtga("fonts.tga");
  135. loadtga("fonts2.tga");
  136. loadtga("dragon.tga");
  137. loadtga("pause.tga");
  138. readmaps("../levels/");
  139. gzclose(pakfile);
  140. }