Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Bloody
Mar 3, 2013

are you looking for a for loop?

Adbot
ADBOT LOVES YOU

Bloody
Mar 3, 2013

you have definitely found the right thread

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Bloody posted:

what does this post even say


hey now that is not in the spirit of this thread

articulating programing things is really hard

bobbilljim
May 29, 2013

this christmas feels like the very first christmas to me
:shittydog::shittydog::shittydog:
try not to use globals if u can help it, it will make your life easier. good luck with that though if your on a micro lol

Fanged Lawn Wormy
Jan 4, 2008

SQUEAK! SQUEAK! SQUEAK!

Bloody posted:

you have definitely found the right thread

truth

i know for loops. it just feels like the way I'm implementing a function that I need to have happen over time is... kludgy. like, surely there is a better way to do it than to have the function re-calculate parts of itself every time it gets called, but then when I get into static variables the function could hold, it limits the ability to run multiple instances of the function during the loop

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Fanged Lawn Wormy posted:

truth

i know for loops. it just feels like the way I'm implementing a function that I need to have happen over time is... kludgy. like, surely there is a better way to do it than to have the function re-calculate parts of itself every time it gets called, but then when I get into static variables the function could hold, it limits the ability to run multiple instances of the function during the loop

this post right here is exactly why C should not be a starter language.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Okay, so correct me if anything here isn't what you're actually doing/trying to do...

1. You want to slowly fade from one colour to another, over time
2. You have some sort of message loop that you're using to call your fade function every now and then
3. Your fade function uses static (i.e. global) variables to keep track of how much fading it's already done/how much it still needs to do

I think the first step is to know what structs are and how to use them. A struct is just a way to wrap up a whole bunch of values and pass them around all in one thing. As a warm-up, try writing a struct to represent "colour", and rewrite your existing functions to use it instead of passing around an array of three values.

The next important thing to do after that is to split out the "I want to do a fade from Cyan to Magenta" bit from the "here's what I call every tick to perform one step of the fade". You'll find structs useful here.

Fanged Lawn Wormy
Jan 4, 2008

SQUEAK! SQUEAK! SQUEAK!
thanks that's reassuring.

I initially wanted to write this function w/ structs, but got intimidated and backed out. I think I'm going to re-try it now with structs to learn that part, and see where it rolls from there

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Fanged Lawn Wormy posted:

thanks that's reassuring.

I initially wanted to write this function w/ structs, but got intimidated and backed out. I think I'm going to re-try it now with structs to learn that part, and see where it rolls from there

I think a struct is a lot like what you're imagining a class is, in your head, so your class instinct was right.

Fanged Lawn Wormy
Jan 4, 2008

SQUEAK! SQUEAK! SQUEAK!
:coolspot:

The Leck
Feb 27, 2001

Fanged Lawn Wormy posted:

truth

i know for loops. it just feels like the way I'm implementing a function that I need to have happen over time is... kludgy. like, surely there is a better way to do it than to have the function re-calculate parts of itself every time it gets called, but then when I get into static variables the function could hold, it limits the ability to run multiple instances of the function during the loop

i'm not entirely sure this is what you're looking for, but a lot of what you're saying strongly points me toward the concept of dynamic programming. specifically, your mentions of calling the function repeatedly and recalculating parts of it each time sort of scream out for some caching to me. basically, the first time you do a fade from a particular state, you cache the inputs and outputs, and next time those particular inputs come up, you go to the cache instead of recalculating. i may be misunderstanding a bit about the amount of potential states and the amount of calculation going on though.

wikipedia posted:

In order to solve a given problem, using a dynamic programming approach, we need to solve different parts of the problem (subproblems), then combine the solutions of the subproblems to reach an overall solution. Often when using a more naive method, many of the subproblems are generated and solved many times. The dynamic programming approach seeks to solve each subproblem only once, thus reducing the number of computations: once the solution to a given subproblem has been computed, it is stored or "memo-ized": the next time the same solution is needed, it is simply looked up.

