From: Côme Chilliet Date: Sun, 4 Dec 2016 07:43:02 +0000 (+0700) Subject: Trying to handle a bit better missing files and avoid segfaults X-Git-Url: https://git.jsancho.org/?p=lugaru.git;a=commitdiff_plain;h=63b9ec8b134f9b42e899bec74bc0e6335d3afb47 Trying to handle a bit better missing files and avoid segfaults Also using multiplatform SDL_ShowSimpleMessageBox instead of Windows only MessageBox --- diff --git a/Source/GameInitDispose.cpp b/Source/GameInitDispose.cpp index 4a700b3..fcd1798 100644 --- a/Source/GameInitDispose.cpp +++ b/Source/GameInitDispose.cpp @@ -146,7 +146,10 @@ void LoadSave(const char *fileName, GLuint *textureid, bool mipmap, GLubyte *arr //Load Image ImageRec texture; - load_image(Folders::getResourcePath(fileName).c_str(), texture); + if (!load_image(Folders::getResourcePath(fileName).c_str(), texture)) { + texdetail = temptexdetail; + return; + } texdetail = temptexdetail; int bytesPerPixel = texture.bpp / 8; diff --git a/Source/GameTick.cpp b/Source/GameTick.cpp index 45b7119..5e4f306 100644 --- a/Source/GameTick.cpp +++ b/Source/GameTick.cpp @@ -819,289 +819,304 @@ void Game::Loadlevel(const std::string& name) int mapvers; FILE *tfile; errno = 0; - tfile = fopen( Folders::getResourcePath("Maps/"+name).c_str(), "rb" ); - if (tfile) { - pause_sound(stream_firesound); - scoreadded = 0; - windialogue = false; - hostiletime = 0; - won = 0; + tfile = Folders::openMandatoryFile( Folders::getResourcePath("Maps/"+name), "rb" ); - animation[bounceidleanim].Load((char *)"Idle", middleheight, neutral); - - Dialog::dialogs.clear(); + pause_sound(stream_firesound); + scoreadded = 0; + windialogue = false; + hostiletime = 0; + won = 0; + + animation[bounceidleanim].Load((char *)"Idle", middleheight, neutral); + + Dialog::dialogs.clear(); + + Dialog::indialogue = -1; + cameramode = 0; + + damagedealt = 0; + damagetaken = 0; + + if (accountactive) + difficulty = accountactive->getDifficulty(); + + numhotspots = 0; + currenthotspot = -1; + bonustime = 1; + + skyboxtexture = 1; + skyboxr = 1; + skyboxg = 1; + skyboxb = 1; + + freeze = 0; + winfreeze = 0; + + for (int i = 0; i < 100; i++) + bonusnum[i] = 0; + + numfalls = 0; + numflipfail = 0; + numseen = 0; + numstaffattack = 0; + numswordattack = 0; + numknifeattack = 0; + numunarmedattack = 0; + numescaped = 0; + numflipped = 0; + numwallflipped = 0; + numthrowkill = 0; + numafterkill = 0; + numreversals = 0; + numattacks = 0; + maxalarmed = 0; + numresponded = 0; + + bonustotal = startbonustotal; + bonus = 0; + gameon = 1; + changedelay = 0; + if (console) { + emit_sound_np(consolesuccesssound); + freeze = 0; + console = false; + } - Dialog::indialogue = -1; - cameramode = 0; + if (!stealthloading) { + terrain.numdecals = 0; + Sprite::deleteSprites(); + for (int i = 0; i < objects.numobjects; i++) + objects.model[i].numdecals = 0; - damagedealt = 0; - damagetaken = 0; + int j = objects.numobjects; + for (int i = 0; i < j; i++) { + objects.DeleteObject(0); + if (visibleloading) + LoadingScreen(); + } - if (accountactive) - difficulty = accountactive->getDifficulty(); + for (int i = 0; i < subdivision; i++) + for (int j = 0; j < subdivision; j++) + terrain.patchobjectnum[i][j] = 0; + if (visibleloading) + LoadingScreen(); + } - numhotspots = 0; - currenthotspot = -1; - bonustime = 1; + weapons.clear(); + Person::players.resize(1); + funpackf(tfile, "Bi", &mapvers); + if (mapvers >= 15) + funpackf(tfile, "Bi", &indemo); + else + indemo = 0; + if (mapvers >= 5) + funpackf(tfile, "Bi", &maptype); + else + maptype = mapkilleveryone; + if (mapvers >= 6) + funpackf(tfile, "Bi", &hostile); + else + hostile = 1; + if (mapvers >= 4) + funpackf(tfile, "Bf Bf", &viewdistance, &fadestart); + else { + viewdistance = 100; + fadestart = .6; + } + if (mapvers >= 2) + funpackf(tfile, "Bb Bf Bf Bf", &skyboxtexture, &skyboxr, &skyboxg, &skyboxb); + else { skyboxtexture = 1; skyboxr = 1; skyboxg = 1; skyboxb = 1; - - freeze = 0; - winfreeze = 0; - - for (int i = 0; i < 100; i++) - bonusnum[i] = 0; - - numfalls = 0; - numflipfail = 0; - numseen = 0; - numstaffattack = 0; - numswordattack = 0; - numknifeattack = 0; - numunarmedattack = 0; - numescaped = 0; - numflipped = 0; - numwallflipped = 0; - numthrowkill = 0; - numafterkill = 0; - numreversals = 0; - numattacks = 0; - maxalarmed = 0; - numresponded = 0; - - bonustotal = startbonustotal; - bonus = 0; - gameon = 1; - changedelay = 0; - if (console) { - emit_sound_np(consolesuccesssound); - freeze = 0; - console = false; + } + if (mapvers >= 10) + funpackf(tfile, "Bf Bf Bf", &skyboxlightr, &skyboxlightg, &skyboxlightb); + else { + skyboxlightr = skyboxr; + skyboxlightg = skyboxg; + skyboxlightb = skyboxb; + } + if (!stealthloading) + funpackf(tfile, "Bf Bf Bf Bf Bf Bi", &Person::players[0]->coords.x, &Person::players[0]->coords.y, &Person::players[0]->coords.z, &Person::players[0]->yaw, &Person::players[0]->targetyaw, &Person::players[0]->num_weapons); + if (stealthloading) + funpackf(tfile, "Bf Bf Bf Bf Bf Bi", &lamefloat, &lamefloat, &lamefloat, &lamefloat, &lamefloat, &Person::players[0]->num_weapons); + if (Person::players[0]->num_weapons > 0 && Person::players[0]->num_weapons < 5) + for (int j = 0; j < Person::players[0]->num_weapons; j++) { + Person::players[0]->weaponids[j] = weapons.size(); + int type; + funpackf(tfile, "Bi", &type); + weapons.push_back(Weapon(type, 0)); } - if (!stealthloading) { - terrain.numdecals = 0; - Sprite::deleteSprites(); - for (int i = 0; i < objects.numobjects; i++) - objects.model[i].numdecals = 0; - - int j = objects.numobjects; - for (int i = 0; i < j; i++) { - objects.DeleteObject(0); - if (visibleloading) - LoadingScreen(); - } + if (visibleloading) + LoadingScreen(); - for (int i = 0; i < subdivision; i++) - for (int j = 0; j < subdivision; j++) - terrain.patchobjectnum[i][j] = 0; - if (visibleloading) - LoadingScreen(); - } + funpackf(tfile, "Bf Bf Bf", &Person::players[0]->armorhead, &Person::players[0]->armorhigh, &Person::players[0]->armorlow); + funpackf(tfile, "Bf Bf Bf", &Person::players[0]->protectionhead, &Person::players[0]->protectionhigh, &Person::players[0]->protectionlow); + funpackf(tfile, "Bf Bf Bf", &Person::players[0]->metalhead, &Person::players[0]->metalhigh, &Person::players[0]->metallow); + funpackf(tfile, "Bf Bf", &Person::players[0]->power, &Person::players[0]->speedmult); - weapons.clear(); - Person::players.resize(1); + funpackf(tfile, "Bi", &Person::players[0]->numclothes); - funpackf(tfile, "Bi", &mapvers); - if (mapvers >= 15) - funpackf(tfile, "Bi", &indemo); - else - indemo = 0; - if (mapvers >= 5) - funpackf(tfile, "Bi", &maptype); - else - maptype = mapkilleveryone; - if (mapvers >= 6) - funpackf(tfile, "Bi", &hostile); - else - hostile = 1; - if (mapvers >= 4) - funpackf(tfile, "Bf Bf", &viewdistance, &fadestart); - else { - viewdistance = 100; - fadestart = .6; - } - if (mapvers >= 2) - funpackf(tfile, "Bb Bf Bf Bf", &skyboxtexture, &skyboxr, &skyboxg, &skyboxb); - else { - skyboxtexture = 1; - skyboxr = 1; - skyboxg = 1; - skyboxb = 1; - } - if (mapvers >= 10) - funpackf(tfile, "Bf Bf Bf", &skyboxlightr, &skyboxlightg, &skyboxlightb); - else { - skyboxlightr = skyboxr; - skyboxlightg = skyboxg; - skyboxlightb = skyboxb; - } - if (!stealthloading) - funpackf(tfile, "Bf Bf Bf Bf Bf Bi", &Person::players[0]->coords.x, &Person::players[0]->coords.y, &Person::players[0]->coords.z, &Person::players[0]->yaw, &Person::players[0]->targetyaw, &Person::players[0]->num_weapons); - if (stealthloading) - funpackf(tfile, "Bf Bf Bf Bf Bf Bi", &lamefloat, &lamefloat, &lamefloat, &lamefloat, &lamefloat, &Person::players[0]->num_weapons); - if (Person::players[0]->num_weapons > 0 && Person::players[0]->num_weapons < 5) - for (int j = 0; j < Person::players[0]->num_weapons; j++) { - Person::players[0]->weaponids[j] = weapons.size(); - int type; - funpackf(tfile, "Bi", &type); - weapons.push_back(Weapon(type, 0)); - } - - if (visibleloading) - LoadingScreen(); + if (mapvers >= 9) + funpackf(tfile, "Bi Bi", &Person::players[0]->whichskin, &Person::players[0]->creature); + else { + Person::players[0]->whichskin = 0; + Person::players[0]->creature = rabbittype; + } - funpackf(tfile, "Bf Bf Bf", &Person::players[0]->armorhead, &Person::players[0]->armorhigh, &Person::players[0]->armorlow); - funpackf(tfile, "Bf Bf Bf", &Person::players[0]->protectionhead, &Person::players[0]->protectionhigh, &Person::players[0]->protectionlow); - funpackf(tfile, "Bf Bf Bf", &Person::players[0]->metalhead, &Person::players[0]->metalhigh, &Person::players[0]->metallow); - funpackf(tfile, "Bf Bf", &Person::players[0]->power, &Person::players[0]->speedmult); + Person::players[0]->lastattack = -1; + Person::players[0]->lastattack2 = -1; + Person::players[0]->lastattack3 = -1; - funpackf(tfile, "Bi", &Person::players[0]->numclothes); + //dialogues + if (mapvers >= 8) { + Dialog::loadDialogs(tfile); + } - if (mapvers >= 9) - funpackf(tfile, "Bi Bi", &Person::players[0]->whichskin, &Person::players[0]->creature); - else { - Person::players[0]->whichskin = 0; - Person::players[0]->creature = rabbittype; - } + for (int k = 0; k < Person::players[0]->numclothes; k++) { + funpackf(tfile, "Bi", &templength); + for (int l = 0; l < templength; l++) + funpackf(tfile, "Bb", &Person::players[0]->clothes[k][l]); + Person::players[0]->clothes[k][templength] = '\0'; + funpackf(tfile, "Bf Bf Bf", &Person::players[0]->clothestintr[k], &Person::players[0]->clothestintg[k], &Person::players[0]->clothestintb[k]); + } - Person::players[0]->lastattack = -1; - Person::players[0]->lastattack2 = -1; - Person::players[0]->lastattack3 = -1; + funpackf(tfile, "Bi", &environment); - //dialogues - if (mapvers >= 8) { - Dialog::loadDialogs(tfile); - } + funpackf(tfile, "Bi", &objects.numobjects); + for (int i = 0; i < objects.numobjects; i++) { + funpackf(tfile, "Bi Bf Bf Bf Bf Bf Bf", &objects.type[i], &objects.yaw[i], &objects.pitch[i], &objects.position[i].x, &objects.position[i].y, &objects.position[i].z, &objects.scale[i]); + if (objects.type[i] == treeleavestype) + objects.scale[i] = objects.scale[i - 1]; + } - for (int k = 0; k < Person::players[0]->numclothes; k++) { + if (mapvers >= 7) { + funpackf(tfile, "Bi", &numhotspots); + for (int i = 0; i < numhotspots; i++) { + funpackf(tfile, "Bi Bf Bf Bf Bf", &hotspottype[i], &hotspotsize[i], &hotspot[i].x, &hotspot[i].y, &hotspot[i].z); funpackf(tfile, "Bi", &templength); - for (int l = 0; l < templength; l++) - funpackf(tfile, "Bb", &Person::players[0]->clothes[k][l]); - Person::players[0]->clothes[k][templength] = '\0'; - funpackf(tfile, "Bf Bf Bf", &Person::players[0]->clothestintr[k], &Person::players[0]->clothestintg[k], &Person::players[0]->clothestintb[k]); + if (templength) + for (int l = 0; l < templength; l++) + funpackf(tfile, "Bb", &hotspottext[i][l]); + hotspottext[i][templength] = '\0'; + if (hotspottype[i] == -111) + indemo = 1; } + } else + numhotspots = 0; - funpackf(tfile, "Bi", &environment); + if (visibleloading) + LoadingScreen(); - funpackf(tfile, "Bi", &objects.numobjects); - for (int i = 0; i < objects.numobjects; i++) { - funpackf(tfile, "Bi Bf Bf Bf Bf Bf Bf", &objects.type[i], &objects.yaw[i], &objects.pitch[i], &objects.position[i].x, &objects.position[i].y, &objects.position[i].z, &objects.scale[i]); - if (objects.type[i] == treeleavestype) - objects.scale[i] = objects.scale[i - 1]; - } + if (!stealthloading) { + objects.center = 0; + for (int i = 0; i < objects.numobjects; i++) + objects.center += objects.position[i]; + objects.center /= objects.numobjects; - if (mapvers >= 7) { - funpackf(tfile, "Bi", &numhotspots); - for (int i = 0; i < numhotspots; i++) { - funpackf(tfile, "Bi Bf Bf Bf Bf", &hotspottype[i], &hotspotsize[i], &hotspot[i].x, &hotspot[i].y, &hotspot[i].z); - funpackf(tfile, "Bi", &templength); - if (templength) - for (int l = 0; l < templength; l++) - funpackf(tfile, "Bb", &hotspottext[i][l]); - hotspottext[i][templength] = '\0'; - if (hotspottype[i] == -111) - indemo = 1; - } - } else - numhotspots = 0; if (visibleloading) LoadingScreen(); - if (!stealthloading) { - objects.center = 0; - for (int i = 0; i < objects.numobjects; i++) - objects.center += objects.position[i]; - objects.center /= objects.numobjects; - - - if (visibleloading) - LoadingScreen(); - - float maxdistance = 0; - float tempdist; - for (int i = 0; i < objects.numobjects; i++) { - tempdist = distsq(&objects.center, &objects.position[i]); - if (tempdist > maxdistance) { - maxdistance = tempdist; - } + float maxdistance = 0; + float tempdist; + for (int i = 0; i < objects.numobjects; i++) { + tempdist = distsq(&objects.center, &objects.position[i]); + if (tempdist > maxdistance) { + maxdistance = tempdist; } - objects.radius = fast_sqrt(maxdistance); } + objects.radius = fast_sqrt(maxdistance); + } - if (visibleloading) - LoadingScreen(); + if (visibleloading) + LoadingScreen(); - int numplayers; - funpackf(tfile, "Bi", &numplayers); - if (numplayers > maxplayers) { - cout << "Warning: this level contains more players than allowed" << endl; - } - for (int i = 1; i < numplayers; i++) { - unsigned j = 1; - try { - Person::players.push_back(shared_ptr(new Person(tfile, mapvers, j))); - j++; - } catch (InvalidPersonException e) { - } + int numplayers; + funpackf(tfile, "Bi", &numplayers); + if (numplayers > maxplayers) { + cout << "Warning: this level contains more players than allowed" << endl; + } + for (int i = 1; i < numplayers; i++) { + unsigned j = 1; + try { + Person::players.push_back(shared_ptr(new Person(tfile, mapvers, j))); + j++; + } catch (InvalidPersonException e) { } - if (visibleloading) - LoadingScreen(); - - funpackf(tfile, "Bi", &numpathpoints); - if (numpathpoints > 30 || numpathpoints < 0) - numpathpoints = 0; - for (int j = 0; j < numpathpoints; j++) { - funpackf(tfile, "Bf Bf Bf Bi", &pathpoint[j].x, &pathpoint[j].y, &pathpoint[j].z, &numpathpointconnect[j]); - for (int k = 0; k < numpathpointconnect[j]; k++) { - funpackf(tfile, "Bi", &pathpointconnect[j][k]); - } + } + if (visibleloading) + LoadingScreen(); + + funpackf(tfile, "Bi", &numpathpoints); + if (numpathpoints > 30 || numpathpoints < 0) + numpathpoints = 0; + for (int j = 0; j < numpathpoints; j++) { + funpackf(tfile, "Bf Bf Bf Bi", &pathpoint[j].x, &pathpoint[j].y, &pathpoint[j].z, &numpathpointconnect[j]); + for (int k = 0; k < numpathpointconnect[j]; k++) { + funpackf(tfile, "Bi", &pathpointconnect[j][k]); } - if (visibleloading) - LoadingScreen(); + } + if (visibleloading) + LoadingScreen(); - funpackf(tfile, "Bf Bf Bf Bf", &mapcenter.x, &mapcenter.y, &mapcenter.z, &mapradius); + funpackf(tfile, "Bf Bf Bf Bf", &mapcenter.x, &mapcenter.y, &mapcenter.z, &mapradius); - SetUpLighting(); - if (environment != oldenvironment) - Setenvironment(environment); - oldenvironment = environment; - - if (!stealthloading) { - int j = objects.numobjects; - objects.numobjects = 0; - for (int i = 0; i < j; i++) { - objects.MakeObject(objects.type[i], objects.position[i], objects.yaw[i], objects.pitch[i], objects.scale[i]); - if (visibleloading) - LoadingScreen(); - } + SetUpLighting(); + if (environment != oldenvironment) + Setenvironment(environment); + oldenvironment = environment; - terrain.DoShadows(); - if (visibleloading) - LoadingScreen(); - objects.DoShadows(); + if (!stealthloading) { + int j = objects.numobjects; + objects.numobjects = 0; + for (int i = 0; i < j; i++) { + objects.MakeObject(objects.type[i], objects.position[i], objects.yaw[i], objects.pitch[i], objects.scale[i]); if (visibleloading) LoadingScreen(); } - fclose(tfile); + terrain.DoShadows(); + if (visibleloading) + LoadingScreen(); + objects.DoShadows(); + if (visibleloading) + LoadingScreen(); + } - for (unsigned i = 0; i < Person::players.size(); i++) { - if (visibleloading) - LoadingScreen(); - Person::players[i]->burnt = 0; - Person::players[i]->bled = 0; - Person::players[i]->onfire = 0; - if (i == 0 || Person::players[i]->scale < 0) - Person::players[i]->scale = .2; - Person::players[i]->skeleton.free = 0; - Person::players[i]->skeleton.id = i; - if (i == 0 && mapvers < 9) - Person::players[i]->creature = rabbittype; + fclose(tfile); + + for (unsigned i = 0; i < Person::players.size(); i++) { + if (visibleloading) + LoadingScreen(); + Person::players[i]->burnt = 0; + Person::players[i]->bled = 0; + Person::players[i]->onfire = 0; + if (i == 0 || Person::players[i]->scale < 0) + Person::players[i]->scale = .2; + Person::players[i]->skeleton.free = 0; + Person::players[i]->skeleton.id = i; + if (i == 0 && mapvers < 9) + Person::players[i]->creature = rabbittype; + if (Person::players[i]->creature != wolftype) { + Person::players[i]->skeleton.Load( + (char *)"Skeleton/BasicFigure", + (char *)"Skeleton/BasicFigureLow", + (char *)"Skeleton/RabbitBelt", + (char *)"Models/Body.solid", + (char *)"Models/Body2.solid", + (char *)"Models/Body3.solid", + (char *)"Models/Body4.solid", + (char *)"Models/Body5.solid", + (char *)"Models/Body6.solid", + (char *)"Models/Body7.solid", + (char *)"Models/BodyLow.solid", + (char *)"Models/Belt.solid", 0); + } else { if (Person::players[i]->creature != wolftype) { Person::players[i]->skeleton.Load( (char *)"Skeleton/BasicFigure", @@ -1115,176 +1130,159 @@ void Game::Loadlevel(const std::string& name) (char *)"Models/Body6.solid", (char *)"Models/Body7.solid", (char *)"Models/BodyLow.solid", - (char *)"Models/Belt.solid", 0); - } else { - if (Person::players[i]->creature != wolftype) { - Person::players[i]->skeleton.Load( - (char *)"Skeleton/BasicFigure", - (char *)"Skeleton/BasicFigureLow", - (char *)"Skeleton/RabbitBelt", - (char *)"Models/Body.solid", - (char *)"Models/Body2.solid", - (char *)"Models/Body3.solid", - (char *)"Models/Body4.solid", - (char *)"Models/Body5.solid", - (char *)"Models/Body6.solid", - (char *)"Models/Body7.solid", - (char *)"Models/BodyLow.solid", - (char *)"Models/Belt.solid", 1); - Person::players[i]->skeleton.drawmodelclothes.textureptr.load("Textures/Belt.png", 1); - } - if (Person::players[i]->creature == wolftype) { - Person::players[i]->skeleton.Load( - (char *)"Skeleton/BasicFigureWolf", - (char *)"Skeleton/BasicFigureWolfLow", - (char *)"Skeleton/RabbitBelt", - (char *)"Models/Wolf.solid", - (char *)"Models/Wolf2.solid", - (char *)"Models/Wolf3.solid", - (char *)"Models/Wolf4.solid", - (char *)"Models/Wolf5.solid", - (char *)"Models/Wolf6.solid", - (char *)"Models/Wolf7.solid", - (char *)"Models/WolfLow.solid", - (char *)"Models/Belt.solid", 0); - } + (char *)"Models/Belt.solid", 1); + Person::players[i]->skeleton.drawmodelclothes.textureptr.load("Textures/Belt.png", 1); } - - Person::players[i]->skeleton.drawmodel.textureptr.load(creatureskin[Person::players[i]->creature][Person::players[i]->whichskin], 1, &Person::players[i]->skeleton.skinText[0], &Person::players[i]->skeleton.skinsize); - - Person::players[i]->addClothes(); - - Person::players[i]->animCurrent = bounceidleanim; - Person::players[i]->animTarget = bounceidleanim; - Person::players[i]->frameCurrent = 0; - Person::players[i]->frameTarget = 1; - Person::players[i]->target = 0; - Person::players[i]->speed = 1 + (float)(Random() % 100) / 1000; - if (difficulty == 0) - Person::players[i]->speed -= .2; - if (difficulty == 1) - Person::players[i]->speed -= .1; - - Person::players[i]->velocity = 0; - Person::players[i]->oldcoords = Person::players[i]->coords; - Person::players[i]->realoldcoords = Person::players[i]->coords; - - Person::players[i]->id = i; - Person::players[i]->skeleton.id = i; - Person::players[i]->updatedelay = 0; - Person::players[i]->normalsupdatedelay = 0; - - Person::players[i]->aitype = passivetype; - - if (i == 0) { - Person::players[i]->proportionhead = 1.2; - Person::players[i]->proportionbody = 1.05; - Person::players[i]->proportionarms = 1.00; - Person::players[i]->proportionlegs = 1.1; - Person::players[i]->proportionlegs.y = 1.05; - } - Person::players[i]->headless = 0; - Person::players[i]->currentoffset = 0; - Person::players[i]->targetoffset = 0; - - Person::players[i]->damagetolerance = 200; - if (Person::players[i]->creature == wolftype) { - if (i == 0 || Person::players[i]->scale < 0) - Person::players[i]->scale = .23; - Person::players[i]->damagetolerance = 300; - } - - if (visibleloading) - LoadingScreen(); - if (cellophane) { - Person::players[i]->proportionhead.z = 0; - Person::players[i]->proportionbody.z = 0; - Person::players[i]->proportionarms.z = 0; - Person::players[i]->proportionlegs.z = 0; + Person::players[i]->skeleton.Load( + (char *)"Skeleton/BasicFigureWolf", + (char *)"Skeleton/BasicFigureWolfLow", + (char *)"Skeleton/RabbitBelt", + (char *)"Models/Wolf.solid", + (char *)"Models/Wolf2.solid", + (char *)"Models/Wolf3.solid", + (char *)"Models/Wolf4.solid", + (char *)"Models/Wolf5.solid", + (char *)"Models/Wolf6.solid", + (char *)"Models/Wolf7.solid", + (char *)"Models/WolfLow.solid", + (char *)"Models/Belt.solid", 0); } + } - Person::players[i]->tempanimation.Load((char *)"Tempanim", 0, 0); - - Person::players[i]->headmorphness = 0; - Person::players[i]->targetheadmorphness = 1; - Person::players[i]->headmorphstart = 0; - Person::players[i]->headmorphend = 0; - - Person::players[i]->pausetime = 0; + Person::players[i]->skeleton.drawmodel.textureptr.load(creatureskin[Person::players[i]->creature][Person::players[i]->whichskin], 1, &Person::players[i]->skeleton.skinText[0], &Person::players[i]->skeleton.skinsize); + + Person::players[i]->addClothes(); + + Person::players[i]->animCurrent = bounceidleanim; + Person::players[i]->animTarget = bounceidleanim; + Person::players[i]->frameCurrent = 0; + Person::players[i]->frameTarget = 1; + Person::players[i]->target = 0; + Person::players[i]->speed = 1 + (float)(Random() % 100) / 1000; + if (difficulty == 0) + Person::players[i]->speed -= .2; + if (difficulty == 1) + Person::players[i]->speed -= .1; + + Person::players[i]->velocity = 0; + Person::players[i]->oldcoords = Person::players[i]->coords; + Person::players[i]->realoldcoords = Person::players[i]->coords; + + Person::players[i]->id = i; + Person::players[i]->skeleton.id = i; + Person::players[i]->updatedelay = 0; + Person::players[i]->normalsupdatedelay = 0; + + Person::players[i]->aitype = passivetype; + + if (i == 0) { + Person::players[i]->proportionhead = 1.2; + Person::players[i]->proportionbody = 1.05; + Person::players[i]->proportionarms = 1.00; + Person::players[i]->proportionlegs = 1.1; + Person::players[i]->proportionlegs.y = 1.05; + } + Person::players[i]->headless = 0; + Person::players[i]->currentoffset = 0; + Person::players[i]->targetoffset = 0; - Person::players[i]->dead = 0; - Person::players[i]->jumppower = 5; - Person::players[i]->damage = 0; - Person::players[i]->permanentdamage = 0; - Person::players[i]->superpermanentdamage = 0; + Person::players[i]->damagetolerance = 200; - Person::players[i]->forwardkeydown = 0; - Person::players[i]->leftkeydown = 0; - Person::players[i]->backkeydown = 0; - Person::players[i]->rightkeydown = 0; - Person::players[i]->jumpkeydown = 0; - Person::players[i]->crouchkeydown = 0; - Person::players[i]->throwkeydown = 0; - - Person::players[i]->collided = -10; - Person::players[i]->loaded = 1; - Person::players[i]->bloodloss = 0; - Person::players[i]->weaponactive = -1; - Person::players[i]->weaponstuck = -1; - Person::players[i]->bleeding = 0; - Person::players[i]->deathbleeding = 0; - Person::players[i]->stunned = 0; - Person::players[i]->hasvictim = 0; - Person::players[i]->wentforweapon = 0; + if (Person::players[i]->creature == wolftype) { + if (i == 0 || Person::players[i]->scale < 0) + Person::players[i]->scale = .23; + Person::players[i]->damagetolerance = 300; } - Person::players[0]->aitype = playercontrolled; - Person::players[0]->weaponactive = -1; - - if (difficulty == 1) { - Person::players[0]->power = 1 / .9; - Person::players[0]->damagetolerance = 250; - } else if (difficulty == 0) { - Person::players[0]->power = 1 / .8; - Person::players[0]->damagetolerance = 300; - Person::players[0]->armorhead *= 1.5; - Person::players[0]->armorhigh *= 1.5; - Person::players[0]->armorlow *= 1.5; + if (visibleloading) + LoadingScreen(); + if (cellophane) { + Person::players[i]->proportionhead.z = 0; + Person::players[i]->proportionbody.z = 0; + Person::players[i]->proportionarms.z = 0; + Person::players[i]->proportionlegs.z = 0; } - cameraloc = Person::players[0]->coords; - cameraloc.y += 5; - yaw = Person::players[0]->yaw; + Person::players[i]->tempanimation.Load((char *)"Tempanim", 0, 0); + + Person::players[i]->headmorphness = 0; + Person::players[i]->targetheadmorphness = 1; + Person::players[i]->headmorphstart = 0; + Person::players[i]->headmorphend = 0; + + Person::players[i]->pausetime = 0; + + Person::players[i]->dead = 0; + Person::players[i]->jumppower = 5; + Person::players[i]->damage = 0; + Person::players[i]->permanentdamage = 0; + Person::players[i]->superpermanentdamage = 0; + + Person::players[i]->forwardkeydown = 0; + Person::players[i]->leftkeydown = 0; + Person::players[i]->backkeydown = 0; + Person::players[i]->rightkeydown = 0; + Person::players[i]->jumpkeydown = 0; + Person::players[i]->crouchkeydown = 0; + Person::players[i]->throwkeydown = 0; + + Person::players[i]->collided = -10; + Person::players[i]->loaded = 1; + Person::players[i]->bloodloss = 0; + Person::players[i]->weaponactive = -1; + Person::players[i]->weaponstuck = -1; + Person::players[i]->bleeding = 0; + Person::players[i]->deathbleeding = 0; + Person::players[i]->stunned = 0; + Person::players[i]->hasvictim = 0; + Person::players[i]->wentforweapon = 0; + } - hawkcoords = Person::players[0]->coords; - hawkcoords.y += 30; + Person::players[0]->aitype = playercontrolled; + Person::players[0]->weaponactive = -1; + + if (difficulty == 1) { + Person::players[0]->power = 1 / .9; + Person::players[0]->damagetolerance = 250; + } else if (difficulty == 0) { + Person::players[0]->power = 1 / .8; + Person::players[0]->damagetolerance = 300; + Person::players[0]->armorhead *= 1.5; + Person::players[0]->armorhigh *= 1.5; + Person::players[0]->armorlow *= 1.5; + } - if (visibleloading) - LoadingScreen(); + cameraloc = Person::players[0]->coords; + cameraloc.y += 5; + yaw = Person::players[0]->yaw; - LOG("Starting background music..."); + hawkcoords = Person::players[0]->coords; + hawkcoords.y += 30; - OPENAL_StopSound(OPENAL_ALL); - if (ambientsound) { - if (environment == snowyenvironment) { - emit_stream_np(stream_wind); - } else if (environment == desertenvironment) { - emit_stream_np(stream_desertambient); - } else if (environment == grassyenvironment) { - emit_stream_np(stream_wind, 100.); - } - } - oldmusicvolume[0] = 0; - oldmusicvolume[1] = 0; - oldmusicvolume[2] = 0; - oldmusicvolume[3] = 0; + if (visibleloading) + LoadingScreen(); - if (!firstload) - firstload = 1; - } else { - perror("Problem"); + LOG("Starting background music..."); + + OPENAL_StopSound(OPENAL_ALL); + if (ambientsound) { + if (environment == snowyenvironment) { + emit_stream_np(stream_wind); + } else if (environment == desertenvironment) { + emit_stream_np(stream_desertambient); + } else if (environment == grassyenvironment) { + emit_stream_np(stream_wind, 100.); + } } + oldmusicvolume[0] = 0; + oldmusicvolume[1] = 0; + oldmusicvolume[2] = 0; + oldmusicvolume[3] = 0; + + if (!firstload) + firstload = 1; + leveltime = 0; visibleloading = 0; } diff --git a/Source/Models.cpp b/Source/Models.cpp index 16f92ba..4879ac2 100644 --- a/Source/Models.cpp +++ b/Source/Models.cpp @@ -408,7 +408,8 @@ bool Model::loadnotex(const char *filename ) type = notextype; color = 0; - tfile = fopen( Folders::getResourcePath(filename).c_str(), "rb" ); + tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" ); + // read model settings fseek(tfile, 0, SEEK_SET); @@ -459,7 +460,7 @@ bool Model::loadnotex(const char *filename ) } boundingsphereradius = fast_sqrt(boundingsphereradius); - return 1; + return true; } @@ -478,9 +479,9 @@ bool Model::load(const char *filename, bool texture ) type = normaltype; color = 0; - tfile = fopen( Folders::getResourcePath(filename).c_str(), "rb" ); - // read model settings + tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" ); + // read model settings fseek(tfile, 0, SEEK_SET); funpackf(tfile, "Bs Bs", &vertexNum, &TriangleNum); @@ -534,7 +535,7 @@ bool Model::load(const char *filename, bool texture ) } boundingsphereradius = fast_sqrt(boundingsphereradius); - return 1; + return true; } bool Model::loaddecal(const char *filename, bool texture ) @@ -550,9 +551,9 @@ bool Model::loaddecal(const char *filename, bool texture ) numdecals = 0; color = 0; - tfile = fopen( Folders::getResourcePath(filename).c_str(), "rb" ); - // read model settings + tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" ); + // read model settings fseek(tfile, 0, SEEK_SET); funpackf(tfile, "Bs Bs", &vertexNum, &TriangleNum); @@ -629,7 +630,7 @@ bool Model::loaddecal(const char *filename, bool texture ) decalposition = (XYZ*)malloc(sizeof(XYZ) * max_model_decals); } - return 1; + return true; } bool Model::loadraw(char *filename ) @@ -644,9 +645,9 @@ bool Model::loadraw(char *filename ) type = rawtype; color = 0; - tfile = fopen( Folders::getResourcePath(filename).c_str(), "rb" ); - // read model settings + tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" ); + // read model settings fseek(tfile, 0, SEEK_SET); funpackf(tfile, "Bs Bs", &vertexNum, &TriangleNum); @@ -684,7 +685,7 @@ bool Model::loadraw(char *filename ) owner[i] = -1; } - return 1; + return true; } diff --git a/Source/Skeleton.cpp b/Source/Skeleton.cpp index a9f63e2..c74f07d 100644 --- a/Source/Skeleton.cpp +++ b/Source/Skeleton.cpp @@ -692,76 +692,75 @@ void Animation::Load(const std::string& filename, int aheight, int aattack) Game::LoadingScreen(); // read file in binary mode - tfile = fopen( filepath.c_str(), "rb" ); - if (tfile) { - // read numframes, joints to know how much memory to allocate - funpackf(tfile, "Bi Bi", &numframes, &joints); + tfile = Folders::openMandatoryFile( filepath, "rb" ); - // allocate memory for everything + // read numframes, joints to know how much memory to allocate + funpackf(tfile, "Bi Bi", &numframes, &joints); - position = (XYZ**)malloc(sizeof(XYZ*) * joints); - for (i = 0; i < joints; i++) - position[i] = (XYZ*)malloc(sizeof(XYZ) * numframes); + // allocate memory for everything - twist = (float**)malloc(sizeof(float*) * joints); - for (i = 0; i < joints; i++) - twist[i] = (float*)malloc(sizeof(float) * numframes); + position = (XYZ**)malloc(sizeof(XYZ*) * joints); + for (i = 0; i < joints; i++) + position[i] = (XYZ*)malloc(sizeof(XYZ) * numframes); - twist2 = (float**)malloc(sizeof(float*) * joints); - for (i = 0; i < joints; i++) - twist2[i] = (float*)malloc(sizeof(float) * numframes); + twist = (float**)malloc(sizeof(float*) * joints); + for (i = 0; i < joints; i++) + twist[i] = (float*)malloc(sizeof(float) * numframes); - speed = (float*)malloc(sizeof(float) * numframes); + twist2 = (float**)malloc(sizeof(float*) * joints); + for (i = 0; i < joints; i++) + twist2[i] = (float*)malloc(sizeof(float) * numframes); - onground = (bool**)malloc(sizeof(bool*) * joints); - for (i = 0; i < joints; i++) - onground[i] = (bool*)malloc(sizeof(bool) * numframes); + speed = (float*)malloc(sizeof(float) * numframes); - forward = (XYZ*)malloc(sizeof(XYZ) * numframes); - weapontarget = (XYZ*)malloc(sizeof(XYZ) * numframes); - label = (int*)malloc(sizeof(int) * numframes); + onground = (bool**)malloc(sizeof(bool*) * joints); + for (i = 0; i < joints; i++) + onground[i] = (bool*)malloc(sizeof(bool) * numframes); - // read binary data as animation + forward = (XYZ*)malloc(sizeof(XYZ) * numframes); + weapontarget = (XYZ*)malloc(sizeof(XYZ) * numframes); + label = (int*)malloc(sizeof(int) * numframes); - // for each frame... - for (i = 0; i < numframes; i++) { - // for each joint in the skeleton... - for (j = 0; j < joints; j++) { - // read joint position - funpackf(tfile, "Bf Bf Bf", &position[j][i].x, &position[j][i].y, &position[j][i].z); - } - for (j = 0; j < joints; j++) { - // read twist - funpackf(tfile, "Bf", &twist[j][i]); - } - for (j = 0; j < joints; j++) { - // read onground (boolean) - unsigned char uch; - funpackf(tfile, "Bb", &uch); - onground[j][i] = (uch != 0); - } - // read frame speed (?) - funpackf(tfile, "Bf", &speed[i]); + // read binary data as animation + + // for each frame... + for (i = 0; i < numframes; i++) { + // for each joint in the skeleton... + for (j = 0; j < joints; j++) { + // read joint position + funpackf(tfile, "Bf Bf Bf", &position[j][i].x, &position[j][i].y, &position[j][i].z); } - // read twist2 for whole animation - for (i = 0; i < numframes; i++) { - for (j = 0; j < joints; j++) { - funpackf(tfile, "Bf", &twist2[j][i]); - } + for (j = 0; j < joints; j++) { + // read twist + funpackf(tfile, "Bf", &twist[j][i]); } - // read label for each frame - for (i = 0; i < numframes; i++) { - funpackf(tfile, "Bf", &label[i]); + for (j = 0; j < joints; j++) { + // read onground (boolean) + unsigned char uch; + funpackf(tfile, "Bb", &uch); + onground[j][i] = (uch != 0); } - // read weapontargetnum - funpackf(tfile, "Bi", &weapontargetnum); - // read weapontarget positions for each frame - for (i = 0; i < numframes; i++) { - funpackf(tfile, "Bf Bf Bf", &weapontarget[i].x, &weapontarget[i].y, &weapontarget[i].z); + // read frame speed (?) + funpackf(tfile, "Bf", &speed[i]); + } + // read twist2 for whole animation + for (i = 0; i < numframes; i++) { + for (j = 0; j < joints; j++) { + funpackf(tfile, "Bf", &twist2[j][i]); } - - fclose(tfile); } + // read label for each frame + for (i = 0; i < numframes; i++) { + funpackf(tfile, "Bf", &label[i]); + } + // read weapontargetnum + funpackf(tfile, "Bi", &weapontargetnum); + // read weapontarget positions for each frame + for (i = 0; i < numframes; i++) { + funpackf(tfile, "Bf Bf Bf", &weapontarget[i].x, &weapontarget[i].y, &weapontarget[i].z); + } + + fclose(tfile); endoffset = 0; // find average position of certain joints on last frames @@ -859,215 +858,211 @@ void Skeleton::Load(const char *filename, const char *lowfilename, const c // load skeleton - tfile = fopen( Folders::getResourcePath(filename).c_str(), "rb" ); + tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" ); - if (1) { // FIXME: should this be if(tfile) ? - // read num_joints - funpackf(tfile, "Bi", &num_joints); + // read num_joints + funpackf(tfile, "Bi", &num_joints); - // allocate memory - if (joints) - delete [] joints; //dealloc2(joints); - joints = (Joint*)new Joint[num_joints]; + // allocate memory + if (joints) + delete [] joints; //dealloc2(joints); + joints = (Joint*)new Joint[num_joints]; - // read info for each joint - for (i = 0; i < num_joints; i++) { - funpackf(tfile, "Bf Bf Bf Bf Bf", &joints[i].position.x, &joints[i].position.y, &joints[i].position.z, &joints[i].length, &joints[i].mass); - funpackf(tfile, "Bb Bb", &joints[i].hasparent, &joints[i].locked); - funpackf(tfile, "Bi", &joints[i].modelnum); - funpackf(tfile, "Bb Bb", &joints[i].visible, &joints[i].sametwist); - funpackf(tfile, "Bi Bi", &joints[i].label, &joints[i].hasgun); - funpackf(tfile, "Bb", &joints[i].lower); - funpackf(tfile, "Bi", &parentID); - if (joints[i].hasparent) - joints[i].parent = &joints[parentID]; - joints[i].velocity = 0; - joints[i].oldposition = joints[i].position; - } - - // read num_muscles - funpackf(tfile, "Bi", &num_muscles); - - // allocate memory - if (muscles) - delete [] muscles; //dealloc2(muscles); - muscles = (Muscle*)new Muscle[num_muscles]; //malloc(sizeof(Muscle)*num_muscles); - - // for each muscle... - for (i = 0; i < num_muscles; i++) { - // read info - funpackf(tfile, "Bf Bf Bf Bf Bf Bi Bi", &muscles[i].length, &muscles[i].targetlength, &muscles[i].minlength, &muscles[i].maxlength, &muscles[i].strength, &muscles[i].type, &muscles[i].numvertices); - - // allocate memory for vertices - muscles[i].vertices = (int*)malloc(sizeof(int) * muscles[i].numvertices); + // read info for each joint + for (i = 0; i < num_joints; i++) { + funpackf(tfile, "Bf Bf Bf Bf Bf", &joints[i].position.x, &joints[i].position.y, &joints[i].position.z, &joints[i].length, &joints[i].mass); + funpackf(tfile, "Bb Bb", &joints[i].hasparent, &joints[i].locked); + funpackf(tfile, "Bi", &joints[i].modelnum); + funpackf(tfile, "Bb Bb", &joints[i].visible, &joints[i].sametwist); + funpackf(tfile, "Bi Bi", &joints[i].label, &joints[i].hasgun); + funpackf(tfile, "Bb", &joints[i].lower); + funpackf(tfile, "Bi", &parentID); + if (joints[i].hasparent) + joints[i].parent = &joints[parentID]; + joints[i].velocity = 0; + joints[i].oldposition = joints[i].position; + } - // read vertices - edit = 0; - for (j = 0; j < muscles[i].numvertices - edit; j++) { - funpackf(tfile, "Bi", &muscles[i].vertices[j + edit]); - if (muscles[i].vertices[j + edit] >= model[0].vertexNum) { - muscles[i].numvertices--; - edit--; - } + // read num_muscles + funpackf(tfile, "Bi", &num_muscles); + + // allocate memory + if (muscles) + delete [] muscles; //dealloc2(muscles); + muscles = (Muscle*)new Muscle[num_muscles]; //malloc(sizeof(Muscle)*num_muscles); + + // for each muscle... + for (i = 0; i < num_muscles; i++) { + // read info + funpackf(tfile, "Bf Bf Bf Bf Bf Bi Bi", &muscles[i].length, &muscles[i].targetlength, &muscles[i].minlength, &muscles[i].maxlength, &muscles[i].strength, &muscles[i].type, &muscles[i].numvertices); + + // allocate memory for vertices + muscles[i].vertices = (int*)malloc(sizeof(int) * muscles[i].numvertices); + + // read vertices + edit = 0; + for (j = 0; j < muscles[i].numvertices - edit; j++) { + funpackf(tfile, "Bi", &muscles[i].vertices[j + edit]); + if (muscles[i].vertices[j + edit] >= model[0].vertexNum) { + muscles[i].numvertices--; + edit--; } - - // read more info - funpackf(tfile, "Bb Bi", &muscles[i].visible, &parentID); - muscles[i].parent1 = &joints[parentID]; - funpackf(tfile, "Bi", &parentID); - muscles[i].parent2 = &joints[parentID]; } - // read forwardjoints (?) - for (j = 0; j < 3; j++) { - funpackf(tfile, "Bi", &forwardjoints[j]); - } - // read lowforwardjoints (?) - for (j = 0; j < 3; j++) { - funpackf(tfile, "Bi", &lowforwardjoints[j]); - } + // read more info + funpackf(tfile, "Bb Bi", &muscles[i].visible, &parentID); + muscles[i].parent1 = &joints[parentID]; + funpackf(tfile, "Bi", &parentID); + muscles[i].parent2 = &joints[parentID]; + } - // ??? - for (j = 0; j < num_muscles; j++) { - for (i = 0; i < muscles[j].numvertices; i++) { - for (int k = 0; k < num_models; k++) { - if (muscles[j].numvertices && muscles[j].vertices[i] < model[k].vertexNum) - model[k].owner[muscles[j].vertices[i]] = j; - } + // read forwardjoints (?) + for (j = 0; j < 3; j++) { + funpackf(tfile, "Bi", &forwardjoints[j]); + } + // read lowforwardjoints (?) + for (j = 0; j < 3; j++) { + funpackf(tfile, "Bi", &lowforwardjoints[j]); + } + + // ??? + for (j = 0; j < num_muscles; j++) { + for (i = 0; i < muscles[j].numvertices; i++) { + for (int k = 0; k < num_models; k++) { + if (muscles[j].numvertices && muscles[j].vertices[i] < model[k].vertexNum) + model[k].owner[muscles[j].vertices[i]] = j; } } + } - // calculate some stuff - FindForwards(); - for (i = 0; i < num_joints; i++) { - joints[i].startpos = joints[i].position; - } - for (i = 0; i < num_muscles; i++) { - FindRotationMuscle(i, -1); - } - // this seems to use opengl purely for matrix calculations - for (int k = 0; k < num_models; k++) { - for (i = 0; i < model[k].vertexNum; i++) { - model[k].vertex[i] = model[k].vertex[i] - (muscles[model[k].owner[i]].parent1->position + muscles[model[k].owner[i]].parent2->position) / 2; - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glLoadIdentity(); - glRotatef(muscles[model[k].owner[i]].rotate3, 0, 1, 0); - glRotatef(muscles[model[k].owner[i]].rotate2 - 90, 0, 0, 1); - glRotatef(muscles[model[k].owner[i]].rotate1 - 90, 0, 1, 0); - glTranslatef(model[k].vertex[i].x, model[k].vertex[i].y, model[k].vertex[i].z); - glGetFloatv(GL_MODELVIEW_MATRIX, M); - model[k].vertex[i].x = M[12] * 1; - model[k].vertex[i].y = M[13] * 1; - model[k].vertex[i].z = M[14] * 1; - glPopMatrix(); - } - model[k].CalculateNormals(0); + // calculate some stuff + FindForwards(); + for (i = 0; i < num_joints; i++) { + joints[i].startpos = joints[i].position; + } + for (i = 0; i < num_muscles; i++) { + FindRotationMuscle(i, -1); + } + // this seems to use opengl purely for matrix calculations + for (int k = 0; k < num_models; k++) { + for (i = 0; i < model[k].vertexNum; i++) { + model[k].vertex[i] = model[k].vertex[i] - (muscles[model[k].owner[i]].parent1->position + muscles[model[k].owner[i]].parent2->position) / 2; + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + glRotatef(muscles[model[k].owner[i]].rotate3, 0, 1, 0); + glRotatef(muscles[model[k].owner[i]].rotate2 - 90, 0, 0, 1); + glRotatef(muscles[model[k].owner[i]].rotate1 - 90, 0, 1, 0); + glTranslatef(model[k].vertex[i].x, model[k].vertex[i].y, model[k].vertex[i].z); + glGetFloatv(GL_MODELVIEW_MATRIX, M); + model[k].vertex[i].x = M[12] * 1; + model[k].vertex[i].y = M[13] * 1; + model[k].vertex[i].z = M[14] * 1; + glPopMatrix(); } + model[k].CalculateNormals(0); } fclose(tfile); // load ??? - tfile = fopen( Folders::getResourcePath(lowfilename).c_str(), "rb" ); + tfile = Folders::openMandatoryFile( Folders::getResourcePath(lowfilename), "rb" ); - if (1) { // FIXME: should this be if(tfile) ? - // skip joints section + // skip joints section - lSize = sizeof(num_joints); + lSize = sizeof(num_joints); + fseek(tfile, lSize, SEEK_CUR); + for (i = 0; i < num_joints; i++) { + // skip joint info + lSize = sizeof(XYZ) + + sizeof(float) + + sizeof(float) + + 1 //sizeof(bool) + + 1 //sizeof(bool) + + sizeof(int) + + 1 //sizeof(bool) + + 1 //sizeof(bool) + + sizeof(int) + + sizeof(int) + + 1 //sizeof(bool) + + sizeof(int); fseek(tfile, lSize, SEEK_CUR); - for (i = 0; i < num_joints; i++) { - // skip joint info - lSize = sizeof(XYZ) - + sizeof(float) - + sizeof(float) - + 1 //sizeof(bool) - + 1 //sizeof(bool) - + sizeof(int) - + 1 //sizeof(bool) - + 1 //sizeof(bool) - + sizeof(int) - + sizeof(int) - + 1 //sizeof(bool) - + sizeof(int); - fseek(tfile, lSize, SEEK_CUR); - if (joints[i].hasparent) - joints[i].parent = &joints[parentID]; - joints[i].velocity = 0; - joints[i].oldposition = joints[i].position; - } - - // read num_muscles - funpackf(tfile, "Bi", &num_muscles); + if (joints[i].hasparent) + joints[i].parent = &joints[parentID]; + joints[i].velocity = 0; + joints[i].oldposition = joints[i].position; + } - for (i = 0; i < num_muscles; i++) { - // skip muscle info - lSize = sizeof(float) - + sizeof(float) - + sizeof(float) - + sizeof(float) - + sizeof(float) - + sizeof(int); - fseek(tfile, lSize, SEEK_CUR); + // read num_muscles + funpackf(tfile, "Bi", &num_muscles); + + for (i = 0; i < num_muscles; i++) { + // skip muscle info + lSize = sizeof(float) + + sizeof(float) + + sizeof(float) + + sizeof(float) + + sizeof(float) + + sizeof(int); + fseek(tfile, lSize, SEEK_CUR); - // read numverticeslow - funpackf(tfile, "Bi", &muscles[i].numverticeslow); + // read numverticeslow + funpackf(tfile, "Bi", &muscles[i].numverticeslow); - if (muscles[i].numverticeslow) { - // allocate memory - muscles[i].verticeslow = (int*)malloc(sizeof(int) * muscles[i].numverticeslow); + if (muscles[i].numverticeslow) { + // allocate memory + muscles[i].verticeslow = (int*)malloc(sizeof(int) * muscles[i].numverticeslow); - // read verticeslow - edit = 0; - for (j = 0; j < muscles[i].numverticeslow - edit; j++) { - funpackf(tfile, "Bi", &muscles[i].verticeslow[j + edit]); - if (muscles[i].verticeslow[j + edit] >= modellow.vertexNum) { - muscles[i].numverticeslow--; - edit--; - } + // read verticeslow + edit = 0; + for (j = 0; j < muscles[i].numverticeslow - edit; j++) { + funpackf(tfile, "Bi", &muscles[i].verticeslow[j + edit]); + if (muscles[i].verticeslow[j + edit] >= modellow.vertexNum) { + muscles[i].numverticeslow--; + edit--; } } - - // skip more stuff - lSize = 1; //sizeof(bool); - fseek ( tfile, lSize, SEEK_CUR); - lSize = sizeof(int); - fseek ( tfile, lSize, SEEK_CUR); - fseek ( tfile, lSize, SEEK_CUR); } - for (j = 0; j < num_muscles; j++) { - for (i = 0; i < muscles[j].numverticeslow; i++) { - if (muscles[j].verticeslow[i] < modellow.vertexNum) - modellow.owner[muscles[j].verticeslow[i]] = j; - } - } + // skip more stuff + lSize = 1; //sizeof(bool); + fseek ( tfile, lSize, SEEK_CUR); + lSize = sizeof(int); + fseek ( tfile, lSize, SEEK_CUR); + fseek ( tfile, lSize, SEEK_CUR); + } - // use opengl for its matrix math - for (i = 0; i < modellow.vertexNum; i++) { - modellow.vertex[i] = modellow.vertex[i] - (muscles[modellow.owner[i]].parent1->position + muscles[modellow.owner[i]].parent2->position) / 2; - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glLoadIdentity(); - glRotatef(muscles[modellow.owner[i]].rotate3, 0, 1, 0); - glRotatef(muscles[modellow.owner[i]].rotate2 - 90, 0, 0, 1); - glRotatef(muscles[modellow.owner[i]].rotate1 - 90, 0, 1, 0); - glTranslatef(modellow.vertex[i].x, modellow.vertex[i].y, modellow.vertex[i].z); - glGetFloatv(GL_MODELVIEW_MATRIX, M); - modellow.vertex[i].x = M[12]; - modellow.vertex[i].y = M[13]; - modellow.vertex[i].z = M[14]; - glPopMatrix(); + for (j = 0; j < num_muscles; j++) { + for (i = 0; i < muscles[j].numverticeslow; i++) { + if (muscles[j].verticeslow[i] < modellow.vertexNum) + modellow.owner[muscles[j].verticeslow[i]] = j; } + } - modellow.CalculateNormals(0); + // use opengl for its matrix math + for (i = 0; i < modellow.vertexNum; i++) { + modellow.vertex[i] = modellow.vertex[i] - (muscles[modellow.owner[i]].parent1->position + muscles[modellow.owner[i]].parent2->position) / 2; + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + glRotatef(muscles[modellow.owner[i]].rotate3, 0, 1, 0); + glRotatef(muscles[modellow.owner[i]].rotate2 - 90, 0, 0, 1); + glRotatef(muscles[modellow.owner[i]].rotate1 - 90, 0, 1, 0); + glTranslatef(modellow.vertex[i].x, modellow.vertex[i].y, modellow.vertex[i].z); + glGetFloatv(GL_MODELVIEW_MATRIX, M); + modellow.vertex[i].x = M[12]; + modellow.vertex[i].y = M[13]; + modellow.vertex[i].z = M[14]; + glPopMatrix(); } + modellow.CalculateNormals(0); + // load clothes if (clothes) { - tfile = fopen( Folders::getResourcePath(clothesfilename).c_str(), "rb" ); // FIXME: where's the check for valid load + tfile = Folders::openMandatoryFile( Folders::getResourcePath(clothesfilename), "rb" ); // skip num_joints lSize = sizeof(num_joints); diff --git a/Source/Terrain.cpp b/Source/Terrain.cpp index c395ebf..41a2181 100644 --- a/Source/Terrain.cpp +++ b/Source/Terrain.cpp @@ -409,7 +409,9 @@ bool Terrain::load(const char *fileName) ImageRec texture; //Load Image - load_image(Folders::getResourcePath(fileName).c_str(), texture); + if (!load_image(Folders::getResourcePath(fileName).c_str(), texture)) { + return false; + } //Is it valid? if (texture.bpp > 24) { @@ -643,7 +645,7 @@ bool Terrain::load(const char *fileName) patch_elements = (patch_size) * (patch_size) * 54; CalculateNormals(); - return 1; + return true; } void Terrain::CalculateNormals() diff --git a/Source/Texture.cpp b/Source/Texture.cpp index 7fb4b94..3106c8f 100644 --- a/Source/Texture.cpp +++ b/Source/Texture.cpp @@ -34,7 +34,10 @@ void TextureRes::load() ImageRec texture; //load image into 'texture' - load_image(filename.c_str(), texture); + if (!load_image(filename.c_str(), texture)) { + cerr << "Texture " << filename << " loading failed" << endl; + return; + } skinsize = texture.sizeX; GLuint type = GL_RGBA; diff --git a/Source/Utils/Folders.cpp b/Source/Utils/Folders.cpp index 9a6f813..c4e86ab 100644 --- a/Source/Utils/Folders.cpp +++ b/Source/Utils/Folders.cpp @@ -135,3 +135,12 @@ bool Folders::makeDirectory(std::string path) { } #endif } + +FILE* Folders::openMandatoryFile(std::string filename, const char* mode) +{ + FILE* tfile = fopen(filename.c_str(), mode); + if (tfile == NULL) { + throw FileNotFoundException(filename); + } + return tfile; +} diff --git a/Source/Utils/Folders.h b/Source/Utils/Folders.h index d511ac2..495f149 100644 --- a/Source/Utils/Folders.h +++ b/Source/Utils/Folders.h @@ -27,6 +27,18 @@ along with Lugaru. If not, see . #define DATA_DIR "Data" #endif +struct FileNotFoundException: public std::exception +{ + std::string errorText; + FileNotFoundException (const std::string& filename) { + errorText = filename + " could not be found"; + } + + const char * what () const throw () { + return errorText.c_str(); + } +}; + class Folders { static const std::string dataDir; @@ -44,6 +56,8 @@ public: /* Returns full path for config file */ static std::string getConfigFilePath(); + static FILE* openMandatoryFile(std::string filename, const char* mode); + private: static const char* getHomeDirectory(); static std::string getGenericDirectory(const char* ENVVAR, const std::string fallback); diff --git a/Source/main.cpp b/Source/main.cpp index 33a7b37..9e1c110 100644 --- a/Source/main.cpp +++ b/Source/main.cpp @@ -111,11 +111,6 @@ static void GLAPIENTRY glDeleteTextures_doNothing(GLsizei n, const GLuint *textu } #endif // __MINGW32__ -#ifdef MessageBox -#undef MessageBox -#endif -#define MessageBox(hwnd,text,title,flags) STUBBED("msgbox") - // Menu defs int kContextWidth; @@ -749,7 +744,7 @@ int main(int argc, char **argv) LOG(e); - MessageBox(g_windowHandle, error.what(), "ERROR", MB_OK | MB_ICONEXCLAMATION); + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Exception catched", error.what(), NULL); return -1; }