void Renderer::drawText(const TextRenderable& txt, bool clear)
{

	SDL_Surface* surfaceMessage = TTF_RenderText_Solid(sFont, (txt.text + txt.addText).c_str(), DEFAULT_TEXT_COLOR);
	SDL_Texture* message = SDL_CreateTextureFromSurface(sRenderer, surfaceMessage); 

	int w, h;
	TTF_SizeText(sFont, txt.text.c_str(), &w, &h);

	//Clear the area
	this->clear(txt.x, txt.y, w, h);
	SDL_Rect rectMessage {txt.x, txt.y, w, h}; 
	SDL_RenderCopy(sRenderer, message, nullptr, &rectMessage);

	SDL_DestroyTexture(message);
	SDL_FreeSurface(surfaceMessage);
}
// Objetivo: Transformar um texto em uma superficie
// Parametros: O endereco da string que contem o texto e a fonte do texto
// Retorno: A superficie do texto ou NULL se ocorrer um erro
Superficie textoParaSuperficie(char *texto, Fonte fonteTexto){
	Superficie superficeTexto = NULL;
	
	// Transforma o texto em uma superficie
	superficeTexto = TTF_RenderText_Solid(fonteTexto, // A fonte do texto
	                                       texto, // O texto que sera tranformado
	                                       COR_FONTE_PRINCIPAL); // A cor do texto
	
	// Verifica se ocorreu um erro ao transformar o text em superficie
	if(superficeTexto == NULL) 
		salvarErro("Erro na funcao 'TTF_RenderText_Solid' de 'textoParaSuperficie'\n");
	
	// Limpa a memoria utilizada pela fonte
	TTF_CloseFont(fonteTexto);
	
	return superficeTexto;
}
Exemple #3
0
        void SDLTrueTypeFont::drawString(fcn::Graphics* graphics, const std::string& text, int x, int y)
        {
            if (text == "")
            {
                return;
            }
        
            fcn::SDLGraphics *sdlGraphics = dynamic_cast<fcn::SDLGraphics *>(graphics);

            if (sdlGraphics == NULL)
            {
                throw FCN_EXCEPTION("SDLTrueTypeFont::drawString. Graphics object not an SDL graphics object!");
                return;
            }
        
            // This is needed for drawing the Glyph in the middle if we have spacing
            int yoffset = getRowSpacing() / 2;
        
            Color col = sdlGraphics->getColor();

            SDL_Color sdlCol;
            sdlCol.b = col.b;
            sdlCol.r = col.r;
            sdlCol.g = col.g;

            SDL_Surface *textSurface;
            if (mAntiAlias)
            {
                textSurface = TTF_RenderText_Blended(mFont, text.c_str(), sdlCol);
            }
            else
            {
                textSurface = TTF_RenderText_Solid(mFont, text.c_str(), sdlCol);
            }
        
            SDL_Rect dst, src;
            dst.x = x;
            dst.y = y + yoffset;
            src.w = textSurface->w;
            src.h = textSurface->h;
            src.x = 0;
            src.y = 0;
        
            sdlGraphics->drawSDLSurface(textSurface, src, dst);
            SDL_FreeSurface(textSurface);        
        }
