int main(int argc, char *argv[]) { char *argv0 = argv[0]; SDL_Surface *screen; TTF_Font *font; SDL_Surface *text, *temp; int ptsize; int i, done; int rdiff, gdiff, bdiff; SDL_Color colors[NUM_COLORS]; SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 }; SDL_Color black = { 0x00, 0x00, 0x00, 0 }; SDL_Color *forecol; SDL_Color *backcol; SDL_Rect dstrect; SDL_Event event; int rendersolid; int renderstyle; int outline; int hinting; int kerning; int dump; enum { RENDER_LATIN1, RENDER_UTF8, RENDER_UNICODE } rendertype; char *message, string[128]; /* Look for special execution mode */ dump = 0; /* Look for special rendering types */ rendersolid = 0; renderstyle = TTF_STYLE_NORMAL; rendertype = RENDER_LATIN1; outline = 0; hinting = TTF_HINTING_NORMAL; kerning = 1; /* Default is black and white */ forecol = &black; backcol = &white; for ( i=1; argv[i] && argv[i][0] == '-'; ++i ) { if ( strcmp(argv[i], "-solid") == 0 ) { rendersolid = 1; } else if ( strcmp(argv[i], "-utf8") == 0 ) { rendertype = RENDER_UTF8; } else if ( strcmp(argv[i], "-unicode") == 0 ) { rendertype = RENDER_UNICODE; } else if ( strcmp(argv[i], "-b") == 0 ) { renderstyle |= TTF_STYLE_BOLD; } else if ( strcmp(argv[i], "-i") == 0 ) { renderstyle |= TTF_STYLE_ITALIC; } else if ( strcmp(argv[i], "-u") == 0 ) { renderstyle |= TTF_STYLE_UNDERLINE; } else if ( strcmp(argv[i], "-s") == 0 ) { renderstyle |= TTF_STYLE_STRIKETHROUGH; } else if ( strcmp(argv[i], "-outline") == 0 ) { if ( sscanf (argv[++i], "%d", &outline) != 1 ) { fprintf(stderr, Usage, argv0); return(1); } } else if ( strcmp(argv[i], "-hintlight") == 0 ) { hinting = TTF_HINTING_LIGHT; } else if ( strcmp(argv[i], "-hintmono") == 0 ) { hinting = TTF_HINTING_MONO; } else if ( strcmp(argv[i], "-hintnone") == 0 ) { hinting = TTF_HINTING_NONE; } else if ( strcmp(argv[i], "-nokerning") == 0 ) { kerning = 0; } else if ( strcmp(argv[i], "-dump") == 0 ) { dump = 1; } else if ( strcmp(argv[i], "-fgcol") == 0 ) { int r, g, b; if ( sscanf (argv[++i], "%d,%d,%d", &r, &g, &b) != 3 ) { fprintf(stderr, Usage, argv0); return(1); } forecol->r = (Uint8)r; forecol->g = (Uint8)g; forecol->b = (Uint8)b; } else if ( strcmp(argv[i], "-bgcol") == 0 ) { int r, g, b; if ( sscanf (argv[++i], "%d,%d,%d", &r, &g, &b) != 3 ) { fprintf(stderr, Usage, argv0); return(1); } backcol->r = (Uint8)r; backcol->g = (Uint8)g; backcol->b = (Uint8)b; } else { fprintf(stderr, Usage, argv0); return(1); } } argv += i; argc -= i; /* Check usage */ if ( ! argv[0] ) { fprintf(stderr, Usage, argv0); return(1); } /* Initialize SDL */ if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); return(2); } /* Initialize the TTF library */ if ( TTF_Init() < 0 ) { fprintf(stderr, "Couldn't initialize TTF: %s\n",SDL_GetError()); SDL_Quit(); return(2); } /* Open the font file with the requested point size */ ptsize = 0; if ( argc > 1 ) { ptsize = atoi(argv[1]); } if ( ptsize == 0 ) { i = 2; ptsize = DEFAULT_PTSIZE; } else { i = 3; } font = TTF_OpenFont(argv[0], ptsize); if ( font == NULL ) { fprintf(stderr, "Couldn't load %d pt font from %s: %s\n", ptsize, argv[0], SDL_GetError()); cleanup(2); } TTF_SetFontStyle(font, renderstyle); TTF_SetFontOutline(font, outline); TTF_SetFontKerning(font, kerning); TTF_SetFontHinting(font, hinting); if( dump ) { for( i = 48; i < 123; i++ ) { SDL_Surface* glyph = NULL; glyph = TTF_RenderGlyph_Shaded( font, i, *forecol, *backcol ); if( glyph ) { char outname[64]; sprintf( outname, "glyph-%d.bmp", i ); SDL_SaveBMP( glyph, outname ); } } cleanup(0); } /* Set a 640x480x8 video mode */ screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE); if ( screen == NULL ) { fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n", SDL_GetError()); cleanup(2); } /* Set a palette that is good for the foreground colored text */ rdiff = backcol->r - forecol->r; gdiff = backcol->g - forecol->g; bdiff = backcol->b - forecol->b; for ( i=0; i<NUM_COLORS; ++i ) { colors[i].r = forecol->r + (i*rdiff)/4; colors[i].g = forecol->g + (i*gdiff)/4; colors[i].b = forecol->b + (i*bdiff)/4; } SDL_SetColors(screen, colors, 0, NUM_COLORS); /* Clear the background to background color */ SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, backcol->r, backcol->g, backcol->b)); SDL_UpdateRect(screen, 0, 0, 0, 0); /* Show which font file we're looking at */ sprintf(string, "Font file: %s", argv[0]); /* possible overflow */ if ( rendersolid ) { text = TTF_RenderText_Solid(font, string, *forecol); } else { text = TTF_RenderText_Shaded(font, string, *forecol, *backcol); } if ( text != NULL ) { dstrect.x = 4; dstrect.y = 4; dstrect.w = text->w; dstrect.h = text->h; SDL_BlitSurface(text, NULL, screen, &dstrect); SDL_FreeSurface(text); } /* Render and center the message */ if ( argc > 2 ) { message = argv[2]; } else { message = DEFAULT_TEXT; } switch (rendertype) { case RENDER_LATIN1: if ( rendersolid ) { text = TTF_RenderText_Solid(font,message,*forecol); } else { text = TTF_RenderText_Shaded(font,message,*forecol,*backcol); } break; case RENDER_UTF8: if ( rendersolid ) { text = TTF_RenderUTF8_Solid(font,message,*forecol); } else { text = TTF_RenderUTF8_Shaded(font,message,*forecol,*backcol); } break; case RENDER_UNICODE: { Uint16 unicode_text[BUFSIZ]; int index; #ifdef HAVE_ICONV /* Use iconv to convert the message into utf-16. * "char" and "" are aliases for the local 8-bit encoding */ iconv_t cd; /*ICONV_CONST*/ char *from_str = message; char *to_str = (char*)unicode_text; size_t from_sz = strlen(message) + 1; size_t to_sz = sizeof(unicode_text); size_t res; int i; if ((cd = iconv_open("UTF-16", "char")) == (iconv_t)-1 && (cd = iconv_open("UTF-16", "")) == (iconv_t)-1) { perror("Couldn't open iconv"); exit(1); } res = iconv(cd, &from_str, &from_sz, &to_str, &to_sz); if (res == -1) { perror("Couldn't use iconv"); exit(1); } iconv_close(cd); #else /* Convert the message from ascii into utf-16. * This is unreliable as a test because it always * gives the local ordering. */ for (index = 0; message[index]; index++) { unicode_text[index] = message[index]; } unicode_text[index] = 0; #endif if ( rendersolid ) { text = TTF_RenderUNICODE_Solid(font, unicode_text, *forecol); } else { text = TTF_RenderUNICODE_Shaded(font, unicode_text, *forecol, *backcol); } } break; default: text = NULL; /* This shouldn't happen */ break; } if ( text == NULL ) { fprintf(stderr, "Couldn't render text: %s\n", SDL_GetError()); TTF_CloseFont(font); cleanup(2); } dstrect.x = (screen->w - text->w)/2; dstrect.y = (screen->h - text->h)/2; dstrect.w = text->w; dstrect.h = text->h; printf("Font is generally %d big, and string is %hd big\n", TTF_FontHeight(font), text->h); /* Blit the text surface */ if ( SDL_BlitSurface(text, NULL, screen, &dstrect) < 0 ) { fprintf(stderr, "Couldn't blit text to display: %s\n", SDL_GetError()); TTF_CloseFont(font); cleanup(2); } SDL_UpdateRect(screen, 0, 0, 0, 0); /* Set the text colorkey and convert to display format */ if ( SDL_SetColorKey(text, SDL_SRCCOLORKEY|SDL_RLEACCEL, 0) < 0 ) { fprintf(stderr, "Warning: Couldn't set text colorkey: %s\n", SDL_GetError()); } temp = SDL_DisplayFormat(text); if ( temp != NULL ) { SDL_FreeSurface(text); text = temp; } /* Wait for a keystroke, and blit text on mouse press */ done = 0; while ( ! done ) { if ( SDL_WaitEvent(&event) < 0 ) { fprintf(stderr, "SDL_PullEvent() error: %s\n", SDL_GetError()); done = 1; continue; } switch (event.type) { case SDL_MOUSEBUTTONDOWN: dstrect.x = event.button.x - text->w/2; dstrect.y = event.button.y - text->h/2; dstrect.w = text->w; dstrect.h = text->h; if ( SDL_BlitSurface(text, NULL, screen, &dstrect) == 0 ) { SDL_UpdateRects(screen, 1, &dstrect); } else { fprintf(stderr, "Couldn't blit text to display: %s\n", SDL_GetError()); } break; case SDL_KEYDOWN: case SDL_QUIT: done = 1; break; default: break; } } SDL_FreeSurface(text); TTF_CloseFont(font); cleanup(0); /* Not reached, but fixes compiler warnings */ return 0; }
ManifestBasedResources(std::string manifest, SDL_Renderer* renderer) { auto configuration = LoadLuaConfiguration(manifest); auto images = configuration.Get<LuaTable>("images"); images.ForAllIntegerKeys([&](int key, LuaType::Value type) { if (type == LuaType::table) { auto value = images.Get<LuaTable>(key); auto id = value.Get<int>("id"); auto path = value.Get<std::string>("path"); auto x = value.Get<int>("x"); auto y = value.Get<int>("y"); auto w = value.Get<int>("w"); auto h = value.Get<int>("h"); SDL_Rect rect; rect.x = x; rect.y = y; rect.w = w; rect.h = h; auto texture = texturesByName.find(path); if (texture == texturesByName.end()) { texturesByName[path] = std::shared_ptr<SDL_Texture>(IMG_LoadTexture(renderer, path.c_str()), SDL_DestroyTexture); texture = texturesByName.find(path); if (!texture->second) { printlog("Image '%s' IMG:%s\n", path.c_str(), IMG_GetError()); } } imageResources[id] = std::shared_ptr<ImageDesc>(new ImageDesc(texture->second, rect)); } }); auto sprites = configuration.Get<LuaTable>("sprites"); sprites.ForAllIntegerKeys([&](int key, LuaType::Value type) { if (type == LuaType::table) { auto sprite = sprites.Get<LuaTable>(key); auto id = sprite.Get<int>("id"); auto imageIds = sprite.Get<LuaTable>("imageIds"); unsigned int delay = sprite.Get<int>("delay"); std::vector< std::shared_ptr<ImageDesc> > imageList; imageIds.ForAllIntegerKeys([&](int key, LuaType::Value type) { auto imageId = imageIds.Get<int>(key); imageList.push_back(GetImage(imageId)); }); SDL_Rect rect = *(imageList[0]->GetRect()); spriteResources[id] = std::shared_ptr<SpriteDesc>(new SpriteDesc(imageList, rect, delay)); } }); auto fonts = configuration.Get<LuaTable>("fonts"); fonts.ForAllIntegerKeys([&](int key, LuaType::Value type) { if (type == LuaType::table) { auto font = fonts.Get<LuaTable>(key); auto id = font.Get<int>("id"); auto path = font.Get<std::string>("path"); auto color = font.Get<LuaTable>("color"); auto ptsize = font.Get<int>("ptsize"); std::stringstream ss; ss << path << "@" << ptsize; auto fontFile = fontsByName.find(ss.str()); if (fontFile == fontsByName.end()) { fontsByName[path] = std::shared_ptr<TTF_Font>(TTF_OpenFont(path.c_str(), ptsize), TTF_CloseFont); fontFile = fontsByName.find(path); TTF_SetFontHinting(fontFile->second.get(), 1); TTF_SetFontKerning(fontFile->second.get(), 1); } SDL_Color c; c.a = 0xff; c.r = (Uint8)color.Get<int>("r"); c.g = (Uint8)color.Get<int>("g"); c.b = (Uint8)color.Get<int>("b"); fontResources[id] = std::shared_ptr<TTFFontDesc>(new TTFFontDesc(fontFile->second, c)); } }); }
int main(int argc, char *argv[]) { char *argv0 = argv[0]; SDL_Window *window; SDL_Renderer *renderer; TTF_Font *font; SDL_Surface *text; Scene scene; int ptsize; int i, done; SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 }; SDL_Color black = { 0x00, 0x00, 0x00, 0 }; SDL_Color *forecol; SDL_Color *backcol; SDL_Event event; int rendersolid; int renderstyle; int outline; int hinting; int kerning; int dump; enum { RENDER_LATIN1, RENDER_UTF8, RENDER_UNICODE } rendertype; char *message, string[128]; /* Look for special execution mode */ dump = 0; /* Look for special rendering types */ rendersolid = 0; renderstyle = TTF_STYLE_NORMAL; rendertype = RENDER_LATIN1; outline = 0; hinting = TTF_HINTING_NORMAL; kerning = 1; /* Default is black and white */ forecol = &black; backcol = &white; for ( i=1; argv[i] && argv[i][0] == '-'; ++i ) { if ( strcmp(argv[i], "-solid") == 0 ) { rendersolid = 1; } else if ( strcmp(argv[i], "-utf8") == 0 ) { rendertype = RENDER_UTF8; } else if ( strcmp(argv[i], "-unicode") == 0 ) { rendertype = RENDER_UNICODE; } else if ( strcmp(argv[i], "-b") == 0 ) { renderstyle |= TTF_STYLE_BOLD; } else if ( strcmp(argv[i], "-i") == 0 ) { renderstyle |= TTF_STYLE_ITALIC; } else if ( strcmp(argv[i], "-u") == 0 ) { renderstyle |= TTF_STYLE_UNDERLINE; } else if ( strcmp(argv[i], "-s") == 0 ) { renderstyle |= TTF_STYLE_STRIKETHROUGH; } else if ( strcmp(argv[i], "-outline") == 0 ) { if ( sscanf (argv[++i], "%d", &outline) != 1 ) { fprintf(stderr, Usage, argv0); return(1); } } else if ( strcmp(argv[i], "-hintlight") == 0 ) { hinting = TTF_HINTING_LIGHT; } else if ( strcmp(argv[i], "-hintmono") == 0 ) { hinting = TTF_HINTING_MONO; } else if ( strcmp(argv[i], "-hintnone") == 0 ) { hinting = TTF_HINTING_NONE; } else if ( strcmp(argv[i], "-nokerning") == 0 ) { kerning = 0; } else if ( strcmp(argv[i], "-dump") == 0 ) { dump = 1; } else if ( strcmp(argv[i], "-fgcol") == 0 ) { int r, g, b; if ( sscanf (argv[++i], "%d,%d,%d", &r, &g, &b) != 3 ) { fprintf(stderr, Usage, argv0); return(1); } forecol->r = (Uint8)r; forecol->g = (Uint8)g; forecol->b = (Uint8)b; } else if ( strcmp(argv[i], "-bgcol") == 0 ) { int r, g, b; if ( sscanf (argv[++i], "%d,%d,%d", &r, &g, &b) != 3 ) { fprintf(stderr, Usage, argv0); return(1); } backcol->r = (Uint8)r; backcol->g = (Uint8)g; backcol->b = (Uint8)b; } else { fprintf(stderr, Usage, argv0); return(1); } } argv += i; argc -= i; /* Check usage */ if ( ! argv[0] ) { fprintf(stderr, Usage, argv0); return(1); } /* Initialize the TTF library */ if ( TTF_Init() < 0 ) { fprintf(stderr, "Couldn't initialize TTF: %s\n",SDL_GetError()); SDL_Quit(); return(2); } /* Open the font file with the requested point size */ ptsize = 0; if ( argc > 1 ) { ptsize = atoi(argv[1]); } if ( ptsize == 0 ) { i = 2; ptsize = DEFAULT_PTSIZE; } else { i = 3; } font = TTF_OpenFont(argv[0], ptsize); if ( font == NULL ) { fprintf(stderr, "Couldn't load %d pt font from %s: %s\n", ptsize, argv[0], SDL_GetError()); cleanup(2); } TTF_SetFontStyle(font, renderstyle); TTF_SetFontOutline(font, outline); TTF_SetFontKerning(font, kerning); TTF_SetFontHinting(font, hinting); if( dump ) { for( i = 48; i < 123; i++ ) { SDL_Surface* glyph = NULL; glyph = TTF_RenderGlyph_Shaded( font, i, *forecol, *backcol ); if( glyph ) { char outname[64]; sprintf( outname, "glyph-%d.bmp", i ); SDL_SaveBMP( glyph, outname ); } } cleanup(0); } /* Create a window */ if (SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, 0, &window, &renderer) < 0) { fprintf(stderr, "SDL_CreateWindowAndRenderer() failed: %s\n", SDL_GetError()); cleanup(2); } /* Show which font file we're looking at */ sprintf(string, "Font file: %s", argv[0]); /* possible overflow */ if ( rendersolid ) { text = TTF_RenderText_Solid(font, string, *forecol); } else { text = TTF_RenderText_Shaded(font, string, *forecol, *backcol); } if ( text != NULL ) { scene.captionRect.x = 4; scene.captionRect.y = 4; scene.captionRect.w = text->w; scene.captionRect.h = text->h; scene.caption = SDL_CreateTextureFromSurface(renderer, text); SDL_FreeSurface(text); } /* Render and center the message */ if ( argc > 2 ) { message = argv[2]; } else { message = DEFAULT_TEXT; } switch (rendertype) { case RENDER_LATIN1: if ( rendersolid ) { text = TTF_RenderText_Solid(font,message,*forecol); } else { text = TTF_RenderText_Shaded(font,message,*forecol,*backcol); } break; case RENDER_UTF8: if ( rendersolid ) { text = TTF_RenderUTF8_Solid(font,message,*forecol); } else { text = TTF_RenderUTF8_Shaded(font,message,*forecol,*backcol); } break; case RENDER_UNICODE: { Uint16 *unicode_text = SDL_iconv_utf8_ucs2(message); if ( rendersolid ) { text = TTF_RenderUNICODE_Solid(font, unicode_text, *forecol); } else { text = TTF_RenderUNICODE_Shaded(font, unicode_text, *forecol, *backcol); } SDL_free(unicode_text); } break; default: text = NULL; /* This shouldn't happen */ break; } if ( text == NULL ) { fprintf(stderr, "Couldn't render text: %s\n", SDL_GetError()); TTF_CloseFont(font); cleanup(2); } scene.messageRect.x = (WIDTH - text->w)/2; scene.messageRect.y = (HEIGHT - text->h)/2; scene.messageRect.w = text->w; scene.messageRect.h = text->h; scene.message = SDL_CreateTextureFromSurface(renderer, text); printf("Font is generally %d big, and string is %d big\n", TTF_FontHeight(font), text->h); draw_scene(renderer, &scene); /* Wait for a keystroke, and blit text on mouse press */ done = 0; while ( ! done ) { if ( SDL_WaitEvent(&event) < 0 ) { fprintf(stderr, "SDL_PullEvent() error: %s\n", SDL_GetError()); done = 1; continue; } switch (event.type) { case SDL_MOUSEBUTTONDOWN: scene.messageRect.x = event.button.x - text->w/2; scene.messageRect.y = event.button.y - text->h/2; scene.messageRect.w = text->w; scene.messageRect.h = text->h; draw_scene(renderer, &scene); break; case SDL_KEYDOWN: case SDL_QUIT: done = 1; break; default: break; } } SDL_FreeSurface(text); TTF_CloseFont(font); SDL_DestroyTexture(scene.caption); SDL_DestroyTexture(scene.message); cleanup(0); /* Not reached, but fixes compiler warnings */ return 0; }
int PDC_scr_open(int argc, char **argv) { PDC_LOG(("PDC_scr_open() - called\n")); SP = calloc(1, sizeof(SCREEN)); if (!SP) return ERR; pdc_own_screen = !pdc_screen; if (pdc_own_screen) { if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0) { fprintf(stderr, "Could not start SDL: %s\n", SDL_GetError()); return ERR; } atexit(_clean); } #ifdef PDC_WIDE if (!pdc_ttffont) { const char *ptsz, *fname; if (TTF_Init() == -1) { fprintf(stderr, "Could not start SDL_TTF: %s\n", SDL_GetError()); return ERR; } ptsz = getenv("PDC_FONT_SIZE"); if (ptsz != NULL) pdc_font_size = atoi(ptsz); if (pdc_font_size <= 0) pdc_font_size = 18; fname = getenv("PDC_FONT"); pdc_ttffont = TTF_OpenFont(fname ? fname : PDC_FONT_PATH, pdc_font_size); } if (!pdc_ttffont) { fprintf(stderr, "Could not load font\n"); return ERR; } TTF_SetFontKerning(pdc_ttffont, 0); TTF_SetFontHinting(pdc_ttffont, TTF_HINTING_MONO); SP->mono = FALSE; #else if (!pdc_font) { const char *fname = getenv("PDC_FONT"); pdc_font = SDL_LoadBMP(fname ? fname : "pdcfont.bmp"); } if (!pdc_font) pdc_font = SDL_LoadBMP_RW(SDL_RWFromMem(font437, sizeof(font437)), 0); if (!pdc_font) { fprintf(stderr, "Could not load font\n"); return ERR; } SP->mono = !pdc_font->format->palette; #endif if (!SP->mono && !pdc_back) { const char *bname = getenv("PDC_BACKGROUND"); pdc_back = SDL_LoadBMP(bname ? bname : "pdcback.bmp"); } if (!SP->mono && (pdc_back || !pdc_own_screen)) { SP->orig_attr = TRUE; SP->orig_fore = COLOR_WHITE; SP->orig_back = -1; } else SP->orig_attr = FALSE; #ifdef PDC_WIDE TTF_SizeText(pdc_ttffont, "W", &pdc_fwidth, &pdc_fheight); pdc_fthick = pdc_font_size / 20 + 1; #else pdc_fheight = pdc_font->h / 8; pdc_fwidth = pdc_font->w / 32; pdc_fthick = 1; if (!SP->mono) pdc_flastc = pdc_font->format->palette->ncolors - 1; #endif if (pdc_own_screen && !pdc_icon) { const char *iname = getenv("PDC_ICON"); pdc_icon = SDL_LoadBMP(iname ? iname : "pdcicon.bmp"); if (!pdc_icon) pdc_icon = SDL_LoadBMP_RW(SDL_RWFromMem(iconbmp, sizeof(iconbmp)), 0); if (pdc_icon) SDL_WM_SetIcon(pdc_icon, NULL); } if (pdc_own_screen) { const SDL_VideoInfo *info = SDL_GetVideoInfo(); max_height = info->current_h; max_width = info->current_w; const char *env = getenv("PDC_LINES"); pdc_sheight = (env ? atoi(env) : 25) * pdc_fheight; env = getenv("PDC_COLS"); pdc_swidth = (env ? atoi(env) : 80) * pdc_fwidth; pdc_screen = SDL_SetVideoMode(pdc_swidth, pdc_sheight, 0, SDL_SWSURFACE|SDL_ANYFORMAT|SDL_RESIZABLE); } else { if (!pdc_sheight) pdc_sheight = pdc_screen->h - pdc_yoffset; if (!pdc_swidth) pdc_swidth = pdc_screen->w - pdc_xoffset; } if (!pdc_screen) { fprintf(stderr, "Couldn't create a surface: %s\n", SDL_GetError()); return ERR; } if (SP->orig_attr) PDC_retile(); _initialize_colors(); SDL_EnableUNICODE(1); PDC_mouse_set(); if (pdc_own_screen) PDC_set_title(argc ? argv[0] : "PDCurses"); SP->lines = PDC_get_rows(); SP->cols = PDC_get_columns(); SP->mouse_wait = PDC_CLICK_PERIOD; SP->audible = FALSE; SP->termattrs = A_COLOR | A_UNDERLINE | A_LEFT | A_RIGHT | A_REVERSE; #ifdef PDC_WIDE SP->termattrs |= A_ITALIC; #endif PDC_reset_prog_mode(); return OK; }
void SDLFont::setKerning(boolType bKerning) { int kerning = (bKerning) ? 1 : 0; TTF_SetFontKerning(this->fontContext, kerning); }
int PDC_scr_open(int argc, char **argv) { int i; PDC_LOG(("PDC_scr_open() - called\n")); SP = calloc(1, sizeof(SCREEN)); if (!SP) return ERR; pdc_own_screen = !pdc_screen; if (pdc_own_screen) { if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0) { fprintf(stderr, "Could not start SDL: %s\n", SDL_GetError()); return ERR; } atexit(_clean); } #ifdef PDC_WIDE if (!pdc_ttffont) { const char *ptsz, *fname; if (TTF_Init() == -1) { fprintf(stderr, "Could not start SDL_TTF: %s\n", SDL_GetError()); return ERR; } ptsz = getenv("PDC_FONT_SIZE"); if (ptsz != NULL) pdc_font_size = atoi(ptsz); if (pdc_font_size <= 0) pdc_font_size = 18; fname = getenv("PDC_FONT"); pdc_ttffont = TTF_OpenFont(fname ? fname : PDC_FONT_PATH, pdc_font_size); } if (!pdc_ttffont) { fprintf(stderr, "Could not load font\n"); return ERR; } TTF_SetFontKerning(pdc_ttffont, 0); TTF_SetFontHinting(pdc_ttffont, TTF_HINTING_MONO); SP->mono = FALSE; #else if (!pdc_font) { const char *fname = getenv("PDC_FONT"); pdc_font = SDL_LoadBMP(fname ? fname : "pdcfont.bmp"); } if (!pdc_font) pdc_font = SDL_LoadBMP_RW(SDL_RWFromMem(deffont, sizeof(deffont)), 0); if (!pdc_font) { fprintf(stderr, "Could not load font\n"); return ERR; } SP->mono = !pdc_font->format->palette; #endif if (!SP->mono && !pdc_back) { const char *bname = getenv("PDC_BACKGROUND"); pdc_back = SDL_LoadBMP(bname ? bname : "pdcback.bmp"); } if (!SP->mono && (pdc_back || !pdc_own_screen)) { SP->orig_attr = TRUE; SP->orig_fore = COLOR_WHITE; SP->orig_back = -1; } else SP->orig_attr = FALSE; #ifdef PDC_WIDE TTF_SizeText(pdc_ttffont, "W", &pdc_fwidth, &pdc_fheight); #else pdc_fheight = pdc_font->h / 8; pdc_fwidth = pdc_font->w / 32; if (!SP->mono) pdc_flastc = pdc_font->format->palette->ncolors - 1; #endif if (pdc_own_screen && !pdc_icon) { const char *iname = getenv("PDC_ICON"); pdc_icon = SDL_LoadBMP(iname ? iname : "pdcicon.bmp"); if (!pdc_icon) pdc_icon = SDL_LoadBMP_RW(SDL_RWFromMem(deficon, sizeof(deficon)), 0); if (pdc_icon) SDL_WM_SetIcon(pdc_icon, NULL); } if (pdc_own_screen) { const char *env = getenv("PDC_LINES"); pdc_sheight = (env ? atoi(env) : 25) * pdc_fheight; env = getenv("PDC_COLS"); pdc_swidth = (env ? atoi(env) : 80) * pdc_fwidth; pdc_screen = SDL_SetVideoMode(pdc_swidth, pdc_sheight, 0, SDL_SWSURFACE|SDL_ANYFORMAT|SDL_RESIZABLE); } else { if (!pdc_sheight) pdc_sheight = pdc_screen->h - pdc_yoffset; if (!pdc_swidth) pdc_swidth = pdc_screen->w - pdc_xoffset; } if (!pdc_screen) { fprintf(stderr, "Couldn't create a surface: %s\n", SDL_GetError()); return ERR; } if (SP->orig_attr) PDC_retile(); for (i = 0; i < 8; i++) { pdc_color[i].r = (i & COLOR_RED) ? 0xc0 : 0; pdc_color[i].g = (i & COLOR_GREEN) ? 0xc0 : 0; pdc_color[i].b = (i & COLOR_BLUE) ? 0xc0 : 0; pdc_color[i + 8].r = (i & COLOR_RED) ? 0xff : 0x40; pdc_color[i + 8].g = (i & COLOR_GREEN) ? 0xff : 0x40; pdc_color[i + 8].b = (i & COLOR_BLUE) ? 0xff : 0x40; } for (i = 0; i < 16; i++) pdc_mapped[i] = SDL_MapRGB(pdc_screen->format, pdc_color[i].r, pdc_color[i].g, pdc_color[i].b); SDL_EnableUNICODE(1); PDC_mouse_set(); if (pdc_own_screen) PDC_set_title(argc ? argv[0] : "PDCurses"); SP->lines = PDC_get_rows(); SP->cols = PDC_get_columns(); SP->mouse_wait = PDC_CLICK_PERIOD; SP->audible = FALSE; PDC_reset_prog_mode(); return OK; }