static void init_font(rgui_handle_t *rgui, const char *path) { for (unsigned i = 0; i < 256; i++) { unsigned y = i / 16; unsigned x = i % 16; copy_glyph(rgui->font_white[i], rgui->font_green[i], rgui->font_buf + 54 + 3 * (256 * (255 - 16 * y) + 16 * x)); } }
static void init_font(rgui_handle_t *rgui, const uint8_t *font_bmp_buf) { uint8_t *font = (uint8_t *) calloc(1, FONT_OFFSET(256)); rgui->alloc_font = true; for (unsigned i = 0; i < 256; i++) { unsigned y = i / 16; unsigned x = i % 16; copy_glyph(&font[FONT_OFFSET(i)], font_bmp_buf + 54 + 3 * (256 * (255 - 16 * y) + 16 * x)); } rgui->font = font; }
// Old style "blitting", so we can render all the fonts in one go. // TODO: Is it possible that fonts could overlap if we blit without alpha blending? static void blit_fonts(gl_t *gl, const struct font_output *head, const struct font_rect *geom) { memset(gl->font_tex_buf, 0, gl->font_tex_w * gl->font_tex_h * sizeof(uint16_t)); while (head) { copy_glyph(head, geom, gl->font_tex_buf, gl->font_tex_w, gl->font_tex_h); head = head->next; } glPixelStorei(GL_UNPACK_ALIGNMENT, 8); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, gl->font_tex_w, gl->font_tex_h, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, gl->font_tex_buf); }
int main(int argc, char *argv[]) { char *input_filename=NULL; char *output_filename=NULL; BITMAP *bmp=NULL; unsigned char *output_buffer=NULL; FILE *fp; int i; STARTUPINFO si; PROCESS_INFORMATION pi; char path[MAX_PATH]; char args[MAX_PATH * 3]; char temp[MAX_PATH], temp2[MAX_PATH]; RGB pal[256]; char xml_filename[MAX_PATH], png_filename[MAX_PATH]; char *p; font_struct font; char *bmfontgen_filename="BMFontGen.exe"; memset(&font, 0, sizeof(font)); if (argc < 4) { printf ("fontgen [options] [filename].txt -o [filename].bin\n"); goto error; } for (i = 1; i < argc;) { if (strcmp(argv[i], "-2") == 0) { round_up_width = TRUE; i++; } else if (strcmp(argv[i], "-b") == 0) { bmfontgen_filename = argv[i+1]; fix_path(bmfontgen_filename); i += 2; } else if (strcmp (argv[i], "-o") == 0) { output_filename = argv[i+1]; fix_path(output_filename); i += 2; } else { input_filename = argv[i]; fix_path(input_filename); i++; } } if (input_filename == NULL) { printf ("no input filename specified\n"); exit (1); } if (output_filename == NULL) { printf ("no output filename specified\n"); exit (1); } convert_slash(input_filename); convert_slash(output_filename); // Execute BMFontGen GetCurrentDirectoryA(sizeof(temp), temp); if (!PathRelativePathToA(temp2, temp, FILE_ATTRIBUTE_DIRECTORY, bmfontgen_filename, FILE_ATTRIBUTE_NORMAL)) strcpy_s(temp2, sizeof(temp2), bmfontgen_filename); sprintf_s(args, sizeof(args), "%s -optfile ", temp2); if (!PathRelativePathToA(temp2, temp, FILE_ATTRIBUTE_DIRECTORY, input_filename, FILE_ATTRIBUTE_NORMAL)) strcpy_s(temp2, sizeof(temp2), input_filename); strcat_s(args, sizeof(args), temp2); strcpy_s(path, sizeof(path), temp2); if ((p = strrchr(path, '\\')) != NULL) p[1] = '\0'; strcat_s(args, sizeof(args), " -output "); strcat_s(args, sizeof(args), path); strcat_s(args, sizeof(args), "font"); ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); si.wShowWindow = SW_HIDE; si.dwFlags = 0; printf("%s\n", args); if (CreateProcessA(NULL, args, NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &si, &pi) == 0) { LPVOID lpMsgBuf; DWORD err=GetLastError(); FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf, 0, NULL); printf("Error executing BMFontGen: %s\n", lpMsgBuf); LocalFree(lpMsgBuf); exit(1); } printf("Creating font..."); WaitForSingleObject(pi.hProcess, INFINITE); printf("done\n"); // Close process and thread handles. CloseHandle( pi.hProcess ); CloseHandle( pi.hThread ); // Load XML file strcpy_s(xml_filename, sizeof(xml_filename), path); strcat_s(xml_filename, sizeof(xml_filename), "font.xml"); read_font_xml(xml_filename, &font); // Start going through XML and copying font to our 12x12 1BPP buffer allegro_init(); loadpng_init(); set_color_depth(8); if (strrchr(font.bitmaps[0].filename, '\\') == NULL) { strcpy_s(png_filename, sizeof(png_filename), input_filename); p = strrchr(png_filename, '\\'); strcpy_s(p+1, sizeof(png_filename)-((p+1)-png_filename), font.bitmaps[0].filename); } else strcpy_s(png_filename, sizeof(png_filename), font.bitmaps[0].filename); if ((bmp = load_bitmap(png_filename, pal)) == NULL) { printf("Error loading generated png file\n"); exit(1); } output_buffer = malloc(12 * 12 / 8 * 1024); if (output_buffer == NULL) { printf("Error allocating buffer\n"); exit(1); } memset(output_buffer, 0, 12 * 12 / 8 * 1024); // Copy glyphs to raw buffer for (i = 0; i < font.num_glyphs; i++) copy_glyph(&font.glyphs[i], bmp, output_buffer); // Write final output if (fopen_s(&fp, output_filename, "wb") != 0) { printf("Error writing output\n"); goto error; } fwrite((void *)output_buffer, 1, 12 * 12 / 8 * 1024, fp); fclose(fp); error: if (bmp) destroy_bitmap(bmp); if (font.bitmaps) free(font.bitmaps); if (font.glyphs) free(font.glyphs); //printf ( "Press any key to continue..." ); //getch(); return 0; }