1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

sokol: fix building of examples and ci tests

This commit is contained in:
Delyan Angelov 2020-01-17 21:05:45 +02:00 committed by Alexander Medvednikov
parent 3d57d3bb7b
commit 24d34a4f2c
12 changed files with 181 additions and 183 deletions

3
examples/sokol_examples/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
fonts
fonts2
drawing

Binary file not shown.

Binary file not shown.

View File

@ -2,22 +2,26 @@ import sokol
import sokol.sapp
import sokol.gfx
import sokol.sgl
import fontstash
struct AppState {
pass_action sg_pass_action
}
const (
used_import = sokol.used_import
)
fn main() {
state := &AppState{
pass_action: gfx.create_clear_pass(0.1, 0.1, 0.1, 1.0)
}
title := 'Sokol Drawing Template'
desc := sapp_desc{
user_data: state
init_userdata_cb: init
frame_userdata_cb: frame
window_title: 'Sokal Drawing Template'.str
window_title: title.str
html5_canvas_name: title.str
}
sapp.run(&desc)
}
@ -33,7 +37,6 @@ fn init(user_data voidptr) {
d3d11_depth_stencil_view_cb: sapp_d3d11_get_depth_stencil_view
}
gfx.setup(&desc)
sgl_desc := sgl_desc_t{}
sgl.setup(&sgl_desc)
}
@ -41,9 +44,7 @@ fn init(user_data voidptr) {
fn frame(user_data voidptr) {
// println('frame')
state := &AppState(user_data)
draw()
gfx.begin_default_pass(&state.pass_action, sapp.width(), sapp.height())
sgl.draw()
gfx.end_pass()
@ -55,10 +56,8 @@ fn draw() {
sgl.defaults()
sgl.matrix_mode_projection()
sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
sgl.c4b(255, 0, 0, 128)
draw_hollow_rect(10, 10, 100, 30)
sgl.c4b(25, 150, 0, 128)
draw_filled_rect(10, 150, 80, 40)
// line(0, 0, 500, 500)

View File

@ -3,7 +3,6 @@ import sokol.sapp
import sokol.gfx
import sokol.sgl
import sokol.sfons
import fontstash
import os
import time
@ -22,27 +21,25 @@ fn main() {
color_action.val[1] = 0.3
color_action.val[2] = 0.32
color_action.val[3] = 1.0
mut pass_action := sg_pass_action{}
pass_action.colors[0] = color_action
state := &AppState{
pass_action: pass_action
fons: &C.FONScontext(0)
}
title := 'V Metal/GL Text Rendering'
desc := sapp_desc{
user_data: state
init_userdata_cb: init
frame_userdata_cb: frame
window_title: 'V Metal/GL Text Rendering'.str
window_title: title.str
html5_canvas_name: title.str
}
sapp.run(&desc)
}
fn init(user_data voidptr) {
mut state := &AppState(user_data)
// dont actually alocate this on the heap in real life
gfx.setup(&sg_desc{
mtl_device: C.sapp_metal_get_device()
@ -53,25 +50,20 @@ fn init(user_data voidptr) {
d3d11_render_target_view_cb: sapp_d3d11_get_render_target_view
d3d11_depth_stencil_view_cb: sapp_d3d11_get_depth_stencil_view
})
s := &C.sgl_desc_t{}
C.sgl_setup(s)
state.fons = C.sfons_create(512, 512, 1)
// or use DroidSerif-Regular.ttf
if bytes := os.read_bytes('assets/ProggyTiny.ttf') {
if bytes := os.read_bytes(os.resource_abs_path('assets/ProggyTiny.ttf')) {
println('loaded font: $bytes.len')
state.font_normal = C.fonsAddFontMem(state.fons, "sans", bytes.data, bytes.len, false)
state.font_normal = C.fonsAddFontMem(state.fons, 'sans', bytes.data, bytes.len, false)
}
}
fn frame(user_data voidptr) {
t := time.ticks()
mut state := &AppState(user_data)
state.render_font()
gfx.begin_default_pass(&state.pass_action, sapp_width(), sapp_height())
sgl.draw()
gfx.end_pass()
@ -90,16 +82,13 @@ fn (state &AppState) render_font() {
brown := C.sfons_rgba(192, 128, 0, 128)
blue := C.sfons_rgba(0, 192, 255, 255)
state.fons.clear_state()
sgl.defaults()
sgl.matrix_mode_projection()
sgl.ortho(0.0, f32(C.sapp_width()), f32(C.sapp_height()), 0.0, -1.0, 1.0)
sx = 0
sy = 50
dx = sx
dy = sy
state.fons.set_font(state.font_normal)
state.fons.set_size(100.0)
ascender := 0.0
@ -109,24 +98,20 @@ fn (state &AppState) render_font() {
dy += lh
C.fonsSetColor(state.fons, white)
dx = C.fonsDrawText(state.fons, dx, dy, c'The quick ', C.NULL)
fonsSetFont(state.fons, state.font_normal)
fonsSetSize(state.fons, 48.0)
fonsSetColor(state.fons, brown)
dx = fonsDrawText(state.fons, dx, dy, c"brown ", C.NULL)
dx = fonsDrawText(state.fons, dx, dy, c'brown ', C.NULL)
fonsSetFont(state.fons, state.font_normal)
fonsSetSize(state.fons, 24.0)
fonsSetColor(state.fons, white)
dx = fonsDrawText(state.fons, dx, dy, c"fox ", C.NULL)
dx = fonsDrawText(state.fons, dx, dy, c'fox ', C.NULL)
dx = sx
dy += lh * 1.2
fonsSetSize(state.fons, 20.0)
fonsSetFont(state.fons, state.font_normal)
fonsSetColor(state.fons, blue)
fonsDrawText(state.fons, dx, dy, c"Now is the time for all good men to come to the aid of the party.", C.NULL)
fonsDrawText(state.fons, dx, dy, c'Now is the time for all good men to come to the aid of the party.', C.NULL)
dx = 300
dy = 350
fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE)
@ -135,8 +120,7 @@ fn (state &AppState) render_font() {
fonsSetColor(state.fons, white)
fonsSetSpacing(state.fons, 5.0)
fonsSetBlur(state.fons, 6.0)
fonsDrawText(state.fons, dx, dy, c"Blurry...", C.NULL)
fonsDrawText(state.fons, dx, dy, c'Blurry...', C.NULL)
dx = 300
dy += 50.0
fonsSetSize(state.fons, 28.0)
@ -144,11 +128,10 @@ fn (state &AppState) render_font() {
fonsSetColor(state.fons, white)
fonsSetSpacing(state.fons, 0.0)
fonsSetBlur(state.fons, 3.0)
fonsDrawText(state.fons, dx,dy + 2, c"DROP SHADOW", C.NULL)
fonsDrawText(state.fons, dx, dy + 2, c'DROP SHADOW', C.NULL)
fonsSetColor(state.fons, black)
fonsSetBlur(state.fons, 0)
fonsDrawText(state.fons, dx,dy, c"DROP SHADOW", C.NULL)
fonsDrawText(state.fons, dx, dy, c'DROP SHADOW', C.NULL)
fonsSetSize(state.fons, 18.0)
fonsSetFont(state.fons, state.font_normal)
fonsSetColor(state.fons, white)
@ -156,28 +139,27 @@ fn (state &AppState) render_font() {
dy = 350
line(dx - 10, dy, dx + 250, dy)
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_TOP)
dx = C.fonsDrawText(state.fons, dx,dy, c"Top",C.NULL)
dx = C.fonsDrawText(state.fons, dx, dy, c'Top', C.NULL)
dx += 10
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_MIDDLE)
dx = C.fonsDrawText(state.fons, dx,dy, c"Middle",C.NULL)
dx = C.fonsDrawText(state.fons, dx, dy, c'Middle', C.NULL)
dx += 10
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE)
dx = C.fonsDrawText(state.fons, dx,dy, c"Baseline",C.NULL)
dx = C.fonsDrawText(state.fons, dx, dy, c'Baseline', C.NULL)
dx += 10
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BOTTOM)
C.fonsDrawText(state.fons, dx,dy, c"Bottom",C.NULL)
C.fonsDrawText(state.fons, dx, dy, c'Bottom', C.NULL)
dx = 150
dy = 400
line(dx, dy - 30, dx, dy + 80.0)
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE)
C.fonsDrawText(state.fons, dx,dy, c"Left",C.NULL)
C.fonsDrawText(state.fons, dx, dy, c'Left', C.NULL)
dy += 30
C.fonsSetAlign(state.fons, C.FONS_ALIGN_CENTER | C.FONS_ALIGN_BASELINE)
C.fonsDrawText(state.fons, dx,dy, c"Center",C.NULL)
C.fonsDrawText(state.fons, dx, dy, c'Center', C.NULL)
dy += 30
C.fonsSetAlign(state.fons, C.FONS_ALIGN_RIGHT | C.FONS_ALIGN_BASELINE)
C.fonsDrawText(state.fons, dx,dy, c"Right",C.NULL)
C.fonsDrawText(state.fons, dx, dy, c'Right', C.NULL)
C.sfons_flush(state.fons)
}

View File

@ -3,13 +3,9 @@ import sokol.sapp
import sokol.gfx
import sokol.sgl
import sokol.sfons
import fontstash
import os
import time
import filepath
// import time
#define FONS_USE_FREETYPE 1
struct AppState {
mut:
pass_action C.sg_pass_action
@ -25,27 +21,25 @@ fn main() {
color_action.val[1] = 0.3
color_action.val[2] = 0.32
color_action.val[3] = 1.0
mut pass_action := sg_pass_action{}
pass_action.colors[0] = color_action
state := &AppState{
pass_action: pass_action
fons: &C.FONScontext(0)
}
title := 'V Metal/GL Text Rendering'
desc := sapp_desc{
user_data: state
init_userdata_cb: init
frame_userdata_cb: frame
window_title: 'V Metal/GL Text Rendering'.str
window_title: title.str
html5_canvas_name: title.str
}
sapp.run(&desc)
}
fn init(user_data voidptr) {
mut state := &AppState(user_data)
// dont actually alocate this on the heap in real life
gfx.setup(&sg_desc{
mtl_device: C.sapp_metal_get_device()
@ -56,39 +50,28 @@ fn init(user_data voidptr) {
d3d11_render_target_view_cb: sapp_d3d11_get_render_target_view
d3d11_depth_stencil_view_cb: sapp_d3d11_get_depth_stencil_view
})
s := &C.sgl_desc_t{}
C.sgl_setup(s)
state.fons = C.sfons_create(512, 512, 1)
//mut font_path := cfg.font_path
//if font_path == '' {
mut font_path := 'RobotoMono-Regular.ttf'
//}
mut font_path := os.resource_abs_path('assets/DroidSerif-Regular.ttf')
if !os.exists(font_path) {
exe_path := os.executable()
exe_dir := filepath.basedir(exe_path)
font_path = filepath.basedir(exe_dir) + '/tetris/$font_path'
font_path = os.resource_abs_path('../tetris/RobotoMono-Regular.ttf')
println(font_path)
}
if !os.exists(font_path) {
println('failed to load $font_path')
return
}
if bytes := os.read_bytes(font_path) {
println('loaded font: $bytes.len')
state.font_normal = C.fonsAddFontMem(state.fons, "sans", bytes.data, bytes.len, false)
state.font_normal = C.fonsAddFontMem(state.fons, 'sans', bytes.data, bytes.len, false)
}
}
fn frame(user_data voidptr) {
t:=time.ticks()
// t:=time.ticks()
mut state := &AppState(user_data)
state.render_font()
gfx.begin_default_pass(&state.pass_action, sapp_width(), sapp_height())
sgl.draw()
gfx.end_pass()
@ -96,7 +79,7 @@ fn frame(user_data voidptr) {
// println(time.ticks()-t)
}
fn (state &AppState) render_font() {
fn (state mut AppState) render_font() {
mut sx := 0.0
mut sy := 0.0
mut dx := 0.0
@ -107,16 +90,13 @@ fn (state &AppState) render_font() {
brown := C.sfons_rgba(192, 128, 0, 128)
blue := C.sfons_rgba(0, 192, 255, 255)
state.fons.clear_state()
sgl.defaults()
sgl.matrix_mode_projection()
sgl.ortho(0.0, f32(C.sapp_width()), f32(C.sapp_height()), 0.0, -1.0, 1.0)
sx = 0
sy = 50
dx = sx
dy = sy
state.fons.set_font(state.font_normal)
state.fons.set_size(100.0)
ascender := 0.0
@ -126,24 +106,20 @@ fn (state &AppState) render_font() {
dy += lh
C.fonsSetColor(state.fons, white)
dx = C.fonsDrawText(state.fons, dx, dy, c'The quick ', C.NULL)
fonsSetFont(state.fons, state.font_normal)
fonsSetSize(state.fons, 48.0)
fonsSetColor(state.fons, brown)
dx = fonsDrawText(state.fons, dx, dy, c"brown ", C.NULL)
dx = fonsDrawText(state.fons, dx, dy, c'brown ', C.NULL)
fonsSetFont(state.fons, state.font_normal)
fonsSetSize(state.fons, 24.0)
fonsSetColor(state.fons, white)
dx = fonsDrawText(state.fons, dx, dy, c"fox ", C.NULL)
dx = fonsDrawText(state.fons, dx, dy, c'fox ', C.NULL)
dx = sx
dy += lh * 1.2
fonsSetSize(state.fons, 20.0)
fonsSetFont(state.fons, state.font_normal)
fonsSetColor(state.fons, blue)
fonsDrawText(state.fons, dx, dy, c"Now is the time for all good men to come to the aid of the party.", C.NULL)
fonsDrawText(state.fons, dx, dy, c'Now is the time for all good men to come to the aid of the party.', C.NULL)
dx = 300
dy = 350
fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE)
@ -152,8 +128,7 @@ fn (state &AppState) render_font() {
fonsSetColor(state.fons, white)
fonsSetSpacing(state.fons, 5.0)
fonsSetBlur(state.fons, 6.0)
fonsDrawText(state.fons, dx, dy, c"Blurry...", C.NULL)
fonsDrawText(state.fons, dx, dy, c'Blurry...', C.NULL)
dx = 300
dy += 50.0
fonsSetSize(state.fons, 28.0)
@ -161,11 +136,10 @@ fn (state &AppState) render_font() {
fonsSetColor(state.fons, white)
fonsSetSpacing(state.fons, 0.0)
fonsSetBlur(state.fons, 3.0)
fonsDrawText(state.fons, dx,dy + 2, c"DROP SHADOW", C.NULL)
fonsDrawText(state.fons, dx, dy + 2, c'DROP SHADOW', C.NULL)
fonsSetColor(state.fons, black)
fonsSetBlur(state.fons, 0)
fonsDrawText(state.fons, dx,dy, c"DROP SHADOW", C.NULL)
fonsDrawText(state.fons, dx, dy, c'DROP SHADOW', C.NULL)
fonsSetSize(state.fons, 18.0)
fonsSetFont(state.fons, state.font_normal)
fonsSetColor(state.fons, white)
@ -173,28 +147,27 @@ fn (state &AppState) render_font() {
dy = 350
line(dx - 10, dy, dx + 250, dy)
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_TOP)
dx = C.fonsDrawText(state.fons, dx,dy, c"Top",C.NULL)
dx = C.fonsDrawText(state.fons, dx, dy, c'Top', C.NULL)
dx += 10
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_MIDDLE)
dx = C.fonsDrawText(state.fons, dx,dy, c"Middle",C.NULL)
dx = C.fonsDrawText(state.fons, dx, dy, c'Middle', C.NULL)
dx += 10
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE)
dx = C.fonsDrawText(state.fons, dx,dy, c"Baseline",C.NULL)
dx = C.fonsDrawText(state.fons, dx, dy, c'Baseline', C.NULL)
dx += 10
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BOTTOM)
C.fonsDrawText(state.fons, dx,dy, c"Bottom",C.NULL)
C.fonsDrawText(state.fons, dx, dy, c'Bottom', C.NULL)
dx = 150
dy = 400
line(dx, dy - 30, dx, dy + 80.0)
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE)
C.fonsDrawText(state.fons, dx,dy, c"Left",C.NULL)
C.fonsDrawText(state.fons, dx, dy, c'Left', C.NULL)
dy += 30
C.fonsSetAlign(state.fons, C.FONS_ALIGN_CENTER | C.FONS_ALIGN_BASELINE)
C.fonsDrawText(state.fons, dx,dy, c"Center",C.NULL)
C.fonsDrawText(state.fons, dx, dy, c'Center', C.NULL)
dy += 30
C.fonsSetAlign(state.fons, C.FONS_ALIGN_RIGHT | C.FONS_ALIGN_BASELINE)
C.fonsDrawText(state.fons, dx,dy, c"Right",C.NULL)
C.fonsDrawText(state.fons, dx, dy, c'Right', C.NULL)
C.sfons_flush(state.fons)
}

View File

@ -2,6 +2,13 @@ module fontstash
#flag -I @VROOT/thirdparty/fontstash
#define FONTSTASH_IMPLEMENTATION
#include "fontstash.h"
pub const (
// TODO: fontstash.used_import is used to keep v from warning about unused imports
used_import = 1
)
// Contructor and destructor.
[inline]

33
vlib/sokol/c/c.v Normal file
View File

@ -0,0 +1,33 @@
module c
pub const (
used_import = 1
)
#flag -I @VROOT/thirdparty/sokol
#flag -I @VROOT/thirdparty/sokol/util
#flag darwin -fobjc-arc
#flag linux -lX11 -lGL
// METAL
// #flag -DSOKOL_METAL
// #flag darwin -framework Metal -framework Cocoa -framework MetalKit -framework QuartzCore
// OPENGL
#flag -DSOKOL_GLCORE33
#flag darwin -framework OpenGL -framework Cocoa -framework QuartzCore
// for simplicity, all header includes are here because import order matters and we dont have any way
// to ensure import order with V yet
#define SOKOL_IMPL
#define SOKOL_NO_ENTRY
#include "sokol_app.h"
#define SOKOL_IMPL
#define SOKOL_NO_DEPRECATED
#include "sokol_gfx.h"
#define SOKOL_GL_IMPL
#include "util/sokol_gl.h"

28
vlib/sokol/f/f.v Normal file
View File

@ -0,0 +1,28 @@
module f
import fontstash
pub const (
used_import = 1 + fontstash.used_import
)
#flag windows -I @VROOT/thirdparty/freetype/include
#flag windows -L @VROOT/thirdparty/freetype/win64
#flag linux -I/usr/include/freetype2
#flag darwin -I/usr/local/include/freetype2
#flag darwin -I/opt/local/include/freetype2
#flag darwin -I/usr/local/Cellar/freetype/2.10.0/include/freetype2/
#flag freebsd -I/usr/local/include/freetype2
#flag freebsd -Wl -L/usr/local/lib
#flag -lfreetype
#flag darwin -lpng -lbz2 -lz
#flag linux -I.
#include "ft2build.h"
#define FONS_USE_FREETYPE
#define SOKOL_FONTSTASH_IMPL
#include "util/sokol_fontstash.h"

View File

@ -1,7 +1,11 @@
module sfons
import fontstash
#flag -I fontstash/thirdparty
const (
// keep v from warning about unused imports
used_import = fontstash.used_import + 1
)
[inline]
pub fn sfons_create(width int, height int, flags int) &C.FONScontext {

View File

@ -1,39 +1,8 @@
module sokol
#flag -I @VROOT/thirdparty/sokol
#flag -I @VROOT/thirdparty/sokol/util
import sokol.c
import sokol.f
#flag darwin -fobjc-arc
#flag linux -lX11 -lGL
#flag darwin -I/usr/local/Cellar/freetype/2.10.0/include/freetype2/
#flag -lfreetype
// METAL
// #flag -DSOKOL_METAL
// #flag darwin -framework Metal -framework Cocoa -framework MetalKit -framework QuartzCore
// OPENGL
#flag -DSOKOL_GLCORE33
#flag darwin -framework OpenGL -framework Cocoa -framework QuartzCore
// for simplicity, all header includes are here because import order matters and we dont have any way
// to ensure import order with V yet
#define SOKOL_IMPL
#define SOKOL_NO_ENTRY
#include "sokol_app.h"
#define SOKOL_IMPL
#define SOKOL_NO_DEPRECATED
#include "sokol_gfx.h"
#define SOKOL_GL_IMPL
#include "util/sokol_gl.h"
#define FONS_USE_FREETYPE
#define FONTSTASH_IMPLEMENTATION
#include "fontstash.h"
#define SOKOL_FONTSTASH_IMPL
#include "util/sokol_fontstash.h"
pub const (
used_import = c.used_import + f.used_import
)