the result in the cache would probably be the next step in the fade, based on what i'm understanding. then you'd basically end up with a cache of [start value, end value, # of steps] -> new RGB value and hopefully those are reusable. i'm assuming that if you get to the same start and end rgb values and number of steps remaining, the result will be the same, regardless of how many total steps you're taking.

comedyblissoption
Mar 15, 2006

Fanged Lawn Wormy posted:

thanks that's reassuring.

I initially wanted to write this function w/ structs, but got intimidated and backed out. I think I'm going to re-try it now with structs to learn that part, and see where it rolls from there
if you're trying to fade from one colour to another over time, that sounds like linear interpolation over time
http://stackoverflow.com/questions/13488957/interpolate-from-one-color-to-another

pass in the initial time, elapsed time, and fade times as parameters to determine how much interpolation there should be between an initial colour and end colour. then use this decided interpolation to return the interpolated colour.

your program will be more composable if you have separate functions for:
  • linearly interpolating between two colours
  • a function for linearly interpolating between two colours over given time parameters calling the above
  • functions that need to get the current colour for some ui component by calling the above function

a recursive function for colour interpolation over time sounds like a bad idea

encapsulating these colour interpolation functions so they don't reference global or static variables will make them re-usable for multiple ui components

comedyblissoption fucked around with this message at 14:49 on Jun 7, 2015

Jerry Bindle
May 16, 2003
on the topic of encapsulating state... its as simple as stuffing all your state variables into a struct. you can then pass those objects to functions with a pointer and treat C functions like OO methods. idk anything about color, so say you're writing a synthesizer instead,

code:
typedef struct waveform {
  double step_size;
  double t; 
  double output;
}
code:
void set_step_size(waveform *w, double sample_rate, double frequency) { w->step_size = ...; }
code:
void step_sine_waveform(waveform *w) {
  w->t += w->step_size;
  w->output = sin(w->t);
}
from this point you can reuse the "methods" on an arbitrary number of objects

Bloody
Mar 3, 2013

stick some function pointers in the struct that take the struct and now you've created an objectouroboros of poo poo

Luigi Thirty
Apr 30, 2006

Emergency confection port.

don't sign your pooooosts

Jerry Bindle
May 16, 2003
there are some good reasons to create a struct of function pointers. like if you want to create an interface/protocol, or group a set of callbacks. e.g. iirc a bsd device driver has an instance of some struct that points to the open/close/read/write/&c functions that do the needful.

C is great/terrible. you can get away with doing basically what ever you want. for instance, you can do single inheritance with structs!

code:
typedef struct  {
  int vehicle_data;
} vehicle;

typedef struct  {
  vehicle super;
  int car_data;
} car;

void do_thing_to_vehicle(vehicle *v) {
  v->vehicle_data = ...;
}

car *c = ...;

do_thing_to_vehicle((vehicle *)c);
this kind of stuff will work, the rules about how structs are created are defined by the spec/implementation and you can abuse this till your heart is content.

dont, tho bc anyone else will look at you and say

Bloody posted:

and now you've created an objectouroboros of poo poo

Bloody
Mar 3, 2013

i unironically frequently create structs of unions of structs of unions of structs

Soricidus
Oct 21, 2010
freedom-hating statist shill
unions are good and will free us from the tyranny of the class system

The Leck
Feb 27, 2001

Soricidus posted:

unions are good and will free us from the tyranny of the class system

suffix
Jul 27, 2013

Wheeee!

Bloody posted:

i unironically frequently create structs of unions of structs of unions of structs

and pay what, two hours of debugging time per byte of ram saved?

Shaggar
Apr 26, 2006

Soricidus posted:

unions are good and will free us from the tyranny of the class system
actually they are bad and are used to promote union leadership within the class system.

Asshole Masonanie
Oct 27, 2009

by vyelkin
been listening to the old quake soundtrack whilst coding and it's very good. A++++ recommended. bye

Bloody
Mar 3, 2013

suffix posted:

and pay what, two hours of debugging time per byte of ram saved?

0 seconds of debugging time for glorious in-place packet munging

Bloody
Mar 3, 2013

its literally my one-size-fits-most plug-n-play serial port interrupt pile-o-poo poo handler

Bloody
Mar 3, 2013

80% of my job most days is fling poo poo from a pc serial port to a spi port or something so its nice to have pleasant ways of doing that and struct-union-struct-union-struct-... blobs are comedically effective at it

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Power Ambient posted:

been listening to the old quake soundtrack whilst coding and it's very good. A++++ recommended. bye

thank you for this excellent life advice

eschaton
Mar 7, 2007

Don't you just hate when you wind up in a store with people who are in a socioeconomic class that is pretty obviously about two levels lower than your own?

Bloody posted:

80% of my job most days is fling poo poo from a pc serial port to a spi port or something so its nice to have pleasant ways of doing that and struct-union-struct-union-struct-... blobs are comedically effective at it

so basically protocol buffers

Jerry Bindle
May 16, 2003
is there any way to do arbitrary bit swizzling with union-struct-...-union-structs? like ABCDEFGH -> AEBFCGDH?

Bloody
Mar 3, 2013

eschaton posted:

so basically protocol buffers

roll-your-own but yeah pmuch

Luigi Thirty
Apr 30, 2006

Emergency confection port.

gonadic io
Feb 16, 2011

>>=
jesus loving christ thanks microsoft for convincing me that i was going insane: http://stackoverflow.com/questions/5918534/why-cant-i-add-a-subfolder-in-a-f-project

Jerry Bindle
May 16, 2003
is there a sane way to use java classes generated by an .xsd xml schema as the data model in a desktop application? as a test, i injected an EventBus.post into the generated classes setters, and then put corresponding event handlers in the viewer, but it feels... not right

AWWNAW
Dec 30, 2008

OData looks kinda cool in theory but I bet it's really not in practice

confirm deny

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

AWWNAW posted:

OData looks kinda cool in theory but I bet it's really not in practice

confirm deny

the first link googling for OBdata is a ufo forum.

not the kind of object oriented source you want to be taking cues from i think

leftist heap
Feb 28, 2013

Fun Shoe

Barnyard Protein posted:

is there a sane way to use java classes generated by an .xsd xml schema as the data model in a desktop application? as a test, i injected an EventBus.post into the generated classes setters, and then put corresponding event handlers in the viewer, but it feels... not right

it doesn't feel right because it's probably dumb. why would pub sub logic be in your POJOs? don't do that.

Jerry Bindle
May 16, 2003

rrrrrrrrrrrt posted:

it doesn't feel right because it's probably dumb. why would pub sub logic be in your POJOs? don't do that.

who does the publishing then? the code that is making changes to the model (ie the controller)?

comedyblissoption
Mar 15, 2006

gonadic io posted:

jesus loving christ thanks microsoft for convincing me that i was going insane: http://stackoverflow.com/questions/5918534/why-cant-i-add-a-subfolder-in-a-f-project
F#, a programming language with worse file and function definition janitoring than C

comedyblissoption
Mar 15, 2006

if you search online more about this F# limitation, youll find a bunch of stockholmed programmers defending it as making your code better because this is obviously the only way to prevent cycles in your definitions

Soricidus
Oct 21, 2010
freedom-hating statist shill
I like the way java does file structure. one public class per file, one package per directory, names must match. it has downsides too and the visibility rules could be better but whenever I read about poo poo like f# I realise how good I have it

Adbot
ADBOT LOVES YOU

leftist heap
Feb 28, 2013

Fun Shoe

Barnyard Protein posted:

who does the publishing then? the code that is making changes to the model (ie the controller)?

sounds fine

  • Locked thread