[SDL] VRAM, RAM & transparency

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

Recommended Posts

Hi ! I'm french so excuse my faults please :) Surfacetemp is a Software SDL_Surface* (RAM) Vramtemp is a Hardware SDL_Surface* (VRAM), filled in black (0,0,0) I've got a SDL_LoadBMP() who loades a bitmap in Surfacetemp. I want to copy this bitmap in Vramtemp and set it the Color Key (0,255,0)[GREEN] In the first time, I tried to blit Surfacetemp on Vramtemp, then set the color key of Vramtemp :
SDL_BlitSurface(Surfacetemp,NULL,Vramtemp,&Recttemp);
SDL_SetColorKey(Vramtemp, SDL_SRCCOLORKEY, SDL_MapRGB(Vramtemp->format, 0, 255, 0));


The bitmap is well blitted on Vramtemp, but the colorkey is not transparent in game ! Then I tried to set the colorkey green on Surfacetemp, to blit Surfacetemp on Vramtemp, then to set the colorkey black on Vramtemp :
SDL_SetColorKey(Surfacetemp, SDL_SRCCOLORKEY, SDL_MapRGB(Surfacetemp->format, 0, 255, 0));
SDL_BlitSurface(Surfacetemp,NULL,Vramtemp,&Recttemp);
SDL_SetColorKey(Vramtemp, SDL_SRCCOLORKEY, SDL_MapRGB(Vramtemp->format, 0, 0, 0));


This second method works, but I wonder why the first didn't work...This method doesn't satisfy me much :Is there a way to do it like I wanted to ? (I hope my english is understandable -___-) Thank you ! :)

Share on other sites
I'm thinking the problem is because the image loaded and the destination are not of the same format, so try something like this:
// Load the BMPSDL_Surface* Surfacetemp = SDL_LoadBMP("YourBMP");// Convert to the Vramtemp surface formatSDL_Surface* temp2 = SDL_ConvertSurface(Surfacetemp,Vramtemp->format,Vramtemp->flags );// Blit this onto the Vramtemp surfaceSDL_BlitSurface(Surfacetemp,NULL,Vramtemp,&Recttemp);// Now set the color keySDL_SetColorKey(Vramtemp, SDL_SRCCOLORKEY, SDL_MapRGB(Vramtemp->format, 0, 255, 0));

Here is the doc page for SDL_ConvertSurface. I'm not sure if the Vramtemp->flags is correct, so you may have to mess around with that.

Another option is to make sure that the Vramtemp is of the same display format as the main screen, then convert the BMP into the main screens format, then blit, and set transparency.

Vramtemp = SDL_DisplayFormat( SDL_GetVideoSurface() );// Load the BMPSDL_Surface* Surfacetemp = SDL_LoadBMP("YourBMP");// Convert to the Vramtemp surface formatSDL_Surface* temp2 = SDL_DisplayFormat(SDL_GetVideoSurface());// Blit this onto the Vramtemp surfaceSDL_BlitSurface(Surfacetemp,NULL,Vramtemp,&Recttemp);// Now set the color keySDL_SetColorKey(Vramtemp, SDL_SRCCOLORKEY, SDL_MapRGB(Vramtemp->format, 0, 255, 0));

Here's the main doc function listing for reference for other functions. Good luck!

Share on other sites
I think I found a best solution using :

SDL_Surface*Vramtemp = SDL_AllocSurface(SDL_HWSURFACE|SDL_SRCCOLORKEY, Ramtemp->w, Ramtemp->h, 24, 0, 255, 0, 255);

Share on other sites
Unless I'm misstaken, you use 24 bits, which is slow (not sure how slow, but just about everyone will warn you about it).. Make sure all surfaces are the same format..

Share on other sites
I've been down this road before. Your original surface is colorkeyed, so when you blit it onto the second surface it only blits the non-colorkeyed pixels. The colorkeyed pixels will be black or randomized on your destination surface. You have to turn off colorkeying when copying surfaces.

psuedo code
function CopySurface(origSurface, destSurface){    origColorKey = origSurface->colorkey;    turnOffColorkey(origSurface);    blitSurface(origSurface, destSurface)    // set colorkey    origSurface->colorKey = origColorKey;    destSurface->colorKey = origColorKey;}

Share on other sites
€dit : The code was bugged, sorry :
[Edited by - Ptival on June 15, 2005 3:41:52 AM]

Share on other sites
Finally I didn't succeed...

Has anyone ever load an image in VideoRAM and Colorkey-d it ?

Share on other sites
Any reason why you don't just use SDL_DisplayFormat on the loaded graphics and forget about trying to explicitly handle video memory?

Share on other sites
Well, I tried this :

http://www.rafb.net/paste/results/Hv551q50.html

But the colorkey does'nt work :(Although if I force the loading in RAM, the color key is well put)

€dit -> Finally it works !

I've just deleted the second ColorKey :)

Thanks to evetyone ;)

[Edited by - Ptival on June 15, 2005 11:36:09 AM]

• 16
• 9
• 13
• 41
• 15