Skip to main content
gamedev.net
🔒 Locked

SDL:Blitting surface-surface-screen

Started by FirstStrike Oct 13, 2004 at 9:56 AM 12 replies 2.2k views
Original Post
FirstStrike
FirstStrike
Hi folks! I am new to SDL and ran into the same problem as: http://www.gamedev.net/community/forums/topic.asp?topic_id=87323 I want to copy a surface to another surface and then draw it on the screen, but all I get is a black window. What I am doing looks like this:

//screen and everything is initialized
SDL_Rect rect = {0, 0, 200, 200};
SDL_Surface* tmpSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, 640, 480, 8,
                                   rmask, gmask, bmask, amask);
				         
   SDL_Surface* s = font.renderBlended(t2, rect);          
   
   //label.output(screen, rect2);    
   SDL_DisplayFormatAlpha(s); 
   SDL_BlitSurface(s, NULL, tmpSurface, ▭);
   //SDL_UpdateRect(tmpSurface, 0, 0, 200, 200); 
   //SDL_DisplayFormatAlpha(tmpSurface); 
   SDL_BlitSurface(tmpSurface, NULL, screen, ▭);
   //SDL_BlitSurface(s, NULL, screen, ▭);   
   SDL_UpdateRect(screen, 0, 0, 200, 200);  

Would I blit s directly to the screen, everything works fine. Can anybody help on this one? Thx
happypirate
happypirate
is your screen surface doublebuffered?
try calling SDL_Flip(screen);
FirstStrike
FirstStrike
No, calling SDL_Flip doesn't save the day.
My screen is initializied with the following settings:

 SDL_Surface *screen;   int WINWIDTH = 640, WINHEIGHT = 480, BPP = 8;   screen = SDL_SetVideoMode( WINWIDTH,WINHEIGHT, BPP, SDL_SWSURFACE );   if( screen == NULL ) {        fprintf(stderr, "Couldn't set %ix%ix%i video mode: %s\n", WINWIDTH,WINHEIGHT,BPP, SDL_GetError());        exit(1);   }
Ryan Clark
Ryan Clark
Hm, does it work if you blit your surface directly to the screen, without the intermediate surface?

Need to make sure your surface actually has something in it before we go on a wild goose chase :)


Ryan
FirstStrike
FirstStrike
Yes, if I blit s directly to the screen (One of the outcommented lines), it works and I see a red textline on the screen, but with the intermediate surface it doesn't.

Has anyone else ever done this and has code which works?
FirstStrike
FirstStrike
Well, I have tried something new to make it more comprehensible
for everyone and changed it. Now I load an image, blitt it to another surface and then to the screen. Same result: It doesn't show up.

