The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL_BlitSurface passed a null surface
richardm


Joined: 06 May 2016
Posts: 3
I have scanned in a page of text. I have then circled a letter on it. Now II want to blit a rectangle containing that letter to a surface then save that surface containing the letter as a bitmap. The error message is SDL_UpperBlit: passed a NULL surface. Only the destination surface is NULL. I have been trying to fix this for days. Any help is appreciated

Here is the code.

//When you have a circled letter this saves the item
void SaveaLetter(int mini, int maxi, int minj, int maxj){
int IconWidth;
int IconHeight;
int total;
int k;
char iconname[32];
SDL_Rect src, dest;
SDL_Surface *tempSurface = NULL;
SDL_Surface *fromSurface = NULL;

fromSurface = SDL_GetWindowSurface(gWindow);
//SDL_UnlockSurface(fromSurface);
//SDL_UnlockSurface(tempSurface);

printf("Printing circled letter mini %d minj %d maxi %d maxj %d \n", mini, minj, maxi, maxj);
IconWidth = maxi - mini + 1;
IconHeight = maxj - minj + 1;
printf("Save a Letter IconHeight %d IconWidth %d itemnumber %d \n", IconHeight, IconWidth, itemnum);

//Save circled icon
itemnum++;
sprintf(iconname, "newitem%04d.bmp", itemnum);
printf("Save a Letter IconName %s itemnumber %d \n", iconname, itemnum);

// Set the surface display parameters that don't change. //
src.x = mini;
src.y = minj;
src.w = maxi - mini;
src.h = maxj - minj;

dest.x = 0;
dest.y = 0;
dest.w = 100;
dest.h = 100;

//SDL_LockSurface(image);
//SDL_LockSurface(bmpcircled);

k = SDL_BlitSurface(fromSurface, &src, tempSurface, NULL);
if (k < 0){
printf("Save a Letter Blit error %s \n", SDL_GetError());
}
if (fromSurface == NULL){
printf ("fromSurface is a null surface \n");
}
if (tempSurface == NULL){
printf ("tempSurface is a null surface \n");
}
// k = IMG_SavePNG(bmpcircled, "out.png");
k = SDL_SaveBMP(tempSurface, iconname);
if (k != 0){
printf("Unable to save screen bitmap k= %d error %s \n", k, SDL_GetError());
exit(EXIT_FAILURE);
}
SDL_FreeSurface(tempSurface);
SDL_FreeSurface(fromSurface);
//SDL_UnlockSurface(bmpcircled);
//SDL_UnlockSurface(image);

return;
}

When I run the code I get these results:
Save a Letter IconHeight 75 IconWidth 69 itemnumber 0
Save a Letter IconName newitem0001.bmp itemnumber 1
Save a Letter Blit error SDL_UpperBlit: passed a NULL surface
tempSurface is a null surface
Peter87


Joined: 16 Sep 2012
Posts: 6
SDL_BlitSurface will not automatically create a surface or anything. You'll have to create the surface you want to blit to before calling SDL_BlitSurface.

You should probably use the SDL_CreateRGBSurface to create the surface.

Code:
tempSurface = SDL_CreateRGBSurface(0,
                                   dest.w,
                                   dest.h,
                                   fromSurface->format->BitsPerPixel,
                                   fromSurface->format->Rmask,
                                   fromSurface->format->Gmask,
                                   fromSurface->format->Bmask,
                                   fromSurface->format->Amask);
richardm


Joined: 06 May 2016
Posts: 3
Great. That worked perfectly. I created a surface the exact size needed then saved the surface to an bmp file.