Exemplo n.º 1
0
Texture TextGenerator::Generate(const std::string &data, int size, float width) {
	Texture texture;
	TTF_Font *font = Font(size);
	if (font) {
		TTF_SetFontOutline(font, 0);
		int w = width == 0 ? INT_MAX : g_Graphics()->to_pixels(width);
		SDL_Surface *surface =
		    TTF_RenderUTF8_Blended_Wrapped(font, data.c_str(), SDL_Color{255, 255, 255, 255}, w); // TODO: fix shadow wrapping
		TTF_SetFontOutline(font, outline);
		SDL_Surface *shadowSurface =
		    TTF_RenderUTF8_Blended_Wrapped(font, data.c_str(), SDL_Color{96, 96, 96, 255}, w);
		if (surface && shadowSurface) {
			SDL_Rect dest;
			dest.x = outline;
			dest.y = outline;
			dest.w = surface->w;
			dest.h = surface->h;
			SDL_BlitSurface(surface, 0, shadowSurface, &dest);
			texture = Texture(shadowSurface);
		}
		if (surface)
			SDL_FreeSurface(surface);
		if (shadowSurface)
			SDL_FreeSurface(shadowSurface);
	}
	return texture;
}
Exemplo n.º 2
0
NE_API void draw_text(font_text* text) {
	if (current_font_asset && text->text[0] != '\0') {
		font_asset* font = get_font(current_font_asset);
		SDL_Color color = { 255, 255, 255, 255 };
		SDL_Surface* surface = nullptr;
		TTF_Font* ttf = (TTF_Font*) font->resource_handle;
		if (text->wrap_length == 0) {
			surface = TTF_RenderUTF8_Blended(ttf, text->text.c_str(), color);
		} else {
			surface = TTF_RenderUTF8_Blended_Wrapped(ttf, text->text.c_str(), color, text->wrap_length);
		}
		if (text->texture.resource_handle == 0) {
			text->texture.resource_handle = create_texture();
		}
		load_texture_from_pixels(&text->texture, (uint32*) surface->pixels, surface->w, surface->h);
		text->old_text = text->text;
		text->text[0] = '\0';
		text->transform.scale = { (float) surface->w, (float) surface->h, 0.0f };
		SDL_FreeSurface(surface);
	}
	if (text->texture.resource_handle != 0 && text->old_text[0] != '\0' && text->text[0] == '\0') {
		set_model_matrix(&text->transform);
		bind_texture(&text->texture);
		draw_vertex_array(&shapes[current_shape].vertex_array);
	} 
}
Exemplo n.º 3
0
    void Label::redraw()
    {
        if(m_font && m_text.length() > 0)
        {
            if(m_renderedText != nullptr)
            {
                SDL_DestroyTexture(m_renderedText);
            }
            SDL_Surface *rendered = nullptr;

            if(getBoundingBox().w == 0)
            {
                rendered = TTF_RenderUTF8_Blended(m_font->getFont(), m_text.c_str(), m_color);
            }
            else
            {
                rendered = TTF_RenderUTF8_Blended_Wrapped(m_font->getFont(), m_text.c_str(), m_color, getBoundingBox().w);
            }

            if(rendered == nullptr)
            {
                cout << "Problem rendering text to surface: " << TTF_GetError() << endl;
            }
            m_renderedText = SDL_CreateTextureFromSurface(getGlobalConfig()->getSDLRenderer(), rendered);
            if(m_renderedText == nullptr)
            {
                cout << "Problem rendering text to texture: " << SDL_GetError() << endl;
            }
            SDL_FreeSurface(rendered);

            SDL_QueryTexture(m_renderedText, nullptr, nullptr, &m_textW, &m_textH);

            updateParent();
        }
    }
