# invalid conversion from const SDL_Rect*' to SDL_Rect*'

This topic is 3593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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,
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 on other sites
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 on other sites
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 on other sites
Declaring the offending member variable mutable should also do the trick.

##### Share on other sites
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 on other sites
Quote:
 Original post by rip-offThe 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.