What on earth is wrong? Transparent PNGs aren't transparent [SDL]


Burbruee

Member
Joined
Sep 28, 2006
Messages
302
Location
Skåne, Sweden.
Website
Visit site
Hi,


I ordered a pandora last night and got so excited I started coding all night long. :ph34r:


Using SDL, and SDL_image respectively I can't seem to get transparency to work. I've used SDL in the past to work with transparent images, and never had an issue.


I've looked over my code again and again, even copied my loading code straight from lazyfoo. But it won't work so there must be something else causing this.


At first I thought maybe some setting in Photoshop prevented the transparency from being saved or something like that, but then I snagged another image from a project I worked on years ago and it has the same issue and there's nothing wrong with that image.


Using IMG_Load to load the png, linking with SDL_image.lib (using visual studio) I just don't see what's wrong.


Ends up looking like this:


201209301622-the_troll_transparent.jpg



Why does it get black around the sprite..? The image IS transparent..


Some code..


main.cpp:



Code:
#include "engine.h"

#include "timer.h"

#include "sprite.h"

#include <iostream>

#include <fstream>

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

{

   //Create the window and stuff


   Engine *engine = new Engine();

   engine->Create();


   //Player sprite

   Sprite spr(engine);

   //LoadTexture runs engine->load_image which is same as lazy_foo with IMG_Load for sdl_image

   spr.LoadTexture(engine, "Textures/player1.png");


   //Color for background used later

   Uint32 col = NULL;


   while (engine->isRunning)

   {

      while (SDL_PollEvent(&engine->event))

      {

         if (engine->event.type == SDL_QUIT)

         {

            engine->isRunning = false;

         }

         if (engine->event.type == SDL_KEYDOWN)

         {

            switch(engine->event.key.keysym.sym)

            {

               case SDLK_ESCAPE:

               engine->isRunning = false;

            }

         }

      }


      //This draws the gradient background, should put this in engine class

      for (int y = 0; y < engine->SCR_H; y++)

      {

         col = SDL_MapRGB(engine->screen->format, 0, 0, 0 + (y * 160 / engine->SCR_H));

         for (int x = 0; x < engine->SCR_W; x++)

         {

            engine->DrawPixel(engine->screen, x, y, col);

         }

      }


      // Draw player, same as apply_surface on lazyfoo

      engine->Draw(spr.getX(), spr.getY(), spr.player, engine->screen);


      //Update screen

      SDL_Flip(engine->screen);

   }


   delete engine;

   return 0;

}

engine probably don't need to be a pointer someone else pointed out, I'm still learning the language and cooked this up at 5 AM so don't complain. ;)


Appreciate any help.. Thanks.
 
Sorry for double, indentation screws up when I try to edit for some reason.


engine.cpp



Code:
void Engine::Draw(int x, int y, SDL_Surface *source, SDL_Surface *destination)

{

   SDL_Rect temp;


   temp.x = x;

   temp.y = y;


   SDL_BlitSurface(source, NULL, destination, &temp);

}


SDL_Surface* Engine::load_image( std::string filename )

{

   //The image that's loaded

   SDL_Surface* loadedImage = NULL;


   //The optimized image that will be used

   SDL_Surface* optimizedImage = NULL;


   //Load the image using SDL_image

   loadedImage = IMG_Load( filename.c_str() );


   //If the image loaded

   if( loadedImage != NULL )

   {

      //Create an optimized image

      optimizedImage = SDL_DisplayFormat( loadedImage );


      //Free the old image

      SDL_FreeSurface( loadedImage );

   }


   //Return the optimized image

   return optimizedImage;

}

sprite.cpp



Code:
void Sprite::LoadTexture(Engine* _eng, std::string texture)

{

   Engine* engine = _eng;


   player = engine->load_image("Textures/player1.png");


   //Set position, center on bottom of the screen. This shouldn't really be here, but it is for now.

   x = engine->SCR_W / 2 - player->w / 2;

   y = engine->SCR_H - player->h;

}
 
Last edited by a moderator:
Back
Top