Sign in to follow this  

Unity SDL:Blitting surface-surface-screen

This topic is 4808 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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, &rect);
   //SDL_UpdateRect(tmpSurface, 0, 0, 200, 200); 
   //SDL_DisplayFormatAlpha(tmpSurface); 
   SDL_BlitSurface(tmpSurface, NULL, screen, &rect);
   //SDL_BlitSurface(s, NULL, screen, &rect);   
   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

Share this post


Link to post
Share on other sites
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);
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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, &rect);
SDL_BlitSurface(tmpSurface, NULL, screen, &rect);
SDL_UpdateRect(screen, 0, 0, 200, 200);

tviewtext tv;
exit(0);
}

Share this post


Link to post
Share on other sites
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 switch


int 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 window
screen = 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 title

SetUp();



//////////////////////////////////////////////////////////////////////////
// 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

}
else
GameLoop();

} // end message loop

CleanUp();
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 screen
if (background == NULL)
{
printf("Unable to load play screen\n");
return 1;
}


SDL_ShowCursor(SDL_DISABLE); // makes the cursor invisible

srand(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;
}

Share this post


Link to post
Share on other sites
@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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

This topic is 4808 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this