6 |
6 |
7 #include "SDL.h" |
7 #include "SDL.h" |
8 #include <SDL/SDL_ttf.h> |
8 #include <SDL/SDL_ttf.h> |
9 |
9 |
10 #define DEFAULT_PTSIZE 30 |
10 #define DEFAULT_PTSIZE 30 |
11 #define DEFAULT_FONT "DroidSansFallback.ttf" |
11 #define DEFAULT_FONT "/System/Library/Fonts/华文细黑.ttf" |
12 #define MAX_TEXT_LENGTH 256 |
12 #define MAX_TEXT_LENGTH 256 |
13 |
13 |
14 SDL_Surface *screen; |
14 SDL_Surface *screen; |
15 TTF_Font *font; |
15 TTF_Font *font; |
16 SDL_Rect textRect, markedRect; |
16 SDL_Rect textRect, markedRect; |
17 Uint32 lineColor, backColor; |
17 Uint32 lineColor, backColor; |
18 SDL_Color textColor = { 0, 0, 0 }; |
18 SDL_Color textColor = { 0, 0, 0 }; |
19 char text[MAX_TEXT_LENGTH], *markedText; |
19 char text[MAX_TEXT_LENGTH], *markedText; |
20 |
20 |
|
21 void usage() |
|
22 { |
|
23 printf("usage: testime [--font fontfile] [--fullscreen]\n"); |
|
24 exit(0); |
|
25 } |
|
26 |
21 void InitVideo(int argc, char *argv[]) |
27 void InitVideo(int argc, char *argv[]) |
22 { |
28 { |
23 int width = 500, height = 250; |
29 int width = 500, height = 250; |
|
30 int flags = SDL_HWSURFACE; |
|
31 const char *fontname = DEFAULT_FONT; |
|
32 int fullscreen = 0; |
|
33 |
|
34 for (argc--, argv++; argc > 0; argc--, argv++) |
|
35 { |
|
36 if (strcmp(argv[0], "--help") == 0) |
|
37 usage(); |
|
38 |
|
39 else if (strcmp(argv[0], "--fullscreen") == 0) |
|
40 fullscreen = 1; |
|
41 |
|
42 else if (strcmp(argv[0], "--font") == 0) |
|
43 { |
|
44 argc--; |
|
45 argv++; |
|
46 |
|
47 if (argc > 0) |
|
48 fontname = argv[0]; |
|
49 else |
|
50 usage(); |
|
51 } |
|
52 } |
24 |
53 |
25 SDL_putenv("SDL_VIDEO_WINDOW_POS=center"); |
54 SDL_putenv("SDL_VIDEO_WINDOW_POS=center"); |
26 if (SDL_Init(SDL_INIT_VIDEO) < 0) |
55 if (SDL_Init(SDL_INIT_VIDEO) < 0) |
27 { |
56 { |
28 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); |
57 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); |
30 } |
59 } |
31 |
60 |
32 /* Initialize fonts */ |
61 /* Initialize fonts */ |
33 TTF_Init(); |
62 TTF_Init(); |
34 |
63 |
35 font = TTF_OpenFont(DEFAULT_FONT, DEFAULT_PTSIZE); |
64 font = TTF_OpenFont(fontname, DEFAULT_PTSIZE); |
36 if (! font) |
65 if (! font) |
37 { |
66 { |
38 fprintf(stderr, "Failed to find font: %s\n", SDL_GetError()); |
67 fprintf(stderr, "Failed to find font: %s\n", SDL_GetError()); |
39 exit(-1); |
68 exit(-1); |
40 } |
69 } |
41 |
70 |
|
71 printf("Using font: %s\n", fontname); |
42 atexit(SDL_Quit); |
72 atexit(SDL_Quit); |
43 |
73 |
44 int flags = SDL_HWSURFACE; |
74 if (fullscreen) |
45 if (argc > 1 && strcmp(argv[1], "--fullscreen") == 0) |
|
46 { |
75 { |
47 SDL_DisplayMode mode; |
76 SDL_DisplayMode mode; |
48 SDL_GetDesktopDisplayMode(&mode); |
77 SDL_GetDesktopDisplayMode(&mode); |
49 |
78 |
50 width = mode.w; |
79 width = mode.w; |
121 // Stop text input because we cannot hold any more characters |
150 // Stop text input because we cannot hold any more characters |
122 SDL_StopTextInput(); |
151 SDL_StopTextInput(); |
123 return; |
152 return; |
124 } |
153 } |
125 |
154 |
|
155 cursorRect = markedRect; |
|
156 cursorRect.w = 2; |
|
157 cursorRect.h = h; |
|
158 |
126 SDL_FillRect(screen, &markedRect, backColor); |
159 SDL_FillRect(screen, &markedRect, backColor); |
127 |
|
128 if (markedText) |
160 if (markedText) |
129 { |
161 { |
130 RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor); |
162 RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor); |
131 TTF_SizeUTF8(font, markedText, &w, &h); |
163 TTF_SizeUTF8(font, markedText, &w, &h); |
132 |
164 |
133 underlineRect = markedRect; |
165 underlineRect = markedRect; |
134 underlineRect.y += (h - 2); |
166 underlineRect.y += (h - 2); |
135 underlineRect.h = 2; |
167 underlineRect.h = 2; |
136 underlineRect.w = w; |
168 underlineRect.w = w; |
|
169 |
|
170 cursorRect.x += w + 1; |
|
171 |
137 SDL_FillRect(screen, &underlineRect, lineColor); |
172 SDL_FillRect(screen, &underlineRect, lineColor); |
138 } |
173 } |
139 |
174 |
140 cursorRect = markedRect; |
|
141 cursorRect.w = 2; |
|
142 cursorRect.h = h; |
|
143 SDL_FillRect(screen, &cursorRect, lineColor); |
175 SDL_FillRect(screen, &cursorRect, lineColor); |
144 |
176 |
145 SDL_Flip(screen); |
177 SDL_Flip(screen); |
146 |
178 |
147 SDL_SetTextInputRect(&markedRect); |
179 SDL_SetTextInputRect(&markedRect); |