You are not logged in.

#1 2016-05-14 20:48:19

ohnonot
...again
Registered: 2015-09-29
Posts: 5,592

xmountaincolors - xmountains, but with configurable colors

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

#2 2016-05-15 23:20:48

Eraph
Member
From: /au/qld/bne
Registered: 2016-02-29
Posts: 282
Website

Re: xmountaincolors - xmountains, but with configurable colors

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! smile


Lenovo IdeaPad Yoga 13 | BunsenLabs Hydrogen (x64)
Intel Core i7-3537U | Intel HD4000 | 8GB DDR3 | 256GB SSD

Offline

#3 2016-05-18 06:00:10

ohnonot
...again
Registered: 2015-09-29
Posts: 5,592

Re: xmountaincolors - xmountains, but with configurable colors

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

#4 2016-05-18 22:57:30

Eraph
Member
From: /au/qld/bne
Registered: 2016-02-29
Posts: 282
Website

Re: xmountaincolors - xmountains, but with configurable colors

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 smile


Lenovo IdeaPad Yoga 13 | BunsenLabs Hydrogen (x64)
Intel Core i7-3537U | Intel HD4000 | 8GB DDR3 | 256GB SSD

Offline

Board footer

Powered by FluxBB