Created
December 14, 2016 22:59
-
-
Save croepha/6735b1d271a9ca56766807434dfc5b06 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if 0 /* (sh-mode) | |
set -e | |
LPATH=/Users/cro/Documents/PersonalProjects/Graphics/ | |
# -Ofast | |
[ ! ]&& time clang++ -g --std=c++11 -fPIC -Wall \ | |
$LPATH/text_test.cpp \ | |
-Wno-writable-strings \ | |
-Wno-unused-function \ | |
-Wno-deprecated-declarations \ | |
-framework Cocoa -framework AppKit -framework OpenGL \ | |
-F /Library/Frameworks/ -framework SDL2 \ | |
-o $LPATH/text_test.cpp.elf | |
[ ! ]&&{ | |
cd $RPATH | |
ulimit -c unlimited | |
rm -fv core | |
pwd | |
time $LPATH/text_test.cpp.elf | |
} | |
true | |
exit # (c++-mode) */ | |
#endif | |
#if defined __APPLE__ | |
#include <SDL2/SDL.h> | |
#include <SDL2/SDL_opengl.h> | |
#else | |
#include <SDL.h> | |
#include <SDL_opengl.h> | |
#endif | |
#define check_gl_error() _check_gl_error(__FILE__,__LINE__) | |
void _check_gl_error(const char *file, int line) { | |
GLenum err (glGetError()); | |
const char * error; | |
switch (err) { | |
case GL_INVALID_OPERATION: error = "INVALID_OPERATION"; break; | |
case GL_INVALID_ENUM: error = "INVALID_ENUM"; break; | |
case GL_INVALID_VALUE: error = "INVALID_VALUE"; break; | |
case GL_OUT_OF_MEMORY: error = "OUT_OF_MEMORY"; break; | |
case GL_INVALID_FRAMEBUFFER_OPERATION: error = "INVALID_FRAMEBUFFER_OPERATION"; break; | |
default: | |
error = "UNKNOWN"; | |
} | |
while(err!=GL_NO_ERROR) { | |
SDL_LogCritical(SDL_LOG_CATEGORY_SYSTEM, "%s:%d OPENGL_ERROR %s\n", file, line, error); | |
err=glGetError(); | |
} | |
} | |
#define STB_TRUETYPE_IMPLEMENTATION // force following include to generate implementation | |
#include "stb/stb_truetype.h" | |
unsigned char ttf_buffer[1<<20]; | |
unsigned char temp_bitmap[2048*2048]; | |
stbtt_bakedchar cdata[96]; // ASCII 32..126 is 95 glyphs | |
GLuint ftex; | |
void my_stbtt_initfont(void) | |
{ | |
auto f = fopen("/Users/cro/Downloads/IndieFlower.ttf", "rb"); | |
assert(f); | |
fread(ttf_buffer, 1, 1<<20, f); | |
stbtt_BakeFontBitmap(ttf_buffer,0, 64.0, temp_bitmap,2048,2048, 32,96, cdata); // no guarantee this fits! | |
// can free ttf_buffer at this point | |
glGenTextures(1, &ftex); | |
glBindTexture(GL_TEXTURE_2D, ftex); | |
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 512,512, 0, GL_ALPHA, GL_UNSIGNED_BYTE, temp_bitmap); | |
// can free temp_bitmap at this point | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
} | |
void my_stbtt_print(float x, float y, char *text) | |
{ | |
// assume orthographic projection with units = screen pixels, origin at top left | |
glEnable(GL_TEXTURE_2D); | |
glBindTexture(GL_TEXTURE_2D, ftex); | |
glBegin(GL_QUADS); | |
while (*text) { | |
if (*text >= 32) { // && *text < 128 | |
stbtt_aligned_quad q; | |
stbtt_GetBakedQuad(cdata, 512,512, *text-32, &x,&y,&q,1);//1=opengl & d3d10+,0=d3d9 | |
glTexCoord2f(q.s0,q.t1); glVertex2f(q.x0,q.y0); | |
glTexCoord2f(q.s1,q.t1); glVertex2f(q.x1,q.y0); | |
glTexCoord2f(q.s1,q.t0); glVertex2f(q.x1,q.y1); | |
glTexCoord2f(q.s0,q.t0); glVertex2f(q.x0,q.y1); | |
} | |
++text; | |
} | |
glEnd(); | |
} | |
int main() { | |
int render_width = 100; | |
int render_height = 100; | |
//Create window | |
SDL_Window * window = SDL_CreateWindow | |
( | |
"Untitled Window", | |
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, | |
render_width, render_height, | |
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL | |
); | |
if(!window) | |
{ | |
SDL_LogCritical(SDL_LOG_CATEGORY_SYSTEM, "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); | |
return -1; | |
} | |
SDL_GLContext context = SDL_GL_CreateContext( window ); | |
if( context == NULL ) | |
{ | |
SDL_LogCritical(SDL_LOG_CATEGORY_SYSTEM, "OpenGL context could not be created! SDL Error: %s\n", SDL_GetError() ); | |
return -3; | |
} | |
if( SDL_GL_SetSwapInterval( 1 ) < 0 ) | |
{ | |
SDL_LogCritical(SDL_LOG_CATEGORY_SYSTEM, "Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError() ); | |
} | |
glEnable(GL_TEXTURE_2D); | |
my_stbtt_initfont(); | |
bool has_focus = false; | |
bool running = true; | |
while(running) { | |
SDL_Event e; | |
while(SDL_PollEvent( &e )) { | |
switch (e.type) { | |
case SDL_WINDOWEVENT: | |
if (e.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) { | |
has_focus = true; | |
} else if (e.window.event == SDL_WINDOWEVENT_FOCUS_LOST) { | |
has_focus = false; | |
} | |
break; | |
case SDL_QUIT: | |
running = false; | |
break; | |
default: | |
break; | |
} | |
} | |
// if (!has_focus) { | |
// SDL_Delay(50); | |
// continue; | |
// } | |
glMatrixMode(GL_PROJECTION); | |
int window_width, window_height; | |
SDL_GetWindowSize(window, &window_width, &window_height); | |
glViewport(0, 0, window_width, window_height); | |
glClearColor(.5f,.0f,.5f,.5f); | |
glClear(GL_COLOR_BUFFER_BIT); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
glOrtho(0, 200, 200, 0, 10, -10); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
my_stbtt_print(10, 20, "hello how are you?"); | |
SDL_GL_SwapWindow(window); | |
check_gl_error(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment