diff --git a/examples/empty_gg_freetype.v b/examples/empty_gg_freetype.v new file mode 100644 index 0000000000..f028a1af98 --- /dev/null +++ b/examples/empty_gg_freetype.v @@ -0,0 +1,56 @@ +module main + +import gg +import freetype +import gx +import glfw + +const ( + win_width = 600 + win_height = 300 + bg_color = gx.white +) + + +struct Context { +mut: + gg &gg.GG + ft &freetype.FreeType +} + +fn main() { + glfw.init_glfw() + mut ctx := &Context{ + gg: gg.new_context(gg.Cfg { + width: win_width + height: win_height + use_ortho: true // This is needed for 2D drawing + create_window: true + window_title: 'Empty window' + window_user_ptr: ctx + }) + } + ctx.gg.window.set_user_ptr(ctx) // TODO remove this when `window_user_ptr:` works + gg.clear(bg_color) + // Try to load font + ctx.ft = freetype.new_context(gg.Cfg{ + width: win_width + height: win_height + use_ortho: true + font_size: 18 + scale: 2 + }) + for { + gg.clear(bg_color) + ctx.draw() + ctx.gg.render() + if ctx.gg.window.should_close() { + ctx.gg.window.destroy() + return + } + } +} + +fn (ctx mut Context) draw() { +} + diff --git a/examples/tetris/tetris.v b/examples/tetris/tetris.v index db0272abd9..01d9f84a1b 100644 --- a/examples/tetris/tetris.v +++ b/examples/tetris/tetris.v @@ -128,7 +128,7 @@ struct Game { // gg context for drawing gg &gg.GG // ft context for font drawing - ft &freetype.Context + ft &freetype.FreeType font_loaded bool } diff --git a/vlib/freetype/freetype.v b/vlib/freetype/freetype.v index 4a765dd867..d7580a3ef1 100644 --- a/vlib/freetype/freetype.v +++ b/vlib/freetype/freetype.v @@ -52,7 +52,7 @@ struct C.FT_Library { _z int } -pub struct Context { +pub struct FreeType { shader gl.Shader // use_ortho bool width int @@ -130,11 +130,11 @@ fn ft_load_char(face C.FT_Face, code i64) Character { } } -pub fn new_context(cfg gg.Cfg) &Context { +pub fn new_context(cfg gg.Cfg) &FreeType { scale := cfg.scale // Can only have text in ortho mode if !cfg.use_ortho { - return &Context{} + return &FreeType{} } mut width := cfg.width * scale mut height := cfg.height * scale @@ -217,7 +217,7 @@ pub fn new_context(cfg gg.Cfg) &Context { // # glVertexAttribPointer(0, 4, GL_FLOAT,false, 4 * sizeof(GLf32), 0); // gl.bind_buffer(GL_ARRAY_BUFFER, uint(0)) // # glBindVertexArray(0); - mut ctx := &Context { + mut ctx := &FreeType { shader: shader width: width height: height @@ -234,7 +234,7 @@ pub fn new_context(cfg gg.Cfg) &Context { /* // A dirty hack to implement rendering of cyrillic letters. // All UTF-8 must be supported. update: no longer needed -fn (ctx mut Context) init_utf8_runes() { +fn (ctx mut FreeType) init_utf8_runes() { s := '≈≠⩽⩾йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ' print('init utf8 runes: ') //println(s) @@ -249,17 +249,17 @@ fn (ctx mut Context) init_utf8_runes() { } */ -pub fn (ctx mut Context) draw_text(_x, _y int, text string, cfg gx.TextCfg) { +pub fn (ctx mut FreeType) draw_text(_x, _y int, text string, cfg gx.TextCfg) { //utext := text.ustring_tmp() utext := text.ustring() ctx.private_draw_text(_x, _y, utext, cfg) } -fn (ctx mut Context) draw_text_fast(_x, _y int, text ustring, cfg gx.TextCfg) { +fn (ctx mut FreeType) draw_text_fast(_x, _y int, text ustring, cfg gx.TextCfg) { ctx.private_draw_text(_x, _y, text, cfg) } -fn (ctx mut Context) private_draw_text(_x, _y int, utext ustring, cfg gx.TextCfg) { +fn (ctx mut FreeType) private_draw_text(_x, _y int, utext ustring, cfg gx.TextCfg) { /* if utext.s.contains('on_seg') { println('\nat(0)') @@ -357,7 +357,7 @@ fn (ctx mut Context) private_draw_text(_x, _y int, utext ustring, cfg gx.TextCfg C.glBindTexture(C.GL_TEXTURE_2D, 0) } -pub fn (ctx mut Context) draw_text_def(x, y int, text string) { +pub fn (ctx mut FreeType) draw_text_def(x, y int, text string) { cfg := gx.TextCfg { color: gx.Black, size: DEFAULT_FONT_SIZE, diff --git a/vlib/glfw/glfw.v b/vlib/glfw/glfw.v index ded92be59a..45a76d77de 100644 --- a/vlib/glfw/glfw.v +++ b/vlib/glfw/glfw.v @@ -86,6 +86,13 @@ pub: // type clickpub fn pub fn (window * GLFWwindow, button, action, mods int) type clickpubfn fn (window voidptr, button, action, mods int) +/* + * TODO broken +fn init() { + init_glfw() +} +*/ + pub fn init_glfw() { C.glfwInit() C.glfwWindowHint(C.GLFW_CONTEXT_VERSION_MAJOR, 3)