Exemplo n.º 4
0
extern DECLSPEC SDL_Surface * SDLCALL
  TTF_RenderUTF8_Blended_Wrapped_p(
    TTF_Font *font,
    const char *text,
    SDL_Color *fg,
    uint32_t wrapLength) {

  return TTF_RenderUTF8_Blended_Wrapped(font, text, *fg, wrapLength);
}
Exemplo n.º 5
0
Arquivo: text.hpp Projeto: craley/Poky
inline SDL_Texture *getTextRenderTexture(SDL_Renderer *renderer, TTF_Font *font,
		const std::string &str, const SDL_Color color={255, 255, 255, 255},
		const uint32_t width=options::WINDOW_WIDTH)
{
	SDL_Surface *textSurface = TTF_RenderUTF8_Blended_Wrapped(
			font,
			str.c_str(),
			color,
			width);
	SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, textSurface);
	SDL_FreeSurface(textSurface);
	return texture;
}
Exemplo n.º 6
0
void StaticText::SetText(cchar* text)
{
	XAGUI::StaticText::SetText(text);

	Release();

	SDL_Color color = { GetRed(), GetGreen(), GetBlue(), GetAlpha() };
	SDL_Surface* surface = TTF_RenderUTF8_Blended_Wrapped(static_cast<Font*>(GetFont())->Get(), GetText(),
		color, GetFont()->GetTextWidth(text));
	_texture = SDL_CreateTextureFromSurface(static_cast<Renderer*>(XAGUI::XAGUI::GetRenderer())->Get(), 
		surface);
	SDL_SetTextureAlphaMod(_texture, color.a);
	SDL_FreeSurface(surface);
}
Exemplo n.º 7
0
void Font::printTextBox(const char *str, u16 x, u16 y, u16 width, u16 height, FontSize size, Color color) {
	TTF_Font *font = NULL;
	
	switch(size) {
		case FONT_SMALL:	font = m_fontSmall;		break;
		case FONT_MEDIUM:	font = m_fontMedium;	break;
		case FONT_LARGE:	font = m_fontLarge;		break;
		case FONT_MAXI:		font = m_fontMaxi;		break;
		default:			error("Bad font size");	return;
	}
	
	SDL_Surface *textSurface = TTF_RenderUTF8_Blended_Wrapped(font, str, SDL_Color{color.r, color.g, color.b, color.a}, width);
	Image textToDisplay(textSurface);
	
	textToDisplay.render(x, y, 0, 0, 0, 0, width, height);
}
Exemplo n.º 8
0
 virtual std::shared_ptr<SDL_Texture> DrawString(SDL_Renderer* renderer, std::string text, int width) const
 {
     SDL_Surface* surf;
     if (width < 0)
     {
         surf = TTF_RenderUTF8_Blended(ttfFont.get(), text.c_str(), color);
     }
     else
     {
         surf = TTF_RenderUTF8_Blended_Wrapped(ttfFont.get(), text.c_str(), color, width);
     }
     
     auto tex = std::shared_ptr<SDL_Texture>(SDL_CreateTextureFromSurface(renderer, surf), SDL_DestroyTexture);
     
     SDL_FreeSurface(surf);
     
     return tex;
 }
