Skip to content

Commit

Permalink
snake_case args.h and remove it from API
Browse files Browse the repository at this point in the history
This should be used strictly in the driver program, and not internally.
Working on that.
  • Loading branch information
vkoskiv committed Nov 12, 2023
1 parent 1f85231 commit db637b9
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 86 deletions.
5 changes: 0 additions & 5 deletions include/c-ray/c-ray.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ struct texture;
char *cr_get_version(void); //The current semantic version
char *cr_get_git_hash(void); //The current git hash of the build

void cr_parse_args(int argc, char **argv);
int cr_is_option_set(char *key);
char *cr_path_arg(void);
void cr_destroy_options(void);

char *cr_get_file_path(char *full_path);


Expand Down
20 changes: 2 additions & 18 deletions src/api/c-ray.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,6 @@ char *cr_get_git_hash() {
return gitHash();
}

void cr_parse_args(int argc, char **argv) {
parseArgs(argc, argv);
}

int cr_is_option_set(char *key) {
return isSet(key);
}

char *cr_path_arg() {
return pathArg();
}

void cr_destroy_options() {
destroyOptions();
}

char *cr_get_file_path(char *full_path) {
return get_file_path(full_path);
}
Expand Down Expand Up @@ -390,12 +374,12 @@ int cr_get_bounces(struct cr_renderer *ext) {

void cr_set_asset_path(struct cr_renderer *ext) {
struct renderer *r = (struct renderer *)ext;
r->prefs.assetPath = cr_is_option_set("inputFile") ? cr_get_file_path(cr_path_arg()) : cr_is_option_set("asset_path") ? stringCopy(specifiedAssetPath()) : stringCopy("./");
r->prefs.assetPath = args_is_set("inputFile") ? cr_get_file_path(args_path()) : args_is_set("asset_path") ? stringCopy(args_asset_path()) : stringCopy("./");
}

void cr_start_renderer(struct cr_renderer *ext) {
struct renderer *r = (struct renderer *)ext;
if (isSet("use_clustering")) {
if (args_is_set("use_clustering")) {
r->prefs.useClustering = true;
r->state.clients = syncWithClients(r, &r->state.clientCount);
free(r->sceneCache);
Expand Down
18 changes: 9 additions & 9 deletions src/datatypes/scene.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ int loadScene(struct renderer *r, char *input) {
// This is where we prepare a cache of scene data to be sent to worker nodes
// We also apply any potential command-line overrides to that cache here as well.
// FIXME: This overrides setting should be integrated with scene loading, probably.
if (isSet("use_clustering")) {
if (args_is_set("use_clustering")) {
// Stash a cache of scene data here
// Apply overrides to the cache here
if (isSet("samples_override")) {
if (args_is_set("samples_override")) {
cJSON *renderer = cJSON_GetObjectItem(json, "renderer");
if (cJSON_IsObject(renderer)) {
int samples = intPref("samples_override");
int samples = args_int("samples_override");
logr(debug, "Overriding cache sample count to %i\n", samples);
if (cJSON_IsNumber(cJSON_GetObjectItem(renderer, "samples"))) {
cJSON_ReplaceItemInObject(renderer, "samples", cJSON_CreateNumber(samples));
Expand All @@ -166,11 +166,11 @@ int loadScene(struct renderer *r, char *input) {
}
}

if (isSet("dims_override")) {
if (args_is_set("dims_override")) {
cJSON *renderer = cJSON_GetObjectItem(json, "renderer");
if (cJSON_IsObject(renderer)) {
int width = intPref("dims_width");
int height = intPref("dims_height");
int width = args_int("dims_width");
int height = args_int("dims_height");
logr(info, "Overriding cache image dimensions to %ix%i\n", width, height);
if (cJSON_IsNumber(cJSON_GetObjectItem(renderer, "width")) && cJSON_IsNumber(cJSON_GetObjectItem(renderer, "height"))) {
cJSON_ReplaceItemInObject(renderer, "width", cJSON_CreateNumber(width));
Expand All @@ -182,11 +182,11 @@ int loadScene(struct renderer *r, char *input) {
}
}

if (isSet("tiledims_override")) {
if (args_is_set("tiledims_override")) {
cJSON *renderer = cJSON_GetObjectItem(json, "renderer");
if (cJSON_IsObject(renderer)) {
int width = intPref("tile_width");
int height = intPref("tile_height");
int width = args_int("tile_width");
int height = args_int("tile_height");
logr(info, "Overriding cache tile dimensions to %ix%i\n", width, height);
if (cJSON_IsNumber(cJSON_GetObjectItem(renderer, "tileWidth")) && cJSON_IsNumber(cJSON_GetObjectItem(renderer, "tileHeight"))) {
cJSON_ReplaceItemInObject(renderer, "tileWidth", cJSON_CreateNumber(width));
Expand Down
15 changes: 8 additions & 7 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,29 @@

#include "utils/logging.h"
#include "utils/fileio.h"
#include "utils/args.h"
#include "utils/platform/terminal.h"

int main(int argc, char *argv[]) {
term_init();
atexit(term_restore);
logr(info, "c-ray v%s [%.8s], © 2015-2023 Valtteri Koskivuori\n", cr_get_version(), cr_get_git_hash());
cr_parse_args(argc, argv);
args_parse(argc, argv);
struct cr_renderer *renderer = cr_new_renderer();
if (!cr_is_option_set("is_worker")) {
if (!args_is_set("is_worker")) {
size_t bytes = 0;
char *input = cr_is_option_set("inputFile") ? load_file(cr_path_arg(), &bytes, NULL) : read_stdin(&bytes);
char *input = args_is_set("inputFile") ? load_file(args_path(), &bytes, NULL) : read_stdin(&bytes);
if (!input) {
logr(info, "No input provided, exiting.\n");
cr_destroy_renderer(renderer);
cr_destroy_options();
args_destroy();
return -1;
}
logr(info, "%zi bytes of input JSON loaded from %s, parsing.\n", bytes, cr_is_option_set("inputFile") ? "file" : "stdin");
logr(info, "%zi bytes of input JSON loaded from %s, parsing.\n", bytes, args_is_set("inputFile") ? "file" : "stdin");
if (cr_load_scene_from_buf(renderer, input) < 0) {
logr(warning, "Scene parse failed, exiting.\n");
cr_destroy_renderer(renderer);
cr_destroy_options();
args_destroy();
return 0;
}

Expand All @@ -43,7 +44,7 @@ int main(int argc, char *argv[]) {
}

cr_destroy_renderer(renderer);
cr_destroy_options();
args_destroy();
logr(info, "Render finished, exiting.\n");
return 0;
}
6 changes: 3 additions & 3 deletions src/renderer/renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct texture *renderFrame(struct renderer *r) {
KNRM,
PLURAL(r->prefs.threads));

logr(info, "Pathtracing%s...\n", isSet("interactive") ? " iteratively" : "");
logr(info, "Pathtracing%s...\n", args_is_set("interactive") ? " iteratively" : "");

r->state.rendering = true;
r->state.render_aborted = false;
Expand All @@ -88,7 +88,7 @@ struct texture *renderFrame(struct renderer *r) {
float avgTimePerTilePass = 0.0f;
int pauser = 0;
int ctr = 1;
bool interactive = isSet("interactive");
bool interactive = args_is_set("interactive");

size_t remoteThreads = 0;
for (size_t i = 0; i < r->state.clientCount; ++i) {
Expand Down Expand Up @@ -382,7 +382,7 @@ struct renderer *renderer_new() {
r->state.finishedPasses = 1;

r->state.tileMutex = mutex_create();
if (isSet("use_clustering")) r->state.file_cache = calloc(1, sizeof(struct file_cache));
if (args_is_set("use_clustering")) r->state.file_cache = calloc(1, sizeof(struct file_cache));

r->scene = calloc(1, sizeof(*r->scene));
r->scene->storage.node_pool = newBlock(NULL, 1024);
Expand Down
24 changes: 12 additions & 12 deletions src/utils/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ bool parseDims(const char *dimStr, int *widthOut, int *heightOut) {
return true;
}

void parseArgs(int argc, char **argv) {
void args_parse(int argc, char **argv) {
g_options = newConstantsDatabase();
static bool inputFileSet = false;
int testIdx = -1;
Expand Down Expand Up @@ -239,7 +239,7 @@ void parseArgs(int argc, char **argv) {
}
logr(debug, "Verbose mode enabled\n");

if (isSet("shutdown") && isSet("nodes_list")) {
if (args_is_set("shutdown") && args_is_set("nodes_list")) {
shutdownClients();
term_restore();
exit(0);
Expand All @@ -250,10 +250,10 @@ void parseArgs(int argc, char **argv) {
alternatePath = NULL;
}

if (isSet("runTests") || isSet("runPerfTests")) {
if (args_is_set("runTests") || args_is_set("runPerfTests")) {
#ifdef CRAY_TESTING
char *suite = NULL;
if (isSet("test_suite")) suite = getDatabaseString(g_options, "test_suite");
if (args_is_set("test_suite")) suite = getDatabaseString(g_options, "test_suite");
switch (testIdx) {
case -3:
printf("%i", getPerfTestCount(suite));
Expand All @@ -264,10 +264,10 @@ void parseArgs(int argc, char **argv) {
exit(0);
break;
case -1:
exit(isSet("runPerfTests") ? runPerfTests(suite) : runTests(suite));
exit(args_is_set("runPerfTests") ? runPerfTests(suite) : runTests(suite));
break;
default:
exit(isSet("runPerfTests") ? runPerfTest(testIdx, suite) : runTest(testIdx, suite));
exit(args_is_set("runPerfTests") ? runPerfTest(testIdx, suite) : runTest(testIdx, suite));
break;
}
#else
Expand All @@ -278,30 +278,30 @@ void parseArgs(int argc, char **argv) {
}
}

bool isSet(const char *key) {
bool args_is_set(const char *key) {
if (!g_options) return false;
return existsInDatabase(g_options, key);
}

int intPref(const char *key) {
int args_int(const char *key) {
ASSERT(existsInDatabase(g_options, key));
return getDatabaseInt(g_options, key);
}

char *stringPref(const char *key) {
char *args_string(const char *key) {
return getDatabaseString(g_options, key);
}

char *pathArg() {
char *args_path() {
ASSERT(existsInDatabase(g_options, "inputFile"));
return getDatabaseString(g_options, "inputFile");
}

char *specifiedAssetPath(void) {
char *args_asset_path(void) {
ASSERT(existsInDatabase(g_options, "asset_path"));
return getDatabaseString(g_options, "asset_path");
}

void destroyOptions() {
void args_destroy() {
freeConstantsDatabase(g_options);
}
14 changes: 7 additions & 7 deletions src/utils/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@

#pragma once

void parseArgs(int argc, char **argv);
void args_parse(int argc, char **argv);

bool isSet(const char *key);
bool args_is_set(const char *key);

int intPref(const char *key);
int args_int(const char *key);

char *stringPref(const char *key);
char *args_string(const char *key);

char *pathArg(void);
char *args_path(void);

char *specifiedAssetPath(void);
char *args_asset_path(void);

void destroyOptions(void);
void args_destroy(void);
2 changes: 1 addition & 1 deletion src/utils/encoders/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

void writeImage(struct imageFile *image) {
char *buf = NULL;
if (isSet("output_path")) {
if (args_is_set("output_path")) {
asprintf(&buf, "%s%s", image->filePath, image->fileName);
image->type = guess_file_type(buf);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void write_file(const unsigned char *buf, size_t bufsize, const char *filePath)


bool is_valid_file(char *path, struct file_cache *cache) {
if (!isSet("use_clustering") && cache) return cache_contains(cache, path);
if (!args_is_set("use_clustering") && cache) return cache_contains(cache, path);
#ifndef WINDOWS
struct stat path_stat = { 0 };
stat(path, &path_stat);
Expand Down
34 changes: 17 additions & 17 deletions src/utils/loaders/sceneloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ void parse_prefs(struct cr_renderer *ext, const cJSON *data) {
// from there.

// Now check and apply potential CLI overrides.
if (isSet("thread_override")) {
size_t threads = intPref("thread_override");
if (args_is_set("thread_override")) {
size_t threads = args_int("thread_override");
int64_t curr = cr_renderer_get_num_pref(ext, cr_renderer_threads);
if (curr != (int64_t)threads) {
logr(info, "Overriding thread count to %zu\n", threads);
Expand All @@ -240,42 +240,42 @@ void parse_prefs(struct cr_renderer *ext, const cJSON *data) {
}
}

if (isSet("samples_override")) {
if (isSet("is_worker")) {
if (args_is_set("samples_override")) {
if (args_is_set("is_worker")) {
logr(warning, "Can't override samples when in worker mode\n");
} else {
int samples = intPref("samples_override");
int samples = args_int("samples_override");
logr(info, "Overriding sample count to %i\n", samples);
cr_renderer_set_num_pref(ext, cr_renderer_samples, samples);
}
}

if (isSet("dims_override")) {
if (isSet("is_worker")) {
if (args_is_set("dims_override")) {
if (args_is_set("is_worker")) {
logr(warning, "Can't override dimensions when in worker mode\n");
} else {
int width = intPref("dims_width");
int height = intPref("dims_height");
int width = args_int("dims_width");
int height = args_int("dims_height");
logr(info, "Overriding image dimensions to %ix%i\n", width, height);
cr_renderer_set_num_pref(ext, cr_renderer_override_width, width);
cr_renderer_set_num_pref(ext, cr_renderer_override_height, height);
}
}

if (isSet("tiledims_override")) {
if (isSet("is_worker")) {
if (args_is_set("tiledims_override")) {
if (args_is_set("is_worker")) {
logr(warning, "Can't override tile dimensions when in worker mode\n");
} else {
int width = intPref("tile_width");
int height = intPref("tile_height");
int width = args_int("tile_width");
int height = args_int("tile_height");
logr(info, "Overriding tile dimensions to %ix%i\n", width, height);
cr_renderer_set_num_pref(ext, cr_renderer_tile_width, width);
cr_renderer_set_num_pref(ext, cr_renderer_tile_height, height);
}
}

if (isSet("cam_index")) {
cr_renderer_set_num_pref(ext, cr_renderer_override_cam, intPref("cam_index"));
if (args_is_set("cam_index")) {
cr_renderer_set_num_pref(ext, cr_renderer_override_cam, args_int("cam_index"));
}
}

Expand Down Expand Up @@ -781,8 +781,8 @@ int parseJSON(struct renderer *r, const cJSON *json) {

parse_prefs((struct cr_renderer *)r, cJSON_GetObjectItem(json, "renderer"));

if (isSet("output_path")) {
char *path = stringPref("output_path");
if (args_is_set("output_path")) {
char *path = args_string("output_path");
logr(info, "Overriding output path to %s\n", path);
free(r->prefs.imgFileName);
free(r->prefs.imgFilePath);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static void printDate() {

void logr(enum logType type, const char *fmt, ...) {
if (!fmt) return;
if (type == debug && !isSet("v")) return;
if (type == debug && !args_is_set("v")) return;

if (type != plain) {
printPrefix(type);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/platform/sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ static void draw_frames(struct texture *overlay, struct render_tile_arr tiles) {
void win_update(struct sdl_window *w, struct renderer *r, struct texture *t) {
if (!w) return;
//Render frames
if (!isSet("interactive") || r->state.clients) {
if (!args_is_set("interactive") || r->state.clients) {
draw_frames(w->overlay, r->state.tiles);
draw_prog_bars(w->overlay, r->state.tiles);
}
Expand Down
Loading

0 comments on commit db637b9

Please sign in to comment.