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.
21 SDL_Surface
*glyphs
[128]; /* first 20 will be NULL */
30 void die(const char *fmt
, ...) __attribute__ ((noreturn
));
31 void die(const char *fmt
, ...)
35 vfprintf(stderr
, fmt
, ap
);
41 return ((double)SDL_GetTicks()) / 1000;
44 void display_text(const char *s
)
53 g
= glyphs
[(unsigned char)*s
];
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;
70 const char *fontname
= argv
[1] ?
72 : "/usr/share/fonts/truetype/ttf-bitstream-vera/VeraMono.ttf";
74 SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
);
77 font
= TTF_OpenFont(fontname
, 96);
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);
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
);
101 die("TTF_RenderGlyph_Solid\n");
109 SDL_FillRect(display
, NULL
, SDL_MapRGB(display
->format
, bg
.r
, bg
.g
, bg
.b
));
113 //sprintf(tmp, "%.3lf %.3lf %.3lf", now, now, now);
114 sprintf(tmp
, "%.3lf", now
);
117 SDL_UpdateRect(display
, 0,0,0,0);
119 usleep(1000/*=1ms*/);