Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdafx.h>
- #include <algorithm>
- #include "encoding.h"
- #include "memstream.h"
- #include "xmltext.h"
- #define MIN_RANGE 2
- #define MAX_RANGE 254
- #define USE_SMALL 0
- void ImageToTile2bppPlanar(Image &img, int sx, int sy, u8 *dest);
- void BuildSet(Image &img, LPCTSTR name)
- {
- u8 tile[4096], *t = tile;
- for(int y = 0; y < 128; y += 8)
- for(int x = 0; x < 128; x += 8, t += 16)
- ImageToTile2bppPlanar(img, x, y, t);
- FlushFile(name, tile, 4096);
- }
- void FillTile(Image &img, int pos, int val)
- {
- int sx = pos % 16 * 8,
- sy = pos / 16 * 8;
- for(int y = 0; y < 8; y++)
- for(int x = 0; x < 8; x++)
- img.SetPixelAt(sx+x,sy+y,val);
- }
- //void BlitEx(Image &img, Image &fnt, u8 ch, int x, int y)
- //{
- //#if USE_SMALL
- // extern u8 vwf_tbl8_1[];
- // int w = vwf_tbl8_1[ch];
- //#else
- // extern u8 vwf_tbl8[];
- // int w = vwf_tbl8[ch];
- //#endif
- //
- // if(x + w > 128)
- // {
- // int reminder = (x + w) & 127;
- // img.BitBlit(&fnt, (ch % 16) * 8, (ch / 16) * 8, w, 8, x, y, Image::dir_normal);
- // img.BitBlit(&fnt, (ch % 16) * 8 + (w - reminder), (ch / 16) * 8, reminder, 8, 0, y+8, Image::dir_normal);
- // }
- // else img.BitBlit(&fnt, (ch % 16) * 8, (ch / 16) * 8, w, 8, x, y, Image::dir_normal);
- //}
- u32 BuildTileHash(Image &img, int sx, int sy)
- {
- //u8 tile[32], *t = tile;
- //// build a temp tile
- //for(int y = 0; y < 8; y++)
- // for(int x = 0; x < 8; x += 2)
- // *t++ = img.GetPixelAt(x, y) | (img.GetPixelAt(x + 1, y) << 4);
- //return GetCrc32(tile, sizeof(tile));
- Image tile;
- tile.Create(8, 8, 4, img.palette);
- tile.BitBlit(&img, sx, sy, 8, 8, 0, 0, Image::dir_normal);
- return GetCrc32(tile.image, 32);
- }
- void InitHash(u32 crc_tbl[])
- {
- for(size_t i = 0; i < 256; i++)
- crc_tbl[i] = -1;
- }
- int FindHash(u32 crc_tbl[], u32 crc)
- {
- for(int i = 0; i < 256; i++)
- {
- if(crc == crc_tbl[i])
- return i;
- }
- return -1;
- }
- int FindHashR(u32 crc_tbl[], u32 crc)
- {
- for(int i = MAX_RANGE; i >= MIN_RANGE; i--)
- {
- if(crc_tbl[i] == (u32)-1)
- break;
- if(crc == crc_tbl[i])
- return i;
- }
- return -1;
- }
- int PushHash(u32 crc_tbl[], u32 crc)
- {
- for(int i = MIN_RANGE; i < MAX_RANGE; i++)
- {
- if(crc_tbl[i] == (u32)-1)
- {
- crc_tbl[i] = crc;
- return i;
- }
- }
- return -1;
- }
- int PushHashR(u32 crc_tbl[], u32 crc)
- {
- for(int i = MAX_RANGE; i >= 0; i--)
- {
- if(crc_tbl[i] == (u32)-1)
- {
- crc_tbl[i] = crc;
- return i;
- }
- }
- return -1;
- }
- //void BuildHashes(Image &img, std::vector<u32> &crc_tbl)
- //{
- // Image b;
- // b.Create(8, 8, 4, img.palette);
- // for(int i = 0, si = (img.width / 8) * (img.height / 8); i < si; i++)
- // {
- // b.BitBlit(&img, (i % 16) * 8, i / 16 * 8, 8, 8, 0, 0, Image::dir_normal);
- // crc_tbl.push_back(GetCrc32(b.image, 32));
- // }
- //}
- void BuildString(GString str, Image &dst, Image &font, u8 *vwf_tbl, u32 *crc_tbl,
- u32 *crc_tblr, int &last_tile, CXmlEntry &e)
- {
- u8 buffer[1024];
- int size = EncodeString(str,buffer);
- // destination image
- Image canvas;
- canvas.Create(256, 8, 4, font.palette);
- // fill with empty shit
- for(int y = 0, sy = canvas.height; y < sy; y++)
- for(int x = 0, sx = canvas.width; x < sx; x++)
- canvas.SetPixelAt(x, y, 1);
- int x = 0;
- for(int i = 0, w; i < size; i++)
- {
- u8 ch = buffer[i];
- if(ch == 0) break;
- ch -= 0x40;
- w = vwf_tbl[ch];
- canvas.BitBlit(&font, ch % 16 * 8, ch / 16 * 8, 8, 8, x, 0, Image::dir_normal);
- x += w;
- }
- x--; // delete last empty pixel
- int tiles = Align(x, 8) / 8;
- for(int i = 0; i < tiles; i++)
- {
- u32 crc = BuildTileHash(canvas, i * 8, 0);
- int ret0 = FindHash (crc_tbl, crc);
- int ret1 = crc_tblr ? FindHashR(crc_tblr, crc) : -1;
- if(ret0 == -1 && ret1 == -1)
- {
- //crc_tbl.push_back(crc);
- PushHash(crc_tbl, crc);
- dst.BitBlit(&canvas, i * 8, 0, 8, 8, last_tile % 16 * 8, last_tile / 16 * 8,Image::dir_normal);
- e.PushByte(last_tile);
- last_tile++;
- }
- // duplicate found somewhere
- else
- {
- // found inside shared table
- if(ret1 != -1) e.PushByte(ret1);
- // found inside current table
- else if(ret0 != -1) e.PushByte(ret0);
- else _tprintf(_T("Warning: this shouldn't happen.\n"));
- }
- }
- }
- void BuildStringR(GString str, Image &dst, Image &font, u8 *vwf_tbl, u32 crc_tbl[256], int &last_tile)
- {
- u8 buffer[1024];
- int size = EncodeString(str,buffer);
- // destination image
- Image canvas;
- canvas.Create(256, 8, 4, font.palette);
- // fill with empty shit
- for(int y = 0, sy = canvas.height; y < sy; y++)
- for(int x = 0, sx = canvas.width; x < sx; x++)
- canvas.SetPixelAt(x, y, 1);
- int x = 0;
- for(int i = 0, w; i < size; i++)
- {
- u8 ch = buffer[i];
- if(ch == 0) break;
- ch -= 0x40;
- w = vwf_tbl[ch];
- canvas.BitBlit(&font, ch % 16 * 8, ch / 16 * 8, 8, 8, x, 0, Image::dir_normal);
- x += w;
- }
- x--; // delete last empty pixel
- int tiles = Align(x, 8) / 8;
- for(int i = 0; i < tiles; i++)
- {
- u32 crc = BuildTileHash(canvas, i * 8, 0);
- int ret = FindHashR(crc_tbl, crc);
- if(ret == -1)
- {
- //crc_tbl.push_back(crc);
- PushHashR(crc_tbl, crc);
- dst.BitBlit(&canvas, i * 8, 0, 8, 8, (255 - last_tile) % 16 * 8, (255 - last_tile) / 16 * 8,Image::dir_normal);
- last_tile++;
- }
- }
- }
- void DrawMenuData(LPCTSTR in_name, LPCTSTR out_name, LPCTSTR tile_name)
- {
- #if USE_SMALL
- extern u8 vwf_tbl8_1[];
- u8 *vwf_tbl = vwf_tbl8_1;
- #else
- extern u8 vwf_tbl8[];
- u8 *vwf_tbl = vwf_tbl8;
- #endif
- CXmlText text;
- text.Parse(in_name);
- int count=text.GetCount();
- Image canvas, shared, font;
- u32 crc_tbl[256], crc_tblr[256];
- #if USE_SMALL
- font.LoadFromFile(_T("..\\..\\CARDTIM\\font8_2t.png"));
- #else
- font.LoadFromFile(_T("..\\..\\CARDTIM\\font8_2.png"));
- #endif
- shared.Create(128, 128, 4, font.palette);
- LPCTSTR names[]=
- {
- _T("st_main.bmp"),
- _T("st_name.bmp"),
- _T("st_shop.bmp"),
- _T("st_fatc.bmp"),
- //_T("st_load.bmp"),
- _T("st_optn.bmp"),
- _T("st_bttl.bmp")
- };
- LPCTSTR namez[]=
- {
- _T("..\\..\\pretty\\st_main.sfc"),
- _T("..\\..\\pretty\\st_name.sfc"),
- _T("..\\..\\pretty\\st_shop.sfc"),
- _T("..\\..\\pretty\\st_fatc.sfc"),
- _T("..\\..\\pretty\\st_optn.sfc"),
- _T("..\\..\\pretty\\st_bttl.sfc")
- };
- // deal with shared crap the easy way
- int shared_tile = 1;
- InitHash(crc_tblr);
- for(int i=0, x = 0; i < count; i++)
- {
- if(text.ent[i]->IsShared())
- {
- for(int j=0, js = text.ent[i]->GetCount(); j < js; j++)
- {
- GString str = text.ent[i]->GetString(j) + _T("<End>");
- BuildStringR(str, shared, font, vwf_tbl, crc_tblr, shared_tile);
- }
- }
- }
- //shared.SaveBitmap(_T("st_share.bmp"));
- // now render all non shared stuff
- for(int k = 0; k < ST_COUNT; k++)
- {
- InitHash(crc_tbl);
- canvas.Create(128, 128, 4, font.palette);
- int last_tile = 2,
- is_shared = text.IsGroupShared(k);
- // parse and draw
- for(int i=0, x = 0; i < count; i++)
- {
- if(!text.ent[i]->IsInGroup(k))
- continue;
- for(int j=0, js = text.ent[i]->GetCount(); j < js; j++)
- {
- GString str = text.ent[i]->GetString(j) + _T("<End>");
- BuildString(str, canvas, font, vwf_tbl, crc_tbl, is_shared ? crc_tblr : NULL, last_tile, *text.ent[i]);
- if( j != js - 1)
- text.ent[i]->PushByte(1);
- else
- text.ent[i]->PushByte(0);
- }
- // prevent further write
- text.ent[i]->SetLock(true);
- }
- // merge with shared tiles
- if(is_shared)
- {
- for(int i = 0; i < shared_tile; i++)
- {
- int t = 256 - shared_tile + i;
- canvas.BitBlit(&shared, t % 16 * 8, t / 16 * 8, 8, 8, t % 16 * 8, t / 16 * 8, Image::dir_normal);
- }
- }
- FillTile(canvas, 0, 0);
- FillTile(canvas, 1, 0);
- FillTile(canvas, 255, 1);
- canvas.SaveBitmap(names[k]);
- BuildSet(canvas, namez[k]);
- }
- #if 0
- MEM_STREAM s;
- MemStreamCreate(&s);
- for(int i=0, x = 0; i < count; i++)
- {
- int ret = text.ent[i]->Write(s);
- if(ret) _tprintf(_T("%d spilling by %d bytes\n"), i, ret);
- }
- MemStreamFlush(&s,_T("menu.tst"));
- MemStreamClose(&s);
- #else
- GString s;
- FILE *f = _tfopen(_T("..\\..\\pretty\\menu_str.s"), _T("wb+"));
- for(int i=0, x = 0; i < count; i++)
- text.ent[i]->Write(s);
- WriteUtf8(s, f);
- fclose(f);
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement