Skip to content
Myr

main.c

This file

Path

tests/oversample/main.c

332 lines · 8.4 KiB · C

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than aeec66c point at it.

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 1–332why, one step back
1#pragma warning(disable:4244; disable:4305; disable:4018)
2#include <assert.h>3#include <ctype.h>45#define STB_WINMAIN6#include "stb_wingraph.h"78#define STB_TRUETYPE_IMPLEMENTATION9#define STB_RECT_PACK_IMPLEMENTATION10#include "stb_rect_pack.h"11#include "stb_truetype.h"1213#ifndef WINGDIAPI14#define CALLBACK __stdcall15#define WINGDIAPI __declspec(dllimport)16#define APIENTRY __stdcall17#endif1819#include <gl/gl.h>20#include <gl/glu.h>2122#define GL_FRAMEBUFFER_SRGB_EXT 0x8DB92324#define SIZE_X 102425#define SIZE_Y 7682627stbtt_packedchar chardata[6][128];2829int sx=SIZE_X, sy=SIZE_Y;3031#define BITMAP_W 51232#define BITMAP_H 51233unsigned char temp_bitmap[BITMAP_W][BITMAP_H];34unsigned char ttf_buffer[1 << 25];35GLuint font_tex;3637float scale[2] = { 24.0f, 14.0f };3839int sf[6] = { 0,1,2, 0,1,2 };4041void load_fonts(void)42{43 stbtt_pack_context pc;44 int i;45 FILE *f;46 char filename[256];47 char *win = getenv("windir");48 if (win == NULL) win = getenv("SystemRoot");4950 f = fopen(stb_wingraph_commandline, "rb");51 if (!f) {52 if (win == NULL)53 sprintf(filename, "arial.ttf", win);54 else55 sprintf(filename, "%s/fonts/arial.ttf", win);56 f = fopen(filename, "rb");57 if (!f) exit(0);58 }5960 fread(ttf_buffer, 1, 1<<25, f);6162 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);70 }71 stbtt_PackEnd(&pc);7273 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);78}7980int black_on_white;8182void draw_init(void)83{84 glDisable(GL_CULL_FACE);85 glDisable(GL_TEXTURE_2D);86 glDisable(GL_LIGHTING);87 glDisable(GL_DEPTH_TEST);8889 glViewport(0,0,sx,sy);90 if (black_on_white)91 glClearColor(255,255,255,0);92 else93 glClearColor(0,0,0,0);94 glClear(GL_COLOR_BUFFER_BIT);9596 glMatrixMode(GL_PROJECTION);97 glLoadIdentity();98 gluOrtho2D(0,sx,sy,0);99 glMatrixMode(GL_MODELVIEW);100 glLoadIdentity();101}102103104void drawBoxTC(float x0, float y0, float x1, float y1, float s0, float t0, float s1, float t1)105{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);110}111112int integer_align;113114void print(float x, float y, int font, char *text)115{116 glEnable(GL_TEXTURE_2D);117 glBindTexture(GL_TEXTURE_2D, font_tex);118 glBegin(GL_QUADS);119 while (*text) {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);123 }124 glEnd();125}126127int font=3;128int translating;129int rotating=0;130int srgb=0;131float rotate_t, translate_t;132int show_tex;133134void draw_world(void)135{136 int sfont = sf[font];137 float x = 20;138 glEnable(GL_BLEND);139 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);140141 if (black_on_white)142 glColor3f(0,0,0);143 else144 glColor3f(1,1,1);145146147 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");154 if (black_on_white)155 print(100,210, sfont, "B: toggle to white-on-black");156 else157 print(100,210, sfont, "B: toggle to black-on-white");158 print(100,235, sfont, "V: view font texture");159160 print(80, 300, sfont, "Current font:");161162 if (!show_tex) {163 if (font < 3)164 print(100, 350, sfont, "Font height: 24 pixels");165 else166 print(100, 350, sfont, "Font height: 14 pixels");167 }168169 if (font%3==1)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");175 else176 print(100, 325, sfont, "1:1 text, one texel = one pixel");177178 if (show_tex) {179 glBegin(GL_QUADS);180 drawBoxTC(200,400, 200+BITMAP_W,300+BITMAP_H, 0,0,1,1);181 glEnd();182 } else {183 glMatrixMode(GL_MODELVIEW);184 glTranslatef(200,350,0);185186 if (translating)187 x += fmod(translate_t*8,30);188189 if (rotating) {190 glTranslatef(100,150,0);191 glRotatef(rotate_t*2,0,0,1);192 glTranslatef(-100,-150,0);193 }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");198 }199}200201void draw(void)202{203 draw_init();204 draw_world();205 stbwingraph_SwapBuffers(NULL);206}207208static int initialized=0;209static float last_dt;210211int move[4];212int raw_mouse_x, raw_mouse_y;213214int loopmode(float dt, int real, int in_client)215{216 float actual_dt = dt;217218 if (!initialized) return 0;219220 rotate_t += dt;221 translate_t += dt;222223// music_sim();224 if (!real)225 return 0;226227 if (dt > 0.25) dt = 0.25;228 if (dt < 0.01) dt = 0.01;229230 draw();231232 return 0;233}234235int winproc(void *data, stbwingraph_event *e)236{237 switch (e->type) {238 case STBWGE_create:239 break;240241 case STBWGE_char:242 switch(e->key) {243 case 27:244 stbwingraph_ShowCursor(NULL,1);245 return STBWINGRAPH_winproc_exit;246 break;247 case 'o': case 'O':248 font = (font+1) % 3 + (font/3)*3;249 break;250 case 's': case 'S':251 font = (font+3) % 6;252 break;253 case 't': case 'T':254 translating = !translating;255 translate_t = 0;256 break;257 case 'r': case 'R':258 rotating = !rotating;259 rotate_t = 0;260 break;261 case 'p': case 'P':262 integer_align = !integer_align;263 break;264 case 'g': case 'G':265 srgb = !srgb;266 if (srgb)267 glEnable(GL_FRAMEBUFFER_SRGB_EXT);268 else269 glDisable(GL_FRAMEBUFFER_SRGB_EXT);270 break;271 case 'v': case 'V':272 show_tex = !show_tex;273 break;274 case 'b': case 'B':275 black_on_white = !black_on_white;276 break;277 }278 break;279280 case STBWGE_mousemove:281 raw_mouse_x = e->mx;282 raw_mouse_y = e->my;283 break;284285#if 0286 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;291#endif292293 case STBWGE_keydown: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;298 break;299 case STBWGE_keyup: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;304 break;305306 case STBWGE_size:307 sx = e->width;308 sy = e->height;309 loopmode(0,1,0);310 break;311312 case STBWGE_draw:313 if (initialized)314 loopmode(0,1,0);315 break;316317 default:318 return STBWINGRAPH_unprocessed;319 }320 return 0;321}322323void stbwingraph_main(void)324{325 stbwingraph_Priority(2);326 stbwingraph_CreateWindow(1, winproc, NULL, "tt", SIZE_X,SIZE_Y, 0, 1, 0, 0);327 stbwingraph_ShowCursor(NULL, 0);328 load_fonts();329 initialized = 1;330 stbwingraph_MainLoop(loopmode, 0.016f); // 30 fps = 0.33331}332