Index: ps/trunk/source/tools/rmgen/output.cpp =================================================================== --- ps/trunk/source/tools/rmgen/output.cpp (revision 2300) +++ ps/trunk/source/tools/rmgen/output.cpp (revision 2301) @@ -1,144 +1,144 @@ #include "stdafx.h" #include "rmgen.h" #include "output.h" #include "map.h" using namespace std; typedef unsigned short u16; typedef unsigned int u32; void OutputXml(Map* m, FILE* f) { - const char* xml = "\ + const char* xml = "\ \n\ \n\ \n\ - \n\ - \n\ - \n\ - \n\ - \n\ - \n\ - \n\ - \n\ - \n\ + \n\ + \n\ + \n\ + \n\ + \n\ + \n\ + \n\ + \n\ + \n\ \n"; - fprintf(f, "%s", xml); + fprintf(f, "%s", xml); } struct Tile { - u16 texture1; // index into terrain_textures[] - u16 texture2; // index, or 0xFFFF for 'none' - u32 priority; // ??? + u16 texture1; // index into terrain_textures[] + u16 texture2; // index, or 0xFFFF for 'none' + u32 priority; // ??? }; void OutputPmp(Map* m, FILE* f) { /* struct String { u32 length; char data[length]; // not NUL-terminated }; struct PMP { char header[4]; // == "PSMP" u32 version; // == 4 u32 data_size; // == filesize-12 u32 map_size; // number of patches (16x16 tiles) per side u16 heightmap[(mapsize*16 + 1)^2]; // (squared, not xor) - vertex heights u32 num_terrain_textures; String terrain_textures[num_terrain_textures]; // filenames (no path), e.g. "cliff1.dds" Tile tiles[(mapsize*16)^2]; };*/ - int size = m->size; - int numTerrains = m->idToName.size(); + int size = m->size; + int numTerrains = m->idToName.size(); - // header - fwrite("PSMP", sizeof(char), 4, f); + // header + fwrite("PSMP", sizeof(char), 4, f); - // file format version - u32 version = 4; - fwrite(&version, sizeof(u32), 1, f); - - // data size (write 0 for now, calculate it at the end) - int temp = 0; - fwrite(&temp, sizeof(u32), 1, f); - - // map size in patches - u32 sizeInPatches = size/16; - fwrite(&sizeInPatches, sizeof(u32), 1, f); - - // heightmap - u16* heightmap = new u16[(size+1)*(size+1)]; - for(int x=0; xheight[x][y] * 256.0f / 0.35f); - if(intHeight > 0xFFFF) { - intHeight = 0xFFFF; - } - else if(intHeight < 0) { - intHeight = 0; - } - heightmap[y*(size+1)+x] = intHeight; - } - } - fwrite(heightmap, sizeof(u16), (size+1)*(size+1), f); - - // num terrain textures - fwrite(&numTerrains, sizeof(u32), 1, f); - - // terrain names - for(int i=0; iidToName[i] + ".dds"; - int len = fname.length(); - fwrite(&len, sizeof(u32), 1, f); - fwrite(fname.c_str(), sizeof(char), fname.length(), f); - } - - // terrain; note that this is an array of 16x16 patches for some reason - Tile* tiles = new Tile[size*size]; - for(int x=0; xterrain[x][y]; - t.texture2 = 0xFFFF; - t.priority = 0; - } - } - fwrite(tiles, sizeof(Tile), size*size, f); - - // data size (file size - 12) - fseek(f, 0, SEEK_END); - int fsize = ftell(f); - u32 dataSize = fsize-12; - fseek(f, 8, SEEK_SET); - fwrite(&dataSize, sizeof(u32), 1, f); + // file format version + u32 version = 4; + fwrite(&version, sizeof(u32), 1, f); + + // data size (write 0 for now, calculate it at the end) + int temp = 0; + fwrite(&temp, sizeof(u32), 1, f); + + // map size in patches + u32 sizeInPatches = size/16; + fwrite(&sizeInPatches, sizeof(u32), 1, f); + + // heightmap + u16* heightmap = new u16[(size+1)*(size+1)]; + for(int x=0; xheight[x][y] * 256.0f / 0.35f); + if(intHeight > 0xFFFF) { + intHeight = 0xFFFF; + } + else if(intHeight < 0) { + intHeight = 0; + } + heightmap[y*(size+1)+x] = intHeight; + } + } + fwrite(heightmap, sizeof(u16), (size+1)*(size+1), f); + + // num terrain textures + fwrite(&numTerrains, sizeof(u32), 1, f); + + // terrain names + for(int i=0; iidToName[i] + ".dds"; + int len = fname.length(); + fwrite(&len, sizeof(u32), 1, f); + fwrite(fname.c_str(), sizeof(char), fname.length(), f); + } + + // terrain; note that this is an array of 16x16 patches for some reason + Tile* tiles = new Tile[size*size]; + for(int x=0; xterrain[x][y]; + t.texture2 = 0xFFFF; + t.priority = 0; + } + } + fwrite(tiles, sizeof(Tile), size*size, f); + + // data size (file size - 12) + fseek(f, 0, SEEK_END); + int fsize = ftell(f); + u32 dataSize = fsize-12; + fseek(f, 8, SEEK_SET); + fwrite(&dataSize, sizeof(u32), 1, f); } void OutputMap(Map* m, const string& outputName) { - string xmlName = outputName + ".xml"; - FILE* xmlFile = fopen(xmlName.c_str(), "w"); - if(!xmlFile) { - cerr << "Cannot open " << xmlName << endl; - Shutdown(1); - } - OutputXml(m, xmlFile); - fclose(xmlFile); - - string pmpName = outputName + ".pmp"; - FILE* pmpFile = fopen(pmpName.c_str(), "wb"); - if(!pmpFile) { - cerr << "Cannot open " << xmlName << endl; - Shutdown(1); - } - OutputPmp(m, pmpFile); - fclose(pmpFile); + string xmlName = outputName + ".xml"; + FILE* xmlFile = fopen(xmlName.c_str(), "w"); + if(!xmlFile) { + cerr << "Cannot open " << xmlName << endl; + Shutdown(1); + } + OutputXml(m, xmlFile); + fclose(xmlFile); + + string pmpName = outputName + ".pmp"; + FILE* pmpFile = fopen(pmpName.c_str(), "wb"); + if(!pmpFile) { + cerr << "Cannot open " << xmlName << endl; + Shutdown(1); + } + OutputPmp(m, pmpFile); + fclose(pmpFile); - cout << "Map outputted to " << outputName << ".*" << endl; + cout << "Map outputted to " << outputName << ".*" << endl; } Index: ps/trunk/source/tools/rmgen/map.h =================================================================== --- ps/trunk/source/tools/rmgen/map.h (revision 2300) +++ ps/trunk/source/tools/rmgen/map.h (revision 2301) @@ -1,27 +1,27 @@ #ifndef __MAP_H__ #define __MAP_H__ class Map { public: - int size; - int** terrain; - float** height; - std::map nameToId; - std::map idToName; + int size; + int** terrain; + float** height; + std::map nameToId; + std::map idToName; - Map(int size, const std::string& baseTerrain, float baseHeight); - ~Map(); + Map(int size, const std::string& baseTerrain, float baseHeight); + ~Map(); - int getId(std::string terrain); + int getId(std::string terrain); - bool validT(int x, int y); - bool validH(int x, int y); + bool validT(int x, int y); + bool validH(int x, int y); - std::string getTerrain(int x, int y); - void setTerrain(int x, int y, const std::string& terrain); + std::string getTerrain(int x, int y); + void setTerrain(int x, int y, const std::string& terrain); - float getHeight(int x, int y); - void setHeight(int x, int y, float height); + float getHeight(int x, int y); + void setHeight(int x, int y, float height); }; #endif \ No newline at end of file Index: ps/trunk/source/tools/rmgen/rmgen.cpp =================================================================== --- ps/trunk/source/tools/rmgen/rmgen.cpp (revision 2300) +++ ps/trunk/source/tools/rmgen/rmgen.cpp (revision 2301) @@ -1,236 +1,236 @@ #include "stdafx.h" #include "rmgen.h" #include "map.h" #include "output.h" using namespace std; JSRuntime *rt = 0; JSContext *cx = 0; JSObject *global = 0; Map* theMap = 0; // JS support functions void ErrorReporter(JSContext *cx, const char *message, JSErrorReport *report) { - cerr << "Error at " << report->filename << ":" << report->lineno << ":\n\t" - << message << endl; - Shutdown(1); + cerr << "Error at " << report->filename << ":" << report->lineno << ":\n\t" + << message << endl; + Shutdown(1); } JSFunctionSpec globalFunctions[] = { // {name, native, args} - {"init", init, 3}, - {"print", print, 1}, - {"getTerrain", getTerrain, 2}, - {"setTerrain", setTerrain, 3}, - {"getHeight", getHeight, 2}, - {"setHeight", setHeight, 3}, - {0} + {"init", init, 3}, + {"print", print, 1}, + {"getTerrain", getTerrain, 2}, + {"setTerrain", setTerrain, 3}, + {"getHeight", getHeight, 2}, + {"setHeight", setHeight, 3}, + {0} }; void InitJS() { rt = JS_NewRuntime(8L * 1024L * 1024L); cx = JS_NewContext(rt, 8192); - JS_SetErrorReporter(cx, ErrorReporter); - - static JSClass globalClass = { - "global",0, - JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,JS_PropertyStub, - JS_EnumerateStub,JS_ResolveStub,JS_ConvertStub,JS_FinalizeStub - }; + JS_SetErrorReporter(cx, ErrorReporter); + + static JSClass globalClass = { + "global",0, + JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,JS_PropertyStub, + JS_EnumerateStub,JS_ResolveStub,JS_ConvertStub,JS_FinalizeStub + }; global = JS_NewObject(cx, &globalClass, NULL, NULL); JS_InitStandardClasses(cx, global); - JS_DefineFunctions(cx, global, globalFunctions); + JS_DefineFunctions(cx, global, globalFunctions); } void Shutdown(int status) { - JS_DestroyContext(cx); - JS_DestroyRuntime(rt); - JS_ShutDown(); - system("pause"); - exit(status); + JS_DestroyContext(cx); + JS_DestroyRuntime(rt); + JS_ShutDown(); + system("pause"); + exit(status); } char* ValToString(jsval val) { - return JS_GetStringBytes(JS_ValueToString(cx, val)); + return JS_GetStringBytes(JS_ValueToString(cx, val)); } jsval NewJSString(const string& str) { - char* buf = (char*) JS_malloc(cx, str.length()); - memcpy(buf, str.c_str(), str.length()); - return STRING_TO_JSVAL(JS_NewString(cx, buf, str.length())); + char* buf = (char*) JS_malloc(cx, str.length()); + memcpy(buf, str.c_str(), str.length()); + return STRING_TO_JSVAL(JS_NewString(cx, buf, str.length())); } // JS API implementation JSBool print(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { - if(argc != 1) { - JS_ReportError(cx, "print: expected 1 argument but got %d", argc); - } - - cout << JS_GetStringBytes(JS_ValueToString(cx, argv[0])); - return JS_TRUE; + if(argc != 1) { + JS_ReportError(cx, "print: expected 1 argument but got %d", argc); + } + + cout << JS_GetStringBytes(JS_ValueToString(cx, argv[0])); + return JS_TRUE; } JSBool init(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { - if(argc != 3) { - JS_ReportError(cx, "init: expected 3 arguments but got %d", argc); - } - if(!JSVAL_IS_INT(argv[0])) { - JS_ReportError(cx, "init: first argument must be an integer"); - } - if(!JSVAL_IS_STRING(argv[1])) { - JS_ReportError(cx, "init: second argument must be a string"); - } - if(!JSVAL_IS_NUMBER(argv[2])) { - JS_ReportError(cx, "init: third argument must be a number"); - } - if(theMap != 0) { - JS_ReportError(cx, "init: cannot be called twice"); - } - - int size = JSVAL_TO_INT(argv[0]); - char* baseTerrain = JS_GetStringBytes(JSVAL_TO_STRING(argv[1])); - jsdouble baseHeight; - JS_ValueToNumber(cx, argv[2], &baseHeight); + if(argc != 3) { + JS_ReportError(cx, "init: expected 3 arguments but got %d", argc); + } + if(!JSVAL_IS_INT(argv[0])) { + JS_ReportError(cx, "init: first argument must be an integer"); + } + if(!JSVAL_IS_STRING(argv[1])) { + JS_ReportError(cx, "init: second argument must be a string"); + } + if(!JSVAL_IS_NUMBER(argv[2])) { + JS_ReportError(cx, "init: third argument must be a number"); + } + if(theMap != 0) { + JS_ReportError(cx, "init: cannot be called twice"); + } + + int size = JSVAL_TO_INT(argv[0]); + char* baseTerrain = JS_GetStringBytes(JSVAL_TO_STRING(argv[1])); + jsdouble baseHeight; + JS_ValueToNumber(cx, argv[2], &baseHeight); - theMap = new Map(size, baseTerrain, (float) baseHeight); - return JS_TRUE; + theMap = new Map(size, baseTerrain, (float) baseHeight); + return JS_TRUE; } JSBool getTerrain(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { - if(argc != 2) { - JS_ReportError(cx, "getTerrain: expected 2 arguments but got %d", argc); - } - if(theMap == 0) { - JS_ReportError(cx, "getTerrain: cannot be called before init()"); - } - if(!JSVAL_IS_INT(argv[0])) { - JS_ReportError(cx, "getTerrain: first argument must be an integer"); - } - if(!JSVAL_IS_INT(argv[1])) { - JS_ReportError(cx, "getTerrain: second argument must be an integer"); - } - - int x = JSVAL_TO_INT(argv[0]); - int y = JSVAL_TO_INT(argv[1]); - string terrain = theMap->getTerrain(x, y); - *rval = NewJSString(terrain); - return JS_TRUE; + if(argc != 2) { + JS_ReportError(cx, "getTerrain: expected 2 arguments but got %d", argc); + } + if(theMap == 0) { + JS_ReportError(cx, "getTerrain: cannot be called before init()"); + } + if(!JSVAL_IS_INT(argv[0])) { + JS_ReportError(cx, "getTerrain: first argument must be an integer"); + } + if(!JSVAL_IS_INT(argv[1])) { + JS_ReportError(cx, "getTerrain: second argument must be an integer"); + } + + int x = JSVAL_TO_INT(argv[0]); + int y = JSVAL_TO_INT(argv[1]); + string terrain = theMap->getTerrain(x, y); + *rval = NewJSString(terrain); + return JS_TRUE; } JSBool setTerrain(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { - if(argc != 3) { - JS_ReportError(cx, "setTerrain: expected 3 arguments but got %d", argc); - } - if(theMap == 0) { - JS_ReportError(cx, "setTerrain: cannot be called before init()"); - } - if(!JSVAL_IS_INT(argv[0])) { - JS_ReportError(cx, "setTerrain: first argument must be an integer"); - } - if(!JSVAL_IS_INT(argv[1])) { - JS_ReportError(cx, "setTerrain: second argument must be an integer"); - } - if(!JSVAL_IS_STRING(argv[2])) { - JS_ReportError(cx, "setTerrain: third argument must be a string"); - } - - int x = JSVAL_TO_INT(argv[0]); - int y = JSVAL_TO_INT(argv[1]); - char* terrain = JS_GetStringBytes(JSVAL_TO_STRING(argv[2])); - theMap->setTerrain(x, y, terrain); - return JS_TRUE; + if(argc != 3) { + JS_ReportError(cx, "setTerrain: expected 3 arguments but got %d", argc); + } + if(theMap == 0) { + JS_ReportError(cx, "setTerrain: cannot be called before init()"); + } + if(!JSVAL_IS_INT(argv[0])) { + JS_ReportError(cx, "setTerrain: first argument must be an integer"); + } + if(!JSVAL_IS_INT(argv[1])) { + JS_ReportError(cx, "setTerrain: second argument must be an integer"); + } + if(!JSVAL_IS_STRING(argv[2])) { + JS_ReportError(cx, "setTerrain: third argument must be a string"); + } + + int x = JSVAL_TO_INT(argv[0]); + int y = JSVAL_TO_INT(argv[1]); + char* terrain = JS_GetStringBytes(JSVAL_TO_STRING(argv[2])); + theMap->setTerrain(x, y, terrain); + return JS_TRUE; } JSBool getHeight(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { - if(argc != 2) { - JS_ReportError(cx, "getHeight: expected 2 arguments but got %d", argc); - } - if(theMap == 0) { - JS_ReportError(cx, "getHeight: cannot be called before init()"); - } - if(!JSVAL_IS_INT(argv[0])) { - JS_ReportError(cx, "getHeight: first argument must be an integer"); - } - if(!JSVAL_IS_INT(argv[1])) { - JS_ReportError(cx, "getHeight: second argument must be an integer"); - } - - int x = JSVAL_TO_INT(argv[0]); - int y = JSVAL_TO_INT(argv[1]); - jsdouble height = theMap->getHeight(x, y); - JS_NewDoubleValue(cx, height, rval); - return JS_TRUE; + if(argc != 2) { + JS_ReportError(cx, "getHeight: expected 2 arguments but got %d", argc); + } + if(theMap == 0) { + JS_ReportError(cx, "getHeight: cannot be called before init()"); + } + if(!JSVAL_IS_INT(argv[0])) { + JS_ReportError(cx, "getHeight: first argument must be an integer"); + } + if(!JSVAL_IS_INT(argv[1])) { + JS_ReportError(cx, "getHeight: second argument must be an integer"); + } + + int x = JSVAL_TO_INT(argv[0]); + int y = JSVAL_TO_INT(argv[1]); + jsdouble height = theMap->getHeight(x, y); + JS_NewDoubleValue(cx, height, rval); + return JS_TRUE; } JSBool setHeight(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { - if(argc != 3) { - JS_ReportError(cx, "setHeight: expected 3 arguments but got %d", argc); - } - if(theMap == 0) { - JS_ReportError(cx, "setHeight: cannot be called before init()"); - } - if(!JSVAL_IS_INT(argv[0])) { - JS_ReportError(cx, "setHeight: first argument must be an integer"); - } - if(!JSVAL_IS_INT(argv[1])) { - JS_ReportError(cx, "setHeight: second argument must be an integer"); - } - if(!JSVAL_IS_NUMBER(argv[2])) { - JS_ReportError(cx, "setHeight: third argument must be a number"); - } - - int x = JSVAL_TO_INT(argv[0]); - int y = JSVAL_TO_INT(argv[1]); - jsdouble height; - JS_ValueToNumber(cx, argv[2], &height); - theMap->setHeight(x, y, (float) height); - return JS_TRUE; + if(argc != 3) { + JS_ReportError(cx, "setHeight: expected 3 arguments but got %d", argc); + } + if(theMap == 0) { + JS_ReportError(cx, "setHeight: cannot be called before init()"); + } + if(!JSVAL_IS_INT(argv[0])) { + JS_ReportError(cx, "setHeight: first argument must be an integer"); + } + if(!JSVAL_IS_INT(argv[1])) { + JS_ReportError(cx, "setHeight: second argument must be an integer"); + } + if(!JSVAL_IS_NUMBER(argv[2])) { + JS_ReportError(cx, "setHeight: third argument must be a number"); + } + + int x = JSVAL_TO_INT(argv[0]); + int y = JSVAL_TO_INT(argv[1]); + jsdouble height; + JS_ValueToNumber(cx, argv[2], &height); + theMap->setHeight(x, y, (float) height); + return JS_TRUE; } // Program entry point int main(int argc, char* argv[]) { - InitJS(); + InitJS(); - if(argc!=3) { - cerr << "Usage: rmgen