Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.05 KB | None | 0 0
  1. #include <stdafx.h>
  2. #include <algorithm>
  3. #include "encoding.h"
  4. #include "memstream.h"
  5. #include "xmltext.h"
  6.  
  7. #define MIN_RANGE   2
  8. #define MAX_RANGE   254
  9.  
  10. #define USE_SMALL   0
  11.  
  12. void ImageToTile2bppPlanar(Image &img, int sx, int sy, u8 *dest);
  13.  
  14. void BuildSet(Image &img, LPCTSTR name)
  15. {
  16.     u8 tile[4096], *t = tile;
  17.  
  18.     for(int y = 0; y < 128; y += 8)
  19.         for(int x = 0; x < 128; x += 8, t += 16)
  20.             ImageToTile2bppPlanar(img, x, y, t);
  21.  
  22.     FlushFile(name, tile, 4096);
  23. }
  24.  
  25. void FillTile(Image &img, int pos, int val)
  26. {
  27.     int sx = pos % 16 * 8,
  28.         sy = pos / 16 * 8;
  29.     for(int y = 0; y < 8; y++)
  30.         for(int x = 0; x < 8; x++)
  31.             img.SetPixelAt(sx+x,sy+y,val);
  32. }
  33.  
  34. //void BlitEx(Image &img, Image &fnt, u8 ch, int x, int y)
  35. //{
  36. //#if USE_SMALL
  37. //  extern u8 vwf_tbl8_1[];
  38. //  int w = vwf_tbl8_1[ch];
  39. //#else
  40. //  extern u8 vwf_tbl8[];
  41. //  int w = vwf_tbl8[ch];
  42. //#endif
  43. //
  44. //  if(x + w > 128)
  45. //  {
  46. //      int reminder = (x + w) & 127;
  47. //      img.BitBlit(&fnt, (ch % 16) * 8, (ch / 16) * 8, w, 8, x, y, Image::dir_normal);
  48. //      img.BitBlit(&fnt, (ch % 16) * 8 + (w - reminder), (ch / 16) * 8, reminder, 8, 0, y+8, Image::dir_normal);
  49. //  }
  50. //  else img.BitBlit(&fnt, (ch % 16) * 8, (ch / 16) * 8, w, 8, x, y, Image::dir_normal);
  51. //}
  52.  
  53. u32 BuildTileHash(Image &img, int sx, int sy)
  54. {
  55.     //u8 tile[32], *t = tile;
  56.     //// build a temp tile
  57.     //for(int y = 0; y < 8; y++)
  58.     //  for(int x = 0; x < 8; x += 2)
  59.     //      *t++ = img.GetPixelAt(x, y) | (img.GetPixelAt(x + 1, y) << 4);
  60.     //return GetCrc32(tile, sizeof(tile));
  61.     Image tile;
  62.     tile.Create(8, 8, 4, img.palette);
  63.     tile.BitBlit(&img, sx, sy, 8, 8, 0, 0, Image::dir_normal);
  64.     return GetCrc32(tile.image, 32);
  65. }
  66.  
  67. void InitHash(u32 crc_tbl[])
  68. {
  69.     for(size_t i = 0; i < 256; i++)
  70.         crc_tbl[i] = -1;
  71. }
  72.  
  73. int FindHash(u32 crc_tbl[], u32 crc)
  74. {
  75.     for(int i = 0; i < 256; i++)
  76.     {
  77.         if(crc == crc_tbl[i])
  78.             return i;
  79.     }
  80.     return -1;
  81. }
  82.  
  83. int FindHashR(u32 crc_tbl[], u32 crc)
  84. {
  85.     for(int i = MAX_RANGE; i >= MIN_RANGE; i--)
  86.     {
  87.         if(crc_tbl[i] == (u32)-1)
  88.             break;
  89.         if(crc == crc_tbl[i])
  90.             return i;
  91.     }
  92.     return -1;
  93. }
  94.  
  95. int PushHash(u32 crc_tbl[], u32 crc)
  96. {
  97.     for(int i = MIN_RANGE; i < MAX_RANGE; i++)
  98.     {
  99.         if(crc_tbl[i] == (u32)-1)
  100.         {
  101.             crc_tbl[i] = crc;
  102.             return i;
  103.         }
  104.     }
  105.  
  106.     return -1;
  107. }
  108.  
  109. int PushHashR(u32 crc_tbl[], u32 crc)
  110. {
  111.     for(int i = MAX_RANGE; i >= 0; i--)
  112.     {
  113.         if(crc_tbl[i] == (u32)-1)
  114.         {
  115.             crc_tbl[i] = crc;
  116.             return i;
  117.         }
  118.     }
  119.  
  120.     return -1;
  121. }
  122.  
  123. //void BuildHashes(Image &img, std::vector<u32> &crc_tbl)
  124. //{
  125. //  Image b;
  126. //  b.Create(8, 8, 4, img.palette);
  127. //  for(int i = 0, si = (img.width / 8) * (img.height / 8); i < si; i++)
  128. //  {
  129. //      b.BitBlit(&img, (i % 16) * 8, i / 16 * 8, 8, 8, 0, 0, Image::dir_normal);
  130. //      crc_tbl.push_back(GetCrc32(b.image, 32));
  131. //  }
  132. //}
  133.  
  134. void BuildString(GString str, Image &dst, Image &font, u8 *vwf_tbl, u32 *crc_tbl,
  135.                  u32 *crc_tblr, int &last_tile, CXmlEntry &e)
  136. {
  137.     u8 buffer[1024];
  138.     int size = EncodeString(str,buffer);
  139.     // destination image
  140.     Image canvas;
  141.     canvas.Create(256, 8, 4, font.palette);
  142.     // fill with empty shit
  143.     for(int y = 0, sy = canvas.height; y < sy; y++)
  144.         for(int x = 0, sx = canvas.width; x < sx; x++)
  145.             canvas.SetPixelAt(x, y, 1);
  146.  
  147.     int x = 0;
  148.     for(int i = 0, w; i < size; i++)
  149.     {
  150.         u8 ch = buffer[i];
  151.         if(ch == 0) break;
  152.         ch -= 0x40;
  153.         w = vwf_tbl[ch];
  154.         canvas.BitBlit(&font, ch % 16 * 8, ch / 16 * 8, 8, 8, x, 0, Image::dir_normal);
  155.         x += w;
  156.     }
  157.     x--;    // delete last empty pixel
  158.  
  159.     int tiles = Align(x, 8) / 8;
  160.     for(int i = 0; i < tiles; i++)
  161.     {
  162.         u32 crc = BuildTileHash(canvas, i * 8, 0);
  163.         int ret0 = FindHash (crc_tbl,  crc);
  164.         int ret1 = crc_tblr ? FindHashR(crc_tblr, crc) : -1;
  165.         if(ret0 == -1 && ret1 == -1)
  166.         {
  167.             //crc_tbl.push_back(crc);
  168.             PushHash(crc_tbl, crc);
  169.             dst.BitBlit(&canvas, i * 8, 0, 8, 8, last_tile % 16 * 8, last_tile / 16 * 8,Image::dir_normal);
  170.             e.PushByte(last_tile);
  171.             last_tile++;
  172.         }
  173.         // duplicate found somewhere
  174.         else
  175.         {
  176.             // found inside shared table
  177.             if(ret1 != -1) e.PushByte(ret1);
  178.             // found inside current table
  179.             else if(ret0 != -1) e.PushByte(ret0);
  180.             else _tprintf(_T("Warning: this shouldn't happen.\n"));
  181.         }
  182.     }
  183. }
  184.  
  185. void BuildStringR(GString str, Image &dst, Image &font, u8 *vwf_tbl, u32 crc_tbl[256], int &last_tile)
  186. {
  187.     u8 buffer[1024];
  188.     int size = EncodeString(str,buffer);
  189.     // destination image
  190.     Image canvas;
  191.     canvas.Create(256, 8, 4, font.palette);
  192.     // fill with empty shit
  193.     for(int y = 0, sy = canvas.height; y < sy; y++)
  194.         for(int x = 0, sx = canvas.width; x < sx; x++)
  195.             canvas.SetPixelAt(x, y, 1);
  196.  
  197.     int x = 0;
  198.     for(int i = 0, w; i < size; i++)
  199.     {
  200.         u8 ch = buffer[i];
  201.         if(ch == 0) break;
  202.         ch -= 0x40;
  203.         w = vwf_tbl[ch];
  204.         canvas.BitBlit(&font, ch % 16 * 8, ch / 16 * 8, 8, 8, x, 0, Image::dir_normal);
  205.         x += w;
  206.     }
  207.     x--;    // delete last empty pixel
  208.  
  209.     int tiles = Align(x, 8) / 8;
  210.     for(int i = 0; i < tiles; i++)
  211.     {
  212.         u32 crc = BuildTileHash(canvas, i * 8, 0);
  213.         int ret = FindHashR(crc_tbl, crc);
  214.         if(ret == -1)
  215.         {
  216.             //crc_tbl.push_back(crc);
  217.             PushHashR(crc_tbl, crc);
  218.             dst.BitBlit(&canvas, i * 8, 0, 8, 8, (255 - last_tile) % 16 * 8, (255 - last_tile) / 16 * 8,Image::dir_normal);
  219.             last_tile++;
  220.         }
  221.     }
  222. }
  223.  
  224. void DrawMenuData(LPCTSTR in_name, LPCTSTR out_name, LPCTSTR tile_name)
  225. {
  226. #if USE_SMALL
  227.     extern u8 vwf_tbl8_1[];
  228.     u8 *vwf_tbl = vwf_tbl8_1;
  229. #else
  230.     extern u8 vwf_tbl8[];
  231.     u8 *vwf_tbl = vwf_tbl8;
  232. #endif
  233.  
  234.     CXmlText text;
  235.     text.Parse(in_name);
  236.     int count=text.GetCount();
  237.  
  238.     Image canvas, shared, font;
  239.     u32 crc_tbl[256], crc_tblr[256];
  240.  
  241. #if USE_SMALL
  242.     font.LoadFromFile(_T("..\\..\\CARDTIM\\font8_2t.png"));
  243. #else
  244.     font.LoadFromFile(_T("..\\..\\CARDTIM\\font8_2.png"));
  245. #endif
  246.  
  247.     shared.Create(128, 128, 4, font.palette);
  248.  
  249.     LPCTSTR names[]=
  250.     {
  251.         _T("st_main.bmp"),
  252.         _T("st_name.bmp"),
  253.         _T("st_shop.bmp"),
  254.         _T("st_fatc.bmp"),
  255.         //_T("st_load.bmp"),
  256.         _T("st_optn.bmp"),
  257.         _T("st_bttl.bmp")
  258.     };
  259.     LPCTSTR namez[]=
  260.     {
  261.         _T("..\\..\\pretty\\st_main.sfc"),
  262.         _T("..\\..\\pretty\\st_name.sfc"),
  263.         _T("..\\..\\pretty\\st_shop.sfc"),
  264.         _T("..\\..\\pretty\\st_fatc.sfc"),
  265.         _T("..\\..\\pretty\\st_optn.sfc"),
  266.         _T("..\\..\\pretty\\st_bttl.sfc")
  267.     };
  268.     // deal with shared crap the easy way
  269.     int shared_tile = 1;
  270.     InitHash(crc_tblr);
  271.     for(int i=0, x = 0; i < count; i++)
  272.     {
  273.         if(text.ent[i]->IsShared())
  274.         {
  275.             for(int j=0, js = text.ent[i]->GetCount(); j < js; j++)
  276.             {
  277.                 GString str = text.ent[i]->GetString(j) + _T("<End>");
  278.                 BuildStringR(str, shared, font, vwf_tbl, crc_tblr, shared_tile);
  279.             }
  280.         }
  281.     }
  282.     //shared.SaveBitmap(_T("st_share.bmp"));
  283.  
  284.     // now render all non shared stuff
  285.     for(int k = 0; k < ST_COUNT; k++)
  286.     {
  287.         InitHash(crc_tbl);
  288.         canvas.Create(128, 128, 4, font.palette);
  289.         int last_tile = 2,
  290.             is_shared = text.IsGroupShared(k);
  291.         // parse and draw
  292.         for(int i=0, x = 0; i < count; i++)
  293.         {
  294.             if(!text.ent[i]->IsInGroup(k))
  295.                     continue;
  296.             for(int j=0, js = text.ent[i]->GetCount(); j < js; j++)
  297.             {
  298.                 GString str = text.ent[i]->GetString(j) + _T("<End>");
  299.                 BuildString(str, canvas, font, vwf_tbl, crc_tbl, is_shared ? crc_tblr : NULL, last_tile, *text.ent[i]);
  300.                 if( j != js - 1)
  301.                     text.ent[i]->PushByte(1);
  302.                 else
  303.                     text.ent[i]->PushByte(0);
  304.             }
  305.             // prevent further write
  306.             text.ent[i]->SetLock(true);
  307.         }
  308.  
  309.         // merge with shared tiles
  310.         if(is_shared)
  311.         {
  312.             for(int i = 0; i < shared_tile; i++)
  313.             {
  314.                 int t = 256 - shared_tile + i;
  315.                 canvas.BitBlit(&shared, t % 16 * 8, t / 16 * 8, 8, 8, t % 16 * 8, t / 16 * 8, Image::dir_normal);
  316.             }
  317.         }
  318.         FillTile(canvas, 0, 0);
  319.         FillTile(canvas, 1, 0);
  320.         FillTile(canvas, 255, 1);
  321.         canvas.SaveBitmap(names[k]);
  322.         BuildSet(canvas, namez[k]);
  323.     }
  324.  
  325. #if 0
  326.     MEM_STREAM s;
  327.     MemStreamCreate(&s);
  328.     for(int i=0, x = 0; i < count; i++)
  329.     {
  330.         int ret = text.ent[i]->Write(s);
  331.         if(ret) _tprintf(_T("%d spilling by %d bytes\n"), i, ret);
  332.     }
  333.     MemStreamFlush(&s,_T("menu.tst"));
  334.     MemStreamClose(&s);
  335. #else
  336.     GString s;
  337.     FILE *f = _tfopen(_T("..\\..\\pretty\\menu_str.s"), _T("wb+"));
  338.     for(int i=0, x = 0; i < count; i++)
  339.         text.ent[i]->Write(s);
  340.     WriteUtf8(s, f);
  341.     fclose(f);
  342. #endif
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement