1 #pragma warning(disable:4244; disable:4305; disable:4018)
6 #include "stb_wingraph.h"
8 #define STB_TRUETYPE_IMPLEMENTATION
9 #define STB_RECT_PACK_IMPLEMENTATION
10 #include "stb_rect_pack.h"
11 #include "stb_truetype.h"
14 #define CALLBACK __stdcall
15 #define WINGDIAPI __declspec(dllimport)
16 #define APIENTRY __stdcall
22 #define GL_FRAMEBUFFER_SRGB_EXT 0x8DB9
27 stbtt_packedchar chardata
[6][128];
29 int sx
=SIZE_X
, sy
=SIZE_Y
;
33 unsigned char temp_bitmap
[BITMAP_W
][BITMAP_H
];
34 unsigned char ttf_buffer
[1 << 25];
37 float scale
[2] = { 24.0f
, 14.0f
};
39 int sf
[6] = { 0,1,2, 0,1,2 };
43 stbtt_pack_context pc
;
47 char *win
= getenv("windir");
48 if (win
== NULL
) win
= getenv("SystemRoot");
50 f
= fopen(stb_wingraph_commandline
, "rb");
53 sprintf(filename
, "arial.ttf", win
);
55 sprintf(filename
, "%s/fonts/arial.ttf", win
);
56 f
= fopen(filename
, "rb");
60 fread(ttf_buffer
, 1, 1<<25, f
);
62 stbtt_PackBegin(&pc
, temp_bitmap
[0], BITMAP_W
, BITMAP_H
, 0, 1, NULL
);
63 for (i
=0; i
< 2; ++i
) {
64 stbtt_PackSetOversampling(&pc
, 1, 1);
65 stbtt_PackFontRange(&pc
, ttf_buffer
, 0, scale
[i
], 32, 95, chardata
[i
*3+0]+32);
66 stbtt_PackSetOversampling(&pc
, 2, 2);
67 stbtt_PackFontRange(&pc
, ttf_buffer
, 0, scale
[i
], 32, 95, chardata
[i
*3+1]+32);
68 stbtt_PackSetOversampling(&pc
, 3, 1);
69 stbtt_PackFontRange(&pc
, ttf_buffer
, 0, scale
[i
], 32, 95, chardata
[i
*3+2]+32);
73 glGenTextures(1, &font_tex
);
74 glBindTexture(GL_TEXTURE_2D
, font_tex
);
75 glTexImage2D(GL_TEXTURE_2D
, 0, GL_ALPHA
, BITMAP_W
, BITMAP_H
, 0, GL_ALPHA
, GL_UNSIGNED_BYTE
, temp_bitmap
);
76 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
77 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
84 glDisable(GL_CULL_FACE
);
85 glDisable(GL_TEXTURE_2D
);
86 glDisable(GL_LIGHTING
);
87 glDisable(GL_DEPTH_TEST
);
89 glViewport(0,0,sx
,sy
);
91 glClearColor(255,255,255,0);
93 glClearColor(0,0,0,0);
94 glClear(GL_COLOR_BUFFER_BIT
);
96 glMatrixMode(GL_PROJECTION
);
98 gluOrtho2D(0,sx
,sy
,0);
99 glMatrixMode(GL_MODELVIEW
);
104 void drawBoxTC(float x0
, float y0
, float x1
, float y1
, float s0
, float t0
, float s1
, float t1
)
106 glTexCoord2f(s0
,t0
); glVertex2f(x0
,y0
);
107 glTexCoord2f(s1
,t0
); glVertex2f(x1
,y0
);
108 glTexCoord2f(s1
,t1
); glVertex2f(x1
,y1
);
109 glTexCoord2f(s0
,t1
); glVertex2f(x0
,y1
);
114 void print(float x
, float y
, int font
, char *text
)
116 glEnable(GL_TEXTURE_2D
);
117 glBindTexture(GL_TEXTURE_2D
, font_tex
);
120 stbtt_aligned_quad q
;
121 stbtt_GetPackedQuad(chardata
[font
], BITMAP_W
, BITMAP_H
, *text
++, &x
, &y
, &q
, font
? 0 : integer_align
);
122 drawBoxTC(q
.x0
,q
.y0
,q
.x1
,q
.y1
, q
.s0
,q
.t0
,q
.s1
,q
.t1
);
131 float rotate_t
, translate_t
;
134 void draw_world(void)
136 int sfont
= sf
[font
];
139 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
147 print(80, 30, sfont
, "Controls:");
148 print(100, 60, sfont
, "S: toggle font size");
149 print(100, 85, sfont
, "O: toggle oversampling");
150 print(100,110, sfont
, "T: toggle translation");
151 print(100,135, sfont
, "R: toggle rotation");
152 print(100,160, sfont
, "P: toggle pixel-snap (only non-oversampled)");
153 print(100,185, sfont
, "G: toggle srgb gamma-correction");
155 print(100,210, sfont
, "B: toggle to white-on-black");
157 print(100,210, sfont
, "B: toggle to black-on-white");
158 print(100,235, sfont
, "V: view font texture");
160 print(80, 300, sfont
, "Current font:");
164 print(100, 350, sfont
, "Font height: 24 pixels");
166 print(100, 350, sfont
, "Font height: 14 pixels");
170 print(100, 325, sfont
, "2x2 oversampled text at 1:1");
171 else if (font
%3 == 2)
172 print(100, 325, sfont
, "3x1 oversampled text at 1:1");
173 else if (integer_align
)
174 print(100, 325, sfont
, "1:1 text, one texel = one pixel, snapped to integer coordinates");
176 print(100, 325, sfont
, "1:1 text, one texel = one pixel");
180 drawBoxTC(200,400, 200+BITMAP_W
,300+BITMAP_H
, 0,0,1,1);
183 glMatrixMode(GL_MODELVIEW
);
184 glTranslatef(200,350,0);
187 x
+= fmod(translate_t
*8,30);
190 glTranslatef(100,150,0);
191 glRotatef(rotate_t
*2,0,0,1);
192 glTranslatef(-100,-150,0);
194 print(x
,100, font
, "This is a test");
195 print(x
,130, font
, "Now is the time for all good men to come to the aid of their country.");
196 print(x
,160, font
, "The quick brown fox jumps over the lazy dog.");
197 print(x
,190, font
, "0123456789");
205 stbwingraph_SwapBuffers(NULL
);
208 static int initialized
=0;
209 static float last_dt
;
212 int raw_mouse_x
, raw_mouse_y
;
214 int loopmode(float dt
, int real
, int in_client
)
216 float actual_dt
= dt
;
218 if (!initialized
) return 0;
227 if (dt
> 0.25) dt
= 0.25;
228 if (dt
< 0.01) dt
= 0.01;
235 int winproc(void *data
, stbwingraph_event
*e
)
244 stbwingraph_ShowCursor(NULL
,1);
245 return STBWINGRAPH_winproc_exit
;
248 font
= (font
+1) % 3 + (font
/3)*3;
254 translating
= !translating
;
258 rotating
= !rotating
;
262 integer_align
= !integer_align
;
267 glEnable(GL_FRAMEBUFFER_SRGB_EXT
);
269 glDisable(GL_FRAMEBUFFER_SRGB_EXT
);
272 show_tex
= !show_tex
;
275 black_on_white
= !black_on_white
;
280 case STBWGE_mousemove
:
286 case STBWGE_mousewheel
: do_mouse(e
,0,0); break;
287 case STBWGE_leftdown
: do_mouse(e
, 1,0); break;
288 case STBWGE_leftup
: do_mouse(e
,-1,0); break;
289 case STBWGE_rightdown
: do_mouse(e
,0, 1); break;
290 case STBWGE_rightup
: do_mouse(e
,0,-1); break;
294 if (e
->key
== VK_RIGHT
) move
[0] = 1;
295 if (e
->key
== VK_LEFT
) move
[1] = 1;
296 if (e
->key
== VK_UP
) move
[2] = 1;
297 if (e
->key
== VK_DOWN
) move
[3] = 1;
300 if (e
->key
== VK_RIGHT
) move
[0] = 0;
301 if (e
->key
== VK_LEFT
) move
[1] = 0;
302 if (e
->key
== VK_UP
) move
[2] = 0;
303 if (e
->key
== VK_DOWN
) move
[3] = 0;
318 return STBWINGRAPH_unprocessed
;
323 void stbwingraph_main(void)
325 stbwingraph_Priority(2);
326 stbwingraph_CreateWindow(1, winproc
, NULL
, "tt", SIZE_X
,SIZE_Y
, 0, 1, 0, 0);
327 stbwingraph_ShowCursor(NULL
, 0);
330 stbwingraph_MainLoop(loopmode
, 0.016f
); // 30 fps = 0.33