|
|
@@ -37,7 +37,8 @@ struct app handle_events(struct app app);
|
|
|
struct glyph {
|
|
|
struct vec2i tile;
|
|
|
int32_t c;
|
|
|
- struct vec4f color;
|
|
|
+ struct vec4f fg_color;
|
|
|
+ struct vec4f bg_color;
|
|
|
};
|
|
|
|
|
|
DA_DEF_STRUCT(struct glyph, glyphs);
|
|
|
@@ -53,7 +54,8 @@ struct glyph_attr {
|
|
|
enum glyph_attr_enum {
|
|
|
GLYPH_ATTR_TILE = 0,
|
|
|
GLYPH_ATTR_C,
|
|
|
- GLYPH_ATTR_COLOR,
|
|
|
+ GLYPH_ATTR_FG_COLOR,
|
|
|
+ GLYPH_ATTR_BG_COLOR,
|
|
|
GLYPH_ATTR_TOTAL
|
|
|
};
|
|
|
|
|
|
@@ -72,12 +74,19 @@ static const struct glyph_attr glyph_attr[GLYPH_ATTR_TOTAL] = {
|
|
|
.stride = sizeof(struct glyph),
|
|
|
.offset = offsetof(struct glyph, c)
|
|
|
},
|
|
|
- [GLYPH_ATTR_COLOR] = {
|
|
|
+ [GLYPH_ATTR_FG_COLOR] = {
|
|
|
.size = 4,
|
|
|
.type = GL_FLOAT,
|
|
|
.norm = GL_FALSE,
|
|
|
.stride = sizeof(struct glyph),
|
|
|
- .offset = offsetof(struct glyph, color)
|
|
|
+ .offset = offsetof(struct glyph, fg_color)
|
|
|
+ },
|
|
|
+ [GLYPH_ATTR_BG_COLOR] = {
|
|
|
+ .size = 4,
|
|
|
+ .type = GL_FLOAT,
|
|
|
+ .norm = GL_FALSE,
|
|
|
+ .stride = sizeof(struct glyph),
|
|
|
+ .offset = offsetof(struct glyph, bg_color)
|
|
|
},
|
|
|
};
|
|
|
|
|
|
@@ -95,10 +104,10 @@ main(int32_t argc, char **argv)
|
|
|
struct app app = app_create("ged");
|
|
|
const char *font_path = "./charmap-oldschool_white.png";
|
|
|
app.font = font_create(app.rdr, font_path, 7, 18);
|
|
|
- app.font.scale = 2.0;
|
|
|
+ app.font.scale = 4.0;
|
|
|
|
|
|
struct glyphs glys;
|
|
|
- glys.cap = 1024;
|
|
|
+ glys.cap = 1024 * 1024;
|
|
|
glys.size = 0;
|
|
|
glys.items = calloc(glys.cap, sizeof(struct glyph));
|
|
|
|
|
|
@@ -168,10 +177,11 @@ main(int32_t argc, char **argv)
|
|
|
}
|
|
|
|
|
|
/* Getting the uniforms */
|
|
|
- /* app.uniforms.time = get_uniform(prog, "time"); */
|
|
|
+ app.uniforms.time = get_uniform(prog, "time");
|
|
|
app.uniforms.resolution = get_uniform(prog, "resolution");
|
|
|
app.uniforms.scale = get_uniform(prog, "scale");
|
|
|
- app.uniforms.cursor = get_uniform(prog, "cursor");
|
|
|
+ app.uniforms.camera = get_uniform(prog, "camera");
|
|
|
+ /* app.uniforms.cursor = get_uniform(prog, "cursor"); */
|
|
|
|
|
|
uint32_t vao = 0;
|
|
|
uint32_t vbo = 0;
|
|
|
@@ -213,20 +223,41 @@ main(int32_t argc, char **argv)
|
|
|
enum buffer_err err_;
|
|
|
RET_UNWRAP2(app.buf, err_,
|
|
|
struct ret_buffer_err,
|
|
|
- buffer_load_from_file(app.buf, "./m.c"));
|
|
|
+ buffer_load_from_file(app.buf, "./src/main.c"));
|
|
|
if ( err_ != BUFFER_ERR_OK ) {
|
|
|
fprintf(stderr, "Failed to read load buffer: %d\n", err_);
|
|
|
exit(EXIT_FAILURE);
|
|
|
}
|
|
|
|
|
|
- glys = gl_render_text(app, glys, vec2is(0));
|
|
|
- glyphs_sync(app, glys);
|
|
|
+ glUniform1f(app.uniforms.scale, (float)app.font.scale);
|
|
|
+ glUniform2f(app.uniforms.scale, 5.0, 5.0);
|
|
|
+
|
|
|
+ double dt = (1.0 / (double)app.target_fps) * 3;
|
|
|
+ (void) dt;
|
|
|
+ uint32_t frame_target_dur = (uint32_t)(1000 / app.target_fps);
|
|
|
+
|
|
|
+ char *fps_text = calloc(1, 128);
|
|
|
+ size_t fps_text_size = 0;
|
|
|
+
|
|
|
+ uint32_t fr_end = 0;
|
|
|
+ uint32_t fr_start = 0;
|
|
|
+ uint32_t fr_dur = 1;
|
|
|
|
|
|
- struct vec2i cur = app_calc_cursor(app);
|
|
|
- glUniform1f(app.uniforms.scale, 5.0);
|
|
|
+ /* struct vec2i cur = app_calc_cursor(app); */
|
|
|
while ( app.running ) {
|
|
|
+ fr_start = SDL_GetTicks();
|
|
|
+ struct buffer *buf = &app.buf;
|
|
|
app = handle_events(app);
|
|
|
|
|
|
+ if ( app.show_fps == true ) {
|
|
|
+ fps_text_size = (size_t) snprintf(fps_text, 128,
|
|
|
+ "FPS: %.2f",
|
|
|
+ 1000.0 / fr_dur);
|
|
|
+ (void) fps_text_size;
|
|
|
+ /* app_render_text(app, fps_text, fps_text_size, */
|
|
|
+ /* vec2s(0), 0xFF00FFFF, 5); */
|
|
|
+ }
|
|
|
+
|
|
|
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
@@ -235,12 +266,33 @@ main(int32_t argc, char **argv)
|
|
|
(int32_t) glys.size);
|
|
|
|
|
|
/* glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); */
|
|
|
+ {
|
|
|
+ struct vec2 cur_pos = app_calc_cur_pos(app);
|
|
|
+ struct vec2 cpos = buf->cam.pos;
|
|
|
+ struct vec2 cvel = buf->cam.vel;
|
|
|
+ cvel = vec2_sub(cur_pos, cpos);
|
|
|
+ cpos = vec2_add(cpos, vec2_mul(cvel, vec2s(dt)));
|
|
|
+ buf->cam.pos = cpos;
|
|
|
+ buf->cam.vel = cvel;
|
|
|
+ glUniform2f(app.uniforms.camera,
|
|
|
+ (float)cpos.x,
|
|
|
+ (float)-cpos.y);
|
|
|
+ }
|
|
|
|
|
|
SDL_GL_SwapWindow(app.win.ptr);
|
|
|
- /* glUniform1f(app.uniforms.time, */
|
|
|
- /* (float) (SDL_GetTicks() / 1000)); */
|
|
|
- cur = app_calc_cursor(app);
|
|
|
- glUniform2i(app.uniforms.cursor, cur.x, cur.y);
|
|
|
+ glUniform1f(app.uniforms.time,
|
|
|
+ (float) (SDL_GetTicks() / 1000));
|
|
|
+ /* cur = app_calc_cursor(app); */
|
|
|
+ /* glUniform2i(app.uniforms.cursor, cur.x, cur.y); */
|
|
|
+ glys.size = 0;
|
|
|
+ glys = gl_render_text(app, glys, vec2is(0));
|
|
|
+ glyphs_sync(app, glys);
|
|
|
+
|
|
|
+ fr_end = SDL_GetTicks();
|
|
|
+ fr_dur = (fr_end - fr_start);
|
|
|
+ if ( fr_dur < frame_target_dur ) {
|
|
|
+ SDL_Delay(frame_target_dur - fr_dur);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
app_destroy(app);
|
|
|
@@ -262,7 +314,8 @@ gl_render_text(struct app app, struct glyphs glys, struct vec2i tile)
|
|
|
struct glyph gly = {
|
|
|
.tile = vec2i_add(pen, vec2i(row, 0)),
|
|
|
.c = c,
|
|
|
- .color = vec4fs(1)
|
|
|
+ .fg_color = vec4fs(1),
|
|
|
+ .bg_color = vec4fs(0)
|
|
|
};
|
|
|
DA_APPEND(glys, gly);
|
|
|
++row;
|
|
|
@@ -288,6 +341,7 @@ get_uniform(uint32_t prog, const char *name)
|
|
|
fprintf(stderr, "Failed getting %s uniform location\n", name);
|
|
|
exit(EXIT_FAILURE);
|
|
|
}
|
|
|
+ printf("Got %s -> %d\n", name, uni);
|
|
|
return uni;
|
|
|
}
|
|
|
|