Exemple #4
0
void xyz_block_text(int x, int y, const char *text) {
  SDL_Surface *rendered_text;
  SDL_Rect dest;
  SDL_Color color;
  Uint8 r, g, b;
  char *one_line = NULL;
  const char *index = text;
  const char *next_newline;

  SDL_GetRGB(current_color, surface->format, &r, &g, &b);
  color.r = r;
  color.g = g;
  color.b = b;

  while(index) {
    int length;

    next_newline = strchr(index, '\n');
    if(!next_newline) {
      one_line = strdup(index);
      index = NULL;
    } else {
      length = next_newline - index;
      one_line = malloc(length + 1);
      memcpy(one_line, index, length);
      one_line[length] = '\0';
      index = next_newline + 1;
    }

    rendered_text = TTF_RenderText_Solid(sanskrit_font_20, one_line, color);
    if(!rendered_text)
      xyz_fatal_error("Couldn't render text: %s!", TTF_GetError());

    dest.x = x;
    dest.y = y;
    y += 25;  /* For next line */
    dest.w = surface->w;
    dest.h = surface->h;

    SDL_BlitSurface(rendered_text, NULL, surface, &dest);
    SDL_FreeSurface(rendered_text);

    free(one_line);
  }
}
Exemple #5
0
Tekst * Tekst_opprett(void * spaceinvader, char * font_navn, int size) {

    Skjerm * skjerm = ((Spaceinvader*)spaceinvader)->skjerm;

    Tekst * tekst = (Tekst*)malloc(sizeof(Tekst));
                 
    tekst->spaceinvader = spaceinvader;

    tekst->font=TTF_OpenFont(font_navn, size);
    
    if(!tekst->font) {
        printf("TTF_OpenFont: %s\n", TTF_GetError());
        return NULL;
    }    

    strcpy(tekst->melding,"nop");
    
    tekst->x = 0;
    
    tekst->y = 0;
    
    SDL_Color farge = {0,255,0};
    SDL_Surface* surface = TTF_RenderText_Solid( tekst->font, tekst->melding, farge );
    
    if (surface == NULL) {
        printf("TTF_RenderText_Solid: %s\n", TTF_GetError());
        return NULL;        
    }
    
    tekst->bredde = surface->w;
    
    tekst->hoeyde = surface->h;
    
    tekst->texture = SDL_CreateTextureFromSurface( skjerm->ren, surface );
    
    if (tekst->texture == NULL) {
        printf("SDL_CreateTextureFromSurface: %s\n", SDL_GetError());
        return NULL;               
    }
            
    SDL_FreeSurface (surface );
                    
    return tekst;
    
}
Font::Font(std::string FontName, int FontSize, std::string Text, int x, int y)
{
	font = NULL;
	sText = Text;
	cX = x;
	cY = y;
	font = TTF_OpenFont(FontName.c_str(), FontSize);
	TextColor.r = 255;
	TextColor.b = 255;
	TextColor.g = 255;
	if(!font)
	{
		Log(std::string("Font failed to load, ") + TTF_GetError());
	}
	cText = sText;
	Color = TextColor;	
	Surface = TTF_RenderText_Solid(font, sText.c_str(), TextColor);
}
Exemple #7
0
void TextLabel::loadTexture(SDL_Renderer *sdlRenderer,TTF_Font *font){
	this->freeTexture();
	SDL_Surface* surfaceMessage = TTF_RenderText_Solid(font, this->message.c_str(), this->textColor);
	if(surfaceMessage == NULL) {
		Log().Get(TAG,logERROR) << "No se pudo cargar la surface: "<<TTF_GetError();
		return;
	}

	this->width = surfaceMessage->w;
	this->height = surfaceMessage->h;

	this->texture = SDL_CreateTextureFromSurface(sdlRenderer, surfaceMessage);
	SDL_FreeSurface( surfaceMessage );
	if (this->texture == NULL){
		Log().Get(TAG,logERROR) << "No se pudo crear la texture: "<<SDL_GetError();
		return;
	}
}
Exemple #8
0
void debug_hud_update_surfaces(DebugHud* self, SDL_Renderer* renderer) {
    for (u32 i = 0; i < self->count; ++i) {
        DebugHudWatch* watch = &self->watches[i];

        if (watch->texture) {
            SDL_DestroyTexture(watch->texture);
            watch->texture = NULL;
        }

        char watchOut[128];
        debug_hud_watch_build_string(watch, watchOut, 128);

        SDL_Color color = debug_hud_watch_internal_get_color(watch);
        SDL_Surface* surface = TTF_RenderText_Solid(self->debugFont, watchOut, color);
        watch->texture = SDL_CreateTextureFromSurface(renderer, surface);
        SDL_FreeSurface(surface);
    }
}
Exemple #9
0
void *textureFromText(char *str, SDL_Color color, Text *text, TTF_Font *font,
                      SDL_Renderer *renderer) {
    SDL_Surface *surface = TTF_RenderText_Solid(font, str, color);
    if (surface == NULL) {
        printf("Unable to render text surface. Shit's f****d, because %s, dude"
               ".", TTF_GetError());
    } else {
        text->texture = SDL_CreateTextureFromSurface(renderer, surface);
        if (text->texture == NULL) {
            printf("Unable to create texture from rendered text. Shit's f****d"
                   " because %s, dude.", SDL_GetError());
        } else {
            text->width = surface->w;
            text->height = surface->h;
        }
        SDL_FreeSurface(surface);
    }
}
Exemple #10
0
void draw_timer(signed int hours, signed int minutes, signed int seconds)
{
    SDL_Color color = { 255, 255, 255 };
    char buffer[20];
    int w, h;

    std::sprintf(buffer, "%02d:%02d:%02d", hours, minutes, seconds);

    if(TTF_SizeText(font_28, buffer, &w, &h))
    {
        log(ERROR, "TTF_SizeText failed. %s.", TTF_GetError());
        w = 180;
    }

    SDL_Surface * s = TTF_RenderText_Solid(font_28, buffer, color);

    apply_surface(s, (WIDTH / 2) - (w / 2), 20);
}
Exemple #11
0
       void game::relatifdetails(const char* c)
{

        SDL_BlitSurface(mario_head,NULL,screen,&rect_mario_head);
    rect_mario_head.x=10;
    rect_mario_head.y=40;
    rect_mario_head.w=30;
    rect_mario_head.h=27;
     mario_head=SDL_DisplayFormat(SDL_LoadBMP("mario_head.bmp"));
     SDL_SetColorKey(mario_head,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,0x00,0xFF,0xFF));
    SDL_Color color={255,255,255};
    SDL_Surface* text=TTF_RenderText_Solid(font,c,color);
    SDL_Rect tmprect={50,40};
    SDL_BlitSurface(text,NULL,screen,&tmprect);
     SDL_BlitSurface(mario_head,NULL,screen,&rect_mario_head);
     // afficher sky qui move

}
Exemple #12
0
bool TRTexture::loadFromRenderedText(std::string renderedText, SDL_Color textColor){
    free();
    SDL_Surface *loadedSurface = TTF_RenderText_Solid(mFont, renderedText.c_str(), textColor);
    if(loadedSurface == NULL){
        //EXCEPTION
        printf( "Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError() );
    }else{
        mTexture = SDL_CreateTextureFromSurface(mRenderer, loadedSurface);
        if(mTexture == NULL){
            //EXCEPTION
            printf( "Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError() );
        }else{
            mWidth = loadedSurface->w;
            mHeight = loadedSurface->h;
        }
    }
    return mTexture != NULL;
}
int fps_draw_msg(SDL_Surface *screen, int x, int y, const char *msg)
{
	SDL_Color bg={0,0,0,0};
	SDL_Color fg={255,255,255,255};

	SDL_Surface *surf= TTF_RenderText_Solid(font, msg, fg);

        SDL_TextureID text = SDL_CreateTextureFromSurface(0, surf);

        //Central alignment for subtitle
        //x=(int) ((320.0*fs_screen_width/640.0) -(surf->w/2.0));

        SDL_Rect rect = {x, y, surf->w, surf->h };
        SDL_RenderCopy(text, NULL, &rect);

        SDL_FreeSurface(surf);
        SDL_DestroyTexture(text);
}
void Font::WriteSolid(int x, int y, const char* text, ...) {
    SDL_Rect pos;
    pos.x = x;
    pos.y = y;

    char* buffer = new char[strlen(text)+1];

    va_list args;
	va_start(args, text);
	vsprintf(buffer, text, args);
	va_end(args);

    SDL_Surface* aux = TTF_RenderText_Solid(font, buffer, fg);
    SDL_BlitSurface(aux, NULL, surface, &pos);

    delete buffer;
    SDL_FreeSurface(aux);
}
    void Painter::drawString(iXY pos, const std::string & text, const std::string & font)
    {
        currentTransform.apply(pos);
        Palette p;
        SDL_Color c = p[brushColor];
        SDL_Color c2;
        c2.r = c.red;
        c2.g = c.green;
        c2.b = c.blue;
                
        SDL_Surface * surface = TTF_RenderText_Solid(fontManager->getFont(font),text.c_str(), c2);

        SDL_Rect r;
        r.x = pos.x;
        r.y = pos.y;

        SDL_BlitSurface(surface, NULL, drawingSurface, &r);
    }
Exemple #16
0
void Label::draw(SDL_Renderer *renderer) {
    if (dirty) {
        if (renderedText) {
            SDL_DestroyTexture(renderedText);
        }
        SDL_Surface* renderedSurface = TTF_RenderText_Solid(UIManager::getInstance().getFont(), text.c_str(), textColor);
        renderedText = SDL_CreateTextureFromSurface(renderer, renderedSurface);
        SDL_FreeSurface(renderedSurface);
    }
    SDL_Rect rect;
    int w, h;
    SDL_QueryTexture(renderedText, nullptr, nullptr, &w, &h);
    rect.x = getX();
    rect.y = getY();
    rect.w = w;
    rect.h = h;
    SDL_RenderCopy(renderer, renderedText, nullptr, &rect);
}
Exemple #17
0
void text_write_raw(SDL_Surface *screen, int x, int y, char *message, SDL_Color color, int points) {
    TTF_Font *tmp;

    tmp = setup_ttf(points);
    SDL_Rect dstrect;
    SDL_Surface *text;

    text = TTF_RenderText_Solid(tmp, message, color);

    dstrect.x = x;
    dstrect.y = y;
    dstrect.w = text->w;
    dstrect.h = text->h;

    SDL_BlitSurface(text, NULL, screen, &dstrect);

    SDL_FreeSurface(text);
}
Exemple #18
0
	void Game::draw_text( const char* str, int x, int y ){
	   SDL_Surface* surface;
	   SDL_Texture* texture;
	   SDL_Rect     rect;

	   surface = TTF_RenderText_Solid( m_font, str, FONT_COLOR );
	   texture = SDL_CreateTextureFromSurface( m_renderer, surface );

	   // display texture
	   rect.x = x;
	   rect.y = y;
	   rect.w = surface->w;
	   rect.h = surface->h;

	   SDL_RenderCopy( m_renderer, texture, NULL, &rect );
	   SDL_FreeSurface( surface );
	   SDL_DestroyTexture( texture );
	}
ScoreboardSurface::Status ScoreboardSurface::Update(
	const std::chrono::time_point<std::chrono::system_clock>& time)
{
	auto ret = AbstractTimerSurface::Update(time);

	std::uint32_t seconds = 0;
	if (m_started && m_running)
	{
		auto diff = time - m_startTime;
		seconds = static_cast<decltype(seconds)>(constants::MAX_TIME 
			- std::chrono::duration_cast<std::chrono::seconds>(diff).count());
	}

	char text[32];
	sprintf_s(text, "%d seconds left.", seconds);
	m_timeText.reset(TTF_RenderText_Solid(m_font.get(), text, m_defaultColor));
	return ret;
}
Exemple #20
0
static void _render_overlay_live_text(overlay *o, surf_coord *text_coord) {
    SDL_Surface *temp_font_surf = TTF_RenderText_Solid(o->font, o->font_text, o->font_col);
    SDL_FillRect(o->font_surf, NULL, o->bg_col);
    SDL_BlitSurface(temp_font_surf, NULL, o->font_surf, NULL);

    glTexSubImage2D(
            GL_TEXTURE_2D,
            0,
            text_coord->x,
            text_coord->y,
            o->font_surf->w,
            o->font_surf->h,
            o->tex_format,
            o->tex_type,
            o->font_surf->pixels
            );
    free(temp_font_surf);
}
Exemple #21
0
bool MyTexture::loadFromRenderedText(std::string textureText, SDL_Color textColor){
    
    free();
    
    SDL_Surface* textSurface = TTF_RenderText_Solid( gFont, textureText.c_str(), textColor);
    
    if(textSurface == NULL) return false;
    
    real_texture = SDL_CreateTextureFromSurface( gRenderer, textSurface);
    
    if(real_texture == NULL) return false;
    
    texture_height = textSurface->h;
    texture_width = textSurface->w;
    
    SDL_FreeSurface( textSurface );
    
    return (real_texture != NULL);
}
SDL_Texture* ServerConnectionView::renderText(string text, SDL_Rect* textSize) {
	SDL_Color textColor = { 0, 0, 0 };
	SDL_Surface* surfaceText = TTF_RenderText_Solid(font, text.c_str(), textColor);
	if (surfaceText == NULL) {
		// Error => sin surface
		return NULL;
	}
	textSize->w = surfaceText->w;
	textSize->h = surfaceText->h;

	SDL_Texture* renderedText = SDL_CreateTextureFromSurface(renderer, surfaceText);
	SDL_FreeSurface(surfaceText);
	if (renderedText == NULL) {
		// Error => sin texto
		textSize->w = 0;
		textSize->h = 0;
	}
	return renderedText;
}
Exemple #23
0
int main()
{
	char str[] = "HeMiao";
	TTF_Font *font;
	if (SDL_Init(SDL_INIT_VIDEO) == -1)
		return 0;
	/* 初始化字体库 */
	if (TTF_Init() == -1)
		return 0;


	/* 打开字库,设字体大小 */

//	 font = TTF_OpenFont("Times New Roman.ttf", 30);
	font = TTF_OpenFont("/system/fonts/DroidSansFallback.ttf", 30);
	if (font == NULL)
	{
		return 0;
	}
	TTF_SetFontStyle(font, TTF_STYLE_BOLD | TTF_STYLE_ITALIC);
	SDL_Surface *pText = TTF_RenderText_Solid(font, str, (SDL_Color) { 0, 255, 255 }
	);
	if(pText==NULL)
	return 0;
	SDL_Surface *s = SDL_SetVideoMode(pText->w, pText->h, 32,
									  SDL_SWSURFACE);

	freopen("sdl.log", "w", stdout);
	printf("%d\n", s->format->BytesPerPixel);
	printf("%d\n", pText->w);
	fflush(stdout);
	// ApplySurface(80, 120, pText, s);
	/* SDL_Rect rect; rect.x=100,rect.y=100,rect.w=pText->w,rect.h=pText->h; */
	SDL_BlitSurface(pText, NULL, s, NULL);
	SDL_FreeSurface(pText);

	worker.save(s, "hello.jpg");
	 SDL_Flip(s);
	SDL_Delay(1000);
	TTF_CloseFont(font);
	TTF_Quit();
	SDL_Quit();
}
Exemple #24
0
void drawButton(btn bt, int x, int y){

	SDL_Surface *b = NULL;
	SDL_Color col;
	if(bt.pressed){
		col.r = 0x55;
		col.b = 0x66;
		col.g = 0x88;
	}
	else col = textColor;
	b = TTF_RenderText_Solid( font, bt.sym, col);

	SDL_Rect DestR;
	
	DestR.x = x;
	DestR.y = y;
	SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
	SDL_BlitSurface( b, NULL, gScreenSurface, &DestR );
}
Exemple #25
0
/*******************************************************************************
 Name:              draw
 Description:       Draws the Object to the given SDL_Surface*

 Input:
    s               SDL_Surface* to be drawn onto
 ******************************************************************************/
void Sling::draw(SDL_Surface* s)
{
    static SDL_Rect loc;
    loc = pos;

    SDL_BlitSurface(launcherImg, NULL, s, &Slingshot);
    SDL_BlitSurface(image, NULL, s, &loc);
    
    char buffer[10];
    sprintf(buffer,"%d",getScore());
    
    message = TTF_RenderText_Solid(font, buffer, fontColor);
    
    static SDL_Rect scoreLoc;
    scoreLoc.x = 1100;
    scoreLoc.y = 30;
    
    SDL_BlitSurface(message, NULL, s, &scoreLoc);
}
Exemple #26
0
void SpeechBubble::InitText(const string phrase)
{
	// Close any existing font
	if (m_Font)
		TTF_CloseFont(m_Font);

	// Create a new text object with the phrase
	m_Font = TTF_OpenFont("joystix monospace.ttf", TEXT_SIZE);

	SDL_Color black;
	black.r = black.g = black.b = 0;

	// Free any existing surface
	if (m_TextSurface)
		SDL_FreeSurface(m_TextSurface);

	// Replace it with a new TTF text surface
	m_TextSurface = TTF_RenderText_Solid(m_Font, m_Phrase.c_str(), black);
}
Exemple #27
0
void Texture_modText(Texture* t, const char* s, int p, SDL_Color c)
{
	SDL_Surface* swap;

	if(t == NULL)
	{
		throw_warning("Modding texture is NULL!");
		return;
	}

	if(t->type != TEXTURE_TEXT)
	{
		throw_warning("Modding texture is the wrong type!");
		return;
	}

	if(t->raw == NULL)
	{
		throw_warning("SDL_Texture of modding texture is NULL!");
		return;
	}

	swap = TTF_RenderText_Solid(GAME_FONT, s, c);
	t->sizeX = (float)strlen(s) * p;

	if(swap == NULL)
	{
		throw_warning("Failed to render modding text!");
		return;
	}

	SDL_DestroyTexture(t->raw);
	t->raw = SDL_CreateTextureFromSurface(RENDERER, swap);

	if(t->raw == NULL)
	{
		throw_warning("Failed to create modding texture!");
		SDL_FreeSurface(swap);
		return;
	}

	SDL_FreeSurface(swap);
}
Exemple #28
0
void Dashboard::putText(int x, int y, const char *text, int size, SDLColor color,
    bool mono)
{
    if (strlen(text) == 0)
        return;
    
    TTF_Font *font = mono ? getMonoFont(size) : getFont(size);
    if (!font)
        return;
    
    SDL_Surface *text_surface;
    text_surface = TTF_RenderText_Solid(font, text, color);
    if (!text_surface)
        return;
    
    SDLRect where(x, y, 0, 0);
    SDL_BlitSurface(text_surface, NULL, screen, &where);
    SDL_FreeSurface(text_surface);
}
Exemple #29
0
void Button::setCaption(std::string cap, TTF_Font* font) {
	SDL_Color whiteText = { 0xFF, 0xFF, 0xFF };

	this->caption = cap;

	SDL_FillRect(this->visual, &this->visual->clip_rect,
			SDL_MapRGB(this->visual->format, 0xFF, 0, 0));

	SDL_Surface* tempSurface = TTF_RenderText_Solid(font, this->caption.c_str(),
			whiteText);
	tempSurface->clip_rect.x = (this->visual->clip_rect.w - tempSurface->clip_rect.w)
			/ 2;
	tempSurface->clip_rect.y = (this->visual->clip_rect.h - tempSurface->clip_rect.h)
			/ 2;

	SDL_BlitSurface(tempSurface, NULL, this->visual, &tempSurface->clip_rect);

	SDL_FreeSurface(tempSurface);
}
Exemple #30
0
void Draw()
{
	
	SDL_Color textColor = { 255, 255,255, 255 };
	textSurface = TTF_RenderText_Solid(gFont,"What does this mean", textColor);

	textTexture = SDL_CreateTextureFromSurface(m_renderer,textSurface);

	int w, h;
	SDL_QueryTexture(textTexture, NULL, NULL, &w, &h);
	SDL_Rect rect = { 400, 400, w, h };
	rendTarget->SetTexture(textTexture);
	
	SDL_SetRenderDrawColor(m_renderer, 0x00, 0x00, 0x00, 0xFF);
	SDL_RenderClear(m_renderer);
	
	rendTarget->SetPosition(200, 200);
//	rendTarget->SetBounds(w, h);

//	text->SetTexture(textTexture);
	text->SetPosition(100, 100);
//	text->SetBounds(w, h);

	SDL_SetRenderDrawColor(m_renderer, 0xFF, 0xFF, 0xFF, 0xFF);
//	SDL_SetRenderDrawColor(m_renderer, 0x00, 0x00, 0x00, 0xFF);


//	SDL_RenderCopy(m_renderer, text->GetTexture(), NULL, NULL);
//	text->Draw(m_renderer);
	text->UpdateTextTexture(m_renderer);
	rendTarget->Draw(m_renderer);
	text->Draw(m_renderer);

//	SDL_RenderCopyEx(m_renderer, textTexture, NULL,&rect,(double)(SDL_GetTicks()/10),NULL, SDL_FLIP_NONE);

//	SDL_Rect fillRect = { SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 };
//	SDL_SetRenderDrawColor(m_renderer, 0xFF, 0x00, 0x00, 0xFF);
//	SDL_RenderFillRect(m_renderer, &fillRect);

	SDL_RenderPresent(m_renderer);

	
}