interfaces fixes; freetype.text_width(); gl and stbi fixes

pull/3412/head
Alexander Medvednikov 2020-01-09 12:00:39 +01:00
parent 938f27e391
commit b6c0b22742
14 changed files with 150 additions and 14 deletions

View File

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

View File

@ -5,7 +5,7 @@ module clipboard
#flag -framework Cocoa
struct Clipboard {
pub struct Clipboard {
pb voidptr
last_cb_serial i64
mut:

View File

@ -97,7 +97,7 @@ enum atom_type {
text_html = 9
}
struct Clipboard {
pub struct Clipboard {
display &Display
mut:
selection Atom //the selection atom

View File

@ -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:

View File

@ -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 {

View File

@ -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(')

View File

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

View File

@ -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
}

View File

@ -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)

View File

@ -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 {

View File

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

View File

@ -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 {

View File

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

View File

@ -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 {