Cosmetics
[rawv.git] / hrclock.cpp
blob9101740ce645e04d2d2f83c028bd36f5737ff7fe
1 /* high resolution clock display
2 * Copyright (C) 2010 Kirill Smelkov <kirr@navytux.spb.ru>
4 * This library is free software: you can Use, Study, Modify and Redistribute
5 * it under the terms of the GNU Lesser General Public License version 2.1, or
6 * any later version. This library is distributed WITHOUT ANY WARRANTY. See
7 * COPYING.LIB file for full License terms.
8 */
10 #include <SDL.h>
11 #include <SDL_ttf.h>
13 #include <unistd.h>
15 // XXX error handling
17 SDL_Surface *display;
19 TTF_Font *font;
20 int font_height;
21 SDL_Surface *glyphs[128]; /* first 20 will be NULL */
23 SDL_Color fg;
24 SDL_Color bg;
28 * Utils
30 void die(const char *fmt, ...) __attribute__ ((noreturn));
31 void die(const char *fmt, ...)
33 va_list ap;
34 va_start(ap, fmt);
35 vfprintf(stderr, fmt, ap);
36 exit(1);
39 double microtime()
41 return ((double)SDL_GetTicks()) / 1000;
44 void display_text(const char *s)
46 SDL_Surface *g;
47 SDL_Rect r;
49 r.x = 0;
50 r.y = 0;
52 for (; *s!=0; ++s) {
53 g = glyphs[(unsigned char)*s];
54 r.w = g->w;
55 r.h = g->h;
56 r.y = font_height - g->h;
57 SDL_BlitSurface(g, NULL/*whole glyph*/, display, &r);
59 r.x += g->w ? g->w : 32;
64 int main(int argc, char *argv[])
66 int width = 300, height = 120;
67 int ch;
68 double now;
70 const char *fontname = argv[1] ?
71 argv[1]
72 : "/usr/share/fonts/truetype/ttf-bitstream-vera/VeraMono.ttf";
74 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
75 TTF_Init();
77 font = TTF_OpenFont(fontname, 96);
78 if (!font)
79 die("TTF_OpenFont: %s\n", TTF_GetError());
82 TTF_SetFontStyle(font, TTF_STYLE_NORMAL);
84 if (!TTF_FontFaceIsFixedWidth(font))
85 fprintf(stderr, "W: non monospaced font...\n");
87 font_height = TTF_FontHeight(font);
90 display = SDL_SetVideoMode(width, height, /*bpp=*/0, /*flags=*/0UL);
91 if (!display)
92 die("SDL_SetVideoMode: %s\n", SDL_GetError());
94 fg.r=0xff; fg.g=0xff; fg.b=0xff;
95 bg.r=0x00; bg.g=0x00; bg.b=0x00;
97 /* render characters */
98 for (ch=20; ch<128; ++ch) {
99 glyphs[ch] = TTF_RenderGlyph_Solid(font, ch, fg);
100 if (!glyphs[ch])
101 die("TTF_RenderGlyph_Solid\n");
106 while (1) {
107 char tmp[80];
109 SDL_FillRect(display, NULL, SDL_MapRGB(display->format, bg.r, bg.g, bg.b));
111 now = microtime();
113 //sprintf(tmp, "%.3lf %.3lf %.3lf", now, now, now);
114 sprintf(tmp, "%.3lf", now);
115 display_text(tmp);
117 SDL_UpdateRect(display, 0,0,0,0);
119 usleep(1000/*=1ms*/);
123 TTF_CloseFont(font);
125 TTF_Quit();
126 SDL_Quit();
128 return 0;