mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
remove "import const" everywhere
This commit is contained in:
168
vlib/gg/gg.v
168
vlib/gg/gg.v
@@ -5,30 +5,22 @@
|
||||
module gg
|
||||
|
||||
import stbi
|
||||
import glm
|
||||
import glm
|
||||
import gl
|
||||
import gx
|
||||
import os
|
||||
import glfw
|
||||
import glfw
|
||||
|
||||
struct Vec2 {
|
||||
pub:
|
||||
pub:
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
import const (
|
||||
GL_STATIC_DRAW
|
||||
GL_FLOAT
|
||||
GL_FALSE
|
||||
GL_UNSIGNED_INT
|
||||
GL_INT
|
||||
)
|
||||
|
||||
pub fn vec2(x, y int) Vec2 {
|
||||
res := Vec2 {
|
||||
x: x
|
||||
y: y
|
||||
x: x
|
||||
y: y
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -41,19 +33,19 @@ pub fn init() {
|
||||
|
||||
|
||||
struct Cfg {
|
||||
pub:
|
||||
pub:
|
||||
width int
|
||||
height int
|
||||
use_ortho bool
|
||||
use_ortho bool
|
||||
retina bool
|
||||
|
||||
|
||||
font_size int
|
||||
font_path string
|
||||
create_window bool
|
||||
window_user_ptr voidptr
|
||||
window_title string
|
||||
always_on_top bool
|
||||
scale int
|
||||
create_window bool
|
||||
window_user_ptr voidptr
|
||||
window_title string
|
||||
always_on_top bool
|
||||
scale int
|
||||
}
|
||||
|
||||
struct GG {
|
||||
@@ -68,29 +60,29 @@ struct GG {
|
||||
line_vbo u32
|
||||
vbo u32
|
||||
scale int // retina = 2 , normal = 1
|
||||
pub mut:
|
||||
window *glfw.Window
|
||||
render_fn fn()
|
||||
pub mut:
|
||||
window *glfw.Window
|
||||
render_fn fn()
|
||||
}
|
||||
|
||||
|
||||
// fn new_context(width, height int, use_ortho bool, font_size int) *GG {
|
||||
pub fn new_context(cfg Cfg) *GG {
|
||||
mut window := &glfw.Window{!}
|
||||
mut window := &glfw.Window{!}
|
||||
if cfg.create_window {
|
||||
window = glfw.create_window(glfw.WinCfg{
|
||||
title: cfg.window_title
|
||||
width: cfg.width
|
||||
height: cfg.height
|
||||
ptr: cfg.window_user_ptr
|
||||
always_on_top: cfg.always_on_top
|
||||
title: cfg.window_title
|
||||
width: cfg.width
|
||||
height: cfg.height
|
||||
ptr: cfg.window_user_ptr
|
||||
always_on_top: cfg.always_on_top
|
||||
})
|
||||
window.make_context_current()
|
||||
init()
|
||||
}
|
||||
init()
|
||||
}
|
||||
shader := gl.new_shader('simple')
|
||||
shader.use()
|
||||
if cfg.use_ortho {
|
||||
if cfg.use_ortho {
|
||||
projection := glm.ortho(0, cfg.width, cfg.height, 0)
|
||||
shader.set_mat4('projection', projection)
|
||||
}
|
||||
@@ -113,15 +105,15 @@ pub fn new_context(cfg Cfg) *GG {
|
||||
//gl.bind_buffer(GL_ARRAY_BUFFER, VBO)
|
||||
//gl.enable_vertex_attrib_array(0)
|
||||
//gl.vertex_attrib_pointer(0, 4, GL_FLOAT, false, 4, 0)
|
||||
todo_remove_me(cfg, scale)
|
||||
todo_remove_me(cfg, scale)
|
||||
return &GG {
|
||||
shader: shader
|
||||
width: cfg.width
|
||||
height: cfg.height
|
||||
vao: vao
|
||||
vbo: vbo
|
||||
window: window
|
||||
|
||||
shader: shader
|
||||
width: cfg.width
|
||||
height: cfg.height
|
||||
vao: vao
|
||||
vbo: vbo
|
||||
window: window
|
||||
|
||||
// /line_vao: gl.gen_vertex_array()
|
||||
// /line_vbo: gl.gen_buffer()
|
||||
//text_ctx: new_context_text(cfg, scale),
|
||||
@@ -133,25 +125,25 @@ pub fn new_context(cfg Cfg) *GG {
|
||||
//return ctx
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
pub fn (gg &GG) render_loop() bool {
|
||||
for !gg.window.show_close() {
|
||||
gg.render_fn()
|
||||
for !gg.window.show_close() {
|
||||
gg.render_fn()
|
||||
gg.window.swap_buffers()
|
||||
glfw.wait_events()
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
pub fn clear(color gx.Color) {
|
||||
pub fn clear(color gx.Color) {
|
||||
gl.clear()
|
||||
gl.clear_color(255, 255, 255, 255)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (gg &GG) render() {
|
||||
pub fn (gg &GG) render() {
|
||||
gg.window.swap_buffers()
|
||||
glfw.wait_events()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (ctx &GG) draw_triangle(x1, y1, x2, y2, x3, y3 f32, c gx.Color) {
|
||||
// println('draw_triangle $x1,$y1 $x2,$y2 $x3,$y3')
|
||||
@@ -165,8 +157,8 @@ pub fn (ctx &GG) draw_triangle(x1, y1, x2, y2, x3, y3 f32, c gx.Color) {
|
||||
// bind the Vertex Array Object first, then bind and set vertex buffer(s),
|
||||
// and then configure vertex attributes(s).
|
||||
gl.bind_vao(ctx.vao)
|
||||
gl.set_vbo(ctx.vbo, vertices, GL_STATIC_DRAW)
|
||||
gl.vertex_attrib_pointer(0, 3, GL_FLOAT, false, 3, 0)
|
||||
gl.set_vbo(ctx.vbo, vertices, C.GL_STATIC_DRAW)
|
||||
gl.vertex_attrib_pointer(0, 3, C.GL_FLOAT, false, 3, 0)
|
||||
gl.enable_vertex_attrib_array(0)
|
||||
// gl.bind_buffer(GL_ARRAY_BUFFER, uint(0))
|
||||
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO,
|
||||
@@ -175,7 +167,7 @@ pub fn (ctx &GG) draw_triangle(x1, y1, x2, y2, x3, y3 f32, c gx.Color) {
|
||||
// (nor VBOs) when it's not directly necessary.
|
||||
// gl.bind_vertex_array(uint(0))
|
||||
// gl.bind_vertex_array(ctx.VAO)
|
||||
gl.draw_arrays(GL_TRIANGLES, 0, 3)
|
||||
gl.draw_arrays(C.GL_TRIANGLES, 0, 3)
|
||||
}
|
||||
|
||||
pub fn (ctx &GG) draw_triangle_tex(x1, y1, x2, y2, x3, y3 f32, c gx.Color) {
|
||||
@@ -188,19 +180,19 @@ pub fn (ctx &GG) draw_triangle_tex(x1, y1, x2, y2, x3, y3 f32, c gx.Color) {
|
||||
x3, y3, 0, 0, 0, 0, 0, 0,
|
||||
] !
|
||||
gl.bind_vao(ctx.vao)
|
||||
gl.set_vbo(ctx.vbo, vertices, GL_STATIC_DRAW)
|
||||
gl.set_vbo(ctx.vbo, vertices, C.GL_STATIC_DRAW)
|
||||
// position attribute
|
||||
gl.vertex_attrib_pointer(0, 3, GL_FLOAT, false, 3, 0)
|
||||
gl.vertex_attrib_pointer(0, 3, C.GL_FLOAT, false, 3, 0)
|
||||
gl.enable_vertex_attrib_array(0)
|
||||
// color attribute
|
||||
gl.vertex_attrib_pointer(1, 3, GL_FLOAT, false, 8, 3)
|
||||
gl.vertex_attrib_pointer(1, 3, C.GL_FLOAT, false, 8, 3)
|
||||
gl.enable_vertex_attrib_array(1)
|
||||
// texture attribute
|
||||
gl.vertex_attrib_pointer(2, 2, GL_FLOAT, false, 8, 6)
|
||||
gl.vertex_attrib_pointer(2, 2, C.GL_FLOAT, false, 8, 6)
|
||||
gl.enable_vertex_attrib_array(2)
|
||||
// /
|
||||
// gl.draw_arrays(GL_TRIANGLES, 0, 3)
|
||||
gl.draw_elements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)
|
||||
gl.draw_elements(C.GL_TRIANGLES, 6, C.GL_UNSIGNED_INT, 0)
|
||||
}
|
||||
|
||||
pub fn (ctx &GG) draw_rect(x, y, w, h f32, c gx.Color) {
|
||||
@@ -214,9 +206,9 @@ pub fn (ctx &GG) draw_rect(x, y, w, h f32, c gx.Color) {
|
||||
ctx.draw_rect2(x, y, w, h, c)
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
fn (ctx mut GG) init_rect_vao() {
|
||||
|
||||
|
||||
ctx.rect_vao = gl.gen_vertex_array()
|
||||
ctx.rect_vbo = gl.gen_buffer()
|
||||
vertices := [
|
||||
@@ -230,11 +222,11 @@ fn (ctx mut GG) init_rect_vao() {
|
||||
1, 2, 3// second triangle
|
||||
] !
|
||||
gl.bind_vao(ctx.rect_vao)
|
||||
gl.set_vbo(ctx.rect_vbo, vertices, GL_STATIC_DRAW)
|
||||
gl.set_vbo(ctx.rect_vbo, vertices, C.GL_STATIC_DRAW)
|
||||
ebo := gl.gen_buffer()
|
||||
// ///////
|
||||
gl.set_ebo(ebo, indices, GL_STATIC_DRAW)
|
||||
}
|
||||
gl.set_ebo(ebo, indices, C.GL_STATIC_DRAW)
|
||||
}
|
||||
*/
|
||||
pub fn (ctx &GG) draw_rect2(x, y, w, h f32, c gx.Color) {
|
||||
C.glDeleteBuffers(1, &ctx.vao)
|
||||
@@ -244,9 +236,9 @@ pub fn (ctx &GG) draw_rect2(x, y, w, h f32, c gx.Color) {
|
||||
ctx.shader.set_int('has_texture', 0)
|
||||
// 4--1
|
||||
// 3--2
|
||||
$if linux {
|
||||
$if linux {
|
||||
// y += h
|
||||
}
|
||||
}
|
||||
vertices := [
|
||||
x + w, y, 0,
|
||||
x + w, y + h, 0,
|
||||
@@ -258,20 +250,20 @@ pub fn (ctx &GG) draw_rect2(x, y, w, h f32, c gx.Color) {
|
||||
1, 2, 3// second triangle
|
||||
] !
|
||||
gl.bind_vao(ctx.vao)
|
||||
gl.set_vbo(ctx.vbo, vertices, GL_STATIC_DRAW)
|
||||
gl.set_vbo(ctx.vbo, vertices, C.GL_STATIC_DRAW)
|
||||
ebo := gl.gen_buffer()
|
||||
// ///////
|
||||
gl.set_ebo(ebo, indices, GL_STATIC_DRAW)// !!! LEAKS
|
||||
gl.set_ebo(ebo, indices, C.GL_STATIC_DRAW)// !!! LEAKS
|
||||
// /////
|
||||
gl.vertex_attrib_pointer(0, 3, GL_FLOAT, false, 3, 0)
|
||||
gl.vertex_attrib_pointer(0, 3, C.GL_FLOAT, false, 3, 0)
|
||||
gl.enable_vertex_attrib_array(0)
|
||||
// gl.bind_vao(ctx.rect_vao)
|
||||
gl.bind_vao(ctx.vao)
|
||||
gl.draw_elements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)
|
||||
gl.draw_elements(C.GL_TRIANGLES, 6, C.GL_UNSIGNED_INT, 0)
|
||||
C.glDeleteBuffers(1, &ebo)
|
||||
}
|
||||
|
||||
fn todo_remove_me(cfg Cfg, scale int) {
|
||||
fn todo_remove_me(cfg Cfg, scale int) {
|
||||
// Can only have text in ortho mode
|
||||
if !cfg.use_ortho {
|
||||
return
|
||||
@@ -279,8 +271,8 @@ fn todo_remove_me(cfg Cfg, scale int) {
|
||||
mut width := cfg.width * scale
|
||||
mut height := cfg.height * scale
|
||||
font_size := cfg.font_size * scale
|
||||
gl.enable(GL_BLEND)
|
||||
//# glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
gl.enable(C.GL_BLEND)
|
||||
//# glBlendFunc(C.GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
shader := gl.new_shader('text')
|
||||
shader.use()
|
||||
projection := glm.ortho(0, width, 0, height)// 0 at BOT
|
||||
@@ -291,18 +283,18 @@ fn todo_remove_me(cfg Cfg, scale int) {
|
||||
//println('new gg text context VAO=$VAO')
|
||||
vbo := gl.gen_buffer()
|
||||
gl.bind_vao(vao)
|
||||
gl.bind_buffer(GL_ARRAY_BUFFER, vbo)
|
||||
gl.bind_buffer(C.GL_ARRAY_BUFFER, vbo)
|
||||
gl.enable_vertex_attrib_array(0)
|
||||
gl.vertex_attrib_pointer(0, 4, GL_FLOAT, false, 4, 0)
|
||||
gl.vertex_attrib_pointer(0, 4, C.GL_FLOAT, false, 4, 0)
|
||||
}
|
||||
|
||||
fn update() {
|
||||
// # ui__post_empty_event();
|
||||
}
|
||||
|
||||
pub fn post_empty_event() {
|
||||
glfw.post_empty_event()
|
||||
}
|
||||
pub fn post_empty_event() {
|
||||
glfw.post_empty_event()
|
||||
}
|
||||
|
||||
pub fn (c GG) circle(x, y, r int) {
|
||||
}
|
||||
@@ -372,7 +364,7 @@ pub fn create_image(file string) u32 {
|
||||
img := stbi.load(file)
|
||||
gl.bind_2d_texture(texture)
|
||||
img.tex_image_2d()
|
||||
gl.generate_mipmap(GL_TEXTURE_2D)
|
||||
gl.generate_mipmap(C.GL_TEXTURE_2D)
|
||||
img.free()
|
||||
// println('gg end')
|
||||
return texture
|
||||
@@ -385,11 +377,11 @@ pub fn (ctx &GG) draw_line_c(x, y, x2, y2 f32, color gx.Color) {
|
||||
ctx.shader.set_color('color', color)
|
||||
vertices := [f32(x), f32(y), f32(x2), f32(y2)] !
|
||||
gl.bind_vao(ctx.vao)
|
||||
gl.set_vbo(ctx.vbo, vertices, GL_STATIC_DRAW)
|
||||
gl.vertex_attrib_pointer(0, 2, GL_FLOAT, false, 2, 0)
|
||||
gl.set_vbo(ctx.vbo, vertices, C.GL_STATIC_DRAW)
|
||||
gl.vertex_attrib_pointer(0, 2, C.GL_FLOAT, false, 2, 0)
|
||||
gl.enable_vertex_attrib_array(0)
|
||||
gl.bind_vao(ctx.vao)
|
||||
gl.draw_arrays(GL_LINES, 0, 2)
|
||||
gl.draw_arrays(C.GL_LINES, 0, 2)
|
||||
}
|
||||
|
||||
pub fn (c &GG) draw_line(x, y, x2, y2 f32) {
|
||||
@@ -425,18 +417,18 @@ pub fn (ctx &GG) draw_image(x, y, w, h f32, tex_id u32) {
|
||||
// VAO := gl.gen_vertex_array()
|
||||
// VBO := gl.gen_buffer()
|
||||
gl.bind_vao(ctx.vao)
|
||||
gl.set_vbo(ctx.vbo, vertices, GL_STATIC_DRAW)
|
||||
gl.set_vbo(ctx.vbo, vertices, C.GL_STATIC_DRAW)
|
||||
ebo := gl.gen_buffer()
|
||||
gl.set_ebo(ebo, indices, GL_STATIC_DRAW)
|
||||
gl.vertex_attrib_pointer(0, 3, GL_FLOAT, false, 8, 0)
|
||||
gl.set_ebo(ebo, indices, C.GL_STATIC_DRAW)
|
||||
gl.vertex_attrib_pointer(0, 3, C.GL_FLOAT, false, 8, 0)
|
||||
gl.enable_vertex_attrib_array(0)
|
||||
gl.vertex_attrib_pointer(1, 3, GL_FLOAT, false, 8, 3)
|
||||
gl.vertex_attrib_pointer(1, 3, C.GL_FLOAT, false, 8, 3)
|
||||
gl.enable_vertex_attrib_array(1)
|
||||
gl.vertex_attrib_pointer(2, 2, GL_FLOAT, false, 8, 6)
|
||||
gl.vertex_attrib_pointer(2, 2, C.GL_FLOAT, false, 8, 6)
|
||||
gl.enable_vertex_attrib_array(2)
|
||||
gl.bind_2d_texture(u32(tex_id))
|
||||
gl.bind_vao(ctx.vao)
|
||||
gl.draw_elements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)
|
||||
gl.draw_elements(C.GL_TRIANGLES, 6, C.GL_UNSIGNED_INT, 0)
|
||||
}
|
||||
|
||||
pub fn (c &GG) draw_empty_rect(x, y, w, h int, color gx.Color) {
|
||||
|
||||
Reference in New Issue
Block a user