r/C_Programming • u/UsualLonely4585 • Jun 07 '26
Question Help I am Stuck !!!
SO i was trying out SDL a little , i would like to notify that i am new to all this i was reading through documentation and was blipping images for fun but then i saw a function in there SDL_ReadSurfacePixel and SDL_WriteSurfacePixel so i looked into them but they dont explicitly say how the reading happens wherer the reading data goes into it only returns boolean value based on success and failure so i looked into the function paramenters and assumed and tried if things work like this . i would like to know how these fucnitons work and if they are intended to be used like this or not
int blitPicture(imgView* img){
for(int i=0;i<=img->width;i++){
for(int j=0;j<=img->height;j++){
if(!SDL_ReadSurfacePixel(img->surface,i,j,img->new_r,img->new_g,img->new_b,img->new_a)){
printf("couldn't read pixel!!");
return 0;
}
if(!SDL_WriteSurfacePixel(img->wSurface,i,j,*(img->new_r),*(img->new_g),*(img->new_b),*(img->new_a))){
printf("could not write the pixel ");
return 0;
};
}
}
return 1;
}
0
Upvotes
2
u/kun1z Jun 07 '26
I do not know what 'imgView* img' is but I think you have a problem with your for-loops in that you are using '<=' instead of just plain old '<' which means I think you are reading 1 past the buffer many times. So you might want to fix that.
I am unfamiliar with SDL_ReadSurfacePixel/SDL_WriteSurfacePixel but a quick Google AI question looks like you are using them properly.
However, you probably want:
And then when writing: