v2 / examples / gg / additive.v
124 lines · 109 sloc · 2.7 KB · bbb61ab3687afe512a1fa12492c876d011626107
Raw
1module main
2
3import os
4import gg
5import math
6
7@[heap]
8pub struct Window {
9pub mut:
10 ctx &gg.Context = unsafe { 0 }
11 image gg.Image
12}
13
14pub fn (mut window Window) init() {
15 logo_path := os.join_path(@VEXEROOT, 'examples/assets/logo.png')
16 window.image = window.ctx.create_image(logo_path) or { panic(err) }
17}
18
19pub fn (mut window Window) draw(_ voidptr) {
20 window.ctx.begin()
21
22 myconfig := gg.DrawImageConfig{
23 img: &window.image
24 img_id: window.image.id
25 img_rect: gg.Rect{
26 x: 400 - window.image.width / 2
27 y: 300 - window.image.height / 2
28 width: window.image.width
29 height: window.image.height
30 }
31 rotation: f32(window.ctx.frame)
32 // effect: .alpha <-- this can be omitted completely as it is alpha by default.
33 }
34 window.ctx.draw_image_with_config(gg.DrawImageConfig{ ...myconfig, flip_x: true })
35
36 // Red
37 window.ctx.draw_image_with_config(gg.DrawImageConfig{
38 ...myconfig
39 img_rect: gg.Rect{
40 ...myconfig.img_rect
41 x: myconfig.img_rect.x + f32(math.sin(f32(window.ctx.frame) / 10.0) * 60)
42 y: myconfig.img_rect.y + f32(math.cos(f32(window.ctx.frame) / 10.0) * 60)
43 }
44 color: gg.Color{255, 0, 0, 255}
45 effect: .add
46 })
47
48 // Green
49 window.ctx.draw_image_with_config(gg.DrawImageConfig{
50 ...myconfig
51 img_rect: gg.Rect{
52 ...myconfig.img_rect
53 x: myconfig.img_rect.x + f32(math.sin(f32(window.ctx.frame) / 10.0) * 80)
54 y: myconfig.img_rect.y + f32(math.cos(f32(window.ctx.frame) / 10.0) * 80)
55 }
56 color: gg.Color{0, 255, 0, 255}
57 effect: .add
58 })
59
60 // Blue
61 window.ctx.draw_image_with_config(gg.DrawImageConfig{
62 ...myconfig
63 img_rect: gg.Rect{
64 ...myconfig.img_rect
65 x: myconfig.img_rect.x + f32(math.sin(f32(window.ctx.frame) / 10.0) * 100)
66 y: myconfig.img_rect.y + f32(math.cos(f32(window.ctx.frame) / 10.0) * 100)
67 }
68 color: gg.Color{0, 0, 255, 255}
69 effect: .add
70 })
71
72 // More examples
73 window.ctx.draw_image_with_config(gg.DrawImageConfig{
74 ...myconfig
75 img_rect: gg.Rect{
76 ...myconfig.img_rect
77 x: 50
78 y: 0
79 }
80 color: gg.Color{255, 0, 0, 255}
81 effect: .add
82 })
83
84 window.ctx.draw_image_with_config(gg.DrawImageConfig{
85 ...myconfig
86 img_rect: gg.Rect{
87 ...myconfig.img_rect
88 x: 50
89 y: 50
90 }
91 color: gg.Color{0, 255, 0, 255}
92 effect: .add
93 })
94
95 window.ctx.draw_image_with_config(gg.DrawImageConfig{
96 ...myconfig
97 img_rect: gg.Rect{
98 ...myconfig.img_rect
99 x: 50
100 y: 100
101 }
102 color: gg.Color{0, 0, 255, 255}
103 effect: .add
104 })
105
106 window.ctx.end()
107}
108
109fn main() {
110 mut window := &Window{}
111
112 window.ctx = gg.new_context(
113 window_title: 'Additive colors & image rotation'
114 width: 800
115 height: 600
116 user_data: window
117 bg_color: gg.gray
118 // FNs
119 init_fn: window.init
120 frame_fn: window.draw
121 )
122
123 window.ctx.run()
124}
125