• 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
brakhage

PNGs with transparency in SDL.

6 posts in this topic

I'm drawing some PNGs with transparency in SDL (using SDL_BlitSurface(), which works just fine), but I'm having problems with changing the alpha values (to fade the image in/out).
 
I've tried doing something like this for all the pixels:
 
*pixel = SDL_MapRGBA(image->format, r, g, b, a);
 
Changing the value for "a" in the call to SDL_MapRGBA() works as expected on PNG files without transparency, but if the image has transparency, I instead end up with this:
 
lolwut.jpg
 
Here, "a" was set to 255. Changing the alpha still works (changes the opacity), but it's like all the pixels have been replaced by black ones, except for the 40 lines of pixels at the top, which are white.
 
Any idea what's going on?
0

Share this post


Link to post
Share on other sites
Alright.

I set the video mode like this:
screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);

Then I load an image using SDL_image:
SDL_Surface* temp  = IMG_Load(file.c_str());
SDL_Surface* image = SDL_DisplayFormatAlpha(temp);

Then I draw it to the screen:
SDL_BlitSurface(source, NULL, destination, NULL);

This works fine, transparency and everything. If I load a .png without transparency (or a .bmp or whatever) I can play around with the alpha bits and it works fine, but like I said, if I try that with a .png with transparency, everything gets screwed up.
0

Share this post


Link to post
Share on other sites
I could start throwing guesses at you but it would be easier if you posted the whole code if it's not too long.
0

Share this post


Link to post
Share on other sites
#include <sdl.h>
#include <sdl_image.h>

void applySurface(SDL_Surface* source, SDL_Surface* destination, int x, int y)
{
    SDL_Rect offset;

    offset.x = x;
    offset.y = y;

    SDL_BlitSurface(source, NULL, destination, &offset);
}

SDL_Surface* loadImage(std::string file)
{
    SDL_Surface* temp = IMG_Load(file.c_str());
    SDL_Surface* image = SDL_DisplayFormatAlpha(temp);
    SDL_FreeSurface(temp);

    return(image);
}

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

    SDL_Surface* screen(nullptr);
    screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);

    SDL_ShowCursor(SDL_DISABLE);

    SDL_Surface* bg = loadImage("data/bg.png");
    SDL_Surface* img1 = loadImage("data/img1.png");

    bool done(false);
    SDL_Event event;

    while(!done)
    {
        while(SDL_PollEvent(&event))
        {
            if(event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE))
            {
                done = true;
            }
        }

        applySurface(bg, screen, 0, 0);

        static Uint8 alpha(0);

        for(int y(0); y < img1->h; ++y)
        {
            for(int x(0); x < img1->w; ++x)
            {
                Uint32* pixel = (Uint32*)img1->pixels + (x + y * img1->w);

                Uint8 r;
                Uint8 g;
                Uint8 b;
                Uint8 a;

                SDL_GetRGBA(*pixel, img1->format, &r, &g, &b, &a);
                *pixel = SDL_MapRGBA(screen->format, r, g, b, alpha);
            }
        }

        ++alpha;

        applySurface(img1, screen, 0, 0);

        SDL_Flip(screen);
    }

    SDL_Quit();

    return(0);
}

I've tried several different ways of changing the alpha value, but all with the same result.
0

Share this post


Link to post
Share on other sites
You are passing the wrong format to SDL_MapRGBA. screen->format should be img1->format.

When you set the alpha value for each pixel you are ignoring the alpha values that the image was saved as so you will see pixels that are not supposed to be seen.

To not destroy the original alpha values you could draw to the screen surface directly. If you change alpha to a floating point type and slowly change it from 0 to 1 and do something like this inside the loop
*screenPixel = SDL_MapRGBA(screen->format, r, g, b, a * alpha);
I think you will get the effect you want.

Another way I thought would work was to set the per-surface alpha value using SDL_SetAlpha but that doesn't appear to work.
2

Share this post


Link to post
Share on other sites

To not destroy the original alpha values you could draw to the screen surface directly. If you change alpha to a floating point type and slowly change it from 0 to 1 and do something like this inside the loop

*screenPixel = SDL_MapRGBA(screen->format, r, g, b, a * alpha);
I think you will get the effect you want.

 


Yes, that works. Thank you. smile.png

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