void BulletAndWeaponInitialize( BulletClasses *b, GunClasses *g, const char *bpath, const char *gpath) { BulletInitialize(b); FILE *bf = NULL; FILE *gf = NULL; json_t *broot = NULL; json_t *groot = NULL; enum json_error e; // 2-pass bullet loading will free root for us bool freeBRoot = true; bf = fopen(bpath, "r"); if (bf == NULL) { printf("Error: cannot load bullets file %s\n", bpath); goto bail; } e = json_stream_parse(bf, &broot); if (e != JSON_OK) { printf("Error parsing bullets file %s [error %d]\n", bpath, (int)e); goto bail; } BulletLoadJSON(b, &b->Classes, broot); WeaponInitialize(g); gf = fopen(gpath, "r"); if (gf == NULL) { printf("Error: cannot load guns file %s\n", gpath); goto bail; } e = json_stream_parse(gf, &groot); if (e != JSON_OK) { printf("Error parsing guns file %s [error %d]\n", gpath, (int)e); goto bail; } WeaponLoadJSON(g, &g->Guns, groot); BulletLoadWeapons(b); freeBRoot = false; bail: if (bf) { fclose(bf); } if (gf) { fclose(gf); } if (freeBRoot) { json_free_value(&broot); } json_free_value(&groot); }
int main(int argc, char *argv[]) { int i; int loaded = 0; printf("C-Dogs SDL Editor\n"); debug(D_NORMAL, "Initialising SDL...\n"); if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO) != 0) { printf("Failed to start SDL!\n"); return -1; } SDL_EnableUNICODE(SDL_ENABLE); printf("Data directory:\t\t%s\n", GetDataFilePath("")); printf("Config directory:\t%s\n\n", GetConfigFilePath("")); EditorBrushInit(&brush); ConfigLoadDefault(&gConfig); ConfigLoad(&gConfig, GetConfigFilePath(CONFIG_FILE)); gConfig.Graphics.IsEditor = 1; if (!PicManagerTryInit( &gPicManager, "graphics/cdogs.px", "graphics/cdogs2.px")) { exit(0); } memcpy(origPalette, gPicManager.palette, sizeof origPalette); BuildTranslationTables(gPicManager.palette); TextManagerInit(&gTextManager, GetDataFilePath("graphics/font.px")); GraphicsInit(&gGraphicsDevice); // Hardcode config settings gConfig.Graphics.ScaleMode = SCALE_MODE_NN; gConfig.Graphics.ScaleFactor = 2; gConfig.Graphics.Res.x = 400; gConfig.Graphics.Res.y = 300; GraphicsInitialize( &gGraphicsDevice, &gConfig.Graphics, gPicManager.palette, 0); if (!gGraphicsDevice.IsInitialized) { printf("Video didn't init!\n"); exit(EXIT_FAILURE); } TextManagerGenerateOldPics(&gTextManager, &gGraphicsDevice); PicManagerLoadDir(&gPicManager, GetDataFilePath("graphics")); BulletInitialize(); WeaponInitialize(&gGunDescriptions, GetDataFilePath("guns.json")); CampaignInit(&gCampaign); MissionInit(&lastMission); MissionInit(¤tMission); PlayerDataInitialize(); MapInit(&gMap); // initialise UI collections // Note: must do this after text init since positions depend on text height sObjs = CreateMainObjs(&gCampaign, &brush, Vec2iNew(320, 240)); memset(&sDrawObjs, 0, sizeof sDrawObjs); DrawBufferInit(&sDrawBuffer, Vec2iNew(X_TILES, Y_TILES), &gGraphicsDevice); // Reset campaign (graphics init may have created dummy campaigns) CampaignSettingTerminate(&gCampaign.Setting); CampaignSettingInit(&gCampaign.Setting); EventInit(&gEventHandlers, NULL, false); for (i = 1; i < argc; i++) { if (!loaded) { debug(D_NORMAL, "Loading map %s\n", argv[i]); memset(lastFile, 0, sizeof(lastFile)); strncpy(lastFile, argv[i], sizeof(lastFile) - 1); if (strchr(lastFile, '.') == NULL && sizeof lastFile - strlen(lastFile) > 3) { strcat(lastFile, ".CPN"); } if (MapNewLoad(lastFile, &gCampaign.Setting) == 0) { loaded = 1; } debug(D_NORMAL, "Loaded map %s\n", argv[i]); } } debug(D_NORMAL, "Starting editor\n"); EditCampaign(); MapTerminate(&gMap); WeaponTerminate(&gGunDescriptions); CampaignTerminate(&gCampaign); MissionTerminate(&lastMission); MissionTerminate(¤tMission); DrawBufferTerminate(&sDrawBuffer); GraphicsTerminate(&gGraphicsDevice); PicManagerTerminate(&gPicManager); TextManagerTerminate(&gTextManager); UIObjectDestroy(sObjs); CArrayTerminate(&sDrawObjs); EditorBrushTerminate(&brush); SDL_Quit(); exit(EXIT_SUCCESS); }