mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-21 02:32:06 +00:00
Use RAII std::string and std::vector in randtilegen (#1325)
This commit is contained in:
@@ -12,18 +12,15 @@
|
|||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <limits.h>
|
|
||||||
#include <png.h>
|
#include <png.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <string>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "platform.hpp"
|
#include "platform.hpp"
|
||||||
|
|
||||||
#define STR(x) #x
|
|
||||||
#define XSTR(x) STR(x)
|
|
||||||
|
|
||||||
struct Attributes {
|
struct Attributes {
|
||||||
unsigned char palette;
|
unsigned char palette;
|
||||||
unsigned char nbColors;
|
unsigned char nbColors;
|
||||||
@@ -32,11 +29,6 @@ struct Attributes {
|
|||||||
static unsigned long long randbits = 0;
|
static unsigned long long randbits = 0;
|
||||||
static unsigned char randcount = 0;
|
static unsigned char randcount = 0;
|
||||||
|
|
||||||
[[noreturn]] static void fatal(char const *error) {
|
|
||||||
fprintf(stderr, "FATAL: %s\n", error);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static FILE *seed;
|
static FILE *seed;
|
||||||
|
|
||||||
static unsigned long long getRandomBits(unsigned count) {
|
static unsigned long long getRandomBits(unsigned count) {
|
||||||
@@ -189,11 +181,8 @@ static void write_image(char const *filename, uint16_t /* const */ palettes[MIN_
|
|||||||
uint8_t const SIZEOF_PIXEL = 4; // Each pixel is 4 bytes (RGBA @ 8 bits/component)
|
uint8_t const SIZEOF_PIXEL = 4; // Each pixel is 4 bytes (RGBA @ 8 bits/component)
|
||||||
assert(width != 0);
|
assert(width != 0);
|
||||||
assert(height != 0);
|
assert(height != 0);
|
||||||
uint8_t *data = (uint8_t *)malloc(height * 8 * width * 8 * SIZEOF_PIXEL);
|
std::vector<uint8_t> data(height * 8 * width * 8 * SIZEOF_PIXEL);
|
||||||
uint8_t **rowPtrs = (uint8_t **)malloc(height * 8 * sizeof(*rowPtrs));
|
std::vector<uint8_t *> rowPtrs(height * 8);
|
||||||
if (data == nullptr || rowPtrs == nullptr) {
|
|
||||||
fatal("Out of memory");
|
|
||||||
}
|
|
||||||
for (uint8_t y = 0; y < height * 8; ++y) {
|
for (uint8_t y = 0; y < height * 8; ++y) {
|
||||||
rowPtrs[y] = &data[y * width * 8 * SIZEOF_PIXEL];
|
rowPtrs[y] = &data[y * width * 8 * SIZEOF_PIXEL];
|
||||||
}
|
}
|
||||||
@@ -213,11 +202,9 @@ static void write_image(char const *filename, uint16_t /* const */ palettes[MIN_
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
png_set_rows(png, pngInfo, rowPtrs);
|
png_set_rows(png, pngInfo, rowPtrs.data());
|
||||||
png_write_png(png, pngInfo, PNG_TRANSFORM_IDENTITY, nullptr);
|
png_write_png(png, pngInfo, PNG_TRANSFORM_IDENTITY, nullptr);
|
||||||
fclose(file);
|
fclose(file);
|
||||||
free(rowPtrs);
|
|
||||||
free(data);
|
|
||||||
png_destroy_write_struct(&png, &pngInfo);
|
png_destroy_write_struct(&png, &pngInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -289,25 +276,21 @@ int main(int argc, char **argv) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t const nameLen = strlen(argv[2]);
|
|
||||||
unsigned long long maxcount = ULLONG_MAX;
|
unsigned long long maxcount = ULLONG_MAX;
|
||||||
if (argc > 3) {
|
if (argc > 3) {
|
||||||
char *error;
|
char *error;
|
||||||
maxcount = strtoull(argv[3], &error, 0);
|
maxcount = strtoull(argv[3], &error, 0);
|
||||||
if (*error != '\0') {
|
if (*error != '\0') {
|
||||||
fatal("invalid count");
|
fputs("FATAL: invalid count\n", stderr);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char *filename = (char *)malloc(nameLen + sizeof(XSTR(ULLONG_MAX) ".png"));
|
|
||||||
if (!filename) {
|
|
||||||
fatal("out of memory");
|
|
||||||
}
|
|
||||||
memcpy(filename, argv[2], nameLen);
|
|
||||||
|
|
||||||
for (unsigned long long count = 0; count < maxcount; count++) {
|
for (unsigned long long count = 0; count < maxcount; count++) {
|
||||||
sprintf(&filename[nameLen], "%llu.png", count);
|
std::string filename(argv[2]);
|
||||||
generate_random_image(filename);
|
filename.append(std::to_string(count));
|
||||||
|
filename.append(".png");
|
||||||
|
generate_random_image(filename.c_str());
|
||||||
// Reset the global random state so that subsequent images don't share a random byte
|
// Reset the global random state so that subsequent images don't share a random byte
|
||||||
randbits = 0;
|
randbits = 0;
|
||||||
randcount = 0;
|
randcount = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user