v/examples/gg/additive.v
Eliyaan (Nopana) bbb61ab368
Some checks failed
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
native backend CI / native-backend-ubuntu (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
native backend CI / native-backend-windows (push) Waiting to run
Shy and PV CI / v-compiles-puzzle-vibes (push) Waiting to run
Sanitized CI / sanitize-undefined-clang (push) Waiting to run
Sanitized CI / sanitize-undefined-gcc (push) Waiting to run
Sanitized CI / tests-sanitize-address-clang (push) Waiting to run
Sanitized CI / sanitize-address-msvc (push) Waiting to run
Sanitized CI / sanitize-address-gcc (push) Waiting to run
Sanitized CI / sanitize-memory-clang (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-macos (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
wasm backend CI / wasm-backend (ubuntu-22.04) (push) Waiting to run
wasm backend CI / wasm-backend (windows-2022) (push) Waiting to run
Workflow Lint / lint-yml-workflows (push) Has been cancelled
gg, gx: deprecate gx and replace all occurences with gg (which now contains all the functionality of gx) (#24966)
2025-08-14 19:53:56 +03:00

124 lines
2.7 KiB
V

module main
import os
import gg
import math
@[heap]
pub struct Window {
pub mut:
ctx &gg.Context = unsafe { 0 }
image gg.Image
}
pub fn (mut window Window) init() {
logo_path := os.join_path(@VEXEROOT, 'examples/assets/logo.png')
window.image = window.ctx.create_image(logo_path) or { panic(err) }
}
pub fn (mut window Window) draw(_ voidptr) {
window.ctx.begin()
myconfig := gg.DrawImageConfig{
img: &window.image
img_id: window.image.id
img_rect: gg.Rect{
x: 400 - window.image.width / 2
y: 300 - window.image.height / 2
width: window.image.width
height: window.image.height
}
rotation: f32(window.ctx.frame)
// effect: .alpha <-- this can be omitted completely as it is alpha by default.
}
window.ctx.draw_image_with_config(gg.DrawImageConfig{ ...myconfig, flip_x: true })
// Red
window.ctx.draw_image_with_config(gg.DrawImageConfig{
...myconfig
img_rect: gg.Rect{
...myconfig.img_rect
x: myconfig.img_rect.x + f32(math.sin(f32(window.ctx.frame) / 10.0) * 60)
y: myconfig.img_rect.y + f32(math.cos(f32(window.ctx.frame) / 10.0) * 60)
}
color: gg.Color{255, 0, 0, 255}
effect: .add
})
// Green
window.ctx.draw_image_with_config(gg.DrawImageConfig{
...myconfig
img_rect: gg.Rect{
...myconfig.img_rect
x: myconfig.img_rect.x + f32(math.sin(f32(window.ctx.frame) / 10.0) * 80)
y: myconfig.img_rect.y + f32(math.cos(f32(window.ctx.frame) / 10.0) * 80)
}
color: gg.Color{0, 255, 0, 255}
effect: .add
})
// Blue
window.ctx.draw_image_with_config(gg.DrawImageConfig{
...myconfig
img_rect: gg.Rect{
...myconfig.img_rect
x: myconfig.img_rect.x + f32(math.sin(f32(window.ctx.frame) / 10.0) * 100)
y: myconfig.img_rect.y + f32(math.cos(f32(window.ctx.frame) / 10.0) * 100)
}
color: gg.Color{0, 0, 255, 255}
effect: .add
})
// More examples
window.ctx.draw_image_with_config(gg.DrawImageConfig{
...myconfig
img_rect: gg.Rect{
...myconfig.img_rect
x: 50
y: 0
}
color: gg.Color{255, 0, 0, 255}
effect: .add
})
window.ctx.draw_image_with_config(gg.DrawImageConfig{
...myconfig
img_rect: gg.Rect{
...myconfig.img_rect
x: 50
y: 50
}
color: gg.Color{0, 255, 0, 255}
effect: .add
})
window.ctx.draw_image_with_config(gg.DrawImageConfig{
...myconfig
img_rect: gg.Rect{
...myconfig.img_rect
x: 50
y: 100
}
color: gg.Color{0, 0, 255, 255}
effect: .add
})
window.ctx.end()
}
fn main() {
mut window := &Window{}
window.ctx = gg.new_context(
window_title: 'Additive colors & image rotation'
width: 800
height: 600
user_data: window
bg_color: gg.gray
// FNs
init_fn: window.init
frame_fn: window.draw
)
window.ctx.run()
}