Drawing transparent box [SDL]
Forum » Forums / Have Your Question Answered » Drawing transparent box [SDL]
started by: kake_fiskkake_fisk
on: 1257347617|%e %b %Y, %H:%M %Z|agohover
number of posts: 6
rss icon RSS: new posts
Drawing transparent box [SDL]
kake_fiskkake_fisk 1257347617|%e %b %Y, %H:%M %Z|agohover

I want to draw a black, but transparent box on the whole game screen.

void draw_messagebox(int x, int y, int width, int height, string msg)
{
    SDL_Rect background;
    background.x = x;
    background.y = y;
    background.w = width;
    background.h = height;
    SDL_Rect fade;
    fade.x = 0;
    fade.y = 0;
    fade.w = MENU_WIDTH;
    fade.h = MENU_WIDTH;
    apply_surface(0,0,bac_menu,screen);
    SDL_FillRect(bac_fade, &fade, SDL_MapRGB(screen->format, 0x0, 0x0, 0x0));
    SDL_SetAlpha(bac_fade, SDL_SRCALPHA, 200);
    apply_surface(0,0,bac_fade,screen);
    SDL_FillRect(screen, &background, SDL_MapRGB(screen->format, 0xFF, 0, 0));
    SDL_Surface *msg_message = TTF_RenderText_Blended(fnt_score, msg.c_str(), BLACK);
    apply_surface(x+16,y+16,msg_message,screen);
}

But the program returns 3, that is the error code that says TTF_Init() failed.
Maybe something doesn't work together or something, I don't know.
But it's kinda odd because TTF_Init() is called right after the window is created, but the error doesn't occur before the draw_messagebox() function is called.

And don't care about the bad way I am doing this, I know I don't need to update the message all the time and such, but I'll fix that later. ;)

unfold Drawing transparent box [SDL] by kake_fiskkake_fisk, 1257347617|%e %b %Y, %H:%M %Z|agohover
Re: Drawing transparent box [SDL]
u9u9 1257404892|%e %b %Y, %H:%M %Z|agohover

void draw_messagebox(int x, int y, int width, int height, string msg)
{
SDL_Rect background; background.x = x; background.y = y; background.w = width; background.h = height;
SDL_Rect fade; fade.x = 0; fade.y = 0; fade.w = MENU_WIDTH; fade.h = MENU_WIDTH;

apply_surface(0, 0, bac_menu, screen);
SDL_FillRect(bac_fade, &fade, SDL_MapRGB(screen->format, 0x0, 0x0, 0x0));
SDL_SetAlpha(bac_fade, SDL_SRCALPHA, 200);

apply_surface(0, 0, bac_fade, screen);
SDL_FillRect(screen, &background, SDL_MapRGB(screen->format, 0xFF, 0, 0));
SDL_Surface *msg_message = TTF_RenderText_Blended(fnt_score, msg.c_str(), BLACK);
apply_surface(x+16, y+16, msg_message, screen);
}
}}

Hi Kake,

You should know, TTF_RenderText_Blended creates a new surface every time, which you don't free. This will have you running out of memory quickly. I can assure you the program does not return 3 from here. Either it crashes horribly or it doesn't. What is your question actually? Do you want to know what makes your progrram crash? Try removing everything inside the function and then activate one line at a time.

unfold Re: Drawing transparent box [SDL] by u9u9, 1257404892|%e %b %Y, %H:%M %Z|agohover
Re: Drawing transparent box [SDL]
kake_fiskkake_fisk 1257442148|%e %b %Y, %H:%M %Z|agohover

I made it static, the same thing is still happening.
But I tried commenting out line for line to see what is causing the crash, and it was 2 lines that did not work.

SDL_FillRect(bac_fade, &fade, SDL_MapRGB(screen->format, 0x0, 0x0, 0x0));
SDL_SetAlpha(bac_fade, SDL_SRCALPHA, 200);

But my question is actually, how can I draw a transparent black box on the screen? I just showed what I had already tried.

unfold Re: Drawing transparent box [SDL] by kake_fiskkake_fisk, 1257442148|%e %b %Y, %H:%M %Z|agohover
Re: Drawing transparent box [SDL]
u9u9 1257532603|%e %b %Y, %H:%M %Z|agohover

I found this book on google. It looks interesting and might help you drawing translucent rectangles:

http://forum.oddthought.com/Audio/Rapthread/Game.Design.eBooks.Pack/Programming/Focus%20on%20SDL.pdf

unfold Re: Drawing transparent box [SDL] by u9u9, 1257532603|%e %b %Y, %H:%M %Z|agohover
Re: Drawing transparent box [SDL]
u9u9 1257533029|%e %b %Y, %H:%M %Z|agohover

Another suggestion: Have you tried setting the alpha color as well:

SDL_FillRect(bac_fade, &fade, SDL_MapRGBA(screen->format, 0x0, 0x0, 0x0, 128));

?

unfold Re: Drawing transparent box [SDL] by u9u9, 1257533029|%e %b %Y, %H:%M %Z|agohover
Re: Drawing transparent box [SDL]
kake_fiskkake_fisk 1257555245|%e %b %Y, %H:%M %Z|agohover

Thank you, the book together with a topic from gamedev and the doc wiki helped me solve it. :D
I had to create an surface using the function SDL_CreateRGBSurface(). And I also had to use the source surfaces format, instead of the destinations'.

This is my code, in case somebody else have the same problem:

static SDL_Surface *bac_fade = SDL_CreateRGBSurface(SDL_SWSURFACE, 640, 480, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
SDL_FillRect(bac_fade, &fade, SDL_MapRGBA(bac_fade->format, 0x0, 0x0, 0x0, 200));
SDL_SetAlpha(bac_fade, SDL_SRCALPHA, 200);
apply_surface(0,0,bac_fade,screen);
unfold Re: Drawing transparent box [SDL] by kake_fiskkake_fisk, 1257555245|%e %b %Y, %H:%M %Z|agohover
new post