SHARE
TWEET

Untitled

a guest Jul 17th, 2017 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void MainWindow::writeToFile(const QString& fileName)
  2. {
  3.     FILE* file = fopen(fileName.toAscii(), "w+");
  4.  
  5.     char header[4] = { 'C', 'H', 'B', 'G' };
  6.     fwrite(header, 1, 4, file);
  7.  
  8.     unsigned short swidth = ui->widthBox->value();
  9.     fwrite(&swidth, 1, 2, file);
  10.  
  11.     unsigned short sheight = ui->heightBox->value();
  12.     fwrite(&sheight, 1, 2, file);
  13.  
  14.     unsigned char tileColorBits = 0x08;
  15.     fwrite(&tileColorBits, 1, 1, file);
  16.  
  17.     BinFile palfile;
  18.     if (!palfile.read(ui->paletteBox->text()))
  19.     {
  20.         QErrorMessage errorMessage;
  21.         errorMessage.showMessage("Failed to open palette file.");
  22.         errorMessage.exec();
  23.  
  24.         fclose(file);
  25.  
  26.         return;
  27.     }
  28.  
  29.     unsigned char numberOfColors = palfile.size / 2;
  30.     unsigned char colorLines = numberOfColors / 16;
  31.     fwrite(&colorLines, 1, 1, file);
  32.  
  33.     BinFile imgfile;
  34.     if (!imgfile.read(ui->imageBox->text()))
  35.     {
  36.         QErrorMessage errorMessage;
  37.         errorMessage.showMessage("Failed to open image file.");
  38.         errorMessage.exec();
  39.  
  40.         fclose(file);
  41.  
  42.         return;
  43.     }
  44.  
  45.     unsigned short numberOfTiles = imgfile.size / 64;
  46.     fwrite(&numberOfTiles, 1, 2, file);
  47.  
  48.     int emptyPart = 0;
  49.     fwrite(&emptyPart, 1, 4, file);
  50.  
  51.     BinFile mapfile;
  52.     if (!mapfile.read(ui->imageBox->text()))
  53.     {
  54.         QErrorMessage errorMessage;
  55.         errorMessage.showMessage("Failed to open map file.");
  56.         errorMessage.exec();
  57.  
  58.         fclose(file);
  59.  
  60.         return;
  61.     }
  62.  
  63.     int written = fwrite(palfile.data, 1, palfile.size, file);
  64.     printf("wrote %d bytes to palette.\n", written);
  65.  
  66.     written = fwrite(mapfile.data, 1, mapfile.size, file);
  67.     printf("wrote %d bytes to puzzle.\n", written);
  68.  
  69.     written = fwrite(imgfile.data, 1, imgfile.size, file);
  70.     printf("wrote %d bytes to tiles.\n", written);
  71.  
  72.     palfile.freeData();
  73.     imgfile.freeData();
  74.     mapfile.freeData();
  75.  
  76.     fclose(file);
  77. }
RAW Paste Data
Top