Exemplo n.º 9
0
static mrb_value
mrb_sdl2_ttf_font_render_UTF8_blended_wrapped(mrb_state *mrb, mrb_value self)
{
  mrb_value text; 
  mrb_int r, g, b, a, wrapLength;
  SDL_Surface * c;
  SDL_Color color;
  mrb_get_args(mrb, "Siiiii", &text, &r, &g, &b, &a, &wrapLength);
  color.r = r;
  color.g = g;
  color.b = b;
  color.a = a;
  c = TTF_RenderUTF8_Blended_Wrapped(mrb_sdl2_font_get_ptr(mrb, self), RSTRING_PTR(text), color, wrapLength);
  if (c == NULL) {
    mruby_sdl2_raise_error(mrb);
    return mrb_false_value();
  }
  return mrb_sdl2_video_surface(mrb, c, 0);
}
Exemplo n.º 10
0
//-----------------------------------------------------------------------------
// Purpose: Draws text to the screen inside the given rectangular region, using the given font
//-----------------------------------------------------------------------------
bool CGameEngineGL::BDrawString( HGAMEFONT hFont, RECT rect, DWORD dwColor, DWORD dwFormat, const char *pchText )
{
	if ( !hFont )
	{
		OutputDebugString( "Someone is calling BDrawString with a null font handle\n" );
		return false;
	}

	if ( !pchText || !*pchText )
	{
		return true;
	}

	// Very simple cache of complete strings as whole textures.
	// There are much better ways of doing efficient text rendering.
	// If nothing else we should expire the strings not being used.
	HGAMETEXTURE hTexture;
	char szFontPrefix[32];
	sprintf( szFontPrefix, "%d:", hFont );
	std::map< std::string, HGAMETEXTURE >::iterator iter;
	iter = m_MapStrings.find( std::string(szFontPrefix) + std::string(pchText) );
	if ( iter == m_MapStrings.end() )
	{
		static SDL_Color white = { 0xff, 0xff, 0xff, 0xff };

		// Calculate the text block size
		int nWrapLength = 0;
		int w, h;
		char *s = (char *)pchText;
		for ( char *p = strchr( s, '\n' ); p; p = strchr( s, '\n' ) )
		{
			*p = '\0';
			if ( TTF_SizeUTF8( m_MapGameFonts[ hFont ], s, &w, &h ) == 0 )
			{
				nWrapLength = std::max( w, nWrapLength );
			}
			*p = '\n';
			s = p + 1;
		}
		if ( TTF_SizeUTF8( m_MapGameFonts[ hFont ], s, &w, &h ) == 0 )
		{
			nWrapLength = std::max( w, nWrapLength );
		}
			
		SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped( m_MapGameFonts[ hFont ], pchText, white, nWrapLength );
		if ( !surface )
		{
			OutputDebugString( "Out of memory\n" );
			return false;
		}

		uint32 uWidth = power_of_two( surface->w );
		uint32 uHeight = power_of_two( surface->h );
		byte *pRGBAData = (byte *)malloc( uWidth*uHeight*4 );
		if ( !pRGBAData )
		{
			OutputDebugString( "Out of memory\n" );
			return false;
		}
		memset( pRGBAData, 0, uWidth*uHeight*4 );

		byte *src = (byte*)surface->pixels;
		byte *dst = pRGBAData;
		memset(dst, 0xff, uWidth*4);
		for ( uint32 row = 0; row < surface->h; ++row )
		{
			memcpy( dst, src, surface->w * 4 );
			src += surface->pitch;
			dst += uWidth * 4;
		}

		hTexture = HCreateTexture( pRGBAData, uWidth, uHeight );
		free( pRGBAData );

		// Record the actual text width and height
		m_MapTextures[ hTexture ].m_uWidth = surface->w;
		m_MapTextures[ hTexture ].m_uHeight = surface->h;

		SDL_FreeSurface( surface );

		m_MapStrings[ std::string(szFontPrefix) + std::string(pchText) ] = hTexture;
	}
	else
	{
		hTexture = iter->second;
	}

	int nWidth = m_MapTextures[ hTexture ].m_uWidth;
	int nHeight = m_MapTextures[ hTexture ].m_uHeight;
	float u = (float)nWidth / power_of_two(nWidth);
	float v = (float)nHeight / power_of_two(nHeight);

	// Get text position
	int nLeft = rect.left, nTop = rect.top;
	if ( dwFormat & TEXTPOS_TOP )
	{
		nTop = rect.top;
	}
	else if ( dwFormat & TEXTPOS_VCENTER )
	{
		nTop = rect.top + ((rect.bottom - rect.top) - nHeight) / 2;
	}
	else if ( dwFormat & TEXTPOS_BOTTOM )
	{
		nTop = rect.bottom - nHeight;
	}
	if ( dwFormat & TEXTPOS_LEFT )
	{
		nLeft = rect.left;
	}
	else if ( dwFormat & TEXTPOS_CENTER )
	{
		nLeft = rect.left + ((rect.right - rect.left) - nWidth) / 2;
	}
	else if ( dwFormat & TEXTPOS_RIGHT )
	{
		nLeft = rect.right - nWidth;
	}

//printf("Drawing text '%s' at %d,%d %dx%d {%d,%d %d,%d}\n", pchText, nLeft, nTop, nWidth, nHeight, rect.left, rect.top, rect.right, rect.bottom);
	return BDrawTexturedQuad( nLeft, nTop, nLeft + nWidth, nTop + nHeight, 0.0f, 0.0f, u, v, dwColor, hTexture );
}