gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Passing an array as the argument to a function (C) *FIXED*

#109974 - misterDtD - Thu Nov 23, 2006 9:00 pm

Sorry if my terms don't make sense to you, I'm from other programming languages.

I made a bit of code that takes a palette from an array and fades it in (from black), I want to make it a function (I think they call them marcos in C {?} ) so I don't have to write the same 5 lines of code every time I want to fade the palette, but I can't figure out how to pass the array to the function.

Please help.

EDIT: Here's the code if it help (People who want palette fading, feel free to take.) (inpal is the palette to use)
Code:
void fadein(const u16 inpal[])
{
int i,h,r,g,b;
for(h=31;h>=0;h-=1)
{
 for(i=0;i<256;i++)
 {
  r=color_get_red(inpal[i])-h;
  if (r<0) {r=0;}
  g=color_get_green(inpal[i])-h;
  if (g<0) {g=0;}
  b=color_get_blue(inpal[i])-h;
  if (b<0) {b=0;}
   
  PAL[i]=color_rgb(r,g,b);
 }
}
}


Here's the code that corresponds with the color_* functions:
Code:
inline u16 color_rgb(u32 r, u32 g, u32 b) {return (r&31) | ((g&31)<<5) | ((b&31)<<10);}
inline u32 color_get_red(u16 c) {return c&0x1F;}//Gets the red of an image.
inline u32 color_get_green(u16 c) {return (c>>5)&0x1F;}//Gets the green of an image.
inline u32 color_get_blue(u16 c) {return (c>>10)&0x1F;}//Gets the blue of an image.


~DtD
(PS, I'm kinda new to C)

Never mind, I got it fixed, just had to add a [] after the argument, but now I get a message saying "warning: passing argument 1 of 'fadein' disgards qualifiers from pointer target type"
It still works, but I would like to remove this notice.


Last edited by misterDtD on Fri Nov 24, 2006 12:18 am; edited 2 times in total

#109979 - tepples - Thu Nov 23, 2006 10:27 pm

If you aren't ever writing to inpal[] from within fadein(), then try changing
Code:
void fadein(u16 inpal[])

to
Code:
void fadein(const u16 inpal[])

_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#109982 - misterDtD - Fri Nov 24, 2006 12:17 am

Thanks tepples, now the warnings are gone =)

~DtD

#110028 - Peter - Fri Nov 24, 2006 12:55 pm

misterDtD wrote:
Here's the code if it help (People who want palette fading, feel free to take.)

I recommend to rather take a look at the blending functions Cearn shared in this post.