Sign in to follow this  
c4c0d3m0n

invalid conversion from const SDL_Rect*' to SDL_Rect*'

Recommended Posts

c4c0d3m0n    100
SDL_Surface* Font::renderText( std::string text ) const
{
    int width = this->getWidth( text );

    SDL_Surface* surface = SDL_CreateRGBSurface( SDL_SRCCOLORKEY,
                                                 width, chars[0].h,
                                                 bitmap->format->BitsPerPixel,
                                                 bitmap->format->Rmask,
                                                 bitmap->format->Gmask,
                                                 bitmap->format->Bmask,
                                                 0 );


    int current_char = 0;
    SDL_Rect rect;
    rect.x = 0;
    rect.y = 0;
    while( current_char < text.length() ) {
        SDL_BlitSurface( bitmap, &chars[text[current_char]], surface, &rect );
        rect.x += chars[text[current_char]].w;
        current_char++;
    }


    return surface;
}

Compiling: font.cpp
E:\Programming\Base-Devel-Physics\font.cpp: In member function `SDL_Surface* Font::renderText(std::string) const':
E:\Programming\Base-Devel-Physics\font.cpp:93: error: invalid conversion from `const SDL_Rect*' to `SDL_Rect*'
E:\Programming\Base-Devel-Physics\font.cpp:93: error:   initializing argument 2 of `int SDL_UpperBlit(SDL_Surface*, SDL_Rect*, SDL_Surface*, SDL_Rect*)'
.. Ugh I need Font::renderText() to be const, because I'm passing constant references of Font objects in certain functions that use this function. Theoretically, a renderText() function that renders to a extranous surface and returns that should be totaly okay with being const. I don't quite understand what I'm doing wrong.

Share this post


Link to post
Share on other sites
ToohrVyk    1596
Well, your function is (I assume, given the differing function names) attempting to modify chars[text[current_char]], which is (I assume, again, given the lack of context) a constant member variable.

Of course, I also suspect that the argument to SDL_BlitSurface is not const mostly because the SDL C++ interface sucks, and it would indeed be a const had they thought for a second about it. Therefore, it should be safe to:

SDL_BlitSurface( bitmap,
const_cast<SDL_Rect*>(&chars[text[current_char]]),
surface,
&rect );


Share this post


Link to post
Share on other sites
c4c0d3m0n    100
Ah, that was exactly what fixed my problem. Sorry for not posting more information about my Font class, I'll try and post as much information as possible from now on.

Share this post


Link to post
Share on other sites
rip-off    10979
The destination rectangle is modified by SDL to reflect the final, clipped image. Therefore const_cast is a bad idea. Simply make a copy:

SDL_Rect copy = chars[text[current_char]];
SDL_BlitSurface( bitmap, &copy, surface, &rect );

Share this post


Link to post
Share on other sites
Trenki    345
Quote:
Original post by rip-off
The destination rectangle is modified by SDL to reflect the final, clipped image. Therefore const_cast is a bad idea. Simply make a copy:
*** Source Snippet Removed ***


Yes, the destination rectangle is modified, but the source rectangle, which was the const offending variable, is not modified, so a const_cast is just fine.

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