bool loadColorsFromFile(const char *file) { FILE *f = fopen(file, "r"); if (f == NULL) { return false; } while (!feof(f)) { char buffer[500]; if (fgets(buffer, 500, f) == NULL) { break; } char *ptr = buffer; while (*ptr == ' ' || *ptr == '\t') { ++ptr; } if (*ptr == '\0' || *ptr == '#') { continue; // This is a comment or empty line, skip } int blockid = atoi(ptr); if (blockid < 1 || blockid > 255) { printf("Skipping invalid blockid %d in colors file\n", blockid); continue; } while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0') { ++ptr; } uint8_t vals[5]; bool valid = true; for (int i = 0; i < 5; ++i) { while (*ptr == ' ' || *ptr == '\t') { ++ptr; } if (*ptr == '\0') { printf("Too few arguments for block %d, ignoring line.\n", blockid); valid = false; break; } vals[i] = clamp(atoi(ptr)); while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0') { ++ptr; } } if (!valid) { continue; } memcpy(colors[blockid], vals, 5); colors[blockid][BRIGHTNESS] = GETBRIGHTNESS(colors[blockid]); } fclose(f); return true; }
/** * This loads grasscolor.png and foliagecolor.png where the * biome colors will be read from upon rendering */ bool loadBiomeColors(const char* path) { size_t len = strlen(path) + 21; char *grass = new char[len], *foliage = new char[len]; snprintf(grass, len + 20, "%s/grasscolor.png", path); snprintf(foliage, len + 20, "%s/foliagecolor.png", path); pngGrass = new PngReader(grass); pngLeaf = new PngReader(foliage); if (!pngGrass->isValidImage() || !pngLeaf->isValidImage()) { delete pngGrass; delete pngLeaf; printf("Could not load %s and %s: no valid PNG files. Biomes disabled.\n", grass, foliage); return false; } if ((pngGrass->getColorType() != PngReader::RGBA && pngGrass->getColorType() != PngReader::RGB) || pngGrass->getBitsPerChannel() != 8 || (pngLeaf->getColorType() != PngReader::RGBA && pngLeaf->getColorType() != PngReader::RGB) || pngLeaf->getBitsPerChannel() != 8 || pngGrass->getWidth() != 256 || pngGrass->getHeight() != 256 || pngLeaf->getWidth() != 256 || pngLeaf->getHeight() != 256) { delete pngGrass; delete pngLeaf; printf("Could not load %s and %s; Expecting RGB(A), 8 bits per channel.\n", grass, foliage); return false; } g_GrasscolorDepth = pngGrass->getBytesPerPixel(); g_FoliageDepth = pngLeaf->getBytesPerPixel(); g_Grasscolor = pngGrass->getImageData(); g_TallGrasscolor = new uint8_t[pngGrass->getBytesPerPixel() * 256 * 256]; g_Leafcolor = pngLeaf->getImageData(); // Adjust brightness to what colors.txt says const int maxG = pngGrass->getWidth() * pngGrass->getHeight() * g_GrasscolorDepth; for (int i = 0; i < maxG; ++i) { g_TallGrasscolor[i] = ((int)g_Grasscolor[i] * (int)colors[TALL_GRASS][BRIGHTNESS]) / 255; g_Grasscolor[i] = ((int)g_Grasscolor[i] * (int)colors[GRASS][BRIGHTNESS]) / 255; } const int maxT = pngLeaf->getWidth() * pngLeaf->getHeight() * g_FoliageDepth; for (int i = 0; i < maxT; ++i) { g_Leafcolor[i] = ((int)g_Leafcolor[i] * (int)colors[LEAVES][BRIGHTNESS]) / 255; } // Now re-calc brightness of those two colors[GRASS][BRIGHTNESS] = GETBRIGHTNESS(g_Grasscolor) - 5; colors[LEAVES][BRIGHTNESS] = colors[PINELEAVES][BRIGHTNESS] = colors[BIRCHLEAVES][BRIGHTNESS] = GETBRIGHTNESS(g_Leafcolor) - 5; printf("Loaded biome color maps from %s\n", path); return true; }
bool loadColorsFromFile(const char* file) { FILE *f = fopen(file, "r"); if (f == NULL) return false; while (!feof(f)) { char buffer[500]; if (fgets(buffer, 500, f) == NULL) break; char *ptr = buffer; while (*ptr == ' ' || *ptr == '\t') ++ptr; if (*ptr == '\0' || *ptr == '#') continue; // This is a comment or empty line, skip int blockid = atoi(ptr); if (blockid < 1 || blockid > 255) { printf("Skipping invalid blockid %d in colors file\n", blockid); continue; } while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0') ++ptr; uint8_t vals[5]; bool valid = true; for (int i = 0; i < 5; ++i) { while (*ptr == ' ' || *ptr == '\t') ++ptr; if (*ptr == '\0') { printf("Too few arguments for block %d, ignoring line.\n", blockid); valid = false; break; } if (i == 0) vals[RED] = uint8_t(atoi(ptr)); else if (i == 2) vals[BLUE] = uint8_t(atoi(ptr)); else vals[i] = uint8_t(atoi(ptr)); while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0') ++ptr; } if (!valid) continue; memcpy(colors[blockid], vals, 5); colors[blockid][PRED] = colors[blockid][RED]; colors[blockid][PGREEN] = colors[blockid][GREEN]; colors[blockid][PBLUE] = colors[blockid][BLUE]; colors[blockid][PALPHA] = colors[blockid][ALPHA]; colors[blockid][BRIGHTNESS] = GETBRIGHTNESS(colors[blockid]); } fclose(f); return true; }