Files

56 lines
1.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
static uint32_t rng_state = 2463534242u;
float randf() {
rng_state ^= rng_state << 13;
rng_state ^= rng_state >> 17;
rng_state ^= rng_state << 5;
uint32_t bits = (rng_state >> 9) | 0x3F800000u;
float f;
__builtin_memcpy(&f, &bits, sizeof f);
return f - 1.0f;
}
static FILE* ppm_file;
static int pixel_count;
static int pixels_per_line;
static int max_lines;
static int line_count;
void ppm_start(int width, int height) {
ppm_file = fopen("image.ppm", "w");
if (ppm_file == NULL) exit(-1);
fprintf(ppm_file, "P3\n%d %d\n 255\n", width, height);
max_lines = height;
pixels_per_line = width;
pixel_count = 0;
line_count = 0;
}
void ppm_pixel(int r, int g, int b) {
if (ppm_file == NULL) exit(-1);
fprintf(ppm_file, "%d %d %d\n", r, g, b);
pixel_count++;
if (pixel_count == pixels_per_line) {
pixel_count = 0;
line_count++;
printf("Progress: %d / %d\r", line_count, max_lines);
fflush(stdout);
}
}
void ppm_finish() {
fflush(ppm_file);
fclose(ppm_file);
ppm_file = NULL;
printf("\33[2K\rDone!\n");
}