examples: empty gg/freetype project

pull/2821/head
Alexander Medvednikov 2019-11-20 07:10:19 +03:00
parent 26fb7e0821
commit 759644ab36
4 changed files with 73 additions and 10 deletions

View File

@ -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() {
}

View File

@ -128,7 +128,7 @@ struct Game {
// gg context for drawing // gg context for drawing
gg &gg.GG gg &gg.GG
// ft context for font drawing // ft context for font drawing
ft &freetype.Context ft &freetype.FreeType
font_loaded bool font_loaded bool
} }

View File

@ -52,7 +52,7 @@ struct C.FT_Library {
_z int _z int
} }
pub struct Context { pub struct FreeType {
shader gl.Shader shader gl.Shader
// use_ortho bool // use_ortho bool
width int 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 scale := cfg.scale
// Can only have text in ortho mode // Can only have text in ortho mode
if !cfg.use_ortho { if !cfg.use_ortho {
return &Context{} return &FreeType{}
} }
mut width := cfg.width * scale mut width := cfg.width * scale
mut height := cfg.height * 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); // # glVertexAttribPointer(0, 4, GL_FLOAT,false, 4 * sizeof(GLf32), 0);
// gl.bind_buffer(GL_ARRAY_BUFFER, uint(0)) // gl.bind_buffer(GL_ARRAY_BUFFER, uint(0))
// # glBindVertexArray(0); // # glBindVertexArray(0);
mut ctx := &Context { mut ctx := &FreeType {
shader: shader shader: shader
width: width width: width
height: height height: height
@ -234,7 +234,7 @@ pub fn new_context(cfg gg.Cfg) &Context {
/* /*
// A dirty hack to implement rendering of cyrillic letters. // A dirty hack to implement rendering of cyrillic letters.
// All UTF-8 must be supported. update: no longer needed // 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 := 'йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ' s := 'йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ'
print('init utf8 runes: ') print('init utf8 runes: ')
//println(s) //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_tmp()
utext := text.ustring() utext := text.ustring()
ctx.private_draw_text(_x, _y, utext, cfg) 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) 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') { if utext.s.contains('on_seg') {
println('\nat(0)') 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) 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 { cfg := gx.TextCfg {
color: gx.Black, color: gx.Black,
size: DEFAULT_FONT_SIZE, size: DEFAULT_FONT_SIZE,

View File

@ -86,6 +86,13 @@ pub:
// type clickpub fn pub fn (window * GLFWwindow, button, action, mods int) // type clickpub fn pub fn (window * GLFWwindow, button, action, mods int)
type clickpubfn fn (window voidptr, button, action, mods int) type clickpubfn fn (window voidptr, button, action, mods int)
/*
* TODO broken
fn init() {
init_glfw()
}
*/
pub fn init_glfw() { pub fn init_glfw() {
C.glfwInit() C.glfwInit()
C.glfwWindowHint(C.GLFW_CONTEXT_VERSION_MAJOR, 3) C.glfwWindowHint(C.GLFW_CONTEXT_VERSION_MAJOR, 3)