examples: make `v -prod build-examples` pass without warnings/errors

pull/6640/head
Delyan Angelov 2020-10-18 09:48:13 +03:00
parent 67ecc04580
commit 8b2e704741
9 changed files with 594 additions and 512 deletions

View File

@ -57,14 +57,15 @@ Let my heart be still a moment and this mystery explore;—
lines = text.split('\n') lines = text.split('\n')
) )
struct App { struct App {
mut: mut:
gg &gg.Context gg &gg.Context
} }
fn main() { fn main() {
mut app := &App{} mut app := &App{
gg: 0
}
app.gg = gg.new_context({ app.gg = gg.new_context({
width: win_width width: win_width
height: win_height height: win_height
@ -74,8 +75,7 @@ fn main() {
user_data: app user_data: app
bg_color: bg_color bg_color: bg_color
frame_fn: frame frame_fn: frame
font_path: os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf') font_path: os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf') // window_user_ptr: ctx
//window_user_ptr: ctx
}) })
app.gg.run() app.gg.run()
} }
@ -89,4 +89,3 @@ fn frame(mut app App) {
} }
app.gg.end() app.gg.end()
} }

View File

@ -16,8 +16,10 @@ mut:
} }
fn main() { fn main() {
mut app := &App{} mut app := &App{
app.gg = gg.new_context( gg: 0
}
app.gg = gg.new_context({
bg_color: gx.white bg_color: gx.white
width: win_width width: win_width
height: win_height height: win_height
@ -27,7 +29,7 @@ fn main() {
frame_fn: frame frame_fn: frame
user_data: app user_data: app
init_fn: init_images init_fn: init_images
) })
app.image = app.gg.create_image(os.resource_abs_path('logo.png')) app.image = app.gg.create_image(os.resource_abs_path('logo.png'))
app.gg.run() app.gg.run()
} }

View File

@ -1,7 +1,6 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved. // Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module main module main
import os import os
@ -64,7 +63,6 @@ const (
gx.rgb(74, 198, 255), // lightblue longest gx.rgb(74, 198, 255), // lightblue longest
gx.rgb(0, 170, 170), // unused ? gx.rgb(0, 170, 170), // unused ?
] ]
background_color = gx.white background_color = gx.white
ui_color = gx.rgba(255, 0, 0, 210) ui_color = gx.rgba(255, 0, 0, 210)
) )
@ -77,8 +75,11 @@ struct Block {
} }
enum GameState { enum GameState {
paused running gameover paused
running
gameover
} }
struct Game { struct Game {
mut: mut:
// Score of the current game // Score of the current game
@ -118,7 +119,9 @@ struct Game {
second_sw time.StopWatch = time.new_stopwatch({}) second_sw time.StopWatch = time.new_stopwatch({})
} }
const ( fpath = os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf') ) const (
fpath = os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf')
)
[if showfps] [if showfps]
fn (mut game Game) showfps() { fn (mut game Game) showfps() {
@ -143,25 +146,22 @@ fn frame(mut game Game) {
game.gg.end() game.gg.end()
} }
fn main() { fn main() {
mut game := &Game{ mut game := &Game{
gg: 0 gg: 0
} }
game.gg = gg.new_context( game.gg = gg.new_context({
bg_color: gx.white bg_color: gx.white
width: win_width width: win_width
height: win_height height: win_height
use_ortho: true // This is needed for 2D drawing use_ortho: true // This is needed for 2D drawing
create_window: true create_window: true
window_title: 'V Tetris' window_title: 'V Tetris' //
//
user_data: game user_data: game
frame_fn: frame frame_fn: frame
event_fn: on_event event_fn: on_event
font_path: fpath font_path: fpath // wait_events: true
//wait_events: true })
)
game.init_game() game.init_game()
go game.run() // Run the game loop in a new thread go game.run() // Run the game loop in a new thread
game.gg.run() // Run the render loop in the main thread game.gg.run() // Run the render loop in the main thread
@ -348,15 +348,16 @@ fn (g &Game) draw_next_tetro() {
} }
} }
fn (g &Game) draw_block_color(i, j int, color gx.Color) { fn (g &Game) draw_block_color(i int, j int, color gx.Color) {
g.gg.draw_rect(f32((j - 1) * block_size), f32((i - 1) * block_size), g.gg.draw_rect(f32((j - 1) * block_size), f32((i - 1) * block_size), f32(block_size - 1),
f32(block_size - 1), f32(block_size - 1), color) f32(block_size - 1), color)
} }
fn (g &Game) draw_block(i, j, color_idx int) { fn (g &Game) draw_block(i int, j int, color_idx int) {
color := if g.state == .gameover { gx.gray } else { colors[color_idx] } color := if g.state == .gameover { gx.gray } else { colors[color_idx] }
g.draw_block_color(i, j, color) g.draw_block_color(i, j, color)
} }
fn (g &Game) draw_field() { fn (g &Game) draw_field() {
for i := 1; i < field_height + 1; i++ { for i := 1; i < field_height + 1; i++ {
for j := 1; j < field_width + 1; j++ { for j := 1; j < field_width + 1; j++ {
@ -373,13 +374,11 @@ fn (mut g Game) draw_ui() {
lines := g.lines.str() lines := g.lines.str()
g.gg.draw_text(win_width - lines.len * text_size, 3, lines, text_cfg) g.gg.draw_text(win_width - lines.len * text_size, 3, lines, text_cfg)
if g.state == .gameover { if g.state == .gameover {
g.gg.draw_rect(0, win_height / 2 - text_size, win_width, g.gg.draw_rect(0, win_height / 2 - text_size, win_width, 5 * text_size, ui_color)
5 * text_size, ui_color)
g.gg.draw_text(1, win_height / 2 + 0 * text_size, 'Game Over', over_cfg) g.gg.draw_text(1, win_height / 2 + 0 * text_size, 'Game Over', over_cfg)
g.gg.draw_text(1, win_height / 2 + 2 * text_size, 'Space to restart', over_cfg) g.gg.draw_text(1, win_height / 2 + 2 * text_size, 'Space to restart', over_cfg)
} else if g.state == .paused { } else if g.state == .paused {
g.gg.draw_rect(0, win_height / 2 - text_size, win_width, g.gg.draw_rect(0, win_height / 2 - text_size, win_width, 5 * text_size, ui_color)
5 * text_size, ui_color)
g.gg.draw_text(1, win_height / 2 + 0 * text_size, 'Game Paused', text_cfg) g.gg.draw_text(1, win_height / 2 + 0 * text_size, 'Game Paused', text_cfg)
g.gg.draw_text(1, win_height / 2 + 2 * text_size, 'SPACE to resume', text_cfg) g.gg.draw_text(1, win_height / 2 + 2 * text_size, 'SPACE to resume', text_cfg)
} }
@ -444,7 +443,6 @@ fn (mut game Game) key_down(key sapp.KeyCode) {
} }
else {} else {}
} }
if game.state != .running { if game.state != .running {
return return
} }
@ -476,7 +474,8 @@ fn (mut game Game) key_down(key sapp.KeyCode) {
game.move_tetro() // drop faster when the player presses <down> game.move_tetro() // drop faster when the player presses <down>
} }
.d { .d {
for game.move_tetro() {} for game.move_tetro() {
}
} }
.g { .g {
game.show_ghost = !game.show_ghost game.show_ghost = !game.show_ghost

View File

@ -13,13 +13,13 @@ import math
// import time // import time
pub type FNCb = fn (x voidptr) 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 FNFail = fn (msg string, x voidptr)
pub type FNKeyDown = fn (c sapp.KeyCode, m sapp.Modifier, 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) pub type FNChar = fn (c u32, x voidptr)
@ -43,13 +43,18 @@ pub:
cleanup_fn FNCb = voidptr(0) cleanup_fn FNCb = voidptr(0)
fail_fn FNFail = voidptr(0) fail_fn FNFail = voidptr(0)
event_fn FNEvent = voidptr(0) event_fn FNEvent = voidptr(0)
keydown_fn FNKeyDown = voidptr(0) // special case of event_fn keydown_fn FNKeyDown = voidptr(0)
char_fn FNChar = voidptr(0) // special case of event_fn // special case of event_fn
move_fn FNMove = voidptr(0) // special case of event_fn char_fn FNChar = voidptr(0)
click_fn FNMove = voidptr(0) // special case of event_fn // 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 wait_events bool // set this to true for UIs, to save power
fullscreen bool fullscreen bool
scale f32 = 1.0 // vid needs this scale f32 = 1.0
// vid needs this
// init_text bool // init_text bool
font_path string font_path string
} }
@ -61,7 +66,8 @@ mut:
// (so that the user can store image ids, not entire Image objects) // (so that the user can store image ids, not entire Image objects)
image_cache []Image image_cache []Image
pub mut: 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 width int
height int height int
clear_pass C.sg_pass_action clear_pass C.sg_pass_action
@ -237,7 +243,7 @@ pub fn (mut ctx Context) set_bg_color(c gx.Color) {
} }
// TODO: Fix alpha // 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 { if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip) 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() 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 { if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip) 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() 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 { if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip) 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() 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 { if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip) 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() sgl.end()
} }
pub fn (ctx &Context) draw_circle(x f32, y f32, r int, segments int, c gx.Color) {
pub fn (ctx &Context) draw_circle(x, y f32, r, segments int, c gx.Color) {
if c.a != 255 { if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip) 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() 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 { if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip) 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() 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 { if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip) sgl.load_pipeline(ctx.timage_pip)
} }
@ -396,7 +401,7 @@ fn abs(a f32) f32 {
return -a 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 { if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip) 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() 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() fn C.WaitMessage()

View File

@ -3,11 +3,11 @@
module gg module gg
// import gx // import gx
// import sokol.sapp
// import sokol.gfx
import os import os
import sokol import sokol
//import sokol.sapp
import sokol.sgl import sokol.sgl
//import sokol.gfx
import stbi import stbi
pub struct Image { pub struct Image {
@ -31,7 +31,9 @@ pub fn (mut ctx Context) create_image(file string) Image {
if !C.sg_isvalid() { if !C.sg_isvalid() {
// Sokol is not initialized yet, add stbi object to a queue/cache // Sokol is not initialized yet, add stbi object to a queue/cache
// ctx.image_queue << file // ctx.image_queue << file
stb_img := stbi.load(file) or { return Image{} } stb_img := stbi.load(file) or {
return Image{}
}
img := Image{ img := Image{
width: stb_img.width width: stb_img.width
height: stb_img.height height: stb_img.height
@ -57,7 +59,9 @@ fn create_image(file string) Image {
println('gg.create_image(): file not found: $file') println('gg.create_image(): file not found: $file')
return Image{} // none return Image{} // none
} }
stb_img := stbi.load(file) or { return Image{} } stb_img := stbi.load(file) or {
return Image{}
}
mut img := Image{ mut img := Image{
width: stb_img.width width: stb_img.width
height: stb_img.height 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 { 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{ mut img := Image{
width: stb_img.width width: stb_img.width
height: stb_img.height height: stb_img.height
@ -109,7 +115,7 @@ pub fn (mut img Image) init_sokol_image() &Image {
return img 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 { if img_.id >= ctx.image_cache.len {
eprintln('gg: draw_image() bad img id $img_.id (img cache len = $ctx.image_cache.len)') eprintln('gg: draw_image() bad img id $img_.id (img cache len = $ctx.image_cache.len)')
return return
@ -141,7 +147,7 @@ pub fn (ctx &Context) draw_image(x, y, width, height f32, img_ &Image) {
} }
// TODO remove copy pasta, merge the functions // 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 { if img_.id >= ctx.image_cache.len {
eprintln('gg: draw_image() bad img id $img_.id (img cache len = $ctx.image_cache.len)') eprintln('gg: draw_image() bad img id $img_.id (img cache len = $ctx.image_cache.len)')
return return
@ -172,9 +178,7 @@ pub fn (ctx &Context) draw_image_flipped(x, y, width, height f32, img_ &Image) {
sgl.disable_texture() 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] img := ctx.image_cache[id]
ctx.draw_image(x, y, width, height, img) ctx.draw_image(x, y, width, height, img)
} }

View File

@ -17,7 +17,6 @@ enum FontVariant {
struct FT { struct FT {
pub: pub:
fons &C.FONScontext fons &C.FONScontext
font_normal int font_normal int
font_bold int font_bold int
font_mono int font_mono int
@ -41,7 +40,6 @@ fn new_ft(c FTConfig) ?&FT{
return none return none
} }
} }
mut bytes := []byte{} mut bytes := []byte{}
$if android { $if android {
bytes = os.read_apk_asset(c.font_path) or { bytes = os.read_apk_asset(c.font_path) or {
@ -75,25 +73,23 @@ fn new_ft(c FTConfig) ?&FT{
font_normal: C.fonsAddFontMem(fons, 'sans', bytes.data, bytes.len, false) 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_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_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 scale: c.scale
} }
} }
fn (ctx &Context) set_cfg(cfg gx.TextCfg) { fn (ctx &Context) set_cfg(cfg gx.TextCfg) {
if !ctx.font_inited { if !ctx.font_inited {
return return
} }
if cfg.bold { if cfg.bold {
ctx.ft.fons.set_font(ctx.ft.font_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) 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) ctx.ft.fons.set_font(ctx.ft.font_italic)
} } else {
else {
ctx.ft.fons.set_font(ctx.ft.font_normal) ctx.ft.fons.set_font(ctx.ft.font_normal)
} }
scale := if ctx.ft.scale == 0 { f32(1) } else { ctx.ft.scale } scale := if ctx.ft.scale == 0 { f32(1) } else { ctx.ft.scale }
@ -111,7 +107,7 @@ fn (ctx &Context) set_cfg(cfg gx.TextCfg) {
ctx.ft.fons.vert_metrics(&ascender, &descender, &lh) 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 { if !ctx.font_inited {
eprintln('gg: draw_text(): font not initialized') eprintln('gg: draw_text(): font not initialized')
return return
@ -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 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, {}) ctx.draw_text(x, y, text, {})
} }
@ -134,7 +130,6 @@ pub fn (ctx &Context) draw_text_def(x, y int, text string) {
pub fn (mut gg FT) init_font() { pub fn (mut gg FT) init_font() {
} }
*/ */
pub fn (ft &FT) flush() { pub fn (ft &FT) flush() {
sfons.flush(ft.fons) sfons.flush(ft.fons)
} }
@ -172,7 +167,6 @@ pub fn (ctx &Context) text_size(s string) (int, int) {
return int((buf[2] - buf[0]) / ctx.scale), int((buf[3] - buf[1]) / ctx.scale) return int((buf[2] - buf[0]) / ctx.scale), int((buf[3] - buf[1]) / ctx.scale)
} }
pub fn system_font_path() string { pub fn system_font_path() string {
env_font := os.getenv('VUI_FONT') env_font := os.getenv('VUI_FONT')
if env_font != '' && os.exists(env_font) { if env_font != '' && os.exists(env_font) {
@ -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') system_fonts := s.output.split('\n')
for line in system_fonts { for line in system_fonts {
for font in fonts { for font in fonts {

View File

@ -1,29 +1,106 @@
module gx module gx
pub const ( pub const (
blue = Color { r: 0, g: 0, b: 255 } blue = Color{
red = Color { r: 255, g: 0, b: 0 } r: 0
green = Color { r: 0, g: 255, b: 0 } g: 0
yellow = Color { r: 255, g: 255, b: 0 } b: 255
}
orange = Color { r: 255, g: 165, b: 0 } red = Color{
purple = Color { r: 128, g: 0, b: 128 } r: 255
g: 0
black = Color { r: 0, g: 0, b: 0 } b: 0
gray = Color { r: 128, g: 128, b: 128 } }
indigo = Color { r: 75, g: 0, b: 130 } green = Color{
pink = Color { r: 255, g: 192, b: 203 } r: 0
violet = Color { r: 238, g: 130, b: 238 } g: 255
white = Color { r: 255, g: 255, b: 255 } b: 0
}
dark_blue = Color { r: 0, g: 0, b: 139 } yellow = Color{
dark_gray = Color { r: 169, g: 169, b: 169 } r: 255
dark_green = Color { r: 0, g: 100, b: 0 } g: 255
dark_red = Color { r: 139, g: 0, b: 0 } b: 0
light_blue = Color { r: 173, g: 216, b: 230 } }
light_gray = Color { r: 211, g: 211, b: 211 } orange = Color{
light_green = Color { r: 144, g: 238, b: 144 } r: 255
light_red = Color { r: 255, g: 204, b: 203 } g: 165
b: 0
}
purple = Color{
r: 128
g: 0
b: 128
}
black = Color{
r: 0
g: 0
b: 0
}
gray = Color{
r: 128
g: 128
b: 128
}
indigo = Color{
r: 75
g: 0
b: 130
}
pink = Color{
r: 255
g: 192
b: 203
}
violet = Color{
r: 238
g: 130
b: 238
}
white = Color{
r: 255
g: 255
b: 255
}
dark_blue = Color{
r: 0
g: 0
b: 139
}
dark_gray = Color{
r: 169
g: 169
b: 169
}
dark_green = Color{
r: 0
g: 100
b: 0
}
dark_red = Color{
r: 139
g: 0
b: 0
}
light_blue = Color{
r: 173
g: 216
b: 230
}
light_gray = Color{
r: 211
g: 211
b: 211
}
light_green = Color{
r: 144
g: 238
b: 144
}
light_red = Color{
r: 255
g: 204
b: 203
}
) )
// Color represents a 32 bit color value in sRGB format // Color represents a 32 bit color value in sRGB format
@ -38,62 +115,62 @@ pub mut:
// hex takes in a 32 bit integer and splits it into 4 byte values // hex takes in a 32 bit integer and splits it into 4 byte values
pub fn hex(color int) Color { pub fn hex(color int) Color {
return Color{ return Color{
r: byte((color >> 24) & 0xFF), r: byte((color >> 24) & 0xFF)
g: byte((color >> 16) & 0xFF), g: byte((color >> 16) & 0xFF)
b: byte((color >> 8) & 0xFF), b: byte((color >> 8) & 0xFF)
a: byte((color) & 0xFF) a: byte((color) & 0xFF)
} }
} }
pub fn rgb(r, g, b byte) Color { pub fn rgb(r byte, g byte, b byte) Color {
return Color{ return Color{
r: r, r: r
g: g, g: g
b: b b: b
} }
} }
pub fn rgba(r, g, b, a byte) Color { pub fn rgba(r byte, g byte, b byte, a byte) Color {
return Color{ return Color{
r: r, r: r
g: g, g: g
b: b, b: b
a: a a: a
} }
} }
pub fn (c Color) +(c2 Color) Color { pub fn (c Color) +(c2 Color) Color {
return Color{ return Color{
r: c.r + c2.r, r: c.r + c2.r
g: c.g + c2.g, g: c.g + c2.g
b: c.b + c2.b, b: c.b + c2.b
a: c.b + c2.a a: c.b + c2.a
} }
} }
pub fn (c Color) -(c2 Color) Color { pub fn (c Color) -(c2 Color) Color {
return Color{ return Color{
r: c.r - c2.r, r: c.r - c2.r
g: c.g - c2.g, g: c.g - c2.g
b: c.b - c2.b, b: c.b - c2.b
a: c.b - c2.a a: c.b - c2.a
} }
} }
pub fn (c Color) *(c2 Color) Color { pub fn (c Color) *(c2 Color) Color {
return Color{ return Color{
r: c.r * c2.r, r: c.r * c2.r
g: c.g * c2.g, g: c.g * c2.g
b: c.b * c2.b, b: c.b * c2.b
a: c.b * c2.a a: c.b * c2.a
} }
} }
pub fn (c Color) /(c2 Color) Color { pub fn (c Color) /(c2 Color) Color {
return Color{ return Color{
r: c.r / c2.r, r: c.r / c2.r
g: c.g / c2.g, g: c.g / c2.g
b: c.b / c2.b, b: c.b / c2.b
a: c.b / c2.a a: c.b / c2.a
} }
} }

View File

@ -27,7 +27,6 @@ pub struct C.sg_desc {
_end_canary u32 _end_canary u32
} }
pub struct C.sg_context_desc { pub struct C.sg_context_desc {
/* /*
sg_pixel_format color_format; sg_pixel_format color_format;
@ -39,7 +38,6 @@ pub struct C.sg_context_desc {
gl C.sg_gl_context_desc gl C.sg_gl_context_desc
metal C.sg_mtl_context_desc metal C.sg_mtl_context_desc
d3d11 C.sg_d3d11_context_desc d3d11 C.sg_d3d11_context_desc
color_format PixelFormat color_format PixelFormat
depth_format PixelFormat depth_format PixelFormat
} }
@ -61,7 +59,6 @@ pub struct C.sg_d3d11_context_desc {
depth_stencil_view_cb fn () voidptr depth_stencil_view_cb fn () voidptr
} }
pub struct C.sg_pipeline_desc { pub struct C.sg_pipeline_desc {
pub mut: pub mut:
_start_canary u32 _start_canary u32
@ -77,15 +74,16 @@ pub mut:
} }
pub struct C.sg_pipeline_info { pub struct C.sg_pipeline_info {
} }
pub struct C.sg_pipeline { pub struct C.sg_pipeline {
pub: pub:
id u32 id u32
} }
pub fn (p C.sg_pipeline) free() { C.sg_destroy_pipeline(p) }
pub fn (p C.sg_pipeline) free() {
C.sg_destroy_pipeline(p)
}
pub struct C.sg_bindings { pub struct C.sg_bindings {
pub mut: pub mut:
@ -123,7 +121,6 @@ pub fn (b &C.sg_bindings) append_index_buffer(data voidptr, element_size int, el
return C.sg_append_buffer(b.index_buffer, data, element_size * element_count) return C.sg_append_buffer(b.index_buffer, data, element_size * element_count)
} }
pub struct C.sg_shader_desc { pub struct C.sg_shader_desc {
pub mut: pub mut:
_start_canary u32 _start_canary u32
@ -156,12 +153,12 @@ pub fn (mut desc C.sg_shader_desc) set_frag_image(index int, name string) &C.sg_
return desc return desc
} }
pub fn (mut desc C.sg_shader_desc) set_vert_uniform_block_size(block_index, size int) &C.sg_shader_desc { pub fn (mut desc C.sg_shader_desc) set_vert_uniform_block_size(block_index int, size int) &C.sg_shader_desc {
desc.vs.uniform_blocks[block_index].size = size desc.vs.uniform_blocks[block_index].size = size
return desc return desc
} }
pub fn (mut desc C.sg_shader_desc) set_frag_uniform_block_size(block_index, size int) &C.sg_shader_desc { pub fn (mut desc C.sg_shader_desc) set_frag_uniform_block_size(block_index int, size int) &C.sg_shader_desc {
desc.fs.uniform_blocks[block_index].size = size desc.fs.uniform_blocks[block_index].size = size
return desc return desc
} }
@ -182,12 +179,11 @@ pub fn (desc &C.sg_shader_desc) make_shader() C.sg_shader {
return C.sg_make_shader(desc) return C.sg_make_shader(desc)
} }
pub struct C.sg_shader_attr_desc { pub struct C.sg_shader_attr_desc {
pub mut: pub mut:
name byteptr /* GLSL vertex attribute name (only required for GLES2) */ name byteptr // GLSL vertex attribute name (only required for GLES2)
sem_name byteptr /* HLSL semantic name */ sem_name byteptr // HLSL semantic name
sem_index int /* HLSL semantic index */ sem_index int // HLSL semantic index
} }
pub struct C.sg_shader_stage_desc { pub struct C.sg_shader_stage_desc {
@ -206,7 +202,6 @@ pub fn (mut desc C.sg_shader_stage_desc) set_image(index int, name string) C.sg_
return *desc return *desc
} }
pub struct C.sg_shader_uniform_block_desc { pub struct C.sg_shader_uniform_block_desc {
pub mut: pub mut:
size int size int
@ -226,7 +221,8 @@ pub mut:
@type ImageType @type ImageType
} }
pub struct C.sg_shader_info {} pub struct C.sg_shader_info {
}
pub struct C.sg_context { pub struct C.sg_context {
id u32 id u32
@ -236,8 +232,10 @@ pub struct C.sg_shader {
pub: pub:
id u32 id u32
} }
pub fn (s C.sg_shader) free() { C.sg_destroy_shader(s) }
pub fn (s C.sg_shader) free() {
C.sg_destroy_shader(s)
}
pub struct C.sg_pass_desc { pub struct C.sg_pass_desc {
pub mut: pub mut:
@ -264,8 +262,10 @@ pub mut:
pub struct C.sg_pass { pub struct C.sg_pass {
id u32 id u32
} }
pub fn (p C.sg_pass) free() { C.sg_destroy_pass(p) }
pub fn (p C.sg_pass) free() {
C.sg_destroy_pass(p)
}
pub struct C.sg_buffer_desc { pub struct C.sg_buffer_desc {
pub mut: pub mut:
@ -275,24 +275,27 @@ pub mut:
usage Usage usage Usage
content byteptr content byteptr
label byteptr label byteptr
/* GL specific */ // GL specific
gl_buffers [2]u32 gl_buffers [2]u32
/* Metal specific */ // Metal specific
mtl_buffers [2]voidptr mtl_buffers [2]voidptr
/* D3D11 specific */ // D3D11 specific
d3d11_buffer voidptr d3d11_buffer voidptr
_end_canary u32 _end_canary u32
} }
pub struct C.sg_buffer_info {} pub struct C.sg_buffer_info {
}
pub struct C.sg_buffer { pub struct C.sg_buffer {
id u32 id u32
} }
pub fn (b C.sg_buffer) free() { C.sg_destroy_buffer(b) }
pub fn (b C.sg_buffer) free() {
C.sg_destroy_buffer(b)
}
pub union DepthLayers { pub struct DepthLayers {
depth int depth int
layers int layers int
} }
@ -304,7 +307,6 @@ pub mut:
render_target bool render_target bool
width int width int
height int height int
depth DepthLayers depth DepthLayers
// depth int // depth int
// union { // union {
@ -326,29 +328,31 @@ pub mut:
max_lod f32 max_lod f32
content C.sg_image_content content C.sg_image_content
label byteptr label byteptr
/* GL specific */ // GL specific
gl_textures [2]u32 gl_textures [2]u32
/* Metal specific */ // Metal specific
mtl_textures [2]voidptr mtl_textures [2]voidptr
/* D3D11 specific */ // D3D11 specific
d3d11_texture voidptr d3d11_texture voidptr
_end_canary u32 _end_canary u32
} }
pub struct C.sg_image_info { pub struct C.sg_image_info {
pub mut: pub mut:
slot C.sg_slot_info /* resource pool slot info */ slot C.sg_slot_info // resource pool slot info
upd_frame_index u32 /* frame index of last sg_update_image() */ upd_frame_index u32 // frame index of last sg_update_image()
num_slots int /* number of renaming-slots for dynamically updated images */ num_slots int // number of renaming-slots for dynamically updated images
active_slot int /* currently active write-slot for dynamically updated images */ active_slot int // currently active write-slot for dynamically updated images
} }
pub struct C.sg_image { pub struct C.sg_image {
pub: pub:
id u32 id u32
} }
pub fn (i C.sg_image) free() { C.sg_destroy_image(i) }
pub fn (i C.sg_image) free() {
C.sg_destroy_image(i)
}
pub struct C.sg_image_content { pub struct C.sg_image_content {
pub mut: pub mut:
@ -357,29 +361,29 @@ pub mut:
pub struct C.sg_subimage_content { pub struct C.sg_subimage_content {
pub mut: pub mut:
ptr voidptr /* pointer to subimage data */ ptr voidptr // pointer to subimage data
size int /* size in bytes of pointed-to subimage data */ size int // size in bytes of pointed-to subimage data
} }
pub struct C.sg_features { pub struct C.sg_features {
pub: pub:
instancing bool /* hardware instancing supported */ instancing bool // hardware instancing supported
origin_top_left bool /* framebuffer and texture origin is in top left corner */ origin_top_left bool // framebuffer and texture origin is in top left corner
multiple_render_targets bool /* offscreen render passes can have multiple render targets attached */ multiple_render_targets bool // offscreen render passes can have multiple render targets attached
msaa_render_targets bool /* offscreen render passes support MSAA antialiasing */ msaa_render_targets bool // offscreen render passes support MSAA antialiasing
imagetype_3d bool /* creation of SG_IMAGETYPE_3D images is supported */ imagetype_3d bool // creation of SG_IMAGETYPE_3D images is supported
imagetype_array bool /* creation of SG_IMAGETYPE_ARRAY images is supported */ imagetype_array bool // creation of SG_IMAGETYPE_ARRAY images is supported
image_clamp_to_border bool /* border color and clamp-to-border UV-wrap mode is supported */ image_clamp_to_border bool // border color and clamp-to-border UV-wrap mode is supported
} }
pub struct C.sg_limits { pub struct C.sg_limits {
pub: pub:
max_image_size_2d u32 /* max width/height of SG_IMAGETYPE_2D images */ max_image_size_2d u32 // max width/height of SG_IMAGETYPE_2D images
max_image_size_cube u32 /* max width/height of SG_IMAGETYPE_CUBE images */ max_image_size_cube u32 // max width/height of SG_IMAGETYPE_CUBE images
max_image_size_3d u32 /* max width/height/depth of SG_IMAGETYPE_3D images */ max_image_size_3d u32 // max width/height/depth of SG_IMAGETYPE_3D images
max_image_size_array u32 /* max width/height pf SG_IMAGETYPE_ARRAY images */ max_image_size_array u32 // max width/height pf SG_IMAGETYPE_ARRAY images
max_image_array_layers u32 /* max number of layers in SG_IMAGETYPE_ARRAY images */ max_image_array_layers u32 // max number of layers in SG_IMAGETYPE_ARRAY images
max_vertex_attrs u32 /* <= SG_MAX_VERTEX_ATTRIBUTES (only on some GLES2 impls) */ max_vertex_attrs u32 // <= SG_MAX_VERTEX_ATTRIBUTES (only on some GLES2 impls)
} }
pub struct C.sg_layout_desc { pub struct C.sg_layout_desc {
@ -447,7 +451,6 @@ pub mut:
depth_bias_clamp f32 depth_bias_clamp f32
} }
pub struct C.sg_color_attachment_action { pub struct C.sg_color_attachment_action {
pub mut: pub mut:
action Action action Action
@ -462,7 +465,6 @@ pub fn (mut action C.sg_color_attachment_action) set_color_values(r, g, b, a f32
action.val[3] = a action.val[3] = a
} }
*/ */
pub struct C.sg_depth_attachment_action { pub struct C.sg_depth_attachment_action {
pub mut: pub mut:
action Action action Action
@ -477,12 +479,12 @@ pub mut:
pub struct C.sg_pixelformat_info { pub struct C.sg_pixelformat_info {
pub: pub:
sample bool /* pixel format can be sampled in shaders */ sample bool // pixel format can be sampled in shaders
filter bool /* pixel format can be sampled with filtering */ filter bool // pixel format can be sampled with filtering
render bool /* pixel format can be used as render target */ render bool // pixel format can be used as render target
blend bool /* alpha-blending is supported */ blend bool // alpha-blending is supported
msaa bool /* pixel format can be used as MSAA render target */ msaa bool // pixel format can be used as MSAA render target
depth bool /* pixel format is a depth format */ depth bool // pixel format is a depth format
} }
pub struct C.sg_attachment_desc { pub struct C.sg_attachment_desc {
@ -490,7 +492,6 @@ pub mut:
image C.sg_image image C.sg_image
mip_level int mip_level int
face int face int
// image sg_image // image sg_image
// mip_level int // mip_level int
// union { // union {

View File

@ -1,6 +1,6 @@
module gfx module gfx
pub fn create_clear_pass(r, g, b, a f32) C.sg_pass_action { pub fn create_clear_pass(r f32, g f32, b f32, a f32) C.sg_pass_action {
mut color_action := C.sg_color_attachment_action{ mut color_action := C.sg_color_attachment_action{
action: C.SG_ACTION_CLEAR action: C.SG_ACTION_CLEAR
} }
@ -9,7 +9,6 @@ color_action.val[0] = r
color_action.val[1] = g color_action.val[1] = g
color_action.val[2] = b color_action.val[2] = b
color_action.val[3] = a color_action.val[3] = a
mut pass_action := C.sg_pass_action{} mut pass_action := C.sg_pass_action{}
pass_action.colors[0] = color_action pass_action.colors[0] = color_action
return pass_action return pass_action