You are not logged in.
xmountaincolors
that was fun, and hard work. i don't know nothing about C. well, now i know a little.
my additions probably are a mess, but it works.
xmountains is so lightweight, you can throw the weirdest configuration options at it and it still doesn't show up in cpu top.
that is what i will do next, find out just how alien and abstract i can make it look...
Last edited by ohnonot (2021-07-03 10:37:31)
Offline
The code looks perfectly fine. I had a look at your .diff and the only critique I would say is that there's a good chunk of repetition that would merit you break your code up into functions. And I must admit it's been so long since I've done C (I'm all C#, JavaScript, "modern" languages these days), so I'm a bit fuzzy about the pointers around the function arguments, but I think it would be something like:
/* Output a list of all colors. */
void OutputColors(config_t *cfg, char colors[], char colorName[]) {
setting = config_lookup(&cfg, colors);
if(setting != NULL)
{
count = config_setting_length(setting);
printf("%s:\n\t%s\t%s\t%s\n", colorName "RED", "GREEN", "BLUE");
for(i = 0; i < count; ++i)
{
config_setting_t *base = config_setting_get_elem(setting, i);
/* Only output the record if all of the expected fields are present. */
double red_conf, blue_conf, green_conf;
if((config_setting_lookup_float(base, "red", &red_conf)
&& config_setting_lookup_float(base, "blue", &blue_conf)
&& config_setting_lookup_float(base, "green", &green_conf)))
printf("\t%3.3f\t%3.3f\t%3.3f\n", red_conf,green_conf,blue_conf);
red[BLACK] = red_conf*COL_RANGE;
green[BLACK] = green_conf*COL_RANGE;
blue[BLACK] = blue_conf*COL_RANGE;
}
}
}
And call it by something like
OutputColors(cfg, "colors.base", "base");
You might need to scope your variables to within the function - you'll figure it out, just giving you a push in the right direction I hope. Gotsta reduce that repetition! Good work though!
Lenovo IdeaPad Yoga 13 | BunsenLabs Hydrogen (x64)
Intel Core i7-3537U | Intel HD4000 | 8GB DDR3 | 256GB SSD
Offline
thanks a lot.
yes, i am aware of the repetitions, fact is i simply didn't know how to make a function.
i will try your code.
Last edited by ohnonot (2021-07-03 10:33:14)
Offline
Looks good!
Functions in C are dead easy. You just specify a return type (I've used void in my example which means it doesn't return anything, you might be more inclined to call it a procedure or a subroutine in this case but C based languages just call them all functions (unless you go into object oriented languages in which case you start calling them methods, but that's another matter)), give it a name, open brackets and specify the input parameters. Like I say, the only bit I'm fuzzy about is passing in pointers, I never did get the hang of them, but I'm sure some online resource will help you out there
Lenovo IdeaPad Yoga 13 | BunsenLabs Hydrogen (x64)
Intel Core i7-3537U | Intel HD4000 | 8GB DDR3 | 256GB SSD
Offline