Advertisement
Reisyukaku

[SMDemo] Vec4 parser

Oct 23rd, 2016
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.19 KB | None | 0 0
  1. /*
  2. *   Pokemon Sun & Moon map Vec4 parser
  3. *       by Rei
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <malloc.h>
  8. #include <string.h>
  9.  
  10. int main(int argc, char **argv){
  11.     if(argc < 2){
  12.         printf("Usage: %s <file>\n", argv[0]);
  13.         return 1;
  14.     }
  15.    
  16.     //Read file
  17.     unsigned *magic = malloc(sizeof(unsigned));
  18.     unsigned *entries = malloc(sizeof(unsigned));
  19.     float *buffer;
  20.     unsigned fileSize = 0;
  21.     FILE *fp;
  22.     fp = fopen(argv[1], "rb");
  23.     fseek(fp, 0, SEEK_END);
  24.     fileSize = ftell(fp);
  25.     fseek(fp, 0, SEEK_SET);
  26.     buffer = malloc(fileSize-8);
  27.     fread(magic, sizeof(unsigned), 1, fp);
  28.     fread(entries, sizeof(unsigned), 1, fp);
  29.     fread(buffer, sizeof(unsigned), (fileSize-8)/4, fp);
  30.     fclose(fp);
  31.    
  32.     //Parse file
  33.     printf("Magic: %X\n", *magic);
  34.     printf("Entries: %d\n", *entries);
  35.     int i, pos;
  36.     for(i = 0, pos = 0; i < *entries; i++, pos+=0x12){
  37.         printf("\n"
  38.             "X=%.3f, Y=%.3f, Z=%.3f, W=%.3f\n"
  39.             "X=%.3f, Y=%.3f, Z=%.3f, W=%.3f\n"
  40.             "X=%.3f, Y=%.3f, Z=%.3f, W=%.3f\n",
  41.             buffer[pos], buffer[pos+1], buffer[pos+2], buffer[pos+3],
  42.             buffer[pos+4], buffer[pos+5], buffer[pos+6], buffer[pos+7],
  43.             buffer[pos+8], buffer[pos+9], buffer[pos+10], buffer[pos+11]
  44.             );
  45.     }
  46.    
  47.     //Write SMD
  48.     FILE *smd;
  49.     smd = fopen("C:\\test.smd", "w");
  50.     char *header = "version 1\nnodes\nend\nskeleton\ntime 0\nend\ntriangles\n";
  51.     fwrite(header, sizeof(char), strlen(header), smd);
  52.     for(i = 0, pos = 0; i < *entries; i++, pos+=0x12){
  53.         char entry[0x100] = {0};
  54.         snprintf(entry, 0x100,
  55.         "my_material\n"
  56.         "0 %.3f %.3f %.3f\t0 0 0\t0 0\t0 0 0\n"
  57.         "0 %.3f %.3f %.3f\t0 0 0\t0 0\t0 0 0\n"
  58.         "0 %.3f %.3f %.3f\t0 0 0\t0 0\t0 0 0\n",
  59.         buffer[pos], buffer[pos+1], buffer[pos+2],
  60.         buffer[pos+4], buffer[pos+5], buffer[pos+6],
  61.         buffer[pos+8], buffer[pos+9], buffer[pos+10]
  62.         );
  63.         fwrite(entry, sizeof(char), strlen(entry), smd);
  64.     }
  65.     fwrite("end", sizeof(char), strlen("end"), smd);
  66.     fclose(smd);
  67.    
  68.     free(magic);
  69.     free(entries);
  70.     free(buffer);
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement