GameStats::GameStats( SurfaceLoader * const sl , Sprite * const pac , Sprite * const bon , Uint32 const bg , CollisionChecker * const _cc , bool * const _gameover ): // SDL_Colors logoColor(0x00, 0x80, 0xff) , textColor(0xff, 0xff, 0xff) , numColor(0x00, 0xff, 0xff) // TTF_Fonts , font_logo(TTF_OpenFont("resources/fonts/Crackman.ttf", 50)) , font_text(TTF_OpenFont("resources/fonts/ATOMICCLOCKRADIO.TTF",25)) , num_text(TTF_OpenFont("resources/fonts/ATOMICCLOCKRADIO.TTF", 25)) , font_game_over(TTF_OpenFont("resources/fonts/Alias.ttf", 76)) // Rectangles , ptitle_level(520, 150, 0, 0) // Levelz , ptitle_score(520, 200, 0, 0) // score , ptitle_lives(520, 250, 0, 0) // lives , ptitle_bonus(0, 0, 0, 0) // TODO is this needed? , plogo(520, 40, 0, 0) , ptitle_fruits(520, 300, 0, 0) // fruits , ppacman(705, 245, 0, 0) // , pgameover(120, 250, 0, 0) , plives(740, 250, 0, 0) , pbonus(730, 295, 0, 0) , tele(496, 232 + LAYOUT_Y_OFFSET, 32, 32) , plevel(728, 150, 0, 0) , pscore(670, 200, 0, 0) // Surfaces , logo(TTF_RenderUTF8_Blended(font_logo, "Pac-Man", logoColor)) , title_score(TTF_RenderUTF8_Blended(font_text,"Score", textColor)) , title_lives(TTF_RenderUTF8_Blended(font_text,"Lives", textColor)) , title_fruits(TTF_RenderUTF8_Blended(font_text,"Bonus",textColor)) , title_seperator(NULL) // TODO is this needed? , fruit(NULL) // TODO is this needed? , pacman ( SurfaceLoader::getInstance()-> loadSurface("./resources/animation_films/pacman.png")) , title_level(TTF_RenderUTF8_Blended(font_text,"Level", textColor)) , num_level(NULL) // TODO is this needed? , num_score(NULL) // TODO is this needed? , num_lives(NULL) // TODO is this needed? , game_over(NULL) , choco ( SurfaceLoader::getInstance()-> loadSurface("./resources/animation_films/chocobonus.png")) // Sprites , _bonus(bon) // bonus sprite , _pacman(pac) // pacman sprite // unsigned ints , score(0) , lives(PACMAN_LIVES) , level(1) , dots(135) //TODO get the real dots // bool , bonus(false) , dead_game(false) , gameover(_gameover) // Uint32 , _bg(bg) // background colour // Collision Checker , cc(_cc) { }
Image *MLIFont::RenderGlyph(uint32_t c) { EnsureUIThread(); CheckScale(); // render char SDL_Color whiteColor = {255, 255, 255, 255}; if (invertedColors) { whiteColor.b = 255 - whiteColor.b; whiteColor.g = 255 - whiteColor.g; whiteColor.a = 255 - whiteColor.a; } string utf8string; utf8::unchecked::append(c, back_inserter(utf8string)); SDL_Surface *pSurface = TTF_RenderUTF8_Blended(pTtfFont, utf8string.c_str(), whiteColor); if (pSurface == NULL) { return NULL; } int scaledStrokeWidth = strokeWidth * GetFontScale() + 0.5; // render outlines if (strokeWidth > 0) { SDL_Color blackColor = {0, 0, 0, 255}; if (invertedColors) { blackColor.b = 255 - blackColor.b; blackColor.g = 255 - blackColor.g; blackColor.a = 255 - blackColor.a; } #ifndef MLI_SDL_FONT_OUTLINING SDL_Surface *pSurfaceOutline = TTF_RenderUTF8_Blended(pTtfFont, utf8string.c_str(), blackColor); SDL_Surface *pSurfaceOutlinedText = SDL_CreateRGBSurface( 0, pSurface->w + scaledStrokeWidth * 2, pSurface->h + scaledStrokeWidth * 2, pSurface->format->BitsPerPixel, pSurface->format->Rmask, pSurface->format->Gmask, pSurface->format->Bmask, pSurface->format->Amask); SDL_SetSurfaceBlendMode(pSurfaceOutline, SDL_BLENDMODE_BLEND); SDL_Rect dstRect = {0, 0, pSurface->w, pSurface->h}; for (dstRect.x = 0; dstRect.x <= scaledStrokeWidth * 2; dstRect.x++) { for (dstRect.y = 0; dstRect.y <= scaledStrokeWidth * 2; dstRect.y++) { SDL_BlitSurface(pSurfaceOutline, NULL, pSurfaceOutlinedText, &dstRect); } } dstRect.x = scaledStrokeWidth; dstRect.y = scaledStrokeWidth; SDL_SetSurfaceBlendMode(pSurfaceOutline, SDL_BLENDMODE_BLEND); SDL_BlitSurface(pSurface, NULL, pSurfaceOutlinedText, &dstRect); SDL_FreeSurface(pSurface); SDL_FreeSurface(pSurfaceOutline); pSurface = pSurfaceOutlinedText; #else TTF_SetFontOutline(pTtfFont, scaledStrokeWidth); SDL_Surface *pSurfaceOutlinedText = TTF_RenderUTF8_Blended(pTtfFont, utf8string.c_str(), blackColor); SDL_SetSurfaceBlendMode(pSurfaceOutlinedText, SDL_BLENDMODE_BLEND); SDL_Rect dstRect = {scaledStrokeWidth, scaledStrokeWidth, pSurface->w, pSurface->h}; SDL_BlitSurface(pSurface, NULL, pSurfaceOutlinedText, &dstRect); SDL_FreeSurface(pSurface); pSurface = pSurfaceOutlinedText; TTF_SetFontOutline(pTtfFont, 0); #endif } // create image Image *pImage = Image::Load(pSurface, true); pImage->FlagFontSource(this); pImage->SetUseScreenScaling(false); return pImage; }
int main() { SDL_Window * window; SDL_Renderer * renderer; TTF_Font * font; SDL_Surface * surface; SDL_Texture * cecilia; SDL_Texture * button; SDL_Texture * damien; SDL_Event e; SDL_Color black = {0, 0, 0, 255}; SDL_Rect dst; SDL_Init(SDL_INIT_VIDEO); window = SDL_CreateWindow( "Font Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1024, 800, 0); renderer = SDL_CreateRenderer(window, -1, 0); SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"); TTF_Init(); font = TTF_OpenFont("../CaviarDreams.ttf", 40); surface = TTF_RenderUTF8_Blended(font, "CECILIA", black); cecilia = SDL_CreateTextureFromSurface(renderer, surface); SDL_FreeSurface(surface); surface = IMG_Load("../UI-button-next.png"); button = SDL_CreateTextureFromSurface(renderer, surface); SDL_FreeSurface(surface); surface = IMG_Load("../damien.png"); damien = SDL_CreateTextureFromSurface(renderer, surface); SDL_FreeSurface(surface); SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL_RenderClear(renderer); dst.x = 100; dst.y = 100; SDL_QueryTexture(cecilia, NULL, NULL, &dst.w, &dst.h); SDL_RenderCopy(renderer, cecilia, NULL, &dst); dst.x = 300; dst.y = 100; SDL_QueryTexture(button, NULL, NULL, &dst.w, &dst.h); SDL_RenderCopy(renderer, button, NULL, &dst); dst.x = 500; dst.y = 100; dst.w = dst.w / 2; dst.h = dst.h / 2; SDL_RenderCopy(renderer, button, NULL, &dst); dst.x = 600; dst.y = 10; SDL_QueryTexture(damien, NULL, NULL, &dst.w, &dst.h); SDL_RenderCopy(renderer, damien, NULL, &dst); dst.x = 150; dst.y = 300; dst.w = dst.w / 2; dst.h = dst.h / 2; SDL_QueryTexture(damien, NULL, NULL, &dst.w, &dst.h); SDL_RenderCopy(renderer, damien, NULL, &dst); dst.x = 30; dst.y = 200; dst.w = dst.w / 2; dst.h = dst.h / 2; SDL_RenderCopy(renderer, damien, NULL, &dst); dst.x = 510; dst.y = 200; dst.w = dst.w / 2; dst.h = dst.h / 2; SDL_RenderCopy(renderer, damien, NULL, &dst); SDL_RenderPresent(renderer); while (!SDL_PollEvent(&e) || e.type != SDL_QUIT); }
int main ( int argc, char *argv[] ) { if ( SDL_Init(SDL_INIT_EVERYTHING) != 0) { std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl; return 2; } if(TTF_Init() == -1) { std::cout << "TTF_Init: " << TTF_GetError() << std::endl; return 7; } SDL_Window *window = SDL_CreateWindow("SDL_TEST", 80, 80, WIDTH, HEIGHT, SDL_WINDOW_SHOWN); if (window == NULL) { std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl; return 3; } SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); if(renderer == NULL) { std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl; return 4; } SDL_RenderClear(renderer); SYSTEMTIME t; SDL_Surface *ClockPlate = SDL_CreateRGBSurface(0, WIDTH, HEIGHT, 32, 0, 0, 0, 0); SDL_Renderer *PlateRender = SDL_CreateSoftwareRenderer(ClockPlate); DrawPlate(PlateRender); SDL_Texture *TexturePlate = SDL_CreateTextureFromSurface(renderer, ClockPlate); SDL_DestroyRenderer(PlateRender); SDL_FreeSurface(ClockPlate); SDL_Color LogoColor = { 255, 0, 0, 255 }; SDL_Rect LogoXY = { WIDTH/2-80, 70, 160, 40 }; TTF_Font *LogoFont = TTF_OpenFont("C:/Windows/Fonts/KhmerUIb.ttf", 30); SDL_Surface *Logo = TTF_RenderUTF8_Blended(LogoFont, "HAMILTON", LogoColor); SDL_Texture *TextureLogo = SDL_CreateTextureFromSurface(renderer, Logo); SDL_FreeSurface(Logo); TTF_CloseFont(LogoFont); SDL_Color NumColor = { 0, 255, 255, 255 }; SDL_Rect NumXY = { WIDTH/2-80, 280, 160, 40 }; TTF_Font *NumFont = TTF_OpenFont("C:/Windows/Fonts/DIGITAL-Regular.ttf", 32); SDL_Surface *NumClock = NULL; SDL_Texture *TextureNum = NULL; char Num[8]; SDL_Color DayColor = { 255, 0, 255, 255 }; SDL_Rect DayXY = { WIDTH/2+75, HEIGHT/2-18, 80, 36 }; TTF_Font *DayFont = TTF_OpenFont("C:/Windows/Fonts/DIGITAL-Regular.ttf", 60); SDL_Surface *SurfaceDay = NULL; SDL_Texture *TextureDay = NULL; char Day[6]; bool Quit = false; SDL_Event event; while(!Quit) { while(SDL_PollEvent(&event)) switch(event.type) { case SDL_KEYDOWN: if(event.key.keysym.sym == SDLK_ESCAPE) Quit = true; break; case SDL_QUIT: Quit = true; break; default: break; } GetLocalTime(&t); SDL_RenderCopy(renderer, TexturePlate, NULL, NULL); SDL_RenderCopy(renderer, TextureLogo, NULL, &LogoXY); sprintf(Day, "%.2d %s", t.wDay, Week(t.wDayOfWeek)); SurfaceDay = TTF_RenderUTF8_Blended(DayFont, Day, DayColor); TextureDay = SDL_CreateTextureFromSurface(renderer, SurfaceDay); SDL_FreeSurface(SurfaceDay); SDL_RenderCopy(renderer, TextureDay, NULL, &DayXY); SDL_DestroyTexture(TextureDay); sprintf(Num, "%.2d:%.2d:%.2d", t.wHour, t.wMinute, t.wSecond); NumClock = TTF_RenderUTF8_Blended(NumFont, Num, NumColor); TextureNum = SDL_CreateTextureFromSurface(renderer, NumClock); SDL_FreeSurface(NumClock); SDL_RenderCopy(renderer, TextureNum, NULL, &NumXY); SDL_DestroyTexture(TextureNum); DrawHand(renderer, t.wHour, t.wMinute, t.wSecond); SDL_RenderPresent(renderer); } SDL_DestroyTexture(TextureLogo); SDL_DestroyTexture(TexturePlate); TTF_CloseFont(NumFont); return 0; }
int ttf_font_info::_draw_text(SDL_Surface *s, const char *text, size_t length, int x, int y, uint32 pixel, uint16 style, bool utf8) const { int clip_top, clip_bottom, clip_left, clip_right; if (draw_clip_rect_active) { clip_top = draw_clip_rect.top; clip_bottom = draw_clip_rect.bottom; clip_left = draw_clip_rect.left; clip_right = draw_clip_rect.right; } else { clip_top = clip_left = 0; clip_right = s->w; clip_bottom = s->h; } SDL_Color c; SDL_GetRGB(pixel, s->format, &c.r, &c.g, &c.b); c.a = 0xff; SDL_Surface *text_surface = 0; if (utf8) { char *temp = process_printable(text, length); if (environment_preferences->smooth_text) text_surface = TTF_RenderUTF8_Blended(get_ttf(style), temp, c); else text_surface = TTF_RenderUTF8_Solid(get_ttf(style), temp, c); } else { uint16 *temp = process_macroman(text, length); if (environment_preferences->smooth_text) text_surface = TTF_RenderUNICODE_Blended(get_ttf(style), temp, c); else text_surface = TTF_RenderUNICODE_Solid(get_ttf(style), temp, c); } if (!text_surface) return 0; SDL_Rect dst_rect; dst_rect.x = x; dst_rect.y = y - TTF_FontAscent(get_ttf(style)); if (draw_clip_rect_active) { SDL_Rect src_rect; src_rect.x = 0; src_rect.y = 0; if (clip_top > dst_rect.y) { src_rect.y += dst_rect.y - clip_top; } if (clip_left > dst_rect.x) { src_rect.x += dst_rect.x - clip_left; } src_rect.w = (clip_right > dst_rect.x) ? clip_right - dst_rect.x : 0; src_rect.h = (clip_bottom > dst_rect.y) ? clip_bottom - dst_rect.y : 0; SDL_BlitSurface(text_surface, &src_rect, s, &dst_rect); } else SDL_BlitSurface(text_surface, NULL, s, &dst_rect); if (s == MainScreenSurface()) MainScreenUpdateRect(x, y - TTF_FontAscent(get_ttf(style)), text_width(text, style, utf8), TTF_FontHeight(get_ttf(style))); int width = text_surface->w; SDL_FreeSurface(text_surface); return width; }
int main() { puts("KPSpamRunner"); SDL_Event event; //event catcher SDL_Rect root;//root pixel coord root.x = 0; root.y = 0; const int windowSizeX=1024; const int windowSizeY=768; SDL_Init(SDL_INIT_VIDEO); //initialize SDL with video module TTF_Init(); //initialize the true type font library TTF_Font *font = NULL; font = TTF_OpenFont("./bolonium.ttf", 70); TTF_font_color blackFont = {0, 0, 0}; if (font == NULL) { puts("Font not found"); return -1; } //create a 32bit color window with bouble buffering and put it into the graphic card memory SDL_Surface *screen = SDL_SetVideoMode( windowSizeX, windowSizeY, 32, SDL_HWSURFACE| SDL_DOUBLEBUF); SDL_WM_SetCaption("KPSpamRunner", NULL); //set the window title //background SDL_FillRect(screen, NULL,SDL_MapRGB(screen->format, 0, 128, 255)); SDL_Surface *background = IMG_Load("./Background.png"); SDL_BlitSurface(background, NULL, screen, &root); //refresh window SDL_Flip(screen); //GAME object Pikatux player; //create the player. he's a Pikatux ! =D int score = 0; SDL_Surface *pressEnter = TTF_RenderUTF8_Blended(font, "Appuyez sur Entrée !",blackFont); SDL_Surface* walkCycle[5]; //5 step of annimation //load it from files walkCycle[0] = IMG_Load("./walk-0.png"); walkCycle[1] = IMG_Load("./walk-1.png"); walkCycle[2] = IMG_Load("./walk-2.png"); walkCycle[3] = IMG_Load("./walk-3.png"); walkCycle[4] = IMG_Load("./walk-4.png"); initPikatux_sprite(&player,walkCycle); initPikatux_Pos(&player); char name[128]; SDL_Surface *dispName = NULL; SDL_Surface *dispTime = NULL; SDL_Surface *dispScore = NULL; char dispScoreString[40]; char dispTimeString[12]; /////////////////////// GAME RUNNING int run = 1; int stage = TITLE; //will start on the title screen SDL_Rect blitCursor; char input[2]; input[1] = '\0'; int curTime = SDL_GetTicks(); int oldTime = curTime; int startTime = 0; int oldPressTime = 0; int pikatuxRunning = 0; while(run) //render loop { //GRAPHIC RENDERING oldTime = curTime; curTime = SDL_GetTicks(); SDL_FillRect(screen, NULL,SDL_MapRGB(screen->format, 0, 128, 255)); SDL_BlitSurface(background, NULL, screen, &root); printf("curTime = %d\n", curTime); switch(stage) { default: case TITLE: blitCursor.x=768/2 -150; blitCursor.y=1024/2 + 20; SDL_BlitSurface(pressEnter, NULL, screen, &blitCursor); break; case SCORE: pikatuxRunning = 0; printf("SCORE = %d\n", score); sprintf(dispScoreString,"SCORE : %d",score); dispScore = TTF_RenderUTF8_Blended(font,dispScoreString,blackFont); blitCursor.y = 1074/2 + 20; blitCursor.x = 768/2 -30; SDL_BlitSurface(dispScore,NULL,screen,&blitCursor); break; case RUN: SDL_BlitSurface(player.sprite[player.step], NULL, screen, &player.coord); if (pikatuxRunning == 0) { startTime = SDL_GetTicks(); pikatuxRunning = 1; } if (curTime - startTime >= 30000) { pikatuxRunning = 0; stage = SCORE; } sprintf(dispTimeString,"Time : %d", 30 - (curTime - startTime)/1000); blitCursor.x = root.x + 10; blitCursor.y = root.x + 10; dispTime = TTF_RenderUTF8_Blended(font,dispTimeString,blackFont); SDL_BlitSurface(dispTime,NULL,screen,&blitCursor); break; case NAME_INPUT: dispName = TTF_RenderText_Blended(font, name, blackFont); SDL_BlitSurface(dispName, NULL ,screen, &root); break; } //EVENT Processing SDL_WaitEvent(&event); //do NOT pause the program switch (event.type) { case SDL_QUIT: run = 0; break; case SDL_KEYDOWN: switch (event.key.keysym.sym) { case SDLK_ESCAPE: run = 0; break; case SDLK_RETURN: if (stage == TITLE) //stage = NAME_INPUT; stage = RUN; if(stage == SCORE) { score = 0; stage = TITLE; } break; case SDLK_SPACE: if(pikatuxRunning) { puts("space down"); score ++; pikatux_step(&player); pikatux_showSpeed(&player,SDL_GetTicks()-oldPressTime); oldPressTime=SDL_GetTicks(); } break; } if (stage == NAME_INPUT) //type text with the keyboard { if(SDL_KEYDOWN) { if (event.key.keysym.sym >= 97 || event.key.keysym.sym <= 122) { if(strlen(input) < 126) { input[0] = (char) event.key.keysym.sym; strcat(name,input); } } if (event.key.keysym.sym == SDLK_BACKSPACE) if(strlen(name) >= 1) name[strlen(name) - 1] = '\0'; } } break; } SDL_Flip(screen); } TTF_Quit(); SDL_Quit(); return 0; }
void registerUpdate(const string &field1, const string &field2, const string &field3, const string &field4, const string &button, const string &title){ if(regSelectTexture[0] == 0) regSelectTexture[0] = loadImg("res/regsel0.png"); if(regSelectTexture[1] == 0) regSelectTexture[1] = loadImg("res/regsel1.png"); if(regSelectTexture[2] == 0) regSelectTexture[2] = loadImg("res/regsel2.png"); if(regSelectTexture[3] == 0) regSelectTexture[3] = loadImg("res/regsel3.png"); if(regSelectTexture[4] == 0) regSelectTexture[4] = loadImg("res/regsel4.png"); if(registerSurface == NULL){ registerSurface = IMG_Load("res/register.png"); SDL_Color white, black; white.r = white.g = white.b = 0xFF; black.r = black.g = black.b = 0x00; SDL_Surface *titleText = TTF_RenderUTF8_Blended(font, title.c_str(), white); SDL_Rect rTitle = {6,5,0,0}; SDL_BlitSurface(titleText, NULL, registerSurface, &rTitle); SDL_FreeSurface(titleText); SDL_Surface *userLblText = TTF_RenderUTF8_Blended(font, "Usuario:", black); SDL_Rect rUserLbl = {8,48,0,0}; SDL_BlitSurface(userLblText, NULL, registerSurface, &rUserLbl); SDL_FreeSurface(userLblText); SDL_Surface *passLblText = TTF_RenderUTF8_Blended(font, "Password:"******"Mail:", black); SDL_Rect rMailLbl = {8,134,0,0}; SDL_BlitSurface(mailLblText, NULL, registerSurface, &rMailLbl); SDL_FreeSurface(mailLblText); SDL_Surface *nameLblText = TTF_RenderUTF8_Blended(font, "Nombre:", black); SDL_Rect rNameLbl = {8,177,0,0}; SDL_BlitSurface(nameLblText, NULL, registerSurface, &rNameLbl); SDL_FreeSurface(nameLblText); SDL_Surface *buttonText = TTF_RenderUTF8_Blended(font, button.c_str(), black); SDL_Rect rButton = {128,234,0,0}; rButton.x -= buttonText->w/2; rButton.y -= buttonText->h/2; SDL_BlitSurface(buttonText, NULL, registerSurface, &rButton); SDL_FreeSurface(buttonText); registerTexture = genTexture(registerSurface, registerTexture); } SDL_Color black; black.r = black.g = black.b = 0x00; SDL_Surface *tempSurface = SDL_ConvertSurface(registerSurface, registerSurface->format, registerSurface->flags); SDL_Surface *f1Text = TTF_RenderUTF8_Blended(font, field1.c_str(), black); SDL_Rect rF1 = {88,48,0,0}; SDL_BlitSurface(f1Text, NULL, tempSurface, &rF1); SDL_FreeSurface(f1Text); SDL_Surface *f2Text = TTF_RenderUTF8_Blended(font, field2.c_str(), black); SDL_Rect rF2 = {88,91,0,0}; SDL_BlitSurface(f2Text, NULL, tempSurface, &rF2); SDL_FreeSurface(f2Text); SDL_Surface *f3Text = TTF_RenderUTF8_Blended(font, field3.c_str(), black); SDL_Rect rF3 = {88,134,0,0}; SDL_BlitSurface(f3Text, NULL, tempSurface, &rF3); SDL_FreeSurface(f3Text); SDL_Surface *f4Text = TTF_RenderUTF8_Blended(font, field4.c_str(), black); SDL_Rect rF4 = {88,177,0,0}; SDL_BlitSurface(f4Text, NULL, tempSurface, &rF4); SDL_FreeSurface(f4Text); registerTexture = genTexture(tempSurface, registerTexture); SDL_FreeSurface(tempSurface); }
// easy text rendering extern void RenderText( SDL_Surface *dest, TTF_Font *font, const char *text, SDL_Rect *rc, SDL_Color colr, int offx, int offy, int *outw, int *outh ) { int width = 12, height = 16; const SDL_Color whitecol = {255,255,255,255}; if ( (text == NULL) || (strlen(text) == 0) ) { fprintf(stderr,"line is empty\n"); return; } TTF_SizeText(font,text,&width,&height); SDL_Surface *stext = SDL_CreateRGBSurface(SDL_SWSURFACE,width,height,dest->format->BitsPerPixel,dest->format->Rmask,dest->format->Gmask,dest->format->Bmask,dest->format->Amask); if ( stext == NULL ) { fprintf(stderr,"couldn't create text box surface (%s)\n", SDL_GetError()); return; } SDL_Rect temprect; temprect.x = 0; temprect.y = 0; temprect.w = width; temprect.h = height; SDL_FillRect(stext,&temprect,SDL_MapRGB(stext->format,0,0,0)); SDL_Surface *stemp = NULL; stemp = TTF_RenderUTF8_Blended(font, text, whitecol); if ( SDL_BlitSurface(stemp,NULL,stext,&temprect) == -1 ) { fprintf(stderr,"error blitting line (%s)\n", SDL_GetError()); SDL_FreeSurface(stext); SDL_FreeSurface(stemp); return; } SDL_FreeSurface(stemp); SDL_Surface *stext2 = NULL; SDL_Surface *fbox = SDL_CreateRGBSurface(SDL_SWSURFACE,width,height,dest->format->BitsPerPixel,dest->format->Rmask,dest->format->Gmask,dest->format->Bmask,dest->format->Amask); SDL_FillRect(fbox,&temprect,SDL_MapRGB(fbox->format,colr.r,colr.g,colr.b)); stext2 = TransferAlpha(fbox,stext); SDL_FreeSurface(fbox); SDL_FreeSurface(stext); if ( rc == NULL ) { temprect.x = offx; temprect.y = offy; } else { temprect.x = rc->x+offx; temprect.y = rc->y+offy; } temprect.w = width; temprect.h = height; if ( SDL_BlitSurface(stext2,NULL,dest,&temprect) == -1 ) { fprintf(stderr,"error blitting text box (%s)\n", SDL_GetError()); SDL_FreeSurface(stext2); return; } SDL_FreeSurface(stext2); if ( outw != NULL ) *outw = width; if ( outh != NULL ) *outh = height; }
void Bitmap::drawText(const IntRect &rect, const char *str, int align) { GUARD_DISPOSED; GUARD_MEGA; if (*str == '\0') return; if (str[0] == ' ' && str[1] == '\0') return; flush(); TTF_Font *font = p->font->getSdlFont(); Color *fontColor = p->font->getColor(); SDL_Color c; fontColor->toSDLColor(c); float txtAlpha = fontColor->norm.w; SDL_Surface *txtSurf; if (shState->rtData().config.solidFonts) txtSurf = TTF_RenderUTF8_Solid(font, str, c); else txtSurf = TTF_RenderUTF8_Blended(font, str, c); p->ensureFormat(txtSurf, SDL_PIXELFORMAT_ARGB8888); int alignX = rect.x; switch (align) { default: case Left : break; case Center : alignX += (rect.w - txtSurf->w) / 2; break; case Right : alignX += rect.w - txtSurf->w; break; } if (alignX < rect.x) alignX = rect.x; int alignY = rect.y + (rect.h - txtSurf->h) / 2; float squeeze = (float) rect.w / txtSurf->w; if (squeeze > 1) squeeze = 1; FloatRect posRect(alignX, alignY, txtSurf->w * squeeze, txtSurf->h); Vec2i gpTexSize; shState->ensureTexSize(txtSurf->w, txtSurf->h, gpTexSize); bool fastBlit = !p->touchesTaintedArea(posRect) && txtAlpha == 1.0; if (fastBlit) { if (squeeze == 1.0) { /* Even faster: upload directly to bitmap texture. * We have to make sure the posRect lies within the texture * boundaries or texSubImage will generate errors. * If it partly lies outside bounds we have to upload * the clipped visible part of it. */ SDL_Rect btmRect; btmRect.x = btmRect.y = 0; btmRect.w = width(); btmRect.h = height(); SDL_Rect txtRect; txtRect.x = posRect.x; txtRect.y = posRect.y; txtRect.w = posRect.w; txtRect.h = posRect.h; SDL_Rect inters; /* If we have no intersection at all, * there's nothing to upload to begin with */ if (SDL_IntersectRect(&btmRect, &txtRect, &inters)) { if (inters.w != txtRect.w || inters.h != txtRect.h) { /* Clip the text surface */ SDL_Rect clipSrc = inters; clipSrc.x -= txtRect.x; clipSrc.y -= txtRect.y; posRect.x = inters.x; posRect.y = inters.y; posRect.w = inters.w; posRect.h = inters.h; PixelStore::setupSubImage(txtSurf->w, clipSrc.x, clipSrc.y); } TEX::bind(p->gl.tex); TEX::uploadSubImage(posRect.x, posRect.y, posRect.w, posRect.h, txtSurf->pixels, GL_BGRA); PixelStore::reset(); } } else { /* Squeezing involved: need to use intermediary TexFBO */ TEXFBO &gpTF = shState->gpTexFBO(txtSurf->w, txtSurf->h); TEX::bind(gpTF.tex); TEX::uploadSubImage(0, 0, txtSurf->w, txtSurf->h, txtSurf->pixels, GL_BGRA); FBO::bind(gpTF.fbo, FBO::Read); p->bindFBO(); FBO::blit(0, 0, txtSurf->w, txtSurf->h, posRect.x, posRect.y, posRect.w, posRect.h, FBO::Linear); } } else { /* Aquire a partial copy of the destination * buffer we're about to render to */ TEXFBO &gpTex2 = shState->gpTexFBO(posRect.w, posRect.h); FBO::bind(gpTex2.fbo, FBO::Draw); FBO::bind(p->gl.fbo, FBO::Read); FBO::blit(posRect.x, posRect.y, 0, 0, posRect.w, posRect.h); FloatRect bltRect(0, 0, (float) gpTexSize.x / gpTex2.width, (float) gpTexSize.y / gpTex2.height); BltShader &shader = shState->shaders().blt; shader.bind(); shader.setTexSize(gpTexSize); shader.setSource(); shader.setDestination(gpTex2.tex); shader.setSubRect(bltRect); shader.setOpacity(txtAlpha); shState->bindTex(); TEX::uploadSubImage(0, 0, txtSurf->w, txtSurf->h, txtSurf->pixels, GL_BGRA); TEX::setSmooth(true); Quad &quad = shState->gpQuad(); quad.setTexRect(FloatRect(0, 0, txtSurf->w, txtSurf->h)); quad.setPosRect(posRect); p->bindFBO(); p->pushSetViewport(shader); glState.blendMode.pushSet(BlendNone); quad.draw(); glState.blendMode.pop(); p->popViewport(); } SDL_FreeSurface(txtSurf); p->addTaintedArea(posRect); modified(); }