stb_rect_pack: fix LARGE_RECT bug
Before adoption: this commit came in with the history the project adopted, so no landing records it.
stb_truetype: oversampling, including oversampling demo app
authored by Sean Barrett · committed by Sean Barrett · parent b6f8358 · Browse files · 11 files·+1549 added−56 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
11 files+1549 added−56 removed
- tests:+1239 added−17 removed
- other:+310 added−39 removed
Provenance
- Commit
f03e352- Tree
18c7f4e- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent
b6f8358
11 files changed+1549 added−56 removed
added:tests/oversample/main.c+272 added
1+#pragma warning(disable:4244; disable:4305; disable:4018)2+#include <assert.h>3+#include <ctype.h>4+5+#define STB_DEFINE6+#define STB_WINMAIN7+#define STB_NO_REGISTRY8+#include "stb_wingraph.h"9+#include "stb.h"10+11+#define STB_TRUETYPE_IMPLEMENTATION12+#define STB_RECT_PACK_IMPLEMENTATION13+#include "stb_rect_pack.h"14+#include "stb_truetype.h"15+16+#ifndef WINGDIAPI17+#define CALLBACK __stdcall18+#define WINGDIAPI __declspec(dllimport)19+#define APIENTRY __stdcall20+#endif21+22+#include <gl/gl.h>23+#include <gl/glu.h>24+25+26+#define SIZE_X 102427+#define SIZE_Y 76828+29+stbtt_packedchar chardata[3][128];30+31+int sx=SIZE_X, sy=SIZE_Y;32+33+#define BITMAP_W 51234+#define BITMAP_H 51235+unsigned char temp_bitmap[BITMAP_W][BITMAP_H];36+unsigned char ttf_buffer[1 << 25];37+GLuint font_tex;38+39+void load_fonts(void)40+{41+ stbtt_pack_context pc;42+ FILE *f = fopen("c:/windows/fonts/times.ttf", "rb");43+ if (!f) exit(0);44+ fread(ttf_buffer, 1, 1<<25, f);45+46+ stbtt_PackBegin(&pc, temp_bitmap[0], BITMAP_W, BITMAP_H, 0, 1, NULL);47+ stbtt_PackFontRange(&pc, ttf_buffer, 0, 24.0, 32, 95, chardata[0]+32);48+ stbtt_PackSetOversampling(&pc, 2, 2);49+ stbtt_PackFontRange(&pc, ttf_buffer, 0, 24.0, 32, 95, chardata[1]+32);50+ stbtt_PackSetOversampling(&pc, 3, 1);51+ stbtt_PackFontRange(&pc, ttf_buffer, 0, 24.0, 32, 95, chardata[2]+32);52+ stbtt_PackEnd(&pc);53+54+ glGenTextures(1, &font_tex);55+ glBindTexture(GL_TEXTURE_2D, font_tex);56+ glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, BITMAP_W, BITMAP_H, 0, GL_ALPHA, GL_UNSIGNED_BYTE, temp_bitmap);57+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);58+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);59+}60+61+void draw_init(void)62+{63+ glDisable(GL_CULL_FACE);64+ glDisable(GL_TEXTURE_2D);65+ glDisable(GL_LIGHTING);66+ glDisable(GL_DEPTH_TEST);67+68+ glViewport(0,0,sx,sy);69+ glClearColor(0,0,0,0);70+ glClear(GL_COLOR_BUFFER_BIT);71+72+ glMatrixMode(GL_PROJECTION);73+ glLoadIdentity();74+ gluOrtho2D(0,sx,sy,0);75+ glMatrixMode(GL_MODELVIEW);76+ glLoadIdentity();77+}78+79+80+void drawBoxTC(float x0, float y0, float x1, float y1, float s0, float t0, float s1, float t1)81+{82+ glTexCoord2f(s0,t0); glVertex2f(x0,y0);83+ glTexCoord2f(s1,t0); glVertex2f(x1,y0);84+ glTexCoord2f(s1,t1); glVertex2f(x1,y1);85+ glTexCoord2f(s0,t1); glVertex2f(x0,y1);86+}87+88+int integer_align;89+90+void print(float x, float y, int font, char *text)91+{92+ glEnable(GL_TEXTURE_2D);93+ glBindTexture(GL_TEXTURE_2D, font_tex);94+ glBegin(GL_QUADS);95+ while (*text) {96+ stbtt_aligned_quad q;97+ stbtt_GetPackedQuad(chardata[font], BITMAP_W, BITMAP_H, *text++, &x, &y, &q, font ? 0 : integer_align);98+ drawBoxTC(q.x0,q.y0,q.x1,q.y1, q.s0,q.t0,q.s1,q.t1);99+ }100+ glEnd();101+}102+103+int font=0;104+int translating;105+int rotating=0;106+float rotate_t, translate_t;107+int show_tex;108+109+void draw_world(void)110+{111+ float x = 20;112+ glEnable(GL_BLEND);113+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);114+ glColor3f(1,1,1);115+116+ if (font==1)117+ print(100, 50, font, "2x2 oversampled text at 1:1");118+ else if (font == 2)119+ print(100, 50, font, "3x1 oversampled text at 1:1");120+ else if (integer_align)121+ print(100, 50, font, "1:1 text, one texel = one pixel, snapped to integer coordinates");122+ else123+ print(100, 50, font, "1:1 text, one texel = one pixel");124+125+ print(100, 80, font, "O: toggle oversampling");126+ print(100,105, font, "T: toggle translation");127+ print(100,130, font, "R: toggle rotation");128+ print(100,155, font, "P: toggle pixel-snap (only non-oversampled)");129+ print(100,180, font, "V: view font texture");130+131+ if (show_tex) {132+ glBegin(GL_QUADS);133+ drawBoxTC(200,200, 200+BITMAP_W,200+BITMAP_H, 0,0,1,1);134+ glEnd();135+ } else {136+ glMatrixMode(GL_MODELVIEW);137+ glTranslatef(200,250,0);138+139+ if (translating)140+ x += fmod(translate_t*8,30);141+142+ if (rotating) {143+ glTranslatef(100,150,0);144+ glRotatef(rotate_t*2,0,0,1);145+ glTranslatef(-100,-150,0);146+ }147+ print(x,100, font, "This is a test");148+ print(x,130, font, "Now is the time for all good men to come to the aid of their country.");149+ print(x,160, font, "The quick brown fox jumps over the lazy dog.");150+ print(x,190, font, "0123456789");151+ }152+}153+154+void draw(void)155+{156+ draw_init();157+ draw_world();158+ stbwingraph_SwapBuffers(NULL);159+}160+161+static int initialized=0;162+static float last_dt;163+164+int move[4];165+int raw_mouse_x, raw_mouse_y;166+167+int loopmode(float dt, int real, int in_client)168+{169+ float actual_dt = dt;170+171+ if (!initialized) return 0;172+173+ rotate_t += dt;174+ translate_t += dt;175+176+// music_sim();177+ if (!real)178+ return 0;179+180+ if (dt > 0.25) dt = 0.25;181+ if (dt < 0.01) dt = 0.01;182+183+ draw();184+185+ return 0;186+}187+188+int winproc(void *data, stbwingraph_event *e)189+{190+ switch (e->type) {191+ case STBWGE_create:192+ break;193+194+ case STBWGE_char:195+ switch(e->key) {196+ case 27:197+ stbwingraph_ShowCursor(NULL,1);198+ return STBWINGRAPH_winproc_exit;199+ break;200+ case 'o': case 'O':201+ font = (font+1) % 3;202+ break;203+ case 't': case 'T':204+ translating = !translating;205+ translate_t = 0;206+ break;207+ case 'r': case 'R':208+ rotating = !rotating;209+ rotate_t = 0;210+ break;211+ case 'p': case 'P':212+ integer_align = !integer_align;213+ break;214+ case 'v': case 'V':215+ show_tex = !show_tex;216+ break;217+ }218+ break;219+220+ case STBWGE_mousemove:221+ raw_mouse_x = e->mx;222+ raw_mouse_y = e->my;223+ break;224+225+#if 0226+ case STBWGE_mousewheel: do_mouse(e,0,0); break;227+ case STBWGE_leftdown: do_mouse(e, 1,0); break;228+ case STBWGE_leftup: do_mouse(e,-1,0); break;229+ case STBWGE_rightdown: do_mouse(e,0, 1); break;230+ case STBWGE_rightup: do_mouse(e,0,-1); break;231+#endif232+233+ case STBWGE_keydown:234+ if (e->key == VK_RIGHT) move[0] = 1;235+ if (e->key == VK_LEFT) move[1] = 1;236+ if (e->key == VK_UP) move[2] = 1;237+ if (e->key == VK_DOWN) move[3] = 1;238+ break;239+ case STBWGE_keyup:240+ if (e->key == VK_RIGHT) move[0] = 0;241+ if (e->key == VK_LEFT) move[1] = 0;242+ if (e->key == VK_UP) move[2] = 0;243+ if (e->key == VK_DOWN) move[3] = 0;244+ break;245+246+ case STBWGE_size:247+ sx = e->width;248+ sy = e->height;249+ loopmode(0,1,0);250+ break;251+252+ case STBWGE_draw:253+ if (initialized)254+ loopmode(0,1,0);255+ break;256+257+ default:258+ return STBWINGRAPH_unprocessed;259+ }260+ return 0;261+}262+263+void stbwingraph_main(void)264+{265+ stbwingraph_Priority(2);266+ stbwingraph_CreateWindow(1, winproc, NULL, "tt", SIZE_X,SIZE_Y, 0, 1, 0, 0);267+ stbwingraph_ShowCursor(NULL, 0);268+ load_fonts();269+ initialized = 1;270+ stbwingraph_MainLoop(loopmode, 0.016f); // 30 fps = 0.33271+}272+added:tests/oversample/oversample.dsp+97 added
1+# Microsoft Developer Studio Project File - Name="oversample" - Package Owner=<4>2+# Microsoft Developer Studio Generated Build File, Format Version 6.003+# ** DO NOT EDIT **4+5+# TARGTYPE "Win32 (x86) Application" 0x01016+7+CFG=oversample - Win32 Debug8+!MESSAGE This is not a valid makefile. To build this project using NMAKE,9+!MESSAGE use the Export Makefile command and run10+!MESSAGE 11+!MESSAGE NMAKE /f "oversample.mak".12+!MESSAGE 13+!MESSAGE You can specify a configuration when running NMAKE14+!MESSAGE by defining the macro CFG on the command line. For example:15+!MESSAGE 16+!MESSAGE NMAKE /f "oversample.mak" CFG="oversample - Win32 Debug"17+!MESSAGE 18+!MESSAGE Possible choices for configuration are:19+!MESSAGE 20+!MESSAGE "oversample - Win32 Release" (based on "Win32 (x86) Application")21+!MESSAGE "oversample - Win32 Debug" (based on "Win32 (x86) Application")22+!MESSAGE 23+24+# Begin Project25+# PROP AllowPerConfigDependencies 026+# PROP Scc_ProjName ""27+# PROP Scc_LocalPath ""28+CPP=cl.exe29+MTL=midl.exe30+RSC=rc.exe31+32+!IF "$(CFG)" == "oversample - Win32 Release"33+34+# PROP BASE Use_MFC 035+# PROP BASE Use_Debug_Libraries 036+# PROP BASE Output_Dir "Release"37+# PROP BASE Intermediate_Dir "Release"38+# PROP BASE Target_Dir ""39+# PROP Use_MFC 040+# PROP Use_Debug_Libraries 041+# PROP Output_Dir "Release"42+# PROP Intermediate_Dir "Release"43+# PROP Ignore_Export_Lib 044+# PROP Target_Dir ""45+# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /YX /FD /c46+# ADD CPP /nologo /W3 /WX /GX /O2 /I "c:\sean\prj\stb" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /FD /c47+# SUBTRACT CPP /YX48+# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win3249+# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win3250+# ADD BASE RSC /l 0x409 /d "NDEBUG"51+# ADD RSC /l 0x409 /d "NDEBUG"52+BSC32=bscmake.exe53+# ADD BASE BSC32 /nologo54+# ADD BSC32 /nologo55+LINK32=link.exe56+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /machine:I38657+# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /machine:I38658+# SUBTRACT LINK32 /map /debug59+60+!ELSEIF "$(CFG)" == "oversample - Win32 Debug"61+62+# PROP BASE Use_MFC 063+# PROP BASE Use_Debug_Libraries 164+# PROP BASE Output_Dir "Debug"65+# PROP BASE Intermediate_Dir "Debug"66+# PROP BASE Target_Dir ""67+# PROP Use_MFC 068+# PROP Use_Debug_Libraries 169+# PROP Output_Dir "Debug"70+# PROP Intermediate_Dir "Debug"71+# PROP Ignore_Export_Lib 072+# PROP Target_Dir ""73+# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /YX /FD /GZ /c74+# ADD CPP /nologo /W3 /WX /Gm /GX /Zi /Od /I "c:\sean\prj\stb" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /FD /GZ /c75+# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win3276+# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win3277+# ADD BASE RSC /l 0x409 /d "_DEBUG"78+# ADD RSC /l 0x409 /d "_DEBUG"79+BSC32=bscmake.exe80+# ADD BASE BSC32 /nologo81+# ADD BSC32 /nologo82+LINK32=link.exe83+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /debug /machine:I386 /pdbtype:sept84+# ADD LINK32 kernel32.lib user32.lib gdi32.lib advapi32.lib winspool.lib comdlg32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /incremental:no /debug /machine:I386 /pdbtype:sept85+86+!ENDIF 87+88+# Begin Target89+90+# Name "oversample - Win32 Release"91+# Name "oversample - Win32 Debug"92+# Begin Source File93+94+SOURCE=.\main.c95+# End Source File96+# End Target97+# End Projectadded:tests/oversample/oversample.dsw+29 added
1+Microsoft Developer Studio Workspace File, Format Version 6.002+# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!3+4+###############################################################################5+6+Project: "oversample"=.\oversample.dsp - Package Owner=<4>7+8+Package=<5>9+{{{10+}}}11+12+Package=<4>13+{{{14+}}}15+16+###############################################################################17+18+Global:19+20+Package=<5>21+{{{22+}}}23+24+Package=<3>25+{{{26+}}}27+28+###############################################################################29+added:tests/oversample/stb_wingraph.h+824 added
1+// stb_wingraph.h v0.01 - public domain windows graphics programming2+// wraps WinMain, ChoosePixelFormat, ChangeDisplayResolution, etc. for3+// doing OpenGL graphics4+//5+// in ONE source file, put '#define STB_DEFINE' before including this6+// OR put '#define STB_WINMAIN' to define a WinMain that calls stbwingraph_main(void)7+//8+// @TODO:9+// 2d rendering interface (that can be done easily in software)10+// STB_WINGRAPH_SOFTWARE -- 2d software rendering only11+// STB_WINGRAPH_OPENGL -- OpenGL only12+13+14+#ifndef INCLUDE_STB_WINGRAPH_H15+#define INCLUDE_STB_WINGRAPH_H16+17+#ifdef STB_WINMAIN18+ #ifndef STB_DEFINE19+ #define STB_DEFINE20+ #define STB_WINGRAPH_DISABLE_DEFINE_AT_END21+ #endif22+#endif23+24+#ifdef STB_DEFINE25+ #pragma comment(lib, "opengl32.lib")26+ #pragma comment(lib, "glu32.lib")27+ #pragma comment(lib, "winmm.lib")28+#endif29+30+#ifdef __cplusplus31+#define STB_EXTERN extern "C"32+#else33+#define STB_EXTERN34+#endif35+36+#ifdef STB_DEFINE37+#ifndef _WINDOWS_38+ #ifdef APIENTRY39+ #undef APIENTRY40+ #endif41+ #ifdef WINGDIAPI42+ #undef WINGDIAPI43+ #endif44+ #define _WIN32_WINNT 0x0400 // WM_MOUSEWHEEL45+ #include <windows.h>46+#endif47+#include <stdio.h>48+#include <math.h>49+#include <time.h>50+#include <string.h>51+#include <assert.h>52+#endif53+54+typedef void * stbwingraph_hwnd;55+typedef void * stbwingraph_hinstance;56+57+enum58+{59+ STBWINGRAPH_unprocessed = -(1 << 24),60+ STBWINGRAPH_do_not_show,61+ STBWINGRAPH_winproc_exit,62+ STBWINGRAPH_winproc_update,63+ STBWINGRAPH_update_exit,64+ STBWINGRAPH_update_pause,65+};66+67+typedef enum68+{69+ STBWGE__none=0,70+71+ STBWGE_create,72+ STBWGE_create_postshow,73+ STBWGE_draw,74+ STBWGE_destroy,75+ STBWGE_char,76+ STBWGE_keydown,77+ STBWGE_syskeydown,78+ STBWGE_keyup,79+ STBWGE_syskeyup,80+ STBWGE_deactivate,81+ STBWGE_activate,82+ STBWGE_size,83+84+ STBWGE_mousemove ,85+ STBWGE_leftdown , STBWGE_leftup ,86+ STBWGE_middledown, STBWGE_middleup,87+ STBWGE_rightdown , STBWGE_rightup ,88+ STBWGE_mousewheel,89+} stbwingraph_event_type;90+91+typedef struct92+{93+ stbwingraph_event_type type;94+95+ // for input events (mouse, keyboard)96+ int mx,my; // mouse x & y97+ int dx,dy;98+ int shift, ctrl, alt;99+100+ // for keyboard events101+ int key;102+103+ // for STBWGE_size:104+ int width, height;105+106+ // for STBWGE_crate107+ int did_share_lists; // if true, wglShareLists succeeded108+109+ void *handle;110+111+} stbwingraph_event;112+113+typedef int (*stbwingraph_window_proc)(void *data, stbwingraph_event *event);114+115+extern stbwingraph_hinstance stbwingraph_app;116+extern stbwingraph_hwnd stbwingraph_primary_window;117+extern int stbwingraph_request_fullscreen;118+extern int stbwingraph_request_windowed;119+120+STB_EXTERN void stbwingraph_ods(char *str, ...);121+STB_EXTERN int stbwingraph_MessageBox(stbwingraph_hwnd win, unsigned int type,122+ char *caption, char *text, ...);123+STB_EXTERN int stbwingraph_ChangeResolution(unsigned int w, unsigned int h,124+ unsigned int bits, int use_message_box);125+STB_EXTERN int stbwingraph_SetPixelFormat(stbwingraph_hwnd win, int color_bits,126+ int alpha_bits, int depth_bits, int stencil_bits, int accum_bits);127+STB_EXTERN int stbwingraph_DefineClass(void *hinstance, char *iconname);128+STB_EXTERN void stbwingraph_SwapBuffers(void *win);129+STB_EXTERN void stbwingraph_Priority(int n);130+131+STB_EXTERN void stbwingraph_MakeFonts(void *window, int font_base);132+STB_EXTERN void stbwingraph_ShowWindow(void *window);133+STB_EXTERN void *stbwingraph_CreateWindow(int primary, stbwingraph_window_proc func, void *data, char *text, int width, int height, int fullscreen, int resizeable, int dest_alpha, int stencil);134+STB_EXTERN void *stbwingraph_CreateWindowSimple(stbwingraph_window_proc func, int width, int height);135+STB_EXTERN void *stbwingraph_CreateWindowSimpleFull(stbwingraph_window_proc func, int fullscreen, int ww, int wh, int fw, int fh);136+STB_EXTERN void stbwingraph_DestroyWindow(void *window);137+STB_EXTERN void stbwingraph_ShowCursor(void *window, int visible);138+STB_EXTERN float stbwingraph_GetTimestep(float minimum_time);139+STB_EXTERN void stbwingraph_SetGLWindow(void *win);140+typedef int (*stbwingraph_update)(float timestep, int real, int in_client);141+STB_EXTERN int stbwingraph_MainLoop(stbwingraph_update func, float mintime);142+143+#ifdef STB_DEFINE144+stbwingraph_hinstance stbwingraph_app;145+stbwingraph_hwnd stbwingraph_primary_window;146+int stbwingraph_request_fullscreen;147+int stbwingraph_request_windowed;148+149+void stbwingraph_ods(char *str, ...)150+{151+ char buffer[1024];152+ va_list v;153+ va_start(v,str);154+ vsprintf(buffer, str, v);155+ va_end(v);156+ OutputDebugString(buffer);157+}158+159+int stbwingraph_MessageBox(stbwingraph_hwnd win, unsigned int type, char *caption, char *text, ...)160+{161+ va_list v;162+ char buffer[1024];163+ va_start(v, text);164+ vsprintf(buffer, text, v);165+ va_end(v);166+ return MessageBox(win, buffer, caption, type);167+}168+169+void stbwingraph_Priority(int n)170+{171+ int p;172+ switch (n) {173+ case -1: p = THREAD_PRIORITY_BELOW_NORMAL; break;174+ case 0: p = THREAD_PRIORITY_NORMAL; break;175+ case 1: p = THREAD_PRIORITY_ABOVE_NORMAL; break;176+ default:177+ if (n < 0) p = THREAD_PRIORITY_LOWEST;178+ else p = THREAD_PRIORITY_HIGHEST;179+ }180+ SetThreadPriority(GetCurrentThread(), p);181+}182+183+static void stbwingraph_ResetResolution(void)184+{185+ ChangeDisplaySettings(NULL, 0);186+}187+188+static void stbwingraph_RegisterResetResolution(void)189+{190+ static int done=0;191+ if (!done) {192+ done = 1;193+ atexit(stbwingraph_ResetResolution);194+ }195+}196+197+int stbwingraph_ChangeResolution(unsigned int w, unsigned int h, unsigned int bits, int use_message_box)198+{199+ DEVMODE mode;200+ int res;201+ 202+ int i, tries=0;203+ for (i=0; ; ++i) {204+ int success = EnumDisplaySettings(NULL, i, &mode);205+ if (!success) break;206+ if (mode.dmBitsPerPel == bits && mode.dmPelsWidth == w && mode.dmPelsHeight == h) {207+ ++tries;208+ success = ChangeDisplaySettings(&mode, CDS_FULLSCREEN); 209+ if (success == DISP_CHANGE_SUCCESSFUL) {210+ stbwingraph_RegisterResetResolution();211+ return TRUE;212+ }213+ break;214+ }215+ }216+217+ if (!tries) {218+ if (use_message_box)219+ stbwingraph_MessageBox(stbwingraph_primary_window, MB_ICONERROR, NULL, "The resolution %d x %d x %d-bits is not supported.", w, h, bits);220+ return FALSE;221+ }222+223+ // we tried but failed, so try explicitly doing it without specifying refresh rate224+225+ // Win95 support logic226+ mode.dmBitsPerPel = bits; 227+ mode.dmPelsWidth = w; 228+ mode.dmPelsHeight = h; 229+ mode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT; 230+231+ res = ChangeDisplaySettings(&mode, CDS_FULLSCREEN);232+233+ switch (res) {234+ case DISP_CHANGE_SUCCESSFUL:235+ stbwingraph_RegisterResetResolution();236+ return TRUE;237+238+ case DISP_CHANGE_RESTART:239+ if (use_message_box)240+ stbwingraph_MessageBox(stbwingraph_primary_window, MB_ICONERROR, NULL, "Please set your desktop to %d-bit color and then try again.");241+ return FALSE;242+243+ case DISP_CHANGE_FAILED:244+ if (use_message_box)245+ stbwingraph_MessageBox(stbwingraph_primary_window, MB_ICONERROR, NULL, "The hardware failed to change modes.");246+ return FALSE;247+248+ case DISP_CHANGE_BADMODE:249+ if (use_message_box)250+ stbwingraph_MessageBox(stbwingraph_primary_window, MB_ICONERROR, NULL, "The resolution %d x %d x %d-bits is not supported.", w, h, bits);251+ return FALSE;252+253+ default:254+ if (use_message_box)255+ stbwingraph_MessageBox(stbwingraph_primary_window, MB_ICONERROR, NULL, "An unknown error prevented a change to a %d x %d x %d-bit display.", w, h, bits);256+ return FALSE;257+ }258+}259+260+int stbwingraph_SetPixelFormat(stbwingraph_hwnd win, int color_bits, int alpha_bits, int depth_bits, int stencil_bits, int accum_bits)261+{262+ HDC dc = GetDC(win);263+ PIXELFORMATDESCRIPTOR pfd = { sizeof(pfd) };264+ int pixel_format;265+266+ pfd.nVersion = 1;267+ pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;268+ pfd.dwLayerMask = PFD_MAIN_PLANE;269+ pfd.iPixelType = PFD_TYPE_RGBA;270+ pfd.cColorBits = color_bits;271+ pfd.cAlphaBits = alpha_bits;272+ pfd.cDepthBits = depth_bits;273+ pfd.cStencilBits = stencil_bits;274+ pfd.cAccumBits = accum_bits;275+276+ pixel_format = ChoosePixelFormat(dc, &pfd);277+ if (!pixel_format) return FALSE;278+279+ if (!DescribePixelFormat(dc, pixel_format, sizeof(PIXELFORMATDESCRIPTOR), &pfd))280+ return FALSE;281+ SetPixelFormat(dc, pixel_format, &pfd);282+283+ return TRUE;284+}285+286+typedef struct287+{288+ // app data289+ stbwingraph_window_proc func;290+ void *data;291+ // creation parameters292+ int color, alpha, depth, stencil, accum;293+ HWND share_window;294+ HWND window;295+ // internal data296+ HGLRC rc;297+ HDC dc;298+ int hide_mouse;299+ int in_client;300+ int active;301+ int did_share_lists;302+ int mx,my; // last mouse positions303+} stbwingraph__window;304+305+static void stbwingraph__inclient(stbwingraph__window *win, int state)306+{307+ if (state != win->in_client) {308+ win->in_client = state;309+ if (win->hide_mouse)310+ ShowCursor(!state);311+ }312+}313+314+static void stbwingraph__key(stbwingraph_event *e, int type, int key, stbwingraph__window *z)315+{316+ e->type = type;317+ e->key = key;318+ e->shift = (GetKeyState(VK_SHIFT) < 0);319+ e->ctrl = (GetKeyState(VK_CONTROL) < 0);320+ e->alt = (GetKeyState(VK_MENU) < 0);321+ if (z) {322+ e->mx = z->mx;323+ e->my = z->my;324+ } else {325+ e->mx = e->my = 0;326+ }327+ e->dx = e->dy = 0;328+}329+330+static void stbwingraph__mouse(stbwingraph_event *e, int type, WPARAM wparam, LPARAM lparam, int capture, void *wnd, stbwingraph__window *z)331+{332+ static int captured = 0;333+ e->type = type;334+ e->mx = (short) LOWORD(lparam);335+ e->my = (short) HIWORD(lparam);336+ if (!z || z->mx == -(1 << 30)) {337+ e->dx = e->dy = 0;338+ } else {339+ e->dx = e->mx - z->mx;340+ e->dy = e->my - z->my;341+ }342+ e->shift = (wparam & MK_SHIFT) != 0;343+ e->ctrl = (wparam & MK_CONTROL) != 0;344+ e->alt = (wparam & MK_ALT) != 0;345+ if (z) {346+ z->mx = e->mx;347+ z->my = e->my;348+ }349+ if (capture) {350+ if (!captured && capture == 1)351+ SetCapture(wnd);352+ captured += capture;353+ if (!captured && capture == -1)354+ ReleaseCapture();355+ if (captured < 0) captured = 0;356+ }357+}358+359+static void stbwingraph__mousewheel(stbwingraph_event *e, int type, WPARAM wparam, LPARAM lparam, int capture, void *wnd, stbwingraph__window *z)360+{361+ // lparam seems bogus!362+ static int captured = 0;363+ e->type = type;364+ if (z) {365+ e->mx = z->mx;366+ e->my = z->my;367+ }368+ e->dx = e->dy = 0;369+ e->shift = (wparam & MK_SHIFT) != 0;370+ e->ctrl = (wparam & MK_CONTROL) != 0;371+ e->alt = (GetKeyState(VK_MENU) < 0);372+ e->key = ((int) wparam >> 16);373+}374+375+int stbwingraph_force_update;376+static int WINAPI stbwingraph_WinProc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)377+{378+ int allow_default = TRUE;379+ stbwingraph_event e = { STBWGE__none };380+ // the following line is wrong for 64-bit windows, but VC6 doesn't have GetWindowLongPtr381+ stbwingraph__window *z = (stbwingraph__window *) GetWindowLong(wnd, GWL_USERDATA);382+383+ switch (msg) {384+385+ case WM_CREATE:386+ {387+ LPCREATESTRUCT lpcs = (LPCREATESTRUCT) lparam;388+ assert(z == NULL);389+ z = (stbwingraph__window *) lpcs->lpCreateParams;390+ SetWindowLong(wnd, GWL_USERDATA, (LONG) z);391+ z->dc = GetDC(wnd);392+ if (stbwingraph_SetPixelFormat(wnd, z->color, z->alpha, z->depth, z->stencil, z->accum)) {393+ z->rc = wglCreateContext(z->dc);394+ if (z->rc) {395+ e.type = STBWGE_create;396+ z->did_share_lists = FALSE;397+ if (z->share_window) {398+ stbwingraph__window *y = (stbwingraph__window *) GetWindowLong(z->share_window, GWL_USERDATA);399+ if (wglShareLists(z->rc, y->rc))400+ z->did_share_lists = TRUE;401+ }402+ wglMakeCurrent(z->dc, z->rc);403+ return 0;404+ }405+ }406+ return -1;407+ }408+409+ case WM_PAINT: {410+ PAINTSTRUCT ps;411+ HDC hdc = BeginPaint(wnd, &ps);412+ SelectObject(hdc, GetStockObject(NULL_BRUSH));413+ e.type = STBWGE_draw;414+ e.handle = wnd;415+ z->func(z->data, &e);416+ EndPaint(wnd, &ps);417+ return 0;418+ }419+420+ case WM_DESTROY:421+ e.type = STBWGE_destroy;422+ e.handle = wnd;423+ if (z && z->func)424+ z->func(z->data, &e);425+ wglMakeCurrent(NULL, NULL) ; 426+ if (z) {427+ if (z->rc) wglDeleteContext(z->rc);428+ z->dc = 0;429+ z->rc = 0;430+ }431+ if (wnd == stbwingraph_primary_window)432+ PostQuitMessage (0);433+ return 0;434+435+ case WM_CHAR: stbwingraph__key(&e, STBWGE_char , wparam, z); break;436+ case WM_KEYDOWN: stbwingraph__key(&e, STBWGE_keydown, wparam, z); break;437+ case WM_KEYUP: stbwingraph__key(&e, STBWGE_keyup , wparam, z); break;438+439+ case WM_NCMOUSEMOVE: stbwingraph__inclient(z,0); break;440+ case WM_MOUSEMOVE: stbwingraph__inclient(z,1); stbwingraph__mouse(&e, STBWGE_mousemove, wparam, lparam,0,wnd, z); break;441+ case WM_LBUTTONDOWN: stbwingraph__mouse(&e, STBWGE_leftdown, wparam, lparam,1,wnd, z); break;442+ case WM_MBUTTONDOWN: stbwingraph__mouse(&e, STBWGE_middledown, wparam, lparam,1,wnd, z); break;443+ case WM_RBUTTONDOWN: stbwingraph__mouse(&e, STBWGE_rightdown, wparam, lparam,1,wnd, z); break;444+ case WM_LBUTTONUP: stbwingraph__mouse(&e, STBWGE_leftup, wparam, lparam,-1,wnd, z); break;445+ case WM_MBUTTONUP: stbwingraph__mouse(&e, STBWGE_middleup, wparam, lparam,-1,wnd, z); break;446+ case WM_RBUTTONUP: stbwingraph__mouse(&e, STBWGE_rightup, wparam, lparam,-1,wnd, z); break;447+ case WM_MOUSEWHEEL: stbwingraph__mousewheel(&e, STBWGE_mousewheel, wparam, lparam,0,wnd, z); break;448+449+ case WM_ACTIVATE:450+ allow_default = FALSE;451+ if (LOWORD(wparam)==WA_INACTIVE ) {452+ wglMakeCurrent(z->dc, NULL);453+ e.type = STBWGE_deactivate;454+ z->active = FALSE;455+ } else {456+ wglMakeCurrent(z->dc, z->rc);457+ e.type = STBWGE_activate;458+ z->active = TRUE;459+ }460+ e.handle = wnd;461+ z->func(z->data, &e);462+ return 0;463+464+ case WM_SIZE: {465+ RECT rect;466+ allow_default = FALSE;467+ GetClientRect(wnd, &rect);468+ e.type = STBWGE_size;469+ e.width = rect.right;470+ e.height = rect.bottom;471+ e.handle = wnd;472+ z->func(z->data, &e);473+ return 0;474+ }475+476+ default:477+ return DefWindowProc (wnd, msg, wparam, lparam);478+ }479+480+ if (e.type != STBWGE__none) {481+ int n;482+ e.handle = wnd;483+ n = z->func(z->data, &e);484+ if (n == STBWINGRAPH_winproc_exit) {485+ PostQuitMessage(0);486+ n = 0;487+ }488+ if (n == STBWINGRAPH_winproc_update) {489+ stbwingraph_force_update = TRUE;490+ return 1;491+ }492+ if (n != STBWINGRAPH_unprocessed)493+ return n;494+ }495+ return DefWindowProc (wnd, msg, wparam, lparam);496+}497+498+int stbwingraph_DefineClass(HINSTANCE hInstance, char *iconname)499+{500+ WNDCLASSEX wndclass;501+502+ stbwingraph_app = hInstance;503+504+ wndclass.cbSize = sizeof(wndclass);505+ wndclass.style = CS_OWNDC;506+ wndclass.lpfnWndProc = (WNDPROC) stbwingraph_WinProc;507+ wndclass.cbClsExtra = 0;508+ wndclass.cbWndExtra = 0;509+ wndclass.hInstance = hInstance;510+ wndclass.hIcon = LoadIcon(hInstance, iconname);511+ wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);512+ wndclass.hbrBackground = GetStockObject(NULL_BRUSH);513+ wndclass.lpszMenuName = "zwingraph";514+ wndclass.lpszClassName = "zwingraph";515+ wndclass.hIconSm = NULL;516+517+ if (!RegisterClassEx(&wndclass))518+ return FALSE;519+ return TRUE;520+}521+522+void stbwingraph_ShowWindow(void *window)523+{524+ stbwingraph_event e = { STBWGE_create_postshow };525+ stbwingraph__window *z = (stbwingraph__window *) GetWindowLong(window, GWL_USERDATA);526+ ShowWindow(window, SW_SHOWNORMAL);527+ InvalidateRect(window, NULL, TRUE);528+ UpdateWindow(window);529+ e.handle = window;530+ z->func(z->data, &e);531+}532+533+void *stbwingraph_CreateWindow(int primary, stbwingraph_window_proc func, void *data, char *text,534+ int width, int height, int fullscreen, int resizeable, int dest_alpha, int stencil)535+{536+ HWND win;537+ DWORD dwstyle;538+ stbwingraph__window *z = (stbwingraph__window *) malloc(sizeof(*z));539+540+ if (z == NULL) return NULL;541+ memset(z, 0, sizeof(*z));542+ z->color = 24;543+ z->depth = 24;544+ z->alpha = dest_alpha;545+ z->stencil = stencil;546+ z->func = func;547+ z->data = data;548+ z->mx = -(1 << 30);549+ z->my = 0;550+551+ if (primary) {552+ if (stbwingraph_request_windowed)553+ fullscreen = FALSE;554+ else if (stbwingraph_request_fullscreen)555+ fullscreen = TRUE;556+ }557+558+ if (fullscreen) {559+ #ifdef STB_SIMPLE560+ stbwingraph_ChangeResolution(width, height, 32, 1);561+ #else562+ if (!stbwingraph_ChangeResolution(width, height, 32, 0))563+ return NULL;564+ #endif565+ dwstyle = WS_POPUP | WS_CLIPSIBLINGS;566+ } else {567+ RECT rect;568+ dwstyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;569+ if (resizeable)570+ dwstyle |= WS_SIZEBOX | WS_MAXIMIZEBOX;571+ rect.top = 0;572+ rect.left = 0;573+ rect.right = width;574+ rect.bottom = height;575+ AdjustWindowRect(&rect, dwstyle, FALSE);576+ width = rect.right - rect.left;577+ height = rect.bottom - rect.top;578+ }579+580+ win = CreateWindow("zwingraph", text ? text : "sample", dwstyle,581+ CW_USEDEFAULT,0, width, height,582+ NULL, NULL, stbwingraph_app, z);583+584+ if (win == NULL) return win;585+586+ if (primary) {587+ if (stbwingraph_primary_window)588+ stbwingraph_DestroyWindow(stbwingraph_primary_window);589+ stbwingraph_primary_window = win;590+ }591+592+ {593+ stbwingraph_event e = { STBWGE_create };594+ stbwingraph__window *z = (stbwingraph__window *) GetWindowLong(win, GWL_USERDATA);595+ z->window = win;596+ e.did_share_lists = z->did_share_lists;597+ e.handle = win;598+ if (z->func(z->data, &e) != STBWINGRAPH_do_not_show)599+ stbwingraph_ShowWindow(win);600+ }601+602+ return win;603+}604+605+void *stbwingraph_CreateWindowSimple(stbwingraph_window_proc func, int width, int height)606+{607+ int fullscreen = 0;608+ #ifndef _DEBUG609+ if (width == 640 && height == 480) fullscreen = 1;610+ if (width == 800 && height == 600) fullscreen = 1;611+ if (width == 1024 && height == 768) fullscreen = 1;612+ if (width == 1280 && height == 1024) fullscreen = 1;613+ if (width == 1600 && height == 1200) fullscreen = 1;614+ //@TODO: widescreen widths615+ #endif616+ return stbwingraph_CreateWindow(1, func, NULL, NULL, width, height, fullscreen, 1, 0, 0);617+}618+619+void *stbwingraph_CreateWindowSimpleFull(stbwingraph_window_proc func, int fullscreen, int ww, int wh, int fw, int fh)620+{621+ if (fullscreen == -1) {622+ #ifdef _DEBUG623+ fullscreen = 0;624+ #else625+ fullscreen = 1;626+ #endif627+ }628+629+ if (fullscreen) {630+ if (fw) ww = fw;631+ if (fh) wh = fh;632+ }633+ return stbwingraph_CreateWindow(1, func, NULL, NULL, ww, wh, fullscreen, 1, 0, 0);634+}635+636+void stbwingraph_DestroyWindow(void *window)637+{638+ stbwingraph__window *z = (stbwingraph__window *) GetWindowLong(window, GWL_USERDATA);639+ DestroyWindow(window);640+ free(z);641+ if (stbwingraph_primary_window == window)642+ stbwingraph_primary_window = NULL;643+}644+645+void stbwingraph_ShowCursor(void *window, int visible)646+{647+ int hide;648+ stbwingraph__window *win;649+ if (!window)650+ window = stbwingraph_primary_window;651+ win = (stbwingraph__window *) GetWindowLong((HWND) window, GWL_USERDATA);652+ hide = !visible;653+ if (hide != win->hide_mouse) {654+ win->hide_mouse = hide;655+ if (!hide)656+ ShowCursor(TRUE);657+ else if (win->in_client)658+ ShowCursor(FALSE);659+ }660+}661+662+float stbwingraph_GetTimestep(float minimum_time)663+{664+ float elapsedTime;665+ double thisTime;666+ static double lastTime = -1;667+ 668+ if (lastTime == -1)669+ lastTime = timeGetTime() / 1000.0 - minimum_time;670+671+ for(;;) {672+ thisTime = timeGetTime() / 1000.0;673+ elapsedTime = (float) (thisTime - lastTime);674+ if (elapsedTime >= minimum_time) {675+ lastTime = thisTime; 676+ return elapsedTime;677+ }678+ #if 1679+ Sleep(2);680+ #endif681+ }682+}683+684+void stbwingraph_SetGLWindow(void *win)685+{686+ stbwingraph__window *z = (stbwingraph__window *) GetWindowLong(win, GWL_USERDATA);687+ if (z)688+ wglMakeCurrent(z->dc, z->rc);689+}690+691+void stbwingraph_MakeFonts(void *window, int font_base)692+{693+ wglUseFontBitmaps(GetDC(window ? window : stbwingraph_primary_window), 0, 256, font_base);694+}695+696+// returns 1 if WM_QUIT, 0 if 'func' returned 0697+int stbwingraph_MainLoop(stbwingraph_update func, float mintime)698+{699+ int needs_drawing = FALSE;700+ MSG msg;701+702+ int is_animating = TRUE;703+ if (mintime <= 0) mintime = 0.01f;704+705+ for(;;) {706+ int n;707+708+ is_animating = TRUE;709+ // wait for a message if: (a) we're animating and there's already a message710+ // or (b) we're not animating711+ if (!is_animating || PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {712+ stbwingraph_force_update = FALSE;713+ if (GetMessage(&msg, NULL, 0, 0)) {714+ TranslateMessage(&msg);715+ DispatchMessage(&msg);716+ } else {717+ return 1; // WM_QUIT718+ }719+720+ // only force a draw for certain messages...721+ // if I don't do this, we peg at 50% for some reason... must722+ // be a bug somewhere, because we peg at 100% when rendering...723+ // very weird... looks like NVIDIA is pumping some messages724+ // through our pipeline? well, ok, I guess if we can get725+ // non-user-generated messages we have to do this726+ if (!stbwingraph_force_update) {727+ switch (msg.message) {728+ case WM_MOUSEMOVE:729+ case WM_NCMOUSEMOVE:730+ break;731+ case WM_CHAR: 732+ case WM_KEYDOWN: 733+ case WM_KEYUP: 734+ case WM_LBUTTONDOWN: 735+ case WM_MBUTTONDOWN: 736+ case WM_RBUTTONDOWN: 737+ case WM_LBUTTONUP: 738+ case WM_MBUTTONUP: 739+ case WM_RBUTTONUP: 740+ case WM_TIMER:741+ case WM_SIZE:742+ case WM_ACTIVATE:743+ needs_drawing = TRUE;744+ break;745+ }746+ } else747+ needs_drawing = TRUE;748+ }749+750+ // if another message, process that first751+ // @TODO: i don't think this is working, because I can't key ahead752+ // in the SVT demo app753+ if (PeekMessage(&msg, NULL, 0,0, PM_NOREMOVE))754+ continue;755+756+ // and now call update757+ if (needs_drawing || is_animating) {758+ int real=1, in_client=1;759+ if (stbwingraph_primary_window) {760+ stbwingraph__window *z = (stbwingraph__window *) GetWindowLong(stbwingraph_primary_window, GWL_USERDATA);761+ if (z && !z->active) {762+ real = 0;763+ }764+ if (z)765+ in_client = z->in_client;766+ }767+768+ if (stbwingraph_primary_window)769+ stbwingraph_SetGLWindow(stbwingraph_primary_window);770+ n = func(stbwingraph_GetTimestep(mintime), real, in_client);771+ if (n == STBWINGRAPH_update_exit)772+ return 0; // update_quit773+774+ is_animating = (n != STBWINGRAPH_update_pause);775+776+ needs_drawing = FALSE;777+ }778+ }779+}780+781+void stbwingraph_SwapBuffers(void *win)782+{783+ stbwingraph__window *z;784+ if (win == NULL) win = stbwingraph_primary_window;785+ z = (stbwingraph__window *) GetWindowLong(win, GWL_USERDATA);786+ if (z && z->dc)787+ SwapBuffers(z->dc);788+}789+#endif790+791+#ifdef STB_WINMAIN 792+void stbwingraph_main(void);793+794+int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)795+{796+ {797+ char buffer[1024];798+ // add spaces to either side of the string799+ buffer[0] = ' ';800+ strcpy(buffer+1, lpCmdLine);801+ strcat(buffer, " ");802+ if (strstr(buffer, " -reset ")) {803+ ChangeDisplaySettings(NULL, 0); 804+ exit(0);805+ }806+ if (strstr(buffer, " -window ") || strstr(buffer, " -windowed "))807+ stbwingraph_request_windowed = TRUE;808+ else if (strstr(buffer, " -full ") || strstr(buffer, " -fullscreen "))809+ stbwingraph_request_fullscreen = TRUE;810+ }811+812+ stbwingraph_DefineClass(hInstance, "appicon");813+ stbwingraph_main();814+815+ return 0;816+}817+#endif818+819+#undef STB_EXTERN820+#ifdef STB_WINGRAPH_DISABLE_DEFINE_AT_END821+#undef STB_DEFINE822+#endif823+824+#endif // INCLUDE_STB_WINGRAPH_Hmodified:tests/image_test.dsp+1 added-1 removed
39 unmodified lines40 # PROP Output_Dir "Release"41 # PROP Intermediate_Dir "Release"42 # PROP Target_Dir ""43 # ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c44-# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c44+# ADD CPP /nologo /W3 /GX /O2 /I ".." /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c45 # ADD BASE RSC /l 0x409 /d "NDEBUG"46 # ADD RSC /l 0x409 /d "NDEBUG"47 BSC32=bscmake.exe48 # ADD BASE BSC32 /nologo48 unmodified linesmodified:tests/stb_cpp.dsp+1 added-1 removed
39 unmodified lines40 # PROP Output_Dir "Release"41 # PROP Intermediate_Dir "Release"42 # PROP Target_Dir ""43 # ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c44-# ADD CPP /nologo /W3 /GX /O2 /I ".." /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c44+# ADD CPP /nologo /MT /W3 /GX /O2 /I ".." /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c45 # ADD BASE RSC /l 0x409 /d "NDEBUG"46 # ADD RSC /l 0x409 /d "NDEBUG"47 BSC32=bscmake.exe48 # ADD BASE BSC32 /nologo49 unmodified linesmodified:tests/stb.dsp+1 added-1 removed
40 unmodified lines41 # PROP Intermediate_Dir "Release"42 # PROP Ignore_Export_Lib 043 # PROP Target_Dir ""44 # ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c45-# ADD CPP /nologo /G6 /MT /W3 /GX /Z7 /O2 /Ob2 /I ".." /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "MAIN_TEST" /FD /c45+# ADD CPP /nologo /G6 /MT /W3 /GX /Z7 /O2 /Ob2 /I ".." /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "TT_TEST" /FD /c46 # ADD BASE RSC /l 0x409 /d "NDEBUG"47 # ADD RSC /l 0x409 /d "NDEBUG"48 BSC32=bscmake.exe49 # ADD BASE BSC32 /nologo119 unmodified linesmodified:tests/stretch_test.dsp+3 added-3 removed
39 unmodified lines40 # PROP Output_Dir "Release"41 # PROP Intermediate_Dir "Release"42 # PROP Target_Dir ""43 # ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c44-# ADD CPP /nologo /W3 /GX /O2 /I "..\.." /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c44+# ADD CPP /nologo /W3 /GX /O2 /I "..\.." /I ".." /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "TT_TEST" /YX /FD /c45 # ADD BASE RSC /l 0x409 /d "NDEBUG"46 # ADD RSC /l 0x409 /d "NDEBUG"47 BSC32=bscmake.exe48 # ADD BASE BSC32 /nologo13 unmodified lines62 # PROP Use_Debug_Libraries 163 # PROP Output_Dir "Debug"64 # PROP Intermediate_Dir "Debug\stretch_test"65 # PROP Target_Dir ""66-# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c67-# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c66+# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c67+# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c68 # ADD BASE RSC /l 0x409 /d "_DEBUG"69 # ADD RSC /l 0x409 /d "_DEBUG"70 BSC32=bscmake.exe71 # ADD BASE BSC32 /nologo17 unmodified linesmodified:tests/test_truetype.c+11 added-11 removed
18 unmodified lines19 stbtt_MakeGlyphBitmap(&font, output, 6, 9, 512, 5.172414E-03f, 5.172414E-03f, 54);20 }21 22 #define BITMAP_W 25623-#define BITMAP_H 40023+#define BITMAP_H 51224 unsigned char temp_bitmap[BITMAP_H][BITMAP_W];25 stbtt_bakedchar cdata[256*2]; // ASCII 32..126 is 95 glyphs26+stbtt_packedchar pdata[256*2];27 int main(int argc, char **argv)28 {29 stbtt_fontinfo font;30 unsigned char *bitmap;6 unmodified lines37 38 stbtt_BakeFontBitmap(ttf_buffer,stbtt_GetFontOffsetForIndex(ttf_buffer,0), 40.0, temp_bitmap[0],BITMAP_W,BITMAP_H, 32,96, cdata); // no guarantee this fits!39 stbi_write_png("fonttest1.png", BITMAP_W, BITMAP_H, 1, temp_bitmap, 0);40 40- stbtt_BakeFontBitmap(ttf_buffer,stbtt_GetFontOffsetForIndex(ttf_buffer,0), 40.0, temp_bitmap[0],BITMAP_W,BITMAP_H, 32,96, cdata); // no guarantee this fits!41-41 {42 stbtt_pack_context pc;44- stbtt_PackBegin(&pc, temp_bitmap[0], BITMAP_W, BITMAP_H, 0, NULL);45- stbtt_PackFontRange(&pc, ttf_buffer, 0, 40.0, 32, 95, cdata);46- stbtt_PackFontRange(&pc, ttf_buffer, 0, 40.0, 0xa0, 0x100-0xa0, cdata);43+ stbtt_PackBegin(&pc, temp_bitmap[0], BITMAP_W, BITMAP_H, 0, 1, NULL);44+ stbtt_PackFontRange(&pc, ttf_buffer, 0, 20.0, 32, 95, pdata);45+ stbtt_PackFontRange(&pc, ttf_buffer, 0, 20.0, 0xa0, 0x100-0xa0, pdata);46 stbtt_PackEnd(&pc);47 stbi_write_png("fonttest2.png", BITMAP_W, BITMAP_H, 1, temp_bitmap, 0);48 }49 50 {51 stbtt_pack_context pc;52 stbtt_pack_range pr[2];54- stbtt_PackBegin(&pc, temp_bitmap[0], BITMAP_W, BITMAP_H, 0, NULL);53+ stbtt_PackBegin(&pc, temp_bitmap[0], BITMAP_W, BITMAP_H, 0, 1, NULL);54 56- pr[0].chardata_for_range = cdata;55+ pr[0].chardata_for_range = pdata;56 pr[0].first_unicode_char_in_range = 32;57 pr[0].num_chars_in_range = 95;59- pr[0].font_size = 40.0;60- pr[1].chardata_for_range = cdata+256;58+ pr[0].font_size = 20.0f;59+ pr[1].chardata_for_range = pdata+256;60 pr[1].first_unicode_char_in_range = 0xa0;61 pr[1].num_chars_in_range = 0x100 - 0xa0;63- pr[1].font_size = 40.0;62+ pr[1].font_size = 20.0f;63 64+ stbtt_PackSetOversampling(&pc, 2, 2);65 stbtt_PackFontRanges(&pc, ttf_buffer, 0, pr, 2);66 stbtt_PackEnd(&pc);67 stbi_write_png("fonttest3.png", BITMAP_W, BITMAP_H, 1, temp_bitmap, 0);68 }13 unmodified linesmodified:stb_rect_pack.h+8 added-2 removed
493 unmodified lines494 stbrp_rect *q = (stbrp_rect *) b;495 return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);496 }497 498+#ifdef STBRP_LARGE_RECTS499+#define STBRP__MAXVAL 0xffffffff500+#else501+#define STBRP__MAXVAL 0xffff502+#endif503+504 STBRP_DEF void stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects)505 {506 int i;507 13 unmodified lines521 if (fr.prev_link) {522 rects[i].x = (stbrp_coord) fr.x;523 rects[i].y = (stbrp_coord) fr.y;524 } else {519- rects[i].x = rects[i].y = 0xffff;525+ rects[i].x = rects[i].y = STBRP__MAXVAL;526 }527 }528 529 // unsort530 qsort(rects, num_rects, sizeof(rects[0]), rect_original_order);531 532 // set was_packed flags533 for (i=0; i < num_rects; ++i)528- rects[i].was_packed = !(rects[i].x == 0xffff && rects[i].y == 0xffff);534+ rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL);535 }536 #endifmodified:stb_truetype.h+302 added-37 removed
33 unmodified lines34 // Johan Duparc35 // Hou Qiming36 // Fabian "ryg" Giesen37 //38+// Misc other:39+// Ryan Gordon40+//41 // VERSION HISTORY42 //43 // 0.99 (2014-09-18) fix multiple bugs with subpixel rendering (ryg)44 // 0.9 (2014-08-07) support certain mac/iOS fonts without an MS platformID385 unmodified lines430 431 typedef struct432 {433 unsigned short x0,y0,x1,y1; // coordinates of bbox in bitmap431- float xoff,yoff,xadvance; 434+ float xoff,yoff,xadvance;435 } stbtt_bakedchar;436 437 extern int stbtt_BakeFontBitmap(const unsigned char *data, int offset, // font location (use offset=0 for plain .ttf)438 float pixel_height, // height of font in pixels34 unmodified lines473 //474 // This provides options for packing multiple fonts into one atlas, not475 // perfectly but better than nothing.476 477+typedef struct478+{479+ unsigned short x0,y0,x1,y1; // coordinates of bbox in bitmap480+ float xoff,yoff,xadvance;481+ float xoff2,yoff2;482+} stbtt_packedchar;483+484 typedef struct stbtt_pack_context stbtt_pack_context;485 typedef struct486 {487 float font_size; // if positive, pixel height; if negative, points488 int first_unicode_char_in_range;489 int num_chars_in_range;480- stbtt_bakedchar *chardata_for_range; // output490+ stbtt_packedchar *chardata_for_range; // output491 } stbtt_pack_range;492 483-extern int stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int pw, int ph, int stride_in_bytes, void *alloc_context);493+extern int stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int pw, int ph, int stride_in_bytes, int padding, void *alloc_context);494 // returns 0 if the allocations fail495 496+extern void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample);497 extern void stbtt_PackEnd (stbtt_pack_context *spc);498 extern int stbtt_PackFontRange(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, float font_size,488- int first_unicode_char_in_range, int num_chars_in_range, stbtt_bakedchar *chardata_for_range);499+ int first_unicode_char_in_range, int num_chars_in_range, stbtt_packedchar *chardata_for_range);500 extern int stbtt_PackFontRanges(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges);501 502 503+extern void stbtt_GetPackedQuad(stbtt_packedchar *chardata, int pw, int ph, // same data as above504+ int char_index, // character to display505+ float *xpos, float *ypos, // pointers to current position in screen pixel space506+ stbtt_aligned_quad *q, // output: quad to draw507+ int align_to_integer);508+509 // this is an opaque structure that you shouldn't mess with which holds510 // all the context needed from PackBegin to PackEnd.511 struct stbtt_pack_context {512 void *user_allocator_context;513 void *pack_info;514 int width;515 int height;516 int stride_in_bytes;517+ int padding;518+ unsigned int h_oversample, v_oversample;519 unsigned char *pixels;520 void *nodes;521 };522 303 unmodified lines826 ////827 828 #ifdef STB_TRUETYPE_IMPLEMENTATION829 830+#ifndef STBTT_MAX_OVERSAMPLE831+#define STBTT_MAX_OVERSAMPLE 8832+#endif833+834+typedef stbtt__test_oversample_pow2[(STBTT_MAX_OVERSAMPLE & (STBTT_MAX_OVERSAMPLE-1)) == 0 ? 1 : -1];835+836 //////////////////////////////////////////////////////////////////////////837 //838 // accessors to parse data from file839 //1131 unmodified lines1971 chardata[i].y1 = (stbtt_int16) (y + gh);1972 chardata[i].xadvance = scale * advance;1973 chardata[i].xoff = (float) x0;1974 chardata[i].yoff = (float) y0;1950- x = x + gw + 2;1951- if (y+gh+2 > bottom_y)1952- bottom_y = y+gh+2;1975+ x = x + gw + 1;1976+ if (y+gh+1 > bottom_y)1977+ bottom_y = y+gh+1;1978 }1979 return bottom_y;1980 }1981 19 unmodified lines2001 }2002 2003 //////////////////////////////////////////////////////////////////////////////2004 //1980-// bitmap baking2005+// rectangle packing replacement routines if you don't have stb_rect_pack.h2006 //1982-// This is SUPER-AWESOME (tm Ryan Gordon) packing using stb_rect_pack.h. If1983-// stb_rect_pack.h isn't available, it uses the BakeFontBitmap strategy.2007 2008 #ifndef STB_RECT_PACK_VERSION1986-// @TODO: simulate STB_RECT_PACK API with trivial logic from BakeFontBitmap,1987-// try to share code?!?1988-#error "no stb_rect_pack"2009+#ifdef _MSC_VER2010+#define STBTT__NOTUSED(v) (void)(v)2011+#else2012+#define STBTT__NOTUSED(v) (void)sizeof(v)2013 #endif2014 1991-#if 01992-struct stbtt_pack_context {1993- void *user_allocator_context;1994- void *pack_info;1995- int width;1996- int height;1997- unsigned char *pixels;1998-};2015+////////////////////////////////////////////////////////////////////////////////////2016+// //2017+// //2018+// COMPILER WARNING ?!?!? //2019+// //2020+// //2021+// if you get a compile warning due to these symbols being defined more than //2022+// once, move #include "stb_rect_pack.h" before #include "stb_truetype.h" //2023+// //2024+////////////////////////////////////////////////////////////////////////////////////2025+2026+typedef struct2027+{2028+ int width,height;2029+ int x,y,bottom_y;2030+} stbrp_context;2031+2032+typedef struct2033+{2034+ unsigned char x;2035+} stbrp_node;2036+2037+typedef struct2038+{2039+ int id,w,h,x,y,was_packed;2040+} stbrp_rect;2041+2042+static void stbrp_init_target(stbrp_context *con, int pw, int ph, stbrp_node *nodes, int num_nodes)2043+{2044+ con->width = pw;2045+ con->height = ph;2046+ con->x = 0;2047+ con->y = 0;2048+ con->bottom_y = 0;2049+ STBTT__NOTUSED(nodes);2050+ STBTT__NOTUSED(num_nodes); 2051+}2052+2053+static void stbrp_pack_rects(stbrp_context *con, stbrp_rect *rects, int num_rects)2054+{2055+ int i;2056+ for (i=0; i < num_rects; ++i) {2057+ if (con->x + rects[i].w > con->width) {2058+ con->x = 0;2059+ con->y = con->bottom_y;2060+ }2061+ if (con->y + rects[i].h > con->height)2062+ break;2063+ rects[i].x = con->x;2064+ rects[i].y = con->y;2065+ rects[i].was_packed = 1;2066+ con->x += rects[i].w;2067+ if (con->y + rects[i].h > con->bottom_y)2068+ con->bottom_y = con->y + rects[i].h;2069+ }2070+ for ( ; i < num_rects; ++i)2071+ rects[i].was_packed = 0;2072+}2073 #endif2074 2075+//////////////////////////////////////////////////////////////////////////////2076+//2077+// bitmap baking2078+//2079+// This is SUPER-AWESOME (tm Ryan Gordon) packing using stb_rect_pack.h. If2080+// stb_rect_pack.h isn't available, it uses the BakeFontBitmap strategy.2081+2001-int stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int pw, int ph, int stride_in_bytes, void *alloc_context)2082+int stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int pw, int ph, int stride_in_bytes, int padding, void *alloc_context)2083 {2084 stbrp_context *context = (stbrp_context *) STBTT_malloc(sizeof(*context) ,alloc_context);2004- int num_nodes = pw-1;2085+ int num_nodes = pw - padding;2086 stbrp_node *nodes = (stbrp_node *) STBTT_malloc(sizeof(*nodes ) * num_nodes,alloc_context);2087 2088 if (context == NULL || nodes == NULL) {2089 if (context != NULL) STBTT_free(context, alloc_context);6 unmodified lines2096 spc->height = ph;2097 spc->pixels = pixels;2098 spc->pack_info = context;2099 spc->nodes = nodes;2100+ spc->padding = padding;2101 spc->stride_in_bytes = stride_in_bytes != 0 ? stride_in_bytes : pw;2102+ spc->h_oversample = 1;2103+ spc->v_oversample = 1;2104 2021- stbrp_init_target(context, pw-1, ph-1, nodes, num_nodes);2105+ stbrp_init_target(context, pw-padding, ph-padding, nodes, num_nodes);2106 2107 STBTT_memset(pixels, 0, pw*ph); // background of 0 around pixels2108 2109 return 1;4 unmodified lines2114 STBTT_free(spc->nodes , spc->user_allocator_context);2115 STBTT_free(spc->pack_info, spc->user_allocator_context);2116 }2117 2118+void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample)2119+{2120+ assert(h_oversample <= STBTT_MAX_OVERSAMPLE);2121+ assert(v_oversample <= STBTT_MAX_OVERSAMPLE);2122+ if (h_oversample <= STBTT_MAX_OVERSAMPLE)2123+ spc->h_oversample = h_oversample;2124+ if (v_oversample <= STBTT_MAX_OVERSAMPLE)2125+ spc->v_oversample = v_oversample;2126+}2127+2128+#define STBTT__OVER_MASK (STBTT_MAX_OVERSAMPLE-1)2129+2130+static void stbtt__h_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width)2131+{2132+ unsigned char buffer[STBTT_MAX_OVERSAMPLE];2133+ int safe_w = w - kernel_width;2134+ int j;2135+ for (j=0; j < h; ++j) {2136+ int i;2137+ unsigned int total;2138+ unsigned char *pixels_ahead = pixels + (kernel_width);2139+ memset(buffer, 0, kernel_width);2140+2141+ total = 0;2142+2143+ // make kernel_width a constant in common cases so compiler can optimize out the divide2144+ switch (kernel_width) {2145+ case 2:2146+ for (i=0; i <= safe_w; ++i) {2147+ total += pixels[i] - buffer[i & STBTT__OVER_MASK];2148+ buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];2149+ pixels[i] = total / 2;2150+ }2151+ break;2152+ case 3:2153+ for (i=0; i <= safe_w; ++i) {2154+ total += pixels[i] - buffer[i & STBTT__OVER_MASK];2155+ buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];2156+ pixels[i] = total / 3;2157+ }2158+ break;2159+ case 4:2160+ for (i=0; i <= safe_w; ++i) {2161+ total += pixels[i] - buffer[i & STBTT__OVER_MASK];2162+ buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];2163+ pixels[i] = total / 4;2164+ }2165+ break;2166+ default:2167+ for (i=0; i <= safe_w; ++i) {2168+ total += pixels[i] - buffer[i & STBTT__OVER_MASK];2169+ buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];2170+ pixels[i] = total / kernel_width;2171+ }2172+ break;2173+ }2174+2175+ for (; i < w; ++i) {2176+ assert(pixels[i] == 0);2177+ total -= buffer[i & STBTT__OVER_MASK];2178+ pixels[i] = total / kernel_width;2179+ }2180+2181+ pixels += stride_in_bytes;2182+ }2183+}2184+2185+static void stbtt__v_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width)2186+{2187+ unsigned char buffer[STBTT_MAX_OVERSAMPLE];2188+ int safe_h = h - kernel_width;2189+ int j;2190+ for (j=0; j < w; ++j) {2191+ int i;2192+ unsigned int total;2193+ unsigned char *pixels_ahead = pixels + (kernel_width)*stride_in_bytes;2194+ memset(buffer, 0, kernel_width);2195+2196+ total = 0;2197+2198+ // make kernel_width a constant in common cases so compiler can optimize out the divide2199+ switch (kernel_width) {2200+ case 2:2201+ for (i=0; i <= safe_h; ++i) {2202+ total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];2203+ buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];2204+ pixels[i*stride_in_bytes] = total / 2;2205+ }2206+ break;2207+ case 3:2208+ for (i=0; i <= safe_h; ++i) {2209+ total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];2210+ buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];2211+ pixels[i*stride_in_bytes] = total / 3;2212+ }2213+ break;2214+ case 4:2215+ for (i=0; i <= safe_h; ++i) {2216+ total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];2217+ buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];2218+ pixels[i*stride_in_bytes] = total / 4;2219+ }2220+ break;2221+ default:2222+ for (i=0; i <= safe_h; ++i) {2223+ total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];2224+ buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];2225+ pixels[i*stride_in_bytes] = total / kernel_width;2226+ }2227+ break;2228+ }2229+2230+ for (; i < h; ++i) {2231+ assert(pixels[i*stride_in_bytes] == 0);2232+ total -= buffer[i & STBTT__OVER_MASK];2233+ pixels[i*stride_in_bytes] = total / kernel_width;2234+ }2235+2236+ pixels += 1;2237+ }2238+}2239+2240 int stbtt_PackFontRanges(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges)2241 {2242 stbtt_fontinfo info;2243+ float recip_h = 1.0f / spc->h_oversample;2244+ float recip_v = 1.0f / spc->v_oversample;2245+ float sub_x = spc->h_oversample ? recip_h : 0;2246+ float sub_y = spc->v_oversample ? recip_v : 0;2247 int i,j,k,n, return_value = 1;2248 stbrp_context *context = (stbrp_context *) spc->pack_info;2249 stbrp_rect *rects;2250 20 unmodified lines2271 //float scale = fh > 0 ? stbtt_ScaleForPixelHeight(&info, fh) : stbtt_ScaleForPointSize(&info, fh);2272 float scale = stbtt_ScaleForPixelHeight(&info, fh);2273 for (j=0; j < ranges[i].num_chars_in_range; ++j) {2274 int x0,y0,x1,y1;2065- stbtt_GetCodepointBitmapBox(&info, ranges[i].first_unicode_char_in_range + j, scale,scale, &x0,&y0,&x1,&y1);2066- rects[k].w = x1-x0+1;2067- rects[k].h = y1-y0+1;2275+ stbtt_GetCodepointBitmapBoxSubpixel(&info, ranges[i].first_unicode_char_in_range + j,2276+ scale * spc->h_oversample,2277+ scale * spc->v_oversample,2278+ 0,0,2279+ &x0,&y0,&x1,&y1);2280+ rects[k].w = x1-x0 + spc->padding + spc->h_oversample-1;2281+ rects[k].h = y1-y0 + spc->padding + spc->v_oversample-1;2282 ++k;2283 }2284 }2285 6 unmodified lines2292 float scale = stbtt_ScaleForPixelHeight(&info, fh);2293 for (j=0; j < ranges[i].num_chars_in_range; ++j) {2294 stbrp_rect *r = &rects[k];2295 if (r->was_packed) {2082- stbtt_bakedchar *bc = &ranges[i].chardata_for_range[j];2296+ stbtt_packedchar *bc = &ranges[i].chardata_for_range[j];2297 int advance, lsb, x0,y0,x1,y1;2298 int glyph = stbtt_FindGlyphIndex(&info, ranges[i].first_unicode_char_in_range + j);2299 2300+ // pad on left and top2301+ r->x += spc->padding;2302+ r->y += spc->padding;2303+ r->w -= spc->padding;2304+ r->h -= spc->padding;2305 stbtt_GetGlyphHMetrics(&info, glyph, &advance, &lsb);2087- stbtt_GetGlyphBitmapBox(&info, glyph, scale,scale, &x0,&y0,&x1,&y1);2306+ stbtt_GetGlyphBitmapBox(&info, glyph,2307+ scale * spc->h_oversample,2308+ scale * spc->v_oversample,2309+ &x0,&y0,&x1,&y1);2310 stbtt_MakeGlyphBitmapSubpixel(&info,2311 spc->pixels + r->x + r->y*spc->stride_in_bytes,2090- r->w-1, r->h-1,2312+ r->w - spc->h_oversample+1,2313+ r->h - spc->v_oversample+1,2314 spc->stride_in_bytes,2092- scale,scale,2093- 0.0f, 0.0f,2315+ scale * spc->h_oversample,2316+ scale * spc->v_oversample,2317+ 0,0,2318 glyph);2319+2320+ if (spc->h_oversample > 1)2321+ stbtt__h_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,2322+ r->w, r->h, spc->stride_in_bytes,2323+ spc->h_oversample);2324+2325+ if (spc->v_oversample > 1)2326+ stbtt__v_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,2327+ r->w, r->h, spc->stride_in_bytes,2328+ spc->v_oversample);2329+2330 bc->x0 = (stbtt_int16) r->x;2331 bc->y0 = (stbtt_int16) r->y;2097- bc->x1 = (stbtt_int16) (r->x + r->w-1);2098- bc->y1 = (stbtt_int16) (r->y + r->h-1);2332+ bc->x1 = (stbtt_int16) (r->x + r->w);2333+ bc->y1 = (stbtt_int16) (r->y + r->h);2334 bc->xadvance = scale * advance;2100- bc->xoff = (float) x0;2101- bc->yoff = (float) y0;2335+ bc->xoff = (float) x0 * recip_h + sub_x;2336+ bc->yoff = (float) y0 * recip_v + sub_y;2337+ bc->xoff2 = (x0 + r->w) * recip_h + sub_x;2338+ bc->yoff2 = (y0 + r->h) * recip_v + sub_y;2339 } else {2340 return_value = 0; // if any fail, report failure2341 }2342 4 unmodified lines2347 return return_value;2348 }2349 2350 int stbtt_PackFontRange(stbtt_pack_context *spc, unsigned char *fontdata, int font_index, float font_size,2114- int first_unicode_char_in_range, int num_chars_in_range, stbtt_bakedchar *chardata_for_range)2351+ int first_unicode_char_in_range, int num_chars_in_range, stbtt_packedchar *chardata_for_range)2352 {2353 stbtt_pack_range range;2354 range.first_unicode_char_in_range = first_unicode_char_in_range;2355 range.num_chars_in_range = num_chars_in_range;1 unmodified line2357 range.font_size = font_size;2358 return stbtt_PackFontRanges(spc, fontdata, font_index, &range, 1);2359 }2360 2361+void stbtt_GetPackedQuad(stbtt_packedchar *chardata, int pw, int ph, int char_index, float *xpos, float *ypos, stbtt_aligned_quad *q, int align_to_integer)2362+{2363+ float ipw = 1.0f / pw, iph = 1.0f / ph;2364+ stbtt_packedchar *b = chardata + char_index;2365+2366+ if (align_to_integer) {2367+ float x = (float) STBTT_ifloor((*xpos + b->xoff) + 0.5);2368+ float y = (float) STBTT_ifloor((*ypos + b->yoff) + 0.5);2369+ q->x0 = x;2370+ q->y0 = y;2371+ q->x1 = x + b->xoff2 - b->xoff;2372+ q->y1 = y + b->yoff2 - b->yoff;2373+ } else {2374+ q->x0 = *xpos + b->xoff;2375+ q->y0 = *ypos + b->yoff;2376+ q->x1 = *xpos + b->xoff2;2377+ q->y1 = *ypos + b->yoff2;2378+ }2379+2380+ q->s0 = b->x0 * ipw;2381+ q->t0 = b->y0 * iph;2382+ q->s1 = b->x1 * ipw;2383+ q->t1 = b->y1 * iph;2384+2385+ *xpos += b->xadvance;2386+}2387+2388+2389 //////////////////////////////////////////////////////////////////////////////2390 //2391 // font name matching -- recommended not to use this2392 //157 unmodified lines