void EntityResourceManager::loadResource(const std::string filepath) { //open up the file std::filebuf fb; fb.open(filepath, std::ios::in); //try to parse it Json::Value root; Json::Reader reader; bool parseSuccessful = reader.parse(std::istream(&fb), root); //now we have it parsed we can close it fb.close(); if(!parseSuccessful) { std::cerr << "Unable to load entity: " << filepath << std::endl; std::cerr << reader.getFormattedErrorMessages() << std::endl; return; } Entity *entityPtr = loadType(root); if(entityPtr == NULL) { std::cerr << "Invalid type in entity file " << filepath << std::endl; return; } loadGenerics(*entityPtr, root); loadDamagingEntity(*entityPtr, root); loadCharacter(*entityPtr, root); std::string behaviour = root.get("behaviour", "").asString(); if(!behaviour.empty()) { loadBehaviour(*entityPtr, root); } Json::Value animations = root["animations"]; if(animations.size() > 0) { loadAnimations(*entityPtr, root); } Json::Value abilities = root["abilities"]; if(abilities.size() > 0) { loadAbilities(*entityPtr, root); } resourceMap[filepath] = entityPtr; }
void init(int argc, char *argv[]) { OSG::osgInit(argc, argv); g = new GlobalVars; int glutWinId = setupGLUT(&argc, argv); g->win = OSG::GLUTWindow::create(); g->win->setGlutId(glutWinId); g->win->init(); g->mgr = OSG::SimpleSceneManager::create(); g->mgr->setWindow(g->win); g->rootN = OSG::makeCoredNode<OSG::Group>(); g->mgr->setRoot(g->rootN); g->mgr->getHeadlight()->setDiffuse (0.7f, 0.7f, 0.5f, 1.f); g->mgr->getHeadlight()->setAmbient (0.3f, 0.3f, 0.3f, 1.f); g->mgr->getHeadlight()->setSpecular(0.3f, 0.3f, 0.3f, 1.f); g->charState = CharIdle; g->prevCharState = CharIdle; g->angle = 0.f; g->angleVel = idleAngleVel; g->renderMode = OSG::SkinnedGeometry::RMSkinnedCPU; loadCharacter (); loadTextures (); initAnimations(); loadBackground(); initFloor (); initShader (); g->mgr->showAll(); }
BdfFont *BdfFont::loadFont(Common::SeekableReadStream &stream) { BdfFontData font; memset(&font, 0, sizeof(font)); font.ascent = -1; font.defaultCharacter = -1; // We only load the first 256 characters font.numCharacters = 256; byte **bitmaps = new byte *[font.numCharacters]; memset(bitmaps, 0, sizeof(byte *) * font.numCharacters); byte *advances = new byte[font.numCharacters]; BdfBoundingBox *boxes = new BdfBoundingBox[font.numCharacters]; int descent = -1; Common::String line; while (true) { line = stream.readLine(); if (stream.err() || stream.eos()) { warning("BdfFont::loadFont: Premature end of file"); freeBitmaps(bitmaps, font.numCharacters); delete[] bitmaps; delete[] advances; delete[] boxes; return 0; } // Only parse and handle declarations we actually need if (line.hasPrefix("FONTBOUNDINGBOX ")) { int width, height, xOffset, yOffset; if (sscanf(line.c_str(), "FONTBOUNDINGBOX %d %d %d %d", &width, &height, &xOffset, &yOffset) != 4) { warning("BdfFont::loadFont: Invalid FONTBOUNDINGBOX"); freeBitmaps(bitmaps, font.numCharacters); delete[] bitmaps; delete[] advances; delete[] boxes; return 0; } font.defaultBox.width = width; font.defaultBox.height = height; font.defaultBox.xOffset = xOffset; font.defaultBox.yOffset = yOffset; } else if (line.hasPrefix("FONT_ASCENT ")) { if (sscanf(line.c_str(), "FONT_ASCENT %d", &font.ascent) != 1) { warning("BdfFont::loadFont: Invalid FONT_ASCENT"); freeBitmaps(bitmaps, font.numCharacters); delete[] bitmaps; delete[] advances; delete[] boxes; return 0; } } else if (line.hasPrefix("FONT_DESCENT ")) { if (sscanf(line.c_str(), "FONT_DESCENT %d", &descent) != 1) { warning("BdfFont::loadFont: Invalid FONT_DESCENT"); freeBitmaps(bitmaps, font.numCharacters); delete[] bitmaps; delete[] advances; delete[] boxes; return 0; } } else if (line.hasPrefix("DEFAULT_CHAR ")) { if (sscanf(line.c_str(), "DEFAULT_CHAR %d", &font.defaultCharacter) != 1) { warning("BdfFont::loadFont: Invalid DEFAULT_CHAR"); freeBitmaps(bitmaps, font.numCharacters); delete[] bitmaps; delete[] advances; delete[] boxes; return 0; } } else if (line.hasPrefix("STARTCHAR ")) { BdfBoundingBox box = font.defaultBox; int encoding = -1; int advance = -1; byte *bitmap = loadCharacter(stream, encoding, advance, box); // Ignore all characters above 255. if (encoding < -1 || encoding >= font.numCharacters) { delete[] bitmap; encoding = -1; } // Calculate the max advance if (encoding != -1 && advance > font.maxAdvance) font.maxAdvance = advance; if (!bitmap && encoding != -1) { warning("BdfFont::loadFont: Character %d invalid", encoding); freeBitmaps(bitmaps, font.numCharacters); delete[] bitmaps; delete[] advances; delete[] boxes; return 0; } if (encoding != -1) { bitmaps[encoding] = bitmap; advances[encoding] = advance; boxes[encoding] = box; } } else if (line == "ENDFONT") { break; } } if (font.ascent < 0 || descent < 0) { warning("BdfFont::loadFont: Invalid ascent or descent"); freeBitmaps(bitmaps, font.numCharacters); delete[] bitmaps; delete[] advances; delete[] boxes; return 0; } font.height = font.ascent + descent; font.bitmaps = bitmaps; font.advances = advances; font.boxes = boxes; int firstCharacter = font.numCharacters; int lastCharacter = -1; bool hasFixedBBox = true; bool hasFixedAdvance = true; for (int i = 0; i < font.numCharacters; ++i) { if (!font.bitmaps[i]) continue; if (i < firstCharacter) firstCharacter = i; if (i > lastCharacter) lastCharacter = i; if (font.advances[i] != font.maxAdvance) hasFixedAdvance = false; const BdfBoundingBox &bbox = font.boxes[i]; if (bbox.width != font.defaultBox.width || bbox.height != font.defaultBox.height || bbox.xOffset != font.defaultBox.xOffset || bbox.yOffset != font.defaultBox.yOffset) hasFixedBBox = false; } if (lastCharacter == -1) { warning("BdfFont::loadFont: No glyphs found"); delete[] font.bitmaps; delete[] font.advances; delete[] font.boxes; return 0; } // Free the advance table, in case all glyphs use the same advance if (hasFixedAdvance) { delete[] font.advances; font.advances = 0; } // Free the box table, in case all glyphs use the same box if (hasFixedBBox) { delete[] font.boxes; font.boxes = 0; } // Adapt for the fact that we never use encoding 0. if (font.defaultCharacter < firstCharacter || font.defaultCharacter > lastCharacter) font.defaultCharacter = -1; font.firstCharacter = firstCharacter; const int charsAvailable = lastCharacter - firstCharacter + 1; // Try to compact the tables if (charsAvailable < font.numCharacters) { byte **newBitmaps = new byte *[charsAvailable]; boxes = 0; advances = 0; if (!hasFixedBBox) boxes = new BdfBoundingBox[charsAvailable]; if (!hasFixedAdvance) advances = new byte[charsAvailable]; for (int i = 0; i < charsAvailable; ++i) { const int encoding = i + firstCharacter; if (font.bitmaps[encoding]) { newBitmaps[i] = bitmaps[encoding]; if (!hasFixedBBox) boxes[i] = font.boxes[encoding]; if (!hasFixedAdvance) advances[i] = font.advances[encoding]; } else { newBitmaps[i] = 0; } } delete[] font.bitmaps; font.bitmaps = newBitmaps; delete[] font.advances; font.advances = advances; delete[] font.boxes; font.boxes = boxes; font.numCharacters = charsAvailable; } return new BdfFont(font, DisposeAfterUse::YES); }
Creature::Creature(const Common::UString &bic, bool local) : Object(kObjectTypeCreature) { init(); loadCharacter(bic, local); }