#include #include #include #include #include FT_FREETYPE_H #include #include struct glyph { int32_t width; int32_t height; int32_t horiBearingX; int32_t horiBearingY; int32_t horiAdvance; SDL_Texture * texture; }; static struct glyph glyphs[0x80 - 0x20] = {0}; int32_t face_height; int load_outline_char(SDL_Renderer * renderer, const FT_Face face, const FT_Int32 load_flags, const FT_Render_Mode render_mode, const FT_ULong char_code) { FT_Error error; FT_UInt glyph_index = FT_Get_Char_Index(face, char_code); error = FT_Load_Glyph(face, glyph_index, load_flags); if (error) { printf("FT_Load_Glyph %s\n", FT_Error_String(error)); return -1; } error = FT_Render_Glyph(face->glyph, render_mode); if (error) { printf("FT_Render_Glyph %s\n", FT_Error_String(error)); return -1; } struct glyph * glyph = &glyphs[char_code - 0x20]; glyph->width = face->glyph->bitmap.width; glyph->height = face->glyph->bitmap.rows; glyph->horiBearingX = face->glyph->metrics.horiBearingX; glyph->horiBearingY = face->glyph->metrics.horiBearingY; glyph->horiAdvance = face->glyph->metrics.horiAdvance; if (face->glyph->bitmap.pitch != 0) { SDL_Surface * surface = SDL_CreateSurface(face->glyph->bitmap.width, face->glyph->bitmap.rows, SDL_PIXELFORMAT_RGBA8888); SDL_LockSurface(surface); for (int y = 0; y < face->glyph->bitmap.rows; y++) { for (int x = 0; x < face->glyph->bitmap.width; x++) { int s_ix = y * face->glyph->bitmap.pitch + x; int d_ix = y * surface->pitch + x * 4; uint8_t gray = face->glyph->bitmap.buffer[s_ix]; ((uint8_t *)surface->pixels)[d_ix + 0] = gray; ((uint8_t *)surface->pixels)[d_ix + 1] = gray; ((uint8_t *)surface->pixels)[d_ix + 2] = gray; ((uint8_t *)surface->pixels)[d_ix + 3] = 255; } } SDL_UnlockSurface(surface); if (glyph->texture != NULL) SDL_DestroyTexture(glyph->texture); glyph->texture = SDL_CreateTextureFromSurface(renderer, surface); if (glyph->texture == NULL) { printf("%s\n", SDL_GetError()); } assert(glyph->texture != NULL); SDL_DestroySurface(surface); } return 0; } int load_font(SDL_Renderer * renderer, int font_size) { FT_Library library; FT_Face face; FT_Error error; error = FT_Init_FreeType(&library); if (error) { printf("FT_Init_FreeType\n"); return -1; } error = FT_New_Face(library, "DejaVuSansMono.ttf", 0, &face); if (error) { printf("FT_New_Face\n"); return -1; } error = FT_Set_Pixel_Sizes(face, 0, font_size); if (error) { printf("FT_Set_Pixel_Sizes: %s %d\n", FT_Error_String(error), error); return -1; } for (int char_code = 0x20; char_code <= 0x7f; char_code++) { load_outline_char(renderer, face, FT_LOAD_DEFAULT, FT_RENDER_MODE_NORMAL, char_code); } printf("loaded size %ld\n", face->size->metrics.height >> 6); face_height = face->size->metrics.height; return 0; } void render(SDL_Renderer * renderer, int window_width, int window_height, const char * s, int length) { int32_t advance = (window_width - (window_width * 5 / 100)) << 6; for (int i = (length - 1); i >= 0; i--) { char c = s[i]; struct glyph * glyph = &glyphs[c - 0x20]; advance -= glyph->horiAdvance; if (glyph->texture != NULL) { float x = (float)(advance + glyph->horiBearingX) / 64.f; float y = (window_height / 2) + (face_height >> 6) / 3 + (float)(-glyph->horiBearingY) / 64.f; SDL_FRect srect = { .x = 0.f, .y = 0.f, .w = glyph->width, .h = glyph->height }; SDL_FRect drect = { .x = x, .y = y, .w = glyph->width, .h = glyph->height }; SDL_RenderTexture(renderer, glyph->texture, &srect, &drect); } } } int max(int a, int b) { return a > b ? a : b; } int main() { int ret; SDL_Window * window; SDL_Renderer * renderer; ret = SDL_Init(SDL_INIT_VIDEO); assert(ret == 0); window = SDL_CreateWindow("timer", 512, // w 512, // h SDL_WINDOW_RESIZABLE);// | SDL_WINDOW_MAXIMIZED); renderer = SDL_CreateRenderer(window, NULL); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_PropertiesID props = SDL_GetRendererProperties(renderer); const char * name = SDL_GetRendererName(renderer); assert(name != NULL); printf("renderer: %s\n", name); const SDL_PixelFormatEnum * formats = SDL_GetProperty(props, SDL_PROP_RENDERER_TEXTURE_FORMATS_POINTER, NULL); assert(formats != NULL); while (*formats != SDL_PIXELFORMAT_UNKNOWN) { printf("%s\n", SDL_GetPixelFormatName(*formats++)); } int last_width = -1; uint64_t ticks = SDL_GetTicks(); const int min_length = 4; int max_length = min_length; /* */ uint8_t read_buf[32] = {'7', '1', '.', '0', '1', '\r'}; struct state state = {0}; int i = 0; while (1) { i++; //enum sp_return sp_ret = sp_nonblocking_read(port, read_buf, (sizeof (read_buf))); enum sp_return sp_ret = 6; if (sp_ret < 0) { printf("sp_nonblocking_read error\n"); break; } handle_state(read_buf, sp_ret, &state); char str_buf[16]; switch (state.counter_state) { case COUNTER_STOPPED: snprintf(str_buf, (sizeof (str_buf)) - 1, "%d.%02d", state.time.whole, state.time.fraction); break; case COUNTER_RUNNING: { uint64_t counter = SDL_GetPerformanceCounter(); uint64_t frequency = SDL_GetPerformanceFrequency(); double duration = (((double)counter) - ((double)state.counter)) / ((double)frequency); snprintf(str_buf, (sizeof (str_buf)) - 1, "%.02f", duration); } break; default: str_buf[0] = 0; break; } int length = strlen(str_buf); SDL_RenderClear(renderer); int window_width; int window_height; ret = SDL_GetWindowSizeInPixels(window, &window_width, &window_height); assert(ret == 0); if ((window_width != last_width) || (length > max_length)) { max_length = max(min_length, length); last_width = window_width; int divisor = max(1, (max_length * 8 / 10)); int font_size = (window_width * 95 / 100) / divisor; printf("fs %d %d %d\n", font_size, window_width, max_length); load_font(renderer, font_size); } render(renderer, window_width, window_height, str_buf, length); while (SDL_GetTicks() - ticks < (1000 / 60)) { SDL_Delay(1); } SDL_RenderPresent(renderer); ticks = SDL_GetTicks(); SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_EVENT_QUIT: goto exit; case SDL_EVENT_KEY_DOWN: if (event.key.keysym.sym == SDLK_ESCAPE) goto exit; break; default: break; } } } exit: SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); }