UIElementTextBox::UIElementTextBox(EventHandler *event_handler, SDL_Surface *screen, PlayingState *parent, std::string text) :UIElement(event_handler) { this->name = "TextBox"; this->screen = screen; this->rect.x = 150; this->rect.y = 150; this->rect.w = 300; this->rect.h = 250; this->parent = parent; this->parent->paused = true; this->surf = SDL_CreateRGBSurface( screen->flags, this->rect.w, this->rect.h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask); SDL_FillRect(surf, &this->rect, 0x0); SDL_SetAlpha(surf,SDL_SRCALPHA, 0xbb); this->font = TTF_OpenFont("data//eartm.ttf",28); SDL_Color fg; fg.r = 255; fg.g = 255; fg.b = 255; SDL_Color bg; bg.r = 0; bg.g = 0; bg.b = 0; SDL_Rect fontrect; fontrect.x = 20; fontrect.y = 0; fontrect.w = 150; fontrect.h = 32; std::string buf = ""; /*split our string up into portions of 30 characters */ for(unsigned i = 0; i < text.size(); i++) { if(text[i] != '\n') buf += text[i]; if(i % 30 == 0 || text[i] == '\n'){ SDL_Surface *fontsurf = TTF_RenderText(font,buf.c_str(),fg, bg); SDL_BlitSurface(fontsurf, NULL, this->surf, &fontrect); if(i != 0) fontrect.y += 32; buf = ""; SDL_FreeSurface(fontsurf); } } SDL_Surface *fontsurf = TTF_RenderText(font,buf.c_str(),fg, bg); SDL_BlitSurface(fontsurf, NULL, this->surf, &fontrect); }
void DrawWinnerBanner(SDL_Surface *screen, GameState *gs) { for(unsigned p = 0; p < gs->player_count; p++) { if(gs->board[p].won){ Sint16 x_offset = gs->board[p].x_offset; Sint16 y_offset = gs->board[p].y_offset; Sint16 width_in_px = gs->board[p].width_in_px; Sint16 height_in_px = gs->board[p].height_in_px; Sint16 piece_width = gs->board[p].piece_width; Sint16 piece_height = gs->board[p].piece_height; int bh = height_in_px / 5; int bw = width_in_px; int bx = x_offset + (p * width_in_px); int by = height_in_px / 2; std::string loser = " You win! "; SDL_Color fg = {0xFF,0xFF,0xFF}; SDL_Color bg = {0x33,0x33,0x33}; SDL_Surface *fontsurf = TTF_RenderText(gs->font, loser.c_str(), fg, bg); SDL_Rect fontblitrect = {bx + 20, bh + 10, bw, bh}; SDL_BlitSurface(fontsurf, NULL, screen, &fontblitrect); SDL_FreeSurface(fontsurf); } } }
void DrawImpendingDoom(SDL_Surface *screen, GameState *gs) { for(unsigned p = 0; p < gs->player_count; p++) { if(gs->board[p].ojamms_pending >0 ){ Sint16 x_offset = gs->board[p].x_offset; Sint16 y_offset = gs->board[p].y_offset; Sint16 width_in_px = gs->board[p].width_in_px; Sint16 piece_width = gs->board[p].piece_width; Sint16 piece_height = gs->board[p].piece_height; int x = x_offset + 15 + (p * width_in_px); int y = 5; int r = (piece_width + piece_height) / 4; Uint32 color = 0x333333FF; filledCircleColor(screen, x+r, y+r, r, color); filledCircleColor(screen, x+r-r/2, y+r+r/4, r/4, 0x000000FF); filledCircleColor(screen, x+r+r/2, y+r+r/4, r/4, 0x000000FF); if(font_on){ std::stringstream s; s << " incoming: " << gs->board[p].ojamms_pending << " "; SDL_Color fg = {0xFF,0xFF,0xFF}; SDL_Color bg = {0x33,0x33,0x33}; SDL_Surface *fontsurf = TTF_RenderText(gs->font, s.str().c_str(),fg, bg); SDL_Rect fontblitrect = {x+r*4, y+2, 50, 50}; SDL_BlitSurface(fontsurf, NULL, screen, &fontblitrect); SDL_FreeSurface(fontsurf); } } } }
void draw_debug_line(char *str, int len) { SDL_Color background = { 0, 0, 0, 0 }; SDL_Color foreground = { 255, 255, 255, 0 }; SDL_Surface *textSurface = TTF_RenderText(font, str, foreground, background); SDL_Rect textLocation = { 0 + camera.x, 0 + camera.y + debug_line_num * 25, len, 25 }; SDL_Texture *text = SDL_CreateTextureFromSurface(renderer, textSurface); renderToBuffer(renderer, text, NULL, &textLocation); debug_line_num++; }
TextSurface::TextSurface(SDL_Renderer *renderer, std::string text, SDL_Color fgColor, TTF_Font *font) { SDL_Color bgColor = { 0, 0, 0, 0 }; SDL_Surface *surface = TTF_RenderText(font, text.c_str(), fgColor, bgColor); _renderHeight = surface->h; _renderWidth = surface->w; _texture = SDL_CreateTextureFromSurface(renderer, surface); SDL_SetTextureBlendMode(_texture, SDL_BLENDMODE_ADD); SDL_FreeSurface(surface); }
void draw(int deltaTimeMs) { float deltaTimeS = (float) deltaTimeMs / 1000; float fps = (float) 1.0 / deltaTimeS; renderToBuffer(renderer, map_tex, NULL, &map_rect); for (int i = 0; i < MAX_ENTITIES; i++) { if (entities[i] != NULL) rendererEntity(renderer, entities[i]); } SDL_Color background = { 0, 0, 0, 0 }; SDL_Color foreground = { 255, 255, 255, 0 }; char str[10]; sprintf(str, "%3.2f fps", fps); SDL_Surface *textSurface = TTF_RenderText(font, str, foreground, background); SDL_Rect textLocation = { 0 + camera.x, 0 + camera.y, 50, 25 }; SDL_Texture *text = SDL_CreateTextureFromSurface(renderer, textSurface); renderToBuffer(renderer, text, NULL, &textLocation); }
void text_layer_set_text(TextLayer *text_layer, const char *text) { text_layer->text = text; SDL_Surface *text_surface; SDL_Color bgcolor = getColor(text_layer->background_color); if(!(text_surface = TTF_RenderText(text_layer->font, text, getColor(text_layer->text_color), bgcolor))) { printf("[ERROR] TTF_RenderText_Solid: %s\n", TTF_GetError()); } else { SDL_Rect dst; dst.x = text_layer->layer.frame.origin.x; dst.y = text_layer->layer.frame.origin.y; if(bgcolor.unused == 255) { SDL_Surface *text_bgsurface = SDL_CreateRGBSurface(SDL_SWSURFACE, text_surface->w, text_surface->h, 32, 0,0,0,0); SDL_FillRect(text_bgsurface, NULL, SDL_MapRGBA(screen->format, bgcolor.r, bgcolor.g, bgcolor.b, bgcolor.unused)); SDL_BlitSurface(text_bgsurface, NULL, screen, &dst); SDL_FreeSurface(text_bgsurface); } SDL_BlitSurface(text_surface, NULL, screen, &dst); SDL_FreeSurface(text_surface); } }