| 1 | // |
| 2 | // Copyright (c) 2009-2010 Mikko Mononen [email protected] |
| 3 | // |
| 4 | // This software is provided 'as-is', without any express or implied |
| 5 | // warranty. In no event will the authors be held liable for any damages |
| 6 | // arising from the use of this software. |
| 7 | // Permission is granted to anyone to use this software for any purpose, |
| 8 | // including commercial applications, and to alter it and redistribute it |
| 9 | // freely, subject to the following restrictions: |
| 10 | // 1. The origin of this software must not be misrepresented; you must not |
| 11 | // claim that you wrote the original software. If you use this software |
| 12 | // in a product, an acknowledgment in the product documentation would be |
| 13 | // appreciated but is not required. |
| 14 | // 2. Altered source versions must be plainly marked as such, and must not be |
| 15 | // misrepresented as being the original software. |
| 16 | // 3. This notice may not be removed or altered from any source distribution. |
| 17 | // |
| 18 | |
| 19 | #include <cmath> |
| 20 | #include <cstdio> |
| 21 | #include "imgui.h" |
| 22 | #include "SDL.h" |
| 23 | #include "SDL_opengl.h" |
| 24 | |
| 25 | // Some math headers don't have PI defined. |
| 26 | static const float PI = 3.14159265f; |
| 27 | |
| 28 | void imguifree(void* ptr, void* userptr); |
| 29 | void* imguimalloc(size_t size, void* userptr); |
| 30 | |
| 31 | #define STBTT_malloc(x,y) imguimalloc(x,y) |
| 32 | #define STBTT_free(x,y) imguifree(x,y) |
| 33 | #define STB_TRUETYPE_IMPLEMENTATION |
| 34 | #include "stb_truetype.h" |
| 35 | |
| 36 | void imguifree(void* ptr, void* /*userptr*/) |
| 37 | { |
| 38 | free(ptr); |
| 39 | } |
| 40 | |
| 41 | void* imguimalloc(size_t size, void* /*userptr*/) |
| 42 | { |
| 43 | return malloc(size); |
| 44 | } |
| 45 | |
| 46 | static const unsigned TEMP_COORD_COUNT = 100; |
| 47 | static float g_tempCoords[TEMP_COORD_COUNT*2]; |
| 48 | static float g_tempNormals[TEMP_COORD_COUNT*2]; |
| 49 | |
| 50 | static const int CIRCLE_VERTS = 8*4; |
| 51 | static float g_circleVerts[CIRCLE_VERTS*2]; |
| 52 | |
| 53 | static stbtt_bakedchar g_cdata[96]; // ASCII 32..126 is 95 glyphs |
| 54 | static GLuint g_ftex = 0; |
| 55 | |
| 56 | inline unsigned int RGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a) |
| 57 | { |
| 58 | return (r) | (g << 8) | (b << 16) | (a << 24); |
| 59 | } |
| 60 | |
| 61 | static void drawPolygon(const float* coords, unsigned numCoords, float r, unsigned int col) |
| 62 | { |
| 63 | if (numCoords > TEMP_COORD_COUNT) numCoords = TEMP_COORD_COUNT; |
| 64 | |
| 65 | for (unsigned i = 0, j = numCoords-1; i < numCoords; j=i++) |
| 66 | { |
| 67 | const float* v0 = &coords[j*2]; |
| 68 | const float* v1 = &coords[i*2]; |
| 69 | float dx = v1[0] - v0[0]; |
| 70 | float dy = v1[1] - v0[1]; |
| 71 | float d = sqrtf(dx*dx+dy*dy); |
| 72 | if (d > 0) |
| 73 | { |
| 74 | d = 1.0f/d; |
| 75 | dx *= d; |
| 76 | dy *= d; |
| 77 | } |
| 78 | g_tempNormals[j*2+0] = dy; |
| 79 | g_tempNormals[j*2+1] = -dx; |
| 80 | } |
| 81 | |
| 82 | for (unsigned i = 0, j = numCoords-1; i < numCoords; j=i++) |
| 83 | { |
| 84 | float dlx0 = g_tempNormals[j*2+0]; |
| 85 | float dly0 = g_tempNormals[j*2+1]; |
| 86 | float dlx1 = g_tempNormals[i*2+0]; |
| 87 | float dly1 = g_tempNormals[i*2+1]; |
| 88 | float dmx = (dlx0 + dlx1) * 0.5f; |
| 89 | float dmy = (dly0 + dly1) * 0.5f; |
| 90 | float dmr2 = dmx*dmx + dmy*dmy; |
| 91 | if (dmr2 > 0.000001f) |
| 92 | { |
| 93 | float scale = 1.0f / dmr2; |
| 94 | if (scale > 10.0f) scale = 10.0f; |
| 95 | dmx *= scale; |
| 96 | dmy *= scale; |
| 97 | } |
| 98 | g_tempCoords[i*2+0] = coords[i*2+0]+dmx*r; |
| 99 | g_tempCoords[i*2+1] = coords[i*2+1]+dmy*r; |
| 100 | } |
| 101 | |
| 102 | unsigned int colTrans = RGBA(col&0xff, (col>>8)&0xff, (col>>16)&0xff, 0); |
| 103 | |
| 104 | glBegin(GL_TRIANGLES); |
| 105 | |
| 106 | glColor4ubv((GLubyte*)&col); |
| 107 | |
| 108 | for (unsigned i = 0, j = numCoords-1; i < numCoords; j=i++) |
| 109 | { |
| 110 | glVertex2fv(&coords[i*2]); |
| 111 | glVertex2fv(&coords[j*2]); |
| 112 | glColor4ubv((GLubyte*)&colTrans); |
| 113 | glVertex2fv(&g_tempCoords[j*2]); |
| 114 | |
| 115 | glVertex2fv(&g_tempCoords[j*2]); |
| 116 | glVertex2fv(&g_tempCoords[i*2]); |
| 117 | |
| 118 | glColor4ubv((GLubyte*)&col); |
| 119 | glVertex2fv(&coords[i*2]); |
| 120 | } |
| 121 | |
| 122 | glColor4ubv((GLubyte*)&col); |
| 123 | for (unsigned i = 2; i < numCoords; ++i) |
| 124 | { |
| 125 | glVertex2fv(&coords[0]); |
| 126 | glVertex2fv(&coords[(i-1)*2]); |
| 127 | glVertex2fv(&coords[i*2]); |
| 128 | } |
| 129 | |
| 130 | glEnd(); |
| 131 | } |
| 132 | |
| 133 | static void drawRect(float x, float y, float w, float h, float fth, unsigned int col) |
| 134 | { |
| 135 | float verts[4*2] = |
| 136 | { |
| 137 | x+0.5f, y+0.5f, |
| 138 | x+w-0.5f, y+0.5f, |
| 139 | x+w-0.5f, y+h-0.5f, |
| 140 | x+0.5f, y+h-0.5f, |
| 141 | }; |
| 142 | drawPolygon(verts, 4, fth, col); |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | static void drawEllipse(float x, float y, float w, float h, float fth, unsigned int col) |
| 147 | { |
| 148 | float verts[CIRCLE_VERTS*2]; |
| 149 | const float* cverts = g_circleVerts; |
| 150 | float* v = verts; |
| 151 | |
| 152 | for (int i = 0; i < CIRCLE_VERTS; ++i) |
| 153 | { |
| 154 | *v++ = x + cverts[i*2]*w; |
| 155 | *v++ = y + cverts[i*2+1]*h; |
| 156 | } |
| 157 | |
| 158 | drawPolygon(verts, CIRCLE_VERTS, fth, col); |
| 159 | } |
| 160 | */ |
| 161 | |
| 162 | static void drawRoundedRect(float x, float y, float w, float h, float r, float fth, unsigned int col) |
| 163 | { |
| 164 | const unsigned n = CIRCLE_VERTS/4; |
| 165 | float verts[(n+1)*4*2]; |
| 166 | const float* cverts = g_circleVerts; |
| 167 | float* v = verts; |
| 168 | |
| 169 | for (unsigned i = 0; i <= n; ++i) |
| 170 | { |
| 171 | *v++ = x+w-r + cverts[i*2]*r; |
| 172 | *v++ = y+h-r + cverts[i*2+1]*r; |
| 173 | } |
| 174 | |
| 175 | for (unsigned i = n; i <= n*2; ++i) |
| 176 | { |
| 177 | *v++ = x+r + cverts[i*2]*r; |
| 178 | *v++ = y+h-r + cverts[i*2+1]*r; |
| 179 | } |
| 180 | |
| 181 | for (unsigned i = n*2; i <= n*3; ++i) |
| 182 | { |
| 183 | *v++ = x+r + cverts[i*2]*r; |
| 184 | *v++ = y+r + cverts[i*2+1]*r; |
| 185 | } |
| 186 | |
| 187 | for (unsigned i = n*3; i < n*4; ++i) |
| 188 | { |
| 189 | *v++ = x+w-r + cverts[i*2]*r; |
| 190 | *v++ = y+r + cverts[i*2+1]*r; |
| 191 | } |
| 192 | *v++ = x+w-r + cverts[0]*r; |
| 193 | *v++ = y+r + cverts[1]*r; |
| 194 | |
| 195 | drawPolygon(verts, (n+1)*4, fth, col); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | static void drawLine(float x0, float y0, float x1, float y1, float r, float fth, unsigned int col) |
| 200 | { |
| 201 | float dx = x1-x0; |
| 202 | float dy = y1-y0; |
| 203 | float d = sqrtf(dx*dx+dy*dy); |
| 204 | if (d > 0.0001f) |
| 205 | { |
| 206 | d = 1.0f/d; |
| 207 | dx *= d; |
| 208 | dy *= d; |
| 209 | } |
| 210 | float nx = dy; |
| 211 | float ny = -dx; |
| 212 | float verts[4*2]; |
| 213 | r -= fth; |
| 214 | r *= 0.5f; |
| 215 | if (r < 0.01f) r = 0.01f; |
| 216 | dx *= r; |
| 217 | dy *= r; |
| 218 | nx *= r; |
| 219 | ny *= r; |
| 220 | |
| 221 | verts[0] = x0-dx-nx; |
| 222 | verts[1] = y0-dy-ny; |
| 223 | |
| 224 | verts[2] = x0-dx+nx; |
| 225 | verts[3] = y0-dy+ny; |
| 226 | |
| 227 | verts[4] = x1+dx+nx; |
| 228 | verts[5] = y1+dy+ny; |
| 229 | |
| 230 | verts[6] = x1+dx-nx; |
| 231 | verts[7] = y1+dy-ny; |
| 232 | |
| 233 | drawPolygon(verts, 4, fth, col); |
| 234 | } |
| 235 | |
| 236 | |
| 237 | bool imguiRenderGLInit(const char* fontpath) |
| 238 | { |
| 239 | for (int i = 0; i < CIRCLE_VERTS; ++i) |
| 240 | { |
| 241 | float a = (float)i/(float)CIRCLE_VERTS * PI*2; |
| 242 | g_circleVerts[i*2+0] = cosf(a); |
| 243 | g_circleVerts[i*2+1] = sinf(a); |
| 244 | } |
| 245 | |
| 246 | // Load font. |
| 247 | FILE* fp = fopen(fontpath, "rb"); |
| 248 | if (!fp) return false; |
| 249 | if (fseek(fp, 0, SEEK_END) != 0) |
| 250 | { |
| 251 | fclose(fp); |
| 252 | return false; |
| 253 | } |
| 254 | long size = ftell(fp); |
| 255 | if (size < 0) |
| 256 | { |
| 257 | fclose(fp); |
| 258 | return false; |
| 259 | } |
| 260 | if (fseek(fp, 0, SEEK_SET) != 0) |
| 261 | { |
| 262 | fclose(fp); |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | unsigned char* ttfBuffer = (unsigned char*)malloc(size); |
| 267 | if (!ttfBuffer) |
| 268 | { |
| 269 | fclose(fp); |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | size_t readLen = fread(ttfBuffer, 1, size, fp); |
| 274 | fclose(fp); |
| 275 | if (readLen != static_cast<size_t>(size)) |
| 276 | { |
| 277 | free(ttfBuffer); |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | fp = 0; |
| 282 | |
| 283 | unsigned char* bmap = (unsigned char*)malloc(512*512); |
| 284 | if (!bmap) |
| 285 | { |
| 286 | free(ttfBuffer); |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | stbtt_BakeFontBitmap(ttfBuffer,0, 15.0f, bmap,512,512, 32,96, g_cdata); |
| 291 | |
| 292 | // can free ttf_buffer at this point |
| 293 | glGenTextures(1, &g_ftex); |
| 294 | glBindTexture(GL_TEXTURE_2D, g_ftex); |
| 295 | glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 512,512, 0, GL_ALPHA, GL_UNSIGNED_BYTE, bmap); |
| 296 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 297 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 298 | |
| 299 | free(ttfBuffer); |
| 300 | free(bmap); |
| 301 | |
| 302 | return true; |
| 303 | } |
| 304 | |
| 305 | void imguiRenderGLDestroy() |
| 306 | { |
| 307 | if (g_ftex) |
| 308 | { |
| 309 | glDeleteTextures(1, &g_ftex); |
| 310 | g_ftex = 0; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | static void getBakedQuad(stbtt_bakedchar *chardata, int pw, int ph, int char_index, |
| 315 | float *xpos, float *ypos, stbtt_aligned_quad *q) |
| 316 | { |
| 317 | stbtt_bakedchar *b = chardata + char_index; |
| 318 | int round_x = STBTT_ifloor(*xpos + b->xoff); |
| 319 | int round_y = STBTT_ifloor(*ypos - b->yoff); |
| 320 | |
| 321 | q->x0 = (float)round_x; |
| 322 | q->y0 = (float)round_y; |
| 323 | q->x1 = (float)round_x + b->x1 - b->x0; |
| 324 | q->y1 = (float)round_y - b->y1 + b->y0; |
| 325 | |
| 326 | q->s0 = b->x0 / (float)pw; |
| 327 | q->t0 = b->y0 / (float)pw; |
| 328 | q->s1 = b->x1 / (float)ph; |
| 329 | q->t1 = b->y1 / (float)ph; |
| 330 | |
| 331 | *xpos += b->xadvance; |
| 332 | } |
| 333 | |
| 334 | static const float g_tabStops[4] = {150, 210, 270, 330}; |
| 335 | |
| 336 | static float getTextLength(stbtt_bakedchar *chardata, const char* text) |
| 337 | { |
| 338 | float xpos = 0; |
| 339 | float len = 0; |
| 340 | while (*text) |
| 341 | { |
| 342 | int c = (unsigned char)*text; |
| 343 | if (c == '\t') |
| 344 | { |
| 345 | for (int i = 0; i < 4; ++i) |
| 346 | { |
| 347 | if (xpos < g_tabStops[i]) |
| 348 | { |
| 349 | xpos = g_tabStops[i]; |
| 350 | break; |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | else if (c >= 32 && c < 128) |
| 355 | { |
| 356 | stbtt_bakedchar *b = chardata + c-32; |
| 357 | int round_x = STBTT_ifloor((xpos + b->xoff) + 0.5); |
| 358 | len = round_x + b->x1 - b->x0 + 0.5f; |
| 359 | xpos += b->xadvance; |
| 360 | } |
| 361 | ++text; |
| 362 | } |
| 363 | return len; |
| 364 | } |
| 365 | |
| 366 | static void drawText(float x, float y, const char *text, int align, unsigned int col) |
| 367 | { |
| 368 | if (!g_ftex) return; |
| 369 | if (!text) return; |
| 370 | |
| 371 | if (align == IMGUI_ALIGN_CENTER) |
| 372 | x -= getTextLength(g_cdata, text)/2; |
| 373 | else if (align == IMGUI_ALIGN_RIGHT) |
| 374 | x -= getTextLength(g_cdata, text); |
| 375 | |
| 376 | glColor4ub(col&0xff, (col>>8)&0xff, (col>>16)&0xff, (col>>24)&0xff); |
| 377 | |
| 378 | glEnable(GL_TEXTURE_2D); |
| 379 | |
| 380 | // assume orthographic projection with units = screen pixels, origin at top left |
| 381 | glBindTexture(GL_TEXTURE_2D, g_ftex); |
| 382 | |
| 383 | glBegin(GL_TRIANGLES); |
| 384 | |
| 385 | const float ox = x; |
| 386 | |
| 387 | while (*text) |
| 388 | { |
| 389 | int c = (unsigned char)*text; |
| 390 | if (c == '\t') |
| 391 | { |
| 392 | for (int i = 0; i < 4; ++i) |
| 393 | { |
| 394 | if (x < g_tabStops[i]+ox) |
| 395 | { |
| 396 | x = g_tabStops[i]+ox; |
| 397 | break; |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | else if (c >= 32 && c < 128) |
| 402 | { |
| 403 | stbtt_aligned_quad q; |
| 404 | getBakedQuad(g_cdata, 512,512, c-32, &x,&y,&q); |
| 405 | |
| 406 | glTexCoord2f(q.s0, q.t0); |
| 407 | glVertex2f(q.x0, q.y0); |
| 408 | glTexCoord2f(q.s1, q.t1); |
| 409 | glVertex2f(q.x1, q.y1); |
| 410 | glTexCoord2f(q.s1, q.t0); |
| 411 | glVertex2f(q.x1, q.y0); |
| 412 | |
| 413 | glTexCoord2f(q.s0, q.t0); |
| 414 | glVertex2f(q.x0, q.y0); |
| 415 | glTexCoord2f(q.s0, q.t1); |
| 416 | glVertex2f(q.x0, q.y1); |
| 417 | glTexCoord2f(q.s1, q.t1); |
| 418 | glVertex2f(q.x1, q.y1); |
| 419 | } |
| 420 | ++text; |
| 421 | } |
| 422 | |
| 423 | glEnd(); |
| 424 | glDisable(GL_TEXTURE_2D); |
| 425 | } |
| 426 | |
| 427 | |
| 428 | void imguiRenderGLDraw() |
| 429 | { |
| 430 | const imguiGfxCmd* q = imguiGetRenderQueue(); |
| 431 | int nq = imguiGetRenderQueueSize(); |
| 432 | |
| 433 | const float s = 1.0f/8.0f; |
| 434 | |
| 435 | glDisable(GL_SCISSOR_TEST); |
| 436 | for (int i = 0; i < nq; ++i) |
| 437 | { |
| 438 | const imguiGfxCmd& cmd = q[i]; |
| 439 | if (cmd.type == IMGUI_GFXCMD_RECT) |
| 440 | { |
| 441 | if (cmd.rect.r == 0) |
| 442 | { |
| 443 | drawRect((float)cmd.rect.x*s+0.5f, (float)cmd.rect.y*s+0.5f, |
| 444 | (float)cmd.rect.w*s-1, (float)cmd.rect.h*s-1, |
| 445 | 1.0f, cmd.col); |
| 446 | } |
| 447 | else |
| 448 | { |
| 449 | drawRoundedRect((float)cmd.rect.x*s+0.5f, (float)cmd.rect.y*s+0.5f, |
| 450 | (float)cmd.rect.w*s-1, (float)cmd.rect.h*s-1, |
| 451 | (float)cmd.rect.r*s, 1.0f, cmd.col); |
| 452 | } |
| 453 | } |
| 454 | else if (cmd.type == IMGUI_GFXCMD_LINE) |
| 455 | { |
| 456 | drawLine(cmd.line.x0*s, cmd.line.y0*s, cmd.line.x1*s, cmd.line.y1*s, cmd.line.r*s, 1.0f, cmd.col); |
| 457 | } |
| 458 | else if (cmd.type == IMGUI_GFXCMD_TRIANGLE) |
| 459 | { |
| 460 | if (cmd.flags == 1) |
| 461 | { |
| 462 | const float verts[3*2] = |
| 463 | { |
| 464 | (float)cmd.rect.x*s+0.5f, (float)cmd.rect.y*s+0.5f, |
| 465 | (float)cmd.rect.x*s+0.5f+(float)cmd.rect.w*s-1, (float)cmd.rect.y*s+0.5f+(float)cmd.rect.h*s/2-0.5f, |
| 466 | (float)cmd.rect.x*s+0.5f, (float)cmd.rect.y*s+0.5f+(float)cmd.rect.h*s-1, |
| 467 | }; |
| 468 | drawPolygon(verts, 3, 1.0f, cmd.col); |
| 469 | } |
| 470 | if (cmd.flags == 2) |
| 471 | { |
| 472 | const float verts[3*2] = |
| 473 | { |
| 474 | (float)cmd.rect.x*s+0.5f, (float)cmd.rect.y*s+0.5f+(float)cmd.rect.h*s-1, |
| 475 | (float)cmd.rect.x*s+0.5f+(float)cmd.rect.w*s/2-0.5f, (float)cmd.rect.y*s+0.5f, |
| 476 | (float)cmd.rect.x*s+0.5f+(float)cmd.rect.w*s-1, (float)cmd.rect.y*s+0.5f+(float)cmd.rect.h*s-1, |
| 477 | }; |
| 478 | drawPolygon(verts, 3, 1.0f, cmd.col); |
| 479 | } |
| 480 | } |
| 481 | else if (cmd.type == IMGUI_GFXCMD_TEXT) |
| 482 | { |
| 483 | drawText(cmd.text.x, cmd.text.y, cmd.text.text, cmd.text.align, cmd.col); |
| 484 | } |
| 485 | else if (cmd.type == IMGUI_GFXCMD_SCISSOR) |
| 486 | { |
| 487 | if (cmd.flags) |
| 488 | { |
| 489 | glEnable(GL_SCISSOR_TEST); |
| 490 | glScissor(cmd.rect.x, cmd.rect.y, cmd.rect.w, cmd.rect.h); |
| 491 | } |
| 492 | else |
| 493 | { |
| 494 | glDisable(GL_SCISSOR_TEST); |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | glDisable(GL_SCISSOR_TEST); |
| 499 | } |