From b6c0b22742b9661c9655162a8398799440e2951f Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 9 Jan 2020 12:00:39 +0100 Subject: [PATCH] interfaces fixes; freetype.text_width(); gl and stbi fixes --- vlib/builtin/array_test.v | 7 +++++ vlib/clipboard/clipboard_darwin.v | 2 +- vlib/clipboard/clipboard_linux.v | 2 +- vlib/clipboard/clipboard_windows.v | 2 +- vlib/compiler/aparser.v | 3 +++ vlib/compiler/expression.v | 12 +++++++-- vlib/compiler/fn.v | 2 +- vlib/freetype/freetype.v | 42 ++++++++++++++++++++++++++++++ vlib/gg/gg.v | 26 ++++++++++++++++-- vlib/gl/1shader.v | 17 ++++++++++++ vlib/glfw/glfw.v | 22 ++++++++++++++++ vlib/os/os.v | 6 ++--- vlib/stbi/stbi.v | 17 ++++++++++++ vlib/v/table/table.v | 4 +-- 14 files changed, 150 insertions(+), 14 deletions(-) diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index 6518321e27..b304bd444d 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -529,3 +529,10 @@ fn test_in_struct() { baz.bar[0]++ assert baz.bar[0] == 3 } + +fn test_bools() { + println('test b') + mut a := [true, false] + a << true + println(a) +} diff --git a/vlib/clipboard/clipboard_darwin.v b/vlib/clipboard/clipboard_darwin.v index 2c20fb5469..3209b40ba1 100644 --- a/vlib/clipboard/clipboard_darwin.v +++ b/vlib/clipboard/clipboard_darwin.v @@ -5,7 +5,7 @@ module clipboard #flag -framework Cocoa -struct Clipboard { +pub struct Clipboard { pb voidptr last_cb_serial i64 mut: diff --git a/vlib/clipboard/clipboard_linux.v b/vlib/clipboard/clipboard_linux.v index 77edb739ed..8ff2eabcfa 100644 --- a/vlib/clipboard/clipboard_linux.v +++ b/vlib/clipboard/clipboard_linux.v @@ -97,7 +97,7 @@ enum atom_type { text_html = 9 } -struct Clipboard { +pub struct Clipboard { display &Display mut: selection Atom //the selection atom diff --git a/vlib/clipboard/clipboard_windows.v b/vlib/clipboard/clipboard_windows.v index 1bb5ac45fb..a00ac0662f 100644 --- a/vlib/clipboard/clipboard_windows.v +++ b/vlib/clipboard/clipboard_windows.v @@ -36,7 +36,7 @@ fn C.SetLastError(error i64) fn C.OpenClipboard(hwnd HWND) int fn C.DestroyWindow(hwnd HWND) -struct Clipboard { +pub struct Clipboard { max_retries int retry_delay int mut: diff --git a/vlib/compiler/aparser.v b/vlib/compiler/aparser.v index 5966631001..e37a3bd6b1 100644 --- a/vlib/compiler/aparser.v +++ b/vlib/compiler/aparser.v @@ -2639,6 +2639,9 @@ fn (p mut Parser) array_init() string { new_arr_ph := p.cgen.add_placeholder() mut i := 0 for p.tok != .rsbr { + if expected_array_type.starts_with('array_') { + p.expected_type = expected_array_type[6..] + } val_typ := p.bool_expression() // Get the type of the first expression if i == 0 { diff --git a/vlib/compiler/expression.v b/vlib/compiler/expression.v index 8712205cb1..6017af8bee 100644 --- a/vlib/compiler/expression.v +++ b/vlib/compiler/expression.v @@ -53,8 +53,13 @@ fn (p mut Parser) bool_expression() string { // `window.widget = button`, widget is an interface if expected != typ && expected.ends_with('er') && expected.contains('I') { tt := typ.replace('*', '_ptr') + /* + if p.fileis('button') || p.fileis('textbox') { + p.warn('exp="$expected" typ="$typ" tt="$tt"') + } + */ p.cgen.set_placeholder(start_ph, - '($expected) { ._interface_idx = _${expected}_${tt}_index, ._object = ' ) + '($expected) { ._interface_idx = /* :) */ _${expected}_${tt}_index, ._object = ' ) p.gen('}') //p.satisfies_interface(expected, typ, true) } @@ -537,6 +542,9 @@ fn (p mut Parser) expression() string { // _PUSH(&a, expression(), tmp, string) tmp := p.get_tmp() tmp_typ := parse_pointer(typ[6..]) // skip "array_" + //p.warn('arr typ $tmp_typ') + p.expected_type = tmp_typ + //println('set expr to $tmp_typ') p.check_space(.left_shift) // Get the value we are pushing p.gen(', (') @@ -552,7 +560,7 @@ fn (p mut Parser) expression() string { } p.gen('/*typ = $typ tmp_typ=$tmp_typ*/') ph_clone := p.cgen.add_placeholder() - expr_type := p.expression() + expr_type := p.bool_expression() // Need to clone the string when appending it to an array? if p.pref.autofree && typ == 'array_string' && expr_type == 'string' { p.cgen.set_placeholder(ph_clone, 'string_clone(') diff --git a/vlib/compiler/fn.v b/vlib/compiler/fn.v index d65e505001..20cba0b0a5 100644 --- a/vlib/compiler/fn.v +++ b/vlib/compiler/fn.v @@ -1094,7 +1094,7 @@ fn (p mut Parser) fn_call_args(f mut Fn, generic_param_types []string) { // _interface_idx = _Speaker_Dog_index }) concrete_type_name := typ.replace('*', '_ptr') p.cgen.set_placeholder(ph, '($arg.typ) { ._object = &') - p.gen(', ._interface_idx = _${arg.typ}_${concrete_type_name}_index} /* i. arg*/') + p.gen(', /*OLD*/ ._interface_idx = _${arg.typ}_${concrete_type_name}_index} /* i. arg*/') p.table.add_gen_type(arg.typ, typ) } } diff --git a/vlib/freetype/freetype.v b/vlib/freetype/freetype.v index 4ca931a745..2bf1c42edd 100644 --- a/vlib/freetype/freetype.v +++ b/vlib/freetype/freetype.v @@ -11,6 +11,7 @@ import ( glm gl filepath + time ) #flag windows -I @VROOT/thirdparty/freetype/include @@ -352,3 +353,44 @@ pub fn (ctx mut FreeType) draw_text_def(x, y int, text string) { } ctx.draw_text(x, y, text, cfg) } + +pub fn (ctx mut FreeType) text_width(s string) int { + //t := time.ticks() + utext := s.ustring() + mut x := f64(0) + for i := 0; i < utext.len; i++ { + _rune := utext.at(i) + mut ch := Character{} + mut found := false + if _rune.len == 1 { + idx := _rune[0] + if idx < 0 || idx >= ctx.chars.len { + println('BADE RUNE $_rune') + continue + } + found = true + ch = ctx.chars[_rune[0]] + } + else if _rune.len > 1 { + // TODO O(1) use map + for j := 0; j < ctx.utf_runes.len; j++ { + rune_j := ctx.utf_runes[j] + if rune_j==_rune { + ch = ctx.utf_chars[j] + found = true + break + } + } + } + if !found && _rune.len > 0 && _rune[0] > 32 { + ch = ft_load_char(ctx.face, _rune.utf32_code()) + ctx.utf_runes << _rune + ctx.utf_chars << ch + } + x += ch.advance >> u32(6) + } + //println('text width "$s" = ${time.ticks() - t} ms') + return int(x) / ctx.scale +} + + diff --git a/vlib/gg/gg.v b/vlib/gg/gg.v index c72efdc125..8a719e133f 100644 --- a/vlib/gg/gg.v +++ b/vlib/gg/gg.v @@ -372,11 +372,22 @@ pub fn create_image(file string) u32 { img.tex_image_2d() gl.generate_mipmap(C.GL_TEXTURE_2D) img.free() - // println('gg end') + return texture +} + +pub fn create_image_from_memory(buf byteptr) u32 { + texture := gl.gen_texture() + img := stbi.load_from_memory(buf) + // TODO copy pasta + gl.bind_2d_texture(texture) + img.tex_image_2d() + gl.generate_mipmap(C.GL_TEXTURE_2D) + img.free() return texture } pub fn (ctx &GG) draw_line_c(x, y, x2, y2 f32, color gx.Color) { + ctx.shader.set_int('has_texture', 0) C.glDeleteBuffers(1, &ctx.vao) C.glDeleteBuffers(1, &ctx.vbo) ctx.shader.use() @@ -403,6 +414,12 @@ pub fn (c &GG) draw_vertical(x, y, height int) { // fn (ctx &GG) draw_image(x, y, w, h f32, img stbi.Image) { pub fn (ctx &GG) draw_image(x, y, w, h f32, tex_id u32) { + + last_array_buffer := 0 + last_texture := 0 + C.glGetIntegerv(C.GL_ARRAY_BUFFER_BINDING, &last_array_buffer) + C.glGetIntegerv(C.GL_TEXTURE_BINDING_2D, &last_texture) + // println('DRAW IMAGE $x $y $w $h $tex_id') ctx.shader.use() // ctx.shader.set_color('color', c) @@ -422,6 +439,7 @@ pub fn (ctx &GG) draw_image(x, y, w, h f32, tex_id u32) { ] ! // VAO := gl.gen_vertex_array() // VBO := gl.gen_buffer() + C.glEnable(C.GL_TEXTURE_2D) gl.bind_vao(ctx.vao) gl.set_vbo(ctx.vbo, vertices, C.GL_STATIC_DRAW) ebo := gl.gen_buffer() @@ -435,9 +453,13 @@ pub fn (ctx &GG) draw_image(x, y, w, h f32, tex_id u32) { gl.bind_2d_texture(tex_id) gl.bind_vao(ctx.vao) gl.draw_elements(C.GL_TRIANGLES, 6, C.GL_UNSIGNED_INT, 0) + C.glDisable(C.GL_TEXTURE_2D) + // restore state + C.glBindBuffer(C.GL_ARRAY_BUFFER, last_array_buffer) + C. glBindTexture(C.GL_TEXTURE_2D, last_texture) } -pub fn (c &GG) draw_empty_rect(x, y, w, h int, color gx.Color) { +pub fn (c &GG) draw_empty_rect(x, y, w, h f32, color gx.Color) { c.draw_line_c(x, y, x + w, y, color) c.draw_line_c(x, y, x, y + h, color) c.draw_line_c(x, y + h, x + w, y + h, color) diff --git a/vlib/gl/1shader.v b/vlib/gl/1shader.v index 97d5b01712..5c6599f0cd 100644 --- a/vlib/gl/1shader.v +++ b/vlib/gl/1shader.v @@ -76,6 +76,23 @@ void main() { // FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); // FragColor = vec4(1.0f, 0.0f, 0.0f, 1.0f); if (has_texture) { + /* + vec3 chromaKeyColor = texture(ourTexture,TexCoord.xy).xyz; + +float alpha; +bool is_cyan = ((chromaKeyColor.x == 0)); // && chromaKeyColor.x <= 1) && (chromaKeyColor.y <= 255) && +bool is_pink= ((chromaKeyColor.y == 0)); +//bool is_pink= ((chromaKeyColor.x <= 255) && (chromaKeyColor.y == 0) &&(chromaKeyColor.z <= 255)); +if (is_cyan || is_pink) { + alpha = 0.; +} +else +{ + alpha = 1.0; +} +FragColor= vec4(texture(ourTexture,TexCoord.xy).xyz,alpha); +*/ + FragColor = texture(ourTexture, TexCoord); } else { diff --git a/vlib/glfw/glfw.v b/vlib/glfw/glfw.v index c10d09d99e..42e226d9bc 100644 --- a/vlib/glfw/glfw.v +++ b/vlib/glfw/glfw.v @@ -220,6 +220,13 @@ pub fn (w &Window) set_clipboard_text(s string) { C.glfwSetClipboardString(w.data, s.str) } +pub fn get_cursor_pos(glfw_window voidptr) (f64, f64) { + x := f64(0) + y := f64(0) + C.glfwGetCursorPos(glfw_window, &x, &y) + return x,y +} + pub fn (w &Window) get_cursor_pos() Pos { x := f64(0) y := f64(0) @@ -230,6 +237,21 @@ pub fn (w &Window) get_cursor_pos() Pos { } } +enum Cursor { + arrow + ibeam + hand +} + +pub fn set_cursor(c Cursor) { + C.glfwSetCursor(0, C.GLFW_IBEAM_CURSOR) +} + +pub fn (w &Window) set_cursor(c Cursor) { + C.glfwSetCursor(w.data, C.GLFW_IBEAM_CURSOR) + +} + pub fn (w &Window) user_ptr() voidptr { return C.glfwGetWindowUserPointer(w.data) } diff --git a/vlib/os/os.v b/vlib/os/os.v index 1418602eb2..755c29a1c7 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -75,21 +75,19 @@ pub fn (f File) is_opened() bool { return f.opened } -/* // read_bytes reads an amount of bytes from the beginning of the file -pub fn (f mut File) read_bytes(size int) []byte { +pub fn (f &File) read_bytes(size int) []byte { return f.read_bytes_at(size, 0) } // read_bytes_at reads an amount of bytes at the given position in the file -pub fn (f mut File) read_bytes_at(size, pos int) []byte { +pub fn (f &File) read_bytes_at(size, pos int) []byte { mut arr := [`0`].repeat(size) C.fseek(f.cfile, pos, C.SEEK_SET) nreadbytes := C.fread(arr.data, 1, size, f.cfile) C.fseek(f.cfile, 0, C.SEEK_SET) return arr[0..nreadbytes] } -*/ pub fn read_bytes(path string) ?[]byte { diff --git a/vlib/stbi/stbi.v b/vlib/stbi/stbi.v index 0ecddde5d8..6f045ad10b 100644 --- a/vlib/stbi/stbi.v +++ b/vlib/stbi/stbi.v @@ -22,6 +22,7 @@ mut: } fn C.stbi_load() voidptr +fn C.stbi_load_from_memory() voidptr fn C.stbi_image_free() fn C.stbi_set_flip_vertically_on_load() @@ -41,6 +42,22 @@ pub fn load(path string) Image { return res } +//pub fn load_from_memory(buf []byte) Image { +pub fn load_from_memory(buf byteptr) Image { + mut res := Image { + ok: true + data: 0 + } + flag := C.STBI_rgb_alpha + res.data = C.stbi_load_from_memory(buf, 3812, &res.width, &res.height, &res.nr_channels, flag) + if isnil(res.data) { + println('stbi image failed to load from memory') + exit(1) + } + return res +} + + pub fn (img Image) free() { C.stbi_image_free(img.data) } diff --git a/vlib/v/table/table.v b/vlib/v/table/table.v index 6a9a366918..e5e7ff8c20 100644 --- a/vlib/v/table/table.v +++ b/vlib/v/table/table.v @@ -245,8 +245,8 @@ pub fn (t mut Table) register_type(typ types.Type, name string, idx int) { } t.type_idxs[name] = idx t.types << typ - efn := []Fn - t.methods << efn // TODO [] breaks V + e := []Fn + t.methods << e // TODO [] breaks V } pub fn (t mut Table) register_struct(typ types.Struct) int {