• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
munchor

SDL Image Transparency

7 posts in this topic

I know there are already many topics on this, but I need some help with this, since I any of the snippets/suggestions given in other topics worked for me.


[code]
SDL_RWops *gold_rwop = SDL_RWFromFile("img/gold.png", "rb");
SDL_Surface *gold = IMG_LoadPNG_RW(gold_rwop);[/code]


This is the code I'm using to load a .png file. And this is the code I am using to display it:


[code]
// TILE_SIZE is defined to 25

SDL_SetAlpha(gold, SDL_SRCALPHA, SDL_ALPHA_TRANSPARENT);
SDL_DisplayFormatAlpha(gold);
SDL_Rect tile = {250 + x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE};
SDL_BlitSurface(gold, NULL, screen, &tile);[/code]

However, the image doesn't have transparency, the transparent part is just black.

Am I doing anything wrong? Thanks.

EDIT: I don't have any more SDL_Image related code, so I might be missing something (do I have to init something?)
0

Share this post


Link to post
Share on other sites
Have you tried blitting it without calling SDL_SetAlpha on the surface? Note that your call to SDL_DisplayFormatAlpha simply leaks memory, as you do not capture the return value.
0

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1324160859' post='4894890']
Have you tried blitting it without calling SDL_SetAlpha on the surface? Note that your call to SDL_DisplayFormatAlpha simply leaks memory, as you do not capture the return value.
[/quote]

I have tried blitting it without SetAlpha, and transparency worked. (I drew a brown block first, and then the 'gold' surface, and it worked). Thanks a lot! I don't get it though, everybody else fixed their problems by setting alpha, and the opposite happened with me.
0

Share this post


Link to post
Share on other sites
IMG_Load prepares the image such that it is ready to blit. So you don't need to do anything special to get alpha transparency here. However, if you want the fastest blit, you might still want to (correctly) use SDL_DisplayFormatAlpha().

Anyway, that is the reason I suggested you try doing nothing to the image first. However I don't understand the behaviour you are describing. I have created a simple test program:
[code]
#include <iostream>
#include <cstdlib>

#include "SDL.h"
#include "SDL_image.h"

int main(int, char**)
{
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cerr << "Failed to initialise SDL: " << SDL_GetError() << '\n';
return 1;
}

std::atexit(&SDL_Quit);

SDL_Surface *screen = SDL_SetVideoMode(800, 600, 0, SDL_SWSURFACE);
if(!screen)
{
std::cerr << "Failed to set video mode: " << SDL_GetError() << '\n';
return 1;
}

SDL_Surface *image = IMG_Load("foo.png");
if(!image)
{
std::cerr << "Failed to load imae: " << IMG_GetError() << '\n';
return 1;
}

#if 1
SDL_Surface *temp = SDL_DisplayFormatAlpha(image);
if(temp)
{
std::swap(temp, image);
SDL_FreeSurface(temp);
}
else
{
std::cerr << "Failed to format image to display: " << SDL_GetError() << '\n';
return 1;
}

#if 1
int result = SDL_SetAlpha(image, SDL_SRCALPHA, SDL_ALPHA_TRANSPARENT);
if(result < 0)
{
std::cerr << "Failed to set image alpha: " << SDL_GetError() << '\n';
return 1;
}
#endif
#endif

int x = 0;
int y = 0;
int vx = 1;
int vy = 1;
bool running = true;
while(running)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
running = false;
}
else if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)
{
running = false;
}
}

x += vx;
y += vy;

if(x < 0 || x + image->w > screen->w)
{
vx *= -1;
}

if(y < 0 || y + image->h > screen->h)
{
vy *= -1;
}

SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0x00, 0x00, 0xff));
SDL_Rect dest = { x, y };
SDL_BlitSurface(image, NULL, screen, &dest);
SDL_Flip(screen);
}

return 0;
}

[/code]
You can use the preprocessor to enable the call to SDL_DisplayFormatAlpha(), and SDL_SetAlpha(). On my system, it looks the same regardess of whether none, one or both are enabled.
0

Share this post


Link to post
Share on other sites
My problem was in another part of the code. Either way, how can I access different parts of an image (like if I have a spritesheet), how to access different parts of the image?
0

Share this post


Link to post
Share on other sites
[quote]
My problem was in another part of the code.
[/quote]
Why is why making a minimal example is really handy. It can isolate "SDL problems" from "weird bugs somewhere in your code". In making such a minimal example, you often will find the problem yourself.

[quote]
Either way, how can I access different parts of an image (like if I have a spritesheet), how to access different parts of the image?
[/quote]
You can specify a "source rectangle" as the second parameter to SDL_BlitSurface(). The image data from the source surface at the given x,y and with the given width and height (clipping will be performed) will be rendered to the destination surface at the given x and y position (the destination with and height are ignored).
0

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1324250295' post='4895118']
[quote]
My problem was in another part of the code.
[/quote]
Why is why making a minimal example is really handy. It can isolate "SDL problems" from "weird bugs somewhere in your code". In making such a minimal example, you often will find the problem yourself.

[quote]
Either way, how can I access different parts of an image (like if I have a spritesheet), how to access different parts of the image?
[/quote]
You can specify a "source rectangle" as the second parameter to SDL_BlitSurface(). The image data from the source surface at the given x,y and with the given width and height (clipping will be performed) will be rendered to the destination surface at the given x and y position (the destination with and height are ignored).
[/quote]

Very true, writing some code, in a new file helps a lot. I often do it, but since this was a very common problem, I immediately deduced it was an alpha problem, and not a code structure problem (which it ended up being).

Yes, I just usually put NULL when blitting a surface, but now I understand what that argument is for, thanks a lot for all the help!

When I started using SDL, I was told there was not much information on this library, and that I'd better use OpenGL, but I'm actually finding everything plain simple and straightforward.
0

Share this post


Link to post
Share on other sites
[quote]
When I started using SDL, I was told there was not much information on this library, and that I'd better use OpenGL, but I'm actually finding everything plain simple and straightforward.
[/quote]
SDL is very easy to use, it is one of the main selling points. I suspect the opposite would be true, I think most beginners to both would encounter more trouble getting OpenGL up and running than SDL.
0

Share this post


Link to post
Share on other sites

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  
Followers 0