int main( int argc, char* argv[] ) {  if( SDL_Init(SDL_INIT_VIDEO)==(-1) ) {       printf("Could not initialize SDL: %s.\n", SDL_GetError());      exit(-1);   }     SDL_Surface *screen;   int WINWIDTH = 640, WINHEIGHT = 480, BPP = 8;   screen = SDL_SetVideoMode( WINWIDTH,WINHEIGHT, BPP, SDL_SWSURFACE );   if( screen == NULL ) {        fprintf(stderr, "Couldn't set %ix%ix%i video mode: %s\n", WINWIDTH,WINHEIGHT,BPP, SDL_GetError());        exit(1);   }   atexit(SDL_Quit);      SDL_Rect rect = {0, 0, 200, 200};          Uint32 rmask, gmask, bmask, amask;   /* SDL interprets each pixel as a 32-bit number, so our masks must depend       on the endianness (byte order) of the machine */#if SDL_BYTEORDER == SDL_BIG_ENDIAN    rmask = 0xff000000;    gmask = 0x00ff0000;    bmask = 0x0000ff00;    amask = 0x000000ff;#else    rmask = 0x000000ff;    gmask = 0x0000ff00;    bmask = 0x00ff0000;    amask = 0xff000000;#endif   SDL_Surface* tmpSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, 600, 400, 8,                                   rmask, gmask, bmask, amask);      SDL_Surface* s = SDL_LoadBMP("test.bmp");      SDL_BlitSurface(s, NULL, tmpSurface, ▭);      SDL_BlitSurface(tmpSurface, NULL, screen, ▭);   SDL_UpdateRect(screen, 0, 0, 200, 200);        tviewtext tv;   exit(0);}
Grellin
Grellin
Here is a small bit of code. Might help you a little.

#include <iostream>#include <stdlib.h>#include <time.h>#include <math.h>#include "SDL.h"using namespace std;int SetUp();int GameLoop();int CleanUp();int Timer;// Prototypes///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////SDL_Surface *screen, *background;SDL_Rect src, dest;Uint32 colorkey;SDL_Event event;enum // different states of game loop{GS_TITLE,};int gamestate = GS_TITLE; // Start the game loop in the GS_MAIN switchint main( int argc, char* argv[] ){if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO ) != 0){printf("Unable to initialize SDL: %s\n", SDL_GetError());return 1;}atexit ( SDL_Quit );//create a windowscreen = SDL_SetVideoMode ( 256 , 256 , 16, SDL_HWSURFACE | SDL_DOUBLEBUF );if (screen == NULL){printf("Unable to set video mode: %s\n", SDL_GetError());return 1;}SDL_WM_SetCaption("boo", NULL); // This sets the window titleSetUp();//////////////////////////////////////////////////////////////////////////// Set the program in an event loop.//////////////////////////////////////////////////////////////////////////for(;;){if( SDL_PollEvent(&event) != 0){switch (event.type){case SDL_MOUSEBUTTONDOWN:{if(event.button.button == 1){// left mouse button code}else if (event.button.button == 3){SDL_SaveBMP(screen, "screenshot.bmp"); // right button saves screen shot}}break;case SDL_KEYDOWN:{if(event.key.keysym.sym==SDLK_ESCAPE){return 0;}}break;case SDL_QUIT:{printf("Quit event. Bye. \n");exit(0);}break;}// end switch}elseGameLoop();} // end message loopCleanUp();return 0;}////////////////////////////////////////////////////////////////////////////////////////// SET UP :: This is where all the game graphics/sounds and fonts will be loaded////////////////////////////////////////////////////////////////////////////////////////int SetUp(){background = SDL_LoadBMP("tmp.bmp"); // Loads the main play screenif (background == NULL){printf("Unable to load play screen\n");return 1;}SDL_ShowCursor(SDL_DISABLE); // makes the cursor invisiblesrand(time(NULL));return 0;}///////////////////////////////////////////////////////////////////////////////////////// GAMELOOP :: Main game loop. This is where all the cool stuff happens///////////////////////////////////////////////////////////////////////////////////////int GameLoop(){	Timer = SDL_GetTicks();	switch(gamestate)	{	case GS_TITLE:	{		// title code goes here		src.x = 0;		src.y = 0;		src.w = background->w;		src.h = background->h;		dest.x = 0;		dest.y = 0;		dest.w = background->w;		dest.h = background->h;		SDL_BlitSurface(background, &src, screen, &dest);	}break;	default:	{	// you should never end up here	}break;}//test = SDL_GetTicks() - Timer;if(SDL_Flip(screen) == 0){}while((SDL_GetTicks() - Timer) <= 33){}	return 0;}///////////////////////////////////////////////////////////////////////////////// CLEAN UP :: Clears up the memory allocations at shutdown.///////////////////////////////////////////////////////////////////////////////int CleanUp(){	SDL_FreeSurface(screen);	SDL_FreeSurface(background);	SDL_ShowCursor(SDL_ENABLE);	return 0;}
Steven Bradley .:Personal Journal:. .:WEBPLATES:. .:CGP Beginners Group:. "Time is our most precious resource yet it is the resource we most often waste." ~ Dr. R.M. Powell
FirstStrike
FirstStrike
@PnP Bios
I have no event loop. I just placed a breakpoint in the last line.

@Grellin
You are doing what works fine for me as well:
You load a bmp and store it. Then you copy it to the screen.

What doesn't work is to copy it to an intermediate surface and then blit it to the screen.
The Rug
The Rug
Just a guess, but don't you have to set up a palette for an 8BPP surface? Maybe the palette for surface tmpSurface is empty...
the rug - funpowered.com
FirstStrike
FirstStrike
Well, I have tried it now with 16 bit color depth, but no, doesn't work. And don't forget, that the picture shows up wiht 8 bit color depth, if I blit it to the screen directly.
The Rug
The Rug
Hmm... it works for me when I set tmpSurface's BPP to 32, but no other value.

Edit: Scratch that.

Try this after creating tmpSurface:
tmpSurface->format->palette = screen->format->palette;


Or instead of screen use surface s... I think its because the temporary surface is created without a palette. My guess is the bitmap would be given a palette when loaded, not sure about where the screen gets it from though.

[Edited by - The Rug on October 14, 2004 5:13:39 AM]
the rug - funpowered.com

Topic Locked

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

Sign in to reply to this topic.