summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2021-02-05 10:12:55 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2021-02-05 10:12:55 -0500
commitdd3887ec47137b6bf993674356a73b3266bb8a51 (patch)
treec9981783a6574b58dd9f503384b2fe651495b224
parent1e7e392b9d5e593aeb9680d82b76afddad8ce2e6 (diff)
got text working with modern OpenGL
-rw-r--r--gl.c1
-rw-r--r--main.c9
-rw-r--r--make.bat2
-rw-r--r--menu.c5
-rw-r--r--text.c21
-rw-r--r--text.h8
-rw-r--r--ui.c9
7 files changed, 25 insertions, 30 deletions
diff --git a/gl.c b/gl.c
index 1f5d7bc..738a2e8 100644
--- a/gl.c
+++ b/gl.c
@@ -28,6 +28,7 @@
do(GENVERTEXARRAYS, GenVertexArrays)\
do(DELETEVERTEXARRAYS, DeleteVertexArrays)\
do(BINDVERTEXARRAY, BindVertexArray)\
+ do(ACTIVETEXTURE, ActiveTexture)\
do(UNIFORM1F, Uniform1f)\
do(UNIFORM2F, Uniform2f)\
do(UNIFORM3F, Uniform3f)\
diff --git a/main.c b/main.c
index 92d6b23..7ef1447 100644
--- a/main.c
+++ b/main.c
@@ -305,13 +305,10 @@ int main(int argc, char **argv) {
glViewport(0, 0, w, h);
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- for (int i = 0; i < 200; ++i) {
- gl_geometry_rect(rect(V2(0, 4*(float)i), V2(100, 100)), 0xffffffff >> i);
-
- gl_geometry_draw();
- }
- text_render(font, "hello", 5, 5);
+ text_render(font, "Hello how are you?", 5, 5, 0xff00aaff);
SDL_GL_SwapWindow(window);
}
diff --git a/make.bat b/make.bat
index 32770d8..ac9269a 100644
--- a/make.bat
+++ b/make.bat
@@ -7,7 +7,7 @@ if _%VCVARS% == _ (
SET CFLAGS=/nologo /W4 /wd4200 /wd4204 /wd4221 /wd4706 /wd4214 /D_CRT_SECURE_NO_WARNINGS /I SDL2/include SDL2/lib/x64/SDL2main.lib SDL2/lib/x64/SDL2.lib opengl32.lib shell32.lib ole32.lib
rc /nologo ted.rc
if _%1 == _ (
- cl main.c text.c ted.res /DDEBUG /DEBUG /Zi %CFLAGS% /Fe:ted
+ cl main.c ted.res /DDEBUG /DEBUG /Zi %CFLAGS% /Fe:ted
)
if _%1 == _release cl main.c ted.res /O2 %CFLAGS% /Fe:ted
if _%1 == _profile cl main.c ted.res /O2 /DPROFILE %CFLAGS% /Fe:ted
diff --git a/menu.c b/menu.c
index f979b72..70aaf9d 100644
--- a/menu.c
+++ b/menu.c
@@ -209,11 +209,10 @@ static void menu_render(Ted *ted, Menu menu) {
menu_y2 -= padding;
- gl_color_rgba(colors[COLOR_TEXT]);
if (menu == MENU_OPEN) {
- text_render(font_bold, "Open...", menu_x1, menu_y1);
+ text_render(font_bold, "Open...", menu_x1, menu_y1, colors[COLOR_TEXT]);
} else if (menu == MENU_SAVE_AS) {
- text_render(font_bold, "Save as...", menu_x1, menu_y1);
+ text_render(font_bold, "Save as...", menu_x1, menu_y1, colors[COLOR_TEXT]);
}
menu_y1 += char_height_bold * 0.75f + padding;
diff --git a/text.c b/text.c
index 3368414..1981051 100644
--- a/text.c
+++ b/text.c
@@ -41,7 +41,7 @@ TextRenderState const text_render_state_default = {
.x = 0, .y = 0,
.min_x = -FLT_MAX, .max_x = +FLT_MAX,
.min_y = -FLT_MAX, .max_y = +FLT_MAX,
- .r = 1, .g = 0, .b = 1, .a = 1,
+ .color = {1, 0, 1, 1},
};
static char text_err[200];
@@ -90,7 +90,7 @@ varying vec2 tex_coord;\n\
uniform sampler2D sampler;\n\
void main() {\n\
vec4 tex_color = texture2D(sampler, tex_coord);\n\
- gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);//vec4(1.0, 1.0, 1.0, tex_color.x * color);\n\
+ gl_FragColor = vec4(1.0, 1.0, 1.0, tex_color.x) * color;\n\
}\n\
";
@@ -237,7 +237,7 @@ void text_chars_end(Font *font) {
if (gl_version_major >= 3)
glBindVertexArray(text_vao);
glBindBuffer(GL_ARRAY_BUFFER, text_vbo);
- glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)(ntriangles * sizeof(TextTriangle)), font->triangles, GL_STREAM_DRAW);
+ glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)(ntriangles * sizeof(TextTriangle)), font->triangles[i], GL_STREAM_DRAW);
glVertexAttribPointer(text_v_pos, 2, GL_FLOAT, 0, sizeof(TextVertex), (void *)offsetof(TextVertex, pos));
glEnableVertexAttribArray(text_v_pos);
glVertexAttribPointer(text_v_tex_coord, 2, GL_FLOAT, 0, sizeof(TextVertex), (void *)offsetof(TextVertex, tex_coord));
@@ -310,7 +310,7 @@ top:
y1 = max_y-1;
}
if (state->render) {
- float r = state->r, g = state->g, b = state->b, a = state->a;
+ float r = state->color[0], g = state->color[1], b = state->color[2], a = state->color[3];
TextVertex v1 = {{x0, y0}, {s0, t0}, {r, g, b, a}};
TextVertex v2 = {{x0, y1}, {s0, t1}, {r, g, b, a}};
TextVertex v3 = {{x1, y1}, {s1, t1}, {r, g, b, a}};
@@ -349,19 +349,20 @@ void text_render_with_state(Font *font, TextRenderState *render_state, char cons
}
-static void text_render_internal(Font *font, char const *text, float *x, float *y, bool render) {
+static void text_render_internal(Font *font, char const *text, float *x, float *y, u32 color, bool render) {
TextRenderState render_state = text_render_state_default;
render_state.render = render;
+ rgba_u32_to_floats(color, render_state.color);
text_render_with_state(font, &render_state, text, *x, *y);
*x = render_state.x;
*y = render_state.y;
}
-void text_render(Font *font, char const *text, float x, float y) {
- text_render_internal(font, text, &x, &y, true);
+void text_render(Font *font, char const *text, float x, float y, u32 color) {
+ text_render_internal(font, text, &x, &y, color, true);
}
-void text_render_anchored(Font *font, char const *text, float x, float y, Anchor anchor) {
+void text_render_anchored(Font *font, char const *text, float x, float y, u32 color, Anchor anchor) {
float w = 0, h = 0; // width, height of text
text_get_size(font, text, &w, &h);
float hw = w * 0.5f, hh = h * 0.5f; // half-width, half-height
@@ -376,12 +377,12 @@ void text_render_anchored(Font *font, char const *text, float x, float y, Anchor
case ANCHOR_BOTTOM_MIDDLE: x -= hw; y -= h; break;
case ANCHOR_BOTTOM_RIGHT: x -= w; y -= h; break;
}
- text_render(font, text, x, y);
+ text_render(font, text, x, y, color);
}
void text_get_size(Font *font, char const *text, float *width, float *height) {
float x = 0, y = 0;
- text_render_internal(font, text, &x, &y, false);
+ text_render_internal(font, text, &x, &y, 0, false);
if (width) *width = x;
if (height) *height = y + font->char_height;
}
diff --git a/text.h b/text.h
index f57fef9..0f0ba68 100644
--- a/text.h
+++ b/text.h
@@ -18,8 +18,8 @@ typedef struct {
float x, y;
// points at which the text should be cut off
float min_x, max_x, min_y, max_y;
- // color
- float r, g, b, a;
+ // [0] = r, [1] = g, [2] = b, [3] = a.
+ float color[4];
} TextRenderState;
typedef enum {
@@ -47,13 +47,13 @@ extern float text_font_char_height(Font *font);
// This is meant to be only used for monospace fonts.
extern float text_font_char_width(Font *font);
// Render some UTF-8 text to the screen (simple interface).
-extern void text_render(Font *font, char const *text, float x, float y);
+extern void text_render(Font *font, char const *text, float x, float y, u32 color);
// Get the dimensions of some text.
extern void text_get_size(Font *font, char const *text, float *width, float *height);
extern void text_get_size32(Font *font, char32_t const *text, u64 len, float *width, float *height);
// Write text, but using a state, starting at (x, y) -- state->x and state->y are ignored. This allows you to control min/max_x/y.
extern void text_render_with_state(Font *font, TextRenderState *state, char const *text, float x, float y);
-extern void text_render_anchored(Font *font, char const *text, float x, float y, Anchor anchor);
+extern void text_render_anchored(Font *font, char const *text, float x, float y, u32 color, Anchor anchor);
// Begin writing characters.
extern void text_chars_begin(Font *font);
// Finish writing characters.
diff --git a/ui.c b/ui.c
index 310f725..1516b8f 100644
--- a/ui.c
+++ b/ui.c
@@ -423,8 +423,7 @@ static void file_selector_render(Ted *ted, FileSelector *fs) {
rect_coords(bounds, &x1, &y1, &x2, &y2);
// current working directory
- gl_color_rgba(colors[COLOR_TEXT]);
- text_render(font, fs->cwd, x1, y1);
+ text_render(font, fs->cwd, x1, y1, colors[COLOR_TEXT]);
y1 += char_height + padding;
// search buffer
@@ -485,9 +484,8 @@ static void button_render(Ted *ted, Rect button, char const *text, u32 color) {
rect_render_border(button, 1);
glEnd();
- gl_color_rgba(color);
v2 pos = rect_center(button);
- text_render_anchored(ted->font, text, pos.x, pos.y, ANCHOR_MIDDLE);
+ text_render_anchored(ted->font, text, pos.x, pos.y, color, ANCHOR_MIDDLE);
}
// returns true if the button was clicked on.
@@ -558,8 +556,7 @@ static void popup_render(Ted *ted, char const *title, char const *body) {
v2 title_size = {0};
text_get_size(font_bold, title, &title_size.x, &title_size.y);
v2 title_pos = v2_sub(V2(window_width * 0.5f, y), V2(title_size.x * 0.5f, 0));
- gl_color_rgba(colors[COLOR_TEXT]);
- text_render(font_bold, title, title_pos.x, title_pos.y);
+ text_render(font_bold, title, title_pos.x, title_pos.y, colors[COLOR_TEXT]);
y += char_height_bold;
// line separating text from body