backgroundManager::backgroundManager(std::string filename, sf::Vector2f screenSize){ this->screenSize=screenSize; scrollSpeed=100; position=sf::Vector2f(0,0); loadImage(filename); compileBackground(); }
void generateLevel(const std::string &level) { cleanLevel(); Json::Value root; // will contains the root value after parsing. curLevel = new Level(level, &root); if(!loadJSONLevel(level, root)) { printf("Error parsing JSON file for level %s!\n", level.c_str()); generateLevel("worldmap"); return; // TODO: Load from a default? } Json::Value tileset = root["tileset"]; Json::Value customEntities = root["entities"]; char line[201] = ""; std::string filename = "levels/"+level+".lvl"; if(DEBUG) printf("Reading file %s\n", filename.c_str()); FILE *fp = fopen(filename.c_str(), "r"); if(!fp) { printf("ERROR: Level %s not found!\n", filename.c_str()); generateLevel("worldmap"); return; } char blockC[] = "-"; int x=0, y=0, biggestX=0, biggestY=0; Entity *ent; for(y=0; fgets(line, sizeof(line), fp); y++) { if(DEBUG) printf("Read line: %s", line); for(x=0; line[x]; x++) { if(line[x] <= 32) continue; blockC[0] = line[x]; if(!!tileset[blockC]) { if(DEBUG) printf("Spawning block(%d,%d) type(%d,%c)\n", x, y, line[x], line[x]); ent = constructEntity(tileset[blockC], x, y); posLookup[x][y] = ent; // TODO: Remove from list, ensure consistency across block movements setEntityProperties(ent, tileset[blockC]); if(x > biggestX) biggestX = x; if(y > biggestY) biggestY = y; } else { printf("Unknown Block: '%c' (%d)\n", line[x], line[x]); } } memset(line, '\0', sizeof(line)); } curLevel->w = max((biggestX+1)*BLOCK_SIZE, WIDTH); curLevel->h = max((biggestY+1)*BLOCK_SIZE, HEIGHT); char sPos[20]; for(Json::ValueIterator iter = customEntities.begin(); iter != customEntities.end(); iter++ ) { strncpy(sPos, iter.memberName(), sizeof(sPos)-1); x = atoi(strtok(sPos+1, ",")); y = atoi(strtok(NULL, ")")); if((ent = posLookup[x][y]) == NULL) { printf("Level setup error: Entity(%d,%d) not found!\n", x, y); continue; } if(DEBUG) printf("Applying Entity(%d,%d) specifics...\n", x, y); setEntityProperties(ent, *iter); } compileBackground(renderer); }