Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


Transparent png not transparent with sdl_image


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 SDL_jesse   Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 March 2012 - 04:33 AM

I created a png in photoshop with a transparent background, and when I load up the png it has transparency. But when I load it into my game using SDL_image it appears white where it should be transparent.I am not use SDL_BlitSurface(),I use getPiexl() and putPiexl() instead,here is my code:
#include <iostream>
#include <string>
#include "SDL/SDL_image.h"
using namespace std;


void putPixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

switch(bpp) {
case 1:
*p = pixel;
break;

case 2:
*(Uint16 *)p = pixel;
break;

case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;

case 4:
*(Uint32 *)p = pixel;
break;
}
}

Uint32 getPixel(SDL_Surface *surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

switch(bpp) {
case 1:
return *p;

case 2:
return *(Uint16 *)p;

case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;

case 4:
return *(Uint32 *)p;

default:
return 0; /* shouldn't happen, but avoids warnings */
}
}



int main(int argc, char *argv[]) {

SDL_Init(SDL_INIT_VIDEO);
SDL_Surface *pScreen = ::SDL_SetVideoMode(640, 480, 0, SDL_SWSURFACE | SDL_DOUBLEBUF);
SDL_Surface *image = IMG_Load("jbutton.png");
SDL_DisplayFormatAlpha(image);
for(int i=0;i<image->w;i++)
{
for(int j=0;j<image->h;j++)
{

Uint32 color=getPixel(image,i,j);
putPixel(pScreen,i,j,color);


}
}
// SDL_BlitSurface(img, 0, video, 0); //this will be ok
SDL_Flip(pScreen);
SDL_Delay(5000);

SDL_Quit();
return 0;
}


Sponsor:

#2 SDL_jesse   Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 March 2012 - 04:36 AM

Does any one has ideas about it ? thank you for your reply!

#3 munchor   Members   -  Reputation: 101

Like
0Likes
Like

Posted 03 March 2012 - 04:45 AM

As I recall, you can set alpha:


//SDL_SetAlpha(surface_name, SDL_SRCALPHA, SDL_ALPHA_TRANSPARENT);
//SDL_DisplayFormatAlpha(surface_name);

However, I never needed to use it to get true transparency.

#4 SDL_jesse   Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 March 2012 - 05:33 AM

sorry , I am a freshman in SDL ,it does not help,the main code is here,:
int main(int argc, char *argv[]) {

SDL_Init(SDL_INIT_VIDEO);
SDL_Surface *pScreen = ::SDL_SetVideoMode(640, 480, 0, SDL_SWSURFACE | SDL_DOUBLEBUF);
SDL_Surface *image = IMG_Load("jbutton.png");
SDL_SetAlpha(image, SDL_SRCALPHA, SDL_ALPHA_TRANSPARENT);
SDL_Surface *screen = SDL_DisplayFormatAlpha(image);
for(int i=0;i<image->w;i++)
{
for(int j=0;j<image->h;j++)
{
Uint32 color=getPixel(screen,i,j);
putPixel(pScreen,i,j,color);

}
}
// SDL_BlitSurface(img, 0, video, 0); //this will be ok
SDL_Flip(pScreen);
SDL_Delay(5000);

SDL_Quit();
return 0;
}


#5 munchor   Members   -  Reputation: 101

Like
0Likes
Like

Posted 03 March 2012 - 05:44 AM

You shouldn't loop the image and draw it pixel by pixel...


SDL_RWops *gold_rwops = SDL_RWFromFile("img/gold.png", "rb");
SDL_Surface *gold = IMG_LoadPNG_RW(gold_rwops);

SDL_Rect tile = {250 + x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE};
SDL_BlitSurface(gold, NULL, screen, &tile);

Here's how I did it.

#6 SDL_jesse   Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 March 2012 - 05:55 AM

thank you for that , but I should change the size of the image ,so I can't use SDL_BlitSurface() to do this in my game.I have to draw it pixel by pixel.
the code above can work with PNG-8 ,but it can't work with PNG-24 or PNG-32

#7 munchor   Members   -  Reputation: 101

Like
0Likes
Like

Posted 03 March 2012 - 06:02 AM

When you use SDL_BlitSurface you can also change the size of the image...

Maybe you should take a look at http://sdl.beuc.net/sdl.wiki/SDL_BlitSurface

#8 SDL_jesse   Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 March 2012 - 06:20 AM

not only size , I have to flip the image also and some other things according to the game logic . Can I use putPiexl() and getPiexl() to do this?

#9 Storyyeller   Members   -  Reputation: 212

Like
0Likes
Like

Posted 17 March 2012 - 09:05 PM

SDL doesn't handle blitting to a surface with alpha properly (it ignores alpha of the destination surface), so alpha to alpha blits have to be done pixel by pixel. Other than that, you should be fine with using regular blits.
I trust exceptions about as far as I can throw them.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS