mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
examples: make v -prod build-examples
pass without warnings/errors
This commit is contained in:
43
vlib/gg/gg.v
43
vlib/gg/gg.v
@ -13,13 +13,13 @@ import math
|
||||
// import time
|
||||
pub type FNCb = fn (x voidptr)
|
||||
|
||||
pub type FNEvent = fn (e, x voidptr)
|
||||
pub type FNEvent = fn (e voidptr, x voidptr)
|
||||
|
||||
pub type FNFail = fn (msg string, x voidptr)
|
||||
|
||||
pub type FNKeyDown = fn (c sapp.KeyCode, m sapp.Modifier, x voidptr)
|
||||
|
||||
pub type FNMove = fn (x, y f32, z voidptr)
|
||||
pub type FNMove = fn (x f32, y f32, z voidptr)
|
||||
|
||||
pub type FNChar = fn (c u32, x voidptr)
|
||||
|
||||
@ -43,13 +43,18 @@ pub:
|
||||
cleanup_fn FNCb = voidptr(0)
|
||||
fail_fn FNFail = voidptr(0)
|
||||
event_fn FNEvent = voidptr(0)
|
||||
keydown_fn FNKeyDown = voidptr(0) // special case of event_fn
|
||||
char_fn FNChar = voidptr(0) // special case of event_fn
|
||||
move_fn FNMove = voidptr(0) // special case of event_fn
|
||||
click_fn FNMove = voidptr(0) // special case of event_fn
|
||||
keydown_fn FNKeyDown = voidptr(0)
|
||||
// special case of event_fn
|
||||
char_fn FNChar = voidptr(0)
|
||||
// special case of event_fn
|
||||
move_fn FNMove = voidptr(0)
|
||||
// special case of event_fn
|
||||
click_fn FNMove = voidptr(0)
|
||||
// special case of event_fn
|
||||
wait_events bool // set this to true for UIs, to save power
|
||||
fullscreen bool
|
||||
scale f32 = 1.0 // vid needs this
|
||||
scale f32 = 1.0
|
||||
// vid needs this
|
||||
// init_text bool
|
||||
font_path string
|
||||
}
|
||||
@ -61,7 +66,8 @@ mut:
|
||||
// (so that the user can store image ids, not entire Image objects)
|
||||
image_cache []Image
|
||||
pub mut:
|
||||
scale f32 = 1.0 // will get set to 2.0 for retina, will remain 1.0 for normal
|
||||
scale f32 = 1.0
|
||||
// will get set to 2.0 for retina, will remain 1.0 for normal
|
||||
width int
|
||||
height int
|
||||
clear_pass C.sg_pass_action
|
||||
@ -237,7 +243,7 @@ pub fn (mut ctx Context) set_bg_color(c gx.Color) {
|
||||
}
|
||||
|
||||
// TODO: Fix alpha
|
||||
pub fn (ctx &Context) draw_rect(x, y, w, h f32, c gx.Color) {
|
||||
pub fn (ctx &Context) draw_rect(x f32, y f32, w f32, h f32, c gx.Color) {
|
||||
if c.a != 255 {
|
||||
sgl.load_pipeline(ctx.timage_pip)
|
||||
}
|
||||
@ -250,7 +256,7 @@ pub fn (ctx &Context) draw_rect(x, y, w, h f32, c gx.Color) {
|
||||
sgl.end()
|
||||
}
|
||||
|
||||
pub fn (ctx &Context) draw_triangle(x, y, x2, y2, x3, y3 f32, c gx.Color) {
|
||||
pub fn (ctx &Context) draw_triangle(x f32, y f32, x2 f32, y2 f32, x3 f32, y3 f32, c gx.Color) {
|
||||
if c.a != 255 {
|
||||
sgl.load_pipeline(ctx.timage_pip)
|
||||
}
|
||||
@ -262,7 +268,7 @@ pub fn (ctx &Context) draw_triangle(x, y, x2, y2, x3, y3 f32, c gx.Color) {
|
||||
sgl.end()
|
||||
}
|
||||
|
||||
pub fn (ctx &Context) draw_empty_rect(x, y, w, h f32, c gx.Color) {
|
||||
pub fn (ctx &Context) draw_empty_rect(x f32, y f32, w f32, h f32, c gx.Color) {
|
||||
if c.a != 255 {
|
||||
sgl.load_pipeline(ctx.timage_pip)
|
||||
}
|
||||
@ -284,7 +290,7 @@ pub fn (ctx &Context) draw_empty_rect(x, y, w, h f32, c gx.Color) {
|
||||
sgl.end()
|
||||
}
|
||||
|
||||
pub fn (ctx &Context) draw_circle_line(x, y f32, r, segments int, c gx.Color) {
|
||||
pub fn (ctx &Context) draw_circle_line(x f32, y f32, r int, segments int, c gx.Color) {
|
||||
if c.a != 255 {
|
||||
sgl.load_pipeline(ctx.timage_pip)
|
||||
}
|
||||
@ -302,8 +308,7 @@ pub fn (ctx &Context) draw_circle_line(x, y f32, r, segments int, c gx.Color) {
|
||||
sgl.end()
|
||||
}
|
||||
|
||||
|
||||
pub fn (ctx &Context) draw_circle(x, y f32, r, segments int, c gx.Color) {
|
||||
pub fn (ctx &Context) draw_circle(x f32, y f32, r int, segments int, c gx.Color) {
|
||||
if c.a != 255 {
|
||||
sgl.load_pipeline(ctx.timage_pip)
|
||||
}
|
||||
@ -322,7 +327,7 @@ pub fn (ctx &Context) draw_circle(x, y f32, r, segments int, c gx.Color) {
|
||||
sgl.end()
|
||||
}
|
||||
|
||||
pub fn (ctx &Context) draw_arc_line(x, y f32, r int, start_angle, arc_angle f32, segments int, c gx.Color) {
|
||||
pub fn (ctx &Context) draw_arc_line(x f32, y f32, r int, start_angle f32, arc_angle f32, segments int, c gx.Color) {
|
||||
if c.a != 255 {
|
||||
sgl.load_pipeline(ctx.timage_pip)
|
||||
}
|
||||
@ -345,7 +350,7 @@ pub fn (ctx &Context) draw_arc_line(x, y f32, r int, start_angle, arc_angle f32,
|
||||
sgl.end()
|
||||
}
|
||||
|
||||
pub fn (ctx &Context) draw_arc(x, y f32, r int, start_angle, arc_angle f32, segments int, c gx.Color) {
|
||||
pub fn (ctx &Context) draw_arc(x f32, y f32, r int, start_angle f32, arc_angle f32, segments int, c gx.Color) {
|
||||
if c.a != 255 {
|
||||
sgl.load_pipeline(ctx.timage_pip)
|
||||
}
|
||||
@ -396,7 +401,7 @@ fn abs(a f32) f32 {
|
||||
return -a
|
||||
}
|
||||
|
||||
pub fn (ctx &Context) draw_line(x, y, x2, y2 f32, c gx.Color) {
|
||||
pub fn (ctx &Context) draw_line(x f32, y f32, x2 f32, y2 f32, c gx.Color) {
|
||||
if c.a != 255 {
|
||||
sgl.load_pipeline(ctx.timage_pip)
|
||||
}
|
||||
@ -419,10 +424,10 @@ pub fn (ctx &Context) draw_line(x, y, x2, y2 f32, c gx.Color) {
|
||||
sgl.end()
|
||||
}
|
||||
|
||||
pub fn (ctx &Context) draw_rounded_rect(x, y, width, height, radius f32, color gx.Color) {
|
||||
pub fn (ctx &Context) draw_rounded_rect(x f32, y f32, width f32, height f32, radius f32, color gx.Color) {
|
||||
}
|
||||
|
||||
pub fn (ctx &Context) draw_empty_rounded_rect(x, y, width, height, radius f32, border_color gx.Color) {
|
||||
pub fn (ctx &Context) draw_empty_rounded_rect(x f32, y f32, width f32, height f32, radius f32, border_color gx.Color) {
|
||||
}
|
||||
|
||||
fn C.WaitMessage()
|
||||
|
@ -2,17 +2,17 @@
|
||||
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
|
||||
module gg
|
||||
|
||||
//import gx
|
||||
// import gx
|
||||
// import sokol.sapp
|
||||
// import sokol.gfx
|
||||
import os
|
||||
import sokol
|
||||
//import sokol.sapp
|
||||
import sokol.sgl
|
||||
//import sokol.gfx
|
||||
import stbi
|
||||
|
||||
pub struct Image {
|
||||
pub mut:
|
||||
id int
|
||||
id int
|
||||
width int
|
||||
height int
|
||||
nr_channels int
|
||||
@ -21,7 +21,7 @@ pub mut:
|
||||
ext string
|
||||
simg_ok bool
|
||||
simg C.sg_image
|
||||
path string
|
||||
path string
|
||||
}
|
||||
|
||||
fn C.sg_isvalid() bool
|
||||
@ -30,8 +30,10 @@ fn C.sg_isvalid() bool
|
||||
pub fn (mut ctx Context) create_image(file string) Image {
|
||||
if !C.sg_isvalid() {
|
||||
// Sokol is not initialized yet, add stbi object to a queue/cache
|
||||
//ctx.image_queue << file
|
||||
stb_img := stbi.load(file) or { return Image{} }
|
||||
// ctx.image_queue << file
|
||||
stb_img := stbi.load(file) or {
|
||||
return Image{}
|
||||
}
|
||||
img := Image{
|
||||
width: stb_img.width
|
||||
height: stb_img.height
|
||||
@ -57,7 +59,9 @@ fn create_image(file string) Image {
|
||||
println('gg.create_image(): file not found: $file')
|
||||
return Image{} // none
|
||||
}
|
||||
stb_img := stbi.load(file) or { return Image{} }
|
||||
stb_img := stbi.load(file) or {
|
||||
return Image{}
|
||||
}
|
||||
mut img := Image{
|
||||
width: stb_img.width
|
||||
height: stb_img.height
|
||||
@ -72,7 +76,9 @@ fn create_image(file string) Image {
|
||||
}
|
||||
|
||||
pub fn create_image_from_memory(buf byteptr, bufsize int) Image {
|
||||
stb_img := stbi.load_from_memory(buf, bufsize) or { return Image{} }
|
||||
stb_img := stbi.load_from_memory(buf, bufsize) or {
|
||||
return Image{}
|
||||
}
|
||||
mut img := Image{
|
||||
width: stb_img.width
|
||||
height: stb_img.height
|
||||
@ -89,7 +95,7 @@ pub fn create_image_from_byte_array(b []byte) Image {
|
||||
}
|
||||
|
||||
pub fn (mut img Image) init_sokol_image() &Image {
|
||||
//println('\n init sokol image $img.path ok=$img.simg_ok')
|
||||
// println('\n init sokol image $img.path ok=$img.simg_ok')
|
||||
mut img_desc := C.sg_image_desc{
|
||||
width: img.width
|
||||
height: img.height
|
||||
@ -109,7 +115,7 @@ pub fn (mut img Image) init_sokol_image() &Image {
|
||||
return img
|
||||
}
|
||||
|
||||
pub fn (ctx &Context) draw_image(x, y, width, height f32, img_ &Image) {
|
||||
pub fn (ctx &Context) draw_image(x f32, y f32, width f32, height f32, img_ &Image) {
|
||||
if img_.id >= ctx.image_cache.len {
|
||||
eprintln('gg: draw_image() bad img id $img_.id (img cache len = $ctx.image_cache.len)')
|
||||
return
|
||||
@ -132,16 +138,16 @@ pub fn (ctx &Context) draw_image(x, y, width, height f32, img_ &Image) {
|
||||
sgl.texture(img.simg)
|
||||
sgl.begin_quads()
|
||||
sgl.c4b(255, 255, 255, 255)
|
||||
sgl.v2f_t2f(x0, y0, u0, v0)
|
||||
sgl.v2f_t2f(x1, y0, u1, v0)
|
||||
sgl.v2f_t2f(x1, y1, u1, v1)
|
||||
sgl.v2f_t2f(x0, y1, u0, v1)
|
||||
sgl.v2f_t2f(x0, y0, u0, v0)
|
||||
sgl.v2f_t2f(x1, y0, u1, v0)
|
||||
sgl.v2f_t2f(x1, y1, u1, v1)
|
||||
sgl.v2f_t2f(x0, y1, u0, v1)
|
||||
sgl.end()
|
||||
sgl.disable_texture()
|
||||
}
|
||||
|
||||
// TODO remove copy pasta, merge the functions
|
||||
pub fn (ctx &Context) draw_image_flipped(x, y, width, height f32, img_ &Image) {
|
||||
pub fn (ctx &Context) draw_image_flipped(x f32, y f32, width f32, height f32, img_ &Image) {
|
||||
if img_.id >= ctx.image_cache.len {
|
||||
eprintln('gg: draw_image() bad img id $img_.id (img cache len = $ctx.image_cache.len)')
|
||||
return
|
||||
@ -164,17 +170,15 @@ pub fn (ctx &Context) draw_image_flipped(x, y, width, height f32, img_ &Image) {
|
||||
sgl.texture(img.simg)
|
||||
sgl.begin_quads()
|
||||
sgl.c4b(255, 255, 255, 255)
|
||||
sgl.v2f_t2f(x0, y0, u1, v0)
|
||||
sgl.v2f_t2f(x1, y0, u0, v0)
|
||||
sgl.v2f_t2f(x1, y1, u0, v1)
|
||||
sgl.v2f_t2f(x0, y1, u1, v1)
|
||||
sgl.v2f_t2f(x0, y0, u1, v0)
|
||||
sgl.v2f_t2f(x1, y0, u0, v0)
|
||||
sgl.v2f_t2f(x1, y1, u0, v1)
|
||||
sgl.v2f_t2f(x0, y1, u1, v1)
|
||||
sgl.end()
|
||||
sgl.disable_texture()
|
||||
}
|
||||
|
||||
pub fn (ctx &Context) draw_image_by_id(x, y, width, height f32, id int) {
|
||||
pub fn (ctx &Context) draw_image_by_id(x f32, y f32, width f32, height f32, id int) {
|
||||
img := ctx.image_cache[id]
|
||||
ctx.draw_image(x,y,width,height,img)
|
||||
ctx.draw_image(x, y, width, height, img)
|
||||
}
|
||||
|
||||
|
||||
|
@ -16,22 +16,21 @@ enum FontVariant {
|
||||
|
||||
struct FT {
|
||||
pub:
|
||||
fons &C.FONScontext
|
||||
|
||||
fons &C.FONScontext
|
||||
font_normal int
|
||||
font_bold int
|
||||
font_mono int
|
||||
font_bold int
|
||||
font_mono int
|
||||
font_italic int
|
||||
scale f32 = 1.0
|
||||
scale f32 = 1.0
|
||||
}
|
||||
|
||||
struct FTConfig {
|
||||
font_path string
|
||||
scale f32 = 1.0
|
||||
scale f32 = 1.0
|
||||
font_size int
|
||||
}
|
||||
|
||||
fn new_ft(c FTConfig) ?&FT{
|
||||
fn new_ft(c FTConfig) ?&FT {
|
||||
if c.font_path == '' {
|
||||
// Load default font
|
||||
}
|
||||
@ -41,7 +40,6 @@ fn new_ft(c FTConfig) ?&FT{
|
||||
return none
|
||||
}
|
||||
}
|
||||
|
||||
mut bytes := []byte{}
|
||||
$if android {
|
||||
bytes = os.read_apk_asset(c.font_path) or {
|
||||
@ -60,40 +58,38 @@ fn new_ft(c FTConfig) ?&FT{
|
||||
bytes
|
||||
}
|
||||
mono_path := get_font_path_variant(c.font_path, .mono)
|
||||
bytes_mono:= os.read_bytes(mono_path) or {
|
||||
bytes_mono := os.read_bytes(mono_path) or {
|
||||
debug_font_println('failed to load font "$mono_path"')
|
||||
bytes
|
||||
}
|
||||
italic_path := get_font_path_variant(c.font_path, .italic)
|
||||
bytes_italic:= os.read_bytes(italic_path) or {
|
||||
bytes_italic := os.read_bytes(italic_path) or {
|
||||
debug_font_println('failed to load font "$italic_path"')
|
||||
bytes
|
||||
}
|
||||
fons := sfons.create(512, 512, 1)
|
||||
return &FT{
|
||||
fons : fons
|
||||
fons: fons
|
||||
font_normal: C.fonsAddFontMem(fons, 'sans', bytes.data, bytes.len, false)
|
||||
font_bold: C.fonsAddFontMem(fons, 'sans', bytes_bold.data, bytes_bold.len, false)
|
||||
font_mono: C.fonsAddFontMem(fons, 'sans', bytes_mono.data, bytes_mono.len, false)
|
||||
font_italic: C.fonsAddFontMem(fons, 'sans', bytes_italic.data, bytes_italic.len, false)
|
||||
font_italic: C.fonsAddFontMem(fons, 'sans', bytes_italic.data, bytes_italic.len,
|
||||
false)
|
||||
scale: c.scale
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn (ctx &Context) set_cfg(cfg gx.TextCfg) {
|
||||
if !ctx.font_inited {
|
||||
return
|
||||
}
|
||||
if cfg.bold {
|
||||
ctx.ft.fons.set_font(ctx.ft.font_bold)
|
||||
}
|
||||
else if cfg.mono {
|
||||
} else if cfg.mono {
|
||||
ctx.ft.fons.set_font(ctx.ft.font_mono)
|
||||
}
|
||||
else if cfg.italic {
|
||||
} else if cfg.italic {
|
||||
ctx.ft.fons.set_font(ctx.ft.font_italic)
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
ctx.ft.fons.set_font(ctx.ft.font_normal)
|
||||
}
|
||||
scale := if ctx.ft.scale == 0 { f32(1) } else { ctx.ft.scale }
|
||||
@ -111,12 +107,12 @@ fn (ctx &Context) set_cfg(cfg gx.TextCfg) {
|
||||
ctx.ft.fons.vert_metrics(&ascender, &descender, &lh)
|
||||
}
|
||||
|
||||
pub fn (ctx &Context) draw_text(x, y int, text_ string, cfg gx.TextCfg) {
|
||||
pub fn (ctx &Context) draw_text(x int, y int, text_ string, cfg gx.TextCfg) {
|
||||
if !ctx.font_inited {
|
||||
eprintln('gg: draw_text(): font not initialized')
|
||||
return
|
||||
}
|
||||
//text := text_.trim_space() // TODO remove/optimize
|
||||
// text := text_.trim_space() // TODO remove/optimize
|
||||
mut text := text_
|
||||
if text.contains('\t') {
|
||||
text = text.replace('\t', ' ')
|
||||
@ -126,7 +122,7 @@ pub fn (ctx &Context) draw_text(x, y int, text_ string, cfg gx.TextCfg) {
|
||||
C.fonsDrawText(ctx.ft.fons, x * scale, y * scale, text.str, 0) // TODO: check offsets/alignment
|
||||
}
|
||||
|
||||
pub fn (ctx &Context) draw_text_def(x, y int, text string) {
|
||||
pub fn (ctx &Context) draw_text_def(x int, y int, text string) {
|
||||
ctx.draw_text(x, y, text, {})
|
||||
}
|
||||
|
||||
@ -134,8 +130,7 @@ pub fn (ctx &Context) draw_text_def(x, y int, text string) {
|
||||
pub fn (mut gg FT) init_font() {
|
||||
}
|
||||
*/
|
||||
|
||||
pub fn (ft &FT) flush(){
|
||||
pub fn (ft &FT) flush() {
|
||||
sfons.flush(ft.fons)
|
||||
}
|
||||
|
||||
@ -165,14 +160,13 @@ pub fn (ctx &Context) text_height(s string) int {
|
||||
pub fn (ctx &Context) text_size(s string) (int, int) {
|
||||
// ctx.set_cfg(cfg) TODO
|
||||
if !ctx.font_inited {
|
||||
return 0,0
|
||||
return 0, 0
|
||||
}
|
||||
mut buf := [4]f32{}
|
||||
C.fonsTextBounds(ctx.ft.fons, 0, 0, s.str, 0, buf)
|
||||
return int((buf[2] - buf[0]) / ctx.scale), int((buf[3] - buf[1]) / ctx.scale)
|
||||
}
|
||||
|
||||
|
||||
pub fn system_font_path() string {
|
||||
env_font := os.getenv('VUI_FONT')
|
||||
if env_font != '' && os.exists(env_font) {
|
||||
@ -182,7 +176,7 @@ pub fn system_font_path() string {
|
||||
return 'C:\\Windows\\Fonts\\arial.ttf'
|
||||
}
|
||||
mut fonts := ['Ubuntu-R.ttf', 'Arial.ttf', 'LiberationSans-Regular.ttf', 'NotoSans-Regular.ttf',
|
||||
'FreeSans.ttf', 'DejaVuSans.ttf']
|
||||
'FreeSans.ttf', 'DejaVuSans.ttf']
|
||||
$if macos {
|
||||
fonts = ['/System/Library/Fonts/SFNS.ttf', '/System/Library/Fonts/SFNSText.ttf', '/Library/Fonts/Arial.ttf']
|
||||
for font in fonts {
|
||||
@ -191,7 +185,9 @@ pub fn system_font_path() string {
|
||||
}
|
||||
}
|
||||
}
|
||||
s := os.exec('fc-list') or { panic('failed to fetch system fonts') }
|
||||
s := os.exec('fc-list') or {
|
||||
panic('failed to fetch system fonts')
|
||||
}
|
||||
system_fonts := s.output.split('\n')
|
||||
for line in system_fonts {
|
||||
for font in fonts {
|
||||
@ -247,7 +243,7 @@ fn get_font_path_variant(font_path string, variant FontVariant) string {
|
||||
}
|
||||
|
||||
fn debug_font_println(s string) {
|
||||
$if debug_font? {
|
||||
$if debug_font ? {
|
||||
println(s)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user