Applying one surface to another (SDL)

Started by
3 comments, last by Plethora 16 years, 6 months ago
What am I missing here? Given this code:


void showWindow(SDL_Surface *screen)
    {
        temp = loadImage("window.png");
        font = TTF_OpenFont("Explosive.ttf", 20);
        temp2 = TTF_RenderText_Solid(font, "Summon", textColor);

        SDL_Color textColor = {200, 0, 100};
        apply_surface(window.x, window.y, temp, screen, NULL);
        apply_surface(10, 25, temp2, temp, NULL);

        SDL_FreeSurface(temp);
        SDL_FreeSurface(temp2);

        TTF_CloseFont(font);
    }

Why can't I get the text to show up on top of the window? I've been looking at tutorials for awhile and haven't found anything that relates to this directly, though I'm probably just dumb and missing something obvious.

I'm working on a game! It's called "Spellbook Tactics". I'd love it if you checked it out, offered some feedback, etc. I am very excited about my progress thus far and confident about future progress as well!

http://infinityelephant.wordpress.com

Advertisement
I think it might be because you are freeing the surface at the end of the function. Try only freeing the surfaces at the end of the program.
Your calls to apply_surface are in the wrong order. The way you're doing it, first temp gets drawn onto screen, then temp2 gets drawn onto temp. temp2 will not be drawn on screen because at the time temp is applied to screen, temp2 has not been applied to it. So try switching the order of the apply_surface calls.
Does this mean that you're loading window.png every time the screen is redrawn? That's a bad idea, because disk access is slow. Load the image once, use it as long as it needs to be used, and only then remove it. The same goes for fonts.
Create-ivity - a game development blog Mouseover for more information.
Na I just wrote up that code quickly to be all inclusive (so you guys could see if I was leaving something out).

I actually got it working another way (but will probably go back and mess around with it some more now) where I just draw the text to screen after everything else but I wrote up a helper function to always apply the text in relation to the window (so it appears to be part of the window surface itself). It works, but its making more work for me now that I'm trying to recognize mouse clicks on the surface.

I'm working on a game! It's called "Spellbook Tactics". I'd love it if you checked it out, offered some feedback, etc. I am very excited about my progress thus far and confident about future progress as well!

http://infinityelephant.wordpress.com

This topic is closed to new replies.

Advertisement