texasm.cpp
资源名称:hge181.rar [点击查看]
上传用户:jnfxsk
上传日期:2022-06-16
资源大小:3675k
文件大小:6k
源码类别:
游戏引擎
开发平台:
Visual C++
- /*
- ** Haaf's Game Engine 1.81
- ** Copyright (C) 2003-2008, Relish Games
- ** hge.relishgames.com
- **
- ** Texture Assembler
- */
- #include "texasm.h"
- #include "texture_assembler.h"
- #include "....includehgeresource.h"
- #include <direct.h>
- HGE *hge = 0;
- void SysLog(const char *format, ...);
- bool ParseTask(char *task, int *resgroup, char *mask, bool *inclusive);
- int main(int argc, char* argv[])
- {
- hgeResourceManager *rm;
- CTextureAssembler *ta;
- char cwd[MAX_PATH];
- hge=hgeCreate(HGE_VERSION);
- hge->System_SetState(HGE_SHOWSPLASH, false);
- hge->System_SetState(HGE_USESOUND, false);
- hge->System_SetState(HGE_WINDOWED, true);
- hge->System_SetState(HGE_SCREENWIDTH, 320);
- hge->System_SetState(HGE_SCREENHEIGHT, 200);
- hge->System_SetState(HGE_LOGFILE, "texasm.log");
- // try to initiate HGE
- if(!hge->System_Initiate())
- {
- SysLog("Can't initiate HGE.n");
- hge->System_Shutdown();
- hge->Release();
- return 1;
- }
- SysLog("nTexture Assembler v0.1nCopyright (C) 2003-2008, Relish Gamesnn");
- // check for args count and display help
- if(argc != 3 && argc != 4)
- {
- SysLog("Usage: TEXASM.EXE <res-file>|<wildcard> <output-wildcard> [<task>]nnn");
- SysLog("<res-file> - resource script to process.n");
- SysLog("To be able to locate the resources, texasm.exe mustn");
- SysLog("be run from the folder of the main project executable.nn");
- SysLog("<wildcard> - a wildcard to search image files for processing.n");
- SysLog("Either <res-file> or <wildcard> should be specified, not both.n");
- SysLog("All image formats supported by HGE can be handled.nn");
- SysLog("<output-wildcard> - wildcard to store the result to.n");
- SysLog("For example, "opt_textures\TitleTex" will generate the following files:n");
- SysLog(" opt_textures\TitleTex1.png, opt_textures\TitleTex1.resn");
- SysLog(" opt_textures\TitleTex2.png, opt_textures\TitleTex2.resn");
- SysLog(" ...nn");
- SysLog("<task> - optional resource filter.n");
- SysLog("If omitted, all resources found in the script/folder will be processed.n");
- SysLog("The default resgroup assigned to folder resources is 0.n");
- SysLog("Example tasks:nn");
- SysLog(" 2n");
- SysLog(" Process all resources from resgroup 2nn");
- SysLog(" 7+sprFlower/sprLeaf/sprTrunkn");
- SysLog(" Process resources from resgroup 7 whosen names start with sprFlower, sprLeaf or sprTrunknn");
- SysLog(" 3-gui\menu\titlen");
- SysLog(" Process resources from resgroup 3 whosen names DON'T start with gui, menu or titlenn");
- SysLog(" +mars/earth/mercuryn");
- SysLog(" Process all resources whose names startn with mars, earth or mercurynn");
- SysLog(" -gui\menu\titlen");
- SysLog(" Process all resources whose names DON'Tn start with gui, menu or titlenn");
- SysLog("nCurrently only Sprite resources and only 32bit PNG output are supported.nn");
- hge->System_Shutdown();
- hge->Release();
- return 0;
- }
- // test if we should process a resource script or a folder
- bool bScript;
- void *script = hge->Resource_Load(argv[1]);
- if(script)
- bScript = true;
- else
- bScript = false;
- hge->Resource_Free(script);
- // parse task argument if present
- int resgroup = 0;
- bool inclusive = true;
- char mask[256] = "";
- if(argc == 4)
- if(!ParseTask(argv[3], &resgroup, mask, &inclusive))
- {
- SysLog("Invalid task: %sn", argv[3]);
- hge->System_Shutdown();
- hge->Release();
- return 1;
- }
- // create resource manager & texture assember
- rm = new hgeResourceManager();
- ta = new CTextureAssembler();
- // (possible enhancement: pass maximum texture size and margin from command line)
- ta->SetMaxTextureSize(1024, 1024);
- ta->SetMargin(1);
- // make the search/output path consistent with texture
- // loading, i.e. relative to the main executable
- _getcwd(cwd, sizeof(cwd));
- _chdir(hge->Resource_MakePath());
- // Add a task into texture assembler. There may be more than one
- // call to AccumulateRMResources() and/or AccumulateFileResources().
- if(bScript)
- {
- rm->ChangeScript(argv[1]);
- ta->AccumulateRMResources(rm, resgroup, mask, inclusive);
- }
- else
- {
- ta->AccumulateFileResources(argv[1], resgroup, mask, inclusive);
- }
- // Generate and save PNG and RES files. The processing includes:
- // - pack sprites as tight as possible onto minimal number of textures
- // - ensure no distortion when scaling or rotating sprites:
- // a) all sprites have empty 1-pixel border outside
- // b) texture data is corrected along the alpha channel border
- // - all extra info is removed from the saved PNGs
- bool success = ta->GenerateTextures(argv[2]);
- // restore current working directory
- _chdir(cwd);
- delete ta;
- delete rm;
- if(success)
- SysLog("nSuccess.n");
- else
- SysLog("nAborted.n");
- hge->System_Shutdown();
- hge->Release();
- return (int)success;
- }
- void SysLog(const char *format, ...)
- {
- char buf[256];
- va_list ap;
- va_start(ap, format);
- vsprintf(buf, format, ap);
- va_end(ap);
- printf(buf);
- hge->System_Log(buf);
- }
- bool ParseTask(char *task, int *resgroup, char *mask, bool *inclusive)
- {
- *resgroup = 0;
- *inclusive = true;
- mask[0] = 0;
- char *p = task;
- // parse resgroup
- while(isdigit(*p))
- {
- *resgroup *= 10;
- *resgroup += *p - '0';
- p++;
- }
- if(!*p) return true;
- // parse inclusiveness
- if(*p == '-')
- *inclusive = false;
- else
- {
- if(*p != '+') return false;
- }
- p++;
- if(!*p) return false;
- // parse mask
- strcpy(mask, p);
- p = mask;
- while(*p)
- {
- if(*p == '\' || *p == '/') *p = 0;
- p++;
- }
- p++;
- *p = 0;
- return true;
- }