From baa67b51dbc0144671084a5e4b9c4a9eca94f252 Mon Sep 17 00:00:00 2001 From: Jean-Michel Gorius Date: Sun, 13 Nov 2022 17:43:49 +0100 Subject: [PATCH] Move to CMake --- .gitignore | 5 ++++- CMakeLists.txt | 20 ++++++++++++++++++++ build.sh | 13 ------------- external/stb_image.c | 2 ++ main.c | 42 ++++++++++++++++++++---------------------- 5 files changed, 46 insertions(+), 36 deletions(-) create mode 100644 CMakeLists.txt delete mode 100755 build.sh create mode 100644 external/stb_image.c diff --git a/.gitignore b/.gitignore index 9e5608d..21b48a3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,15 @@ build/ +cmake-build-*/ output.ppm -.clangd/ .cache/ +.clangd/ +.idea/ .vs/ .vscode/ +compile_commands.json compile_flags.txt vgcore.* diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..ab771af --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.10) + +project(raytracing-in-one-weekend C) + +add_executable(raytracer main.c + aabb.c + arena.c + camera.c + color.c + hittable.c + material.c + perlin.c + point3.c + ray.c + texture.c + utils.c + vec3.c + + external/stb_image.c) +target_link_libraries(raytracer m) diff --git a/build.sh b/build.sh deleted file mode 100755 index e04c886..0000000 --- a/build.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash - -CC=${CC:-"gcc"} -CFLAGS=${CFLAGS:-"-Wall -Wextra -Wno-unused-function -std=gnu18 -fno-strict-aliasing -O3"} -LDFLAGS=${LDFLAGS:-"-lm"} -MAIN_FILE=${MAIN_FILE:-"main.c"} - -# Write out compile_flags.txt for clangd -echo "${CFLAGS}" | tr ' ' '\n' > compile_flags.txt - -mkdir -p build - -${CC} ${CFLAGS} ${MAIN_FILE} -o build/raytracer ${LDFLAGS} diff --git a/external/stb_image.c b/external/stb_image.c new file mode 100644 index 0000000..8ddfd1f --- /dev/null +++ b/external/stb_image.c @@ -0,0 +1,2 @@ +#define STB_IMAGE_IMPLEMENTATION +#include "stb_image.h" diff --git a/main.c b/main.c index 13f9326..882e7a8 100644 --- a/main.c +++ b/main.c @@ -1,8 +1,9 @@ +#include #include -#include -#include #include #include +#include +#include #include "arena.h" #include "camera.h" @@ -185,8 +186,20 @@ static Hittable *simple_light(Arena *arena) { return world; } -int main(void) { - srand(42); +int main(int argc, char *argv[]) { + srand(time(0)); + + if (argc < 2) { + fprintf(stderr, "Expected output file name\n"); + return 1; + } + + FILE *output_file = fopen(argv[1], "wb"); + if (!output_file) { + fprintf(stderr, "Failed to open output file %s: %s", argv[1], + strerror(errno)); + return 1; + } /* Memory management */ @@ -255,7 +268,7 @@ int main(void) { camera_init(&camera, look_from, look_at, up, vfov, aspect_ratio, aperture, dist_to_focus, 0.0, 1.0); - printf("P3\n%u %u\n255\n", image_width, image_height); + fprintf(output_file, "P3\n%u %u\n255\n", image_width, image_height); for (int j = image_height - 1; j >= 0; --j) { fprintf(stderr, "\rScanlines remaining: %d ", j); @@ -268,29 +281,14 @@ int main(void) { pixel_color = color_add( pixel_color, ray_color(r, background_color, world, max_depth)); } - color_write(stdout, pixel_color, samples_per_pixel); + color_write(output_file, pixel_color, samples_per_pixel); } } fprintf(stderr, "\nDone.\n"); free(buffer); + fclose(output_file); return 0; } - -#include "aabb.c" -#include "arena.c" -#include "camera.c" -#include "color.c" -#include "hittable.c" -#include "material.c" -#include "perlin.c" -#include "point3.c" -#include "ray.c" -#include "texture.c" -#include "utils.c" -#include "vec3.c" - -#define STB_IMAGE_IMPLEMENTATION -#include "external/stb_image.h"