Point TextTexture::load(const TextLabelInfo &ti) { m_ti = ti; TTF_Font *font = TTF_OpenFont(ti.fontName.c_str(), ti.fontSize); if(!font) { std::cerr << "Could not load font \"" << ti.fontName <<"\" : "<< TTF_GetError() <<std::endl; exit(-1); } SDL_Color c = {ti.color.r(), ti.color.g(), ti.color.b(), ti.color.a() }; SDL_Surface * pictureSurface = NULL; pictureSurface = TTF_RenderText_Blended(font, ti.text.c_str(), c); if(pictureSurface == NULL) { std::cerr<<"Could not render text : "<<TTF_GetError()<<std::endl; exit(-1); } dumpSurface(pictureSurface); TTF_CloseFont(font); SDL_Surface * convertedSurface = NULL; //First convert the texture to a format that is more suitable for openGL loading convertedSurface = Texture::convertSurface(pictureSurface); //this texture is not needed any more SDL_FreeSurface(pictureSurface); //then create a texture form the surface surfaceToTexture(convertedSurface); //get the size of the surface before deleting it Point textureSize(convertedSurface->w, convertedSurface->h); SDL_FreeSurface(convertedSurface); return textureSize; }
Sprite::Sprite(SDL_Surface* const surface_) : sdlTexture(surfaceToTexture(surface_)), width(0), height(0), path("font"), flipHorizontal(false) { // Display error log if image wasn't loaded. if(this->sdlTexture == nullptr){ Log(ERROR) << "Sprite load failed: " << this->path; } }
void TextObject::createTextTexture() { if (texture != nullptr) SDL_DestroyTexture(texture); SDL_Surface* surface = TTF_RenderText_Solid(font, text.c_str(), color); texture = surfaceToTexture(surface); SDL_QueryTexture(texture, NULL, NULL, &rect.w, &rect.h); rect.x = p.getX(); rect.y = p.getY(); }
void Sprite::loadFrom(const std::string& path_){ assert(Window::getRenderer() != nullptr && "Window renderer should not be null!"); SDL_Surface* loadedSurface = IMG_Load(path_.c_str()); // Returns whether the Sprites texture is null or not. this->sdlTexture = surfaceToTexture(loadedSurface); // Display error log if image wasn't loaded. if(this->sdlTexture == nullptr){ Log(ERROR) << "Sprite load failed: " << path_; } }
Texture::Texture(std::string filename) { sdl_tex = load_image(filename); // Loads texture into memory. tex = surfaceToTexture(sdl_tex); }