# Problem

What is the problem with this function. SDL crusges when o use it:

void findcolor()
{
Slock(back);
Uint32 cur;
Uint8 r1,b1,g1;
int x,e,i;
for(e=0;e<shag;e++)
{
for(i=0;i<height; i++)
{
for(x=0;x<kolvo; x++)
{
cur=getpixel(back,abs((x)+e),-(abs(sin(x/20))*-i)+960/2);
SDL_GetRGB(cur,fmt,r,g,b);
color[e][i][x]=(*r);
color[e][i][x]=(*g);
color[e][i][x]=(*b);
}
}
}
Sulock(back);
color2=250;
color2=236;
color2=40;
for(e=0; e<shag; e++)
{
for(x=0; x<kolvo; x++)
{
for(i=0; i<50; i++)
{
r1=Smooth(250,232,i,50);
g1=Smooth(239,109,i,50);
b1=Smooth(40,26,i,50);
color2[e][i][x]=r1;
color2[e][i][x]=g1;
color2[e][i][x]=b1;
}
}
}
for(e=0; e<shag; e++)
{
for(x=0;x<kolvo; x++)
{
color2[e][x]=232;
color2[e][x]=109;
color2[e][x]=26;
}
}
for(e=0; e<shag; e++)
{
for(i=0; i<shag; i++)
{
for(x=0; x<kolvo; x++)
{
r1=Smooth(232,color[e][i+52][x],i,68);
g1=Smooth(109,color[e][i+52][x],i,68);
b1=Smooth(26,color[e][i+52][x],i,68);
color2[e][i+52][x]=r1;
color2[e][i+52][x]=g1;
color2[e][i+52][x]=b1;
}
}
}
for(e=0;e<shag;e++)
{
for(i=66;i<69;i++)
{
for(x=0;x<kolvo;x++)
{
color2[e][i+52][x]=Smooth(0,color[e][i+52][x],i,68);
color2[e][i+52][x]=Smooth(0,color[e][i+52][x],i,68);
color2[e][i+52][x]=Smooth(0,color[e][i+52][x],i,68);
}
}
}
}

Where Smooth is:> Uint8 Smooth(Uint8 x,Uint8 y,Uint8 now, Uint8 total)

{
return(abs(x+(y-x)*now/total));
}

[something]

``````  for(x=0;x<kolvo; x++)
{
cur=getpixel(back,abs((x)+e),-(abs(sin(x/20))*-i)+960/2);
SDL_GetRGB(cur,fmt,r,g,b);
color[e][i][x]=(*r);
color[e][i][x]=(*g);
color[e][i][x]=(*b);
}
``````

[something]

How is color initialised? Aren’t you getting out of bounds?
Where exactly program hangs?
K.

Krata wrote:

[something]

`````` for(x=0;x<kolvo; x++)
{
cur=getpixel(back,abs((x)+e),-(abs(sin(x/20))*-i)+960/2);
SDL_GetRGB(cur,fmt,r,g,b);
color[e][i][x]=(*r);
color[e][i][x]=(*g);
color[e][i][x]=(*b);
}
``````

[something]

How is color initialised? Aren’t you getting out of bounds?
Where exactly program hangs?
K.

Here how color is initialised:

Uint8 color[shag][kolvo][height];
Uint8 color2[shag][kolvo][height];

Actually i do not know wher exactly it crashes! But when i add findcolor
in to my main, program crashes!

Actually i do not know wher exactly it crashes! But when i add
findcolor
in to my main, program crashes!

Looks like you need to brush up on your debugging skills. You
already found out that adding the findcolor function crashes your
program. Great! Now figure out where in findcolor it crashes. Run
the program in a debugger, set a breakpoint, and single-step through
the function. If you don’t have a debugger, put a few printf’s here
and there and try to narrow the problem down that way. Once you know
where exactly the crash occurs, it is usually fairly simply to
figure out why your program crashes. And with that knowledge,
coming up with a fix is probably really simple, in this case. Or, on
a bad day, you may be forced to rethink your whole algorithm ;-)–
Matthijs Hollemans
www.allyoursoftware.com

Matthijs Hollemans wrote:

Actually i do not know wher exactly it crashes! But when i add

findcolor

in to my main, program crashes!

Looks like you need to brush up on your debugging skills. You
already found out that adding the findcolor function crashes your
program. Great! Now figure out where in findcolor it crashes. Run
the program in a debugger, set a breakpoint, and single-step through
the function. If you don’t have a debugger, put a few printf’s here
and there and try to narrow the problem down that way. Once you know
where exactly the crash occurs, it is usually fairly simply to
figure out why your program crashes. And with that knowledge,
coming up with a fix is probably really simple, in this case. Or, on
a bad day, you may be forced to rethink your whole algorithm The algoritm is fine! I used it my old DOS program,whish was writteh on
TP7.0 then on TMT3.x.
So OK? i figured out where it crashes it os right over here:
SDL_GetRGB(cur,fmt,r,g,b);
So,
Uint32 cur, is ok!
r,g,b are uint8? thats soposed to be ok too.
SDL_PixelFormat *fmt. so it is ok too. Like i probably know what is
problem fmt, do not have any value, because i just eckared it,
and did not do anything! I hav to read docs 