| 276 | } |
| 277 | |
| 278 | bool Terrain::LoadCachedSimplifiedTerrain() { |
| 279 | bool success = false; |
| 280 | bool rewrite_cache = false; |
| 281 | |
| 282 | char cache_rel_path[kPathSize]; |
| 283 | FormatString(cache_rel_path, kPathSize, "%s.cache", heightmap_.path().c_str()); |
| 284 | |
| 285 | char uv2_rel_path[kPathSize]; |
| 286 | FormatString(uv2_rel_path, kPathSize, "%s.obj_UV2", heightmap_.path().c_str()); |
| 287 | |
| 288 | char abs_uv2_path[kPathSize]; |
| 289 | // bool found_uv2 = false; |
| 290 | unsigned short uv2_checksum = 0; |
| 291 | if (FindFilePath(uv2_rel_path, abs_uv2_path, kPathSize, kDataPaths | kModPaths, false, NULL) == 0) { |
| 292 | // found_uv2 = true; |
| 293 | uv2_checksum = Checksum(abs_uv2_path); |
| 294 | } |
| 295 | |
| 296 | const int kMaxPaths = 5; |
| 297 | char abs_cache_paths[kPathSize * kMaxPaths]; |
| 298 | int num_paths_found = FindFilePaths(cache_rel_path, abs_cache_paths, kPathSize, kMaxPaths, kAnyPath, true, NULL, NULL); |
| 299 | |
| 300 | if (num_paths_found > 0) { |
| 301 | for (int path_index = 0; path_index < num_paths_found; ++path_index) { |
| 302 | char *curr_path = &abs_cache_paths[kPathSize * path_index]; |
| 303 | FILE *cache_file = my_fopen(curr_path, "rb"); |
| 304 | if (cache_file) { // bug: sometimes cache_file is not null when fopen fails |
| 305 | uint16_t version; |
| 306 | fread(&version, sizeof(version), 1, cache_file); |
| 307 | if (version == _terrain_cache_file_version_number) { |
| 308 | uint16_t checksum = 0; |
| 309 | fread(&checksum, sizeof(checksum), 1, cache_file); |
| 310 | if (checksum == heightmap_.checksum()) { |
| 311 | uint16_t uv2_checksum_read = 0; |
| 312 | fread(&uv2_checksum_read, sizeof(uv2_checksum_read), 1, cache_file); |
| 313 | if (uv2_checksum_read == uv2_checksum) { |
| 314 | AddLoadingText("Loading cached terrain..."); |
| 315 | if (model_id == -1) { |
| 316 | model_id = Models::Instance()->AddModel(); |
| 317 | } |
| 318 | Model &terrain_simplified_model = Models::Instance()->GetModel(model_id); |
| 319 | terrain_simplified_model.Dispose(); |
| 320 | terrain_simplified_model.ReadFromFile(cache_file); |
| 321 | terrain_simplified_model.calcBoundingBox(); |
| 322 | terrain_simplified_model.calcBoundingSphere(); // TO DO: This should be cached |
| 323 | terrain_simplified_model.vbo_enabled = true; |
| 324 | success = true; |
| 325 | LOGI << "Loaded cached terrain: \"" << cache_rel_path << "\"" << std::endl; |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | fclose(cache_file); |
| 330 | } |
| 331 | if (success) { |
| 332 | break; |
| 333 | } |
| 334 | } |
| 335 | } |
nothing calls this directly
no test coverage detected