Skip to main content
gamedev.net
🔒 Locked

SDL_BlitSurface Problem

Started by kcowolf 1 reply 650+ views
Original Post
kcowolf
kcowolf
Hey everyone. I'm working on a game project using SDL and Dev-C++. I'm learning from the Cone3D tutorials, but I'm having a problem I can't seem to solve. The goal of this part of the program is to copy a portion of the screen surface and draw it in another location. However, whenever I run the program, it doesn't seem to copy anything from screen to testbox. I can blit other images onto the screen, but I can't find a successful way to copy from one part of the screen to another part. I am including part of the code below. The line that doesn't seem to function as I would like has been marked with an appropriate comment. I've tried to debug that line by using "if (SDL_BlitSurface... )". It returns zero (successful blit, as far as I know) each time. If anyone has a way I could fix this, I would certainly appreciate it. Benjamin Stauffer (kcowolf@gmail.com) Computer Science University of Missouri-Rolla --- Begin portion of code: screen=SDL_SetVideoMode(800,600,32,SDL_SWSURFACE|SDL_RESIZABLE|SDL_HWPALETTE); SDL_Surface *back, *temp1, *temp2; temp1 = SDL_LoadBMP("back.bmp"); temp2 = SDL_DisplayFormat(temp1); SDL_FreeSurface(temp1); back = temp2; SDL_Rect backdest; backdest.x = 0; backdest.y = 0; SDL_BlitSurface(back, NULL, screen, &backdest); *snip* SDL_Surface *testbox; SDL_Rect src; src.x = 400; src.y = 200; src.w = 50; src.h = 50; SDL_Rect dest; dest.x = 0; dest.y = 0; SDL_BlitSurface(screen, &src, testbox, NULL); // THIS LINE DOESN'T WORK SDL_BlitSurface(testbox, NULL, screen, &dest); // Paste in new location
Drew_Benton
Drew_Benton
When you use SDL surfaces, you must create the surface yourself. When you have: SDL_Surface *testbox;, that should be: SDL_Surface *testbox = SDL_CreateRGBSurface(SDL_HWSURFACE, Width, Height, 32, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask); SDL_FillRect(testbox, 0, 0);.

By doing that you create an empty surface with the specified Width and Height, and then clear it with black. Then you can blit to it, then continue on from that. Key thing though is that you only call the SDL_CreateRGBSurface once to create the surface then call SDL_FreeSurface on it at the end.
kcowolf
kcowolf
Thank you for the quick response! Looks like it works perfectly.

Benjamin Stauffer (kcowolf@gmail.com)
Computer Science
University of Missouri-Rolla

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.