aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/examples/gg
diff options
context:
space:
mode:
authorIndrajith K L2022-12-03 17:00:20 +0530
committerIndrajith K L2022-12-03 17:00:20 +0530
commitf5c4671bfbad96bf346bd7e9a21fc4317b4959df (patch)
tree2764fc62da58f2ba8da7ed341643fc359873142f /v_windows/v/examples/gg
downloadcli-tools-windows-master.tar.gz
cli-tools-windows-master.tar.bz2
cli-tools-windows-master.zip
Adds most of the toolsHEADmaster
Diffstat (limited to 'v_windows/v/examples/gg')
-rw-r--r--v_windows/v/examples/gg/bezier.v34
-rw-r--r--v_windows/v/examples/gg/bezier_anim.v68
-rw-r--r--v_windows/v/examples/gg/logo.pngbin0 -> 4328 bytes
-rw-r--r--v_windows/v/examples/gg/polygons.v34
-rw-r--r--v_windows/v/examples/gg/random.v63
-rw-r--r--v_windows/v/examples/gg/raven_text_rendering.v104
-rw-r--r--v_windows/v/examples/gg/rectangles.v63
-rw-r--r--v_windows/v/examples/gg/set_pixels.v39
-rw-r--r--v_windows/v/examples/gg/stars.v134
-rw-r--r--v_windows/v/examples/gg/worker_thread.v90
10 files changed, 629 insertions, 0 deletions
diff --git a/v_windows/v/examples/gg/bezier.v b/v_windows/v/examples/gg/bezier.v
new file mode 100644
index 0000000..e5cb675
--- /dev/null
+++ b/v_windows/v/examples/gg/bezier.v
@@ -0,0 +1,34 @@
+module main
+
+import gg
+import gx
+
+const (
+ points = [f32(200.0), 200.0, 200.0, 100.0, 400.0, 100.0, 400.0, 300.0]
+)
+
+struct App {
+mut:
+ gg &gg.Context
+}
+
+fn main() {
+ mut app := &App{
+ gg: 0
+ }
+ app.gg = gg.new_context(
+ bg_color: gx.rgb(174, 198, 255)
+ width: 600
+ height: 400
+ window_title: 'Cubic Bézier curve'
+ frame_fn: frame
+ user_data: app
+ )
+ app.gg.run()
+}
+
+fn frame(mut app App) {
+ app.gg.begin()
+ app.gg.draw_cubic_bezier(points, gx.blue)
+ app.gg.end()
+}
diff --git a/v_windows/v/examples/gg/bezier_anim.v b/v_windows/v/examples/gg/bezier_anim.v
new file mode 100644
index 0000000..2f54ac9
--- /dev/null
+++ b/v_windows/v/examples/gg/bezier_anim.v
@@ -0,0 +1,68 @@
+module main
+
+import gg
+import gx
+
+const rate = f32(1) / 60 * 10
+
+struct App {
+mut:
+ gg &gg.Context
+ anim &Anim
+}
+
+struct Anim {
+mut:
+ time f32
+ reverse bool
+}
+
+fn (mut anim Anim) advance() {
+ if anim.reverse {
+ anim.time -= 1 * rate
+ } else {
+ anim.time += 1 * rate
+ }
+ // Use some arbitrary value that fits 60 fps
+ if anim.time > 80 * rate || anim.time < -80 * rate {
+ anim.reverse = !anim.reverse
+ }
+}
+
+fn main() {
+ mut app := &App{
+ gg: 0
+ anim: &Anim{}
+ }
+ app.gg = gg.new_context(
+ bg_color: gx.rgb(174, 198, 255)
+ width: 600
+ height: 400
+ window_title: 'Animated cubic Bézier curve'
+ frame_fn: frame
+ user_data: app
+ )
+ app.gg.run()
+}
+
+fn frame(mut app App) {
+ time := app.anim.time
+
+ p1_x := f32(200.0)
+ p1_y := f32(200.0) + (10 * time)
+
+ p2_x := f32(400.0)
+ p2_y := f32(200.0) + (10 * time)
+
+ ctrl_p1_x := f32(200.0) + (40 * time)
+ ctrl_p1_y := f32(100.0)
+ ctrl_p2_x := f32(400.0) + (-40 * time)
+ ctrl_p2_y := f32(100.0)
+
+ points := [p1_x, p1_y, ctrl_p1_x, ctrl_p1_y, ctrl_p2_x, ctrl_p2_y, p2_x, p2_y]
+
+ app.gg.begin()
+ app.gg.draw_cubic_bezier(points, gx.blue)
+ app.gg.end()
+ app.anim.advance()
+}
diff --git a/v_windows/v/examples/gg/logo.png b/v_windows/v/examples/gg/logo.png
new file mode 100644
index 0000000..98ebfb1
--- /dev/null
+++ b/v_windows/v/examples/gg/logo.png
Binary files differ
diff --git a/v_windows/v/examples/gg/polygons.v b/v_windows/v/examples/gg/polygons.v
new file mode 100644
index 0000000..9ae2037
--- /dev/null
+++ b/v_windows/v/examples/gg/polygons.v
@@ -0,0 +1,34 @@
+module main
+
+import gg
+import gx
+
+struct App {
+mut:
+ gg &gg.Context
+}
+
+fn main() {
+ mut app := &App{
+ gg: 0
+ }
+ app.gg = gg.new_context(
+ bg_color: gx.rgb(174, 198, 255)
+ width: 600
+ height: 400
+ window_title: 'Polygons'
+ frame_fn: frame
+ user_data: app
+ )
+ app.gg.run()
+}
+
+fn frame(mut app App) {
+ app.gg.begin()
+ app.gg.draw_convex_poly([f32(100.0), 100.0, 200.0, 100.0, 300.0, 200.0, 200.0, 300.0, 100.0,
+ 300.0,
+ ], gx.blue)
+ app.gg.draw_empty_poly([f32(50.0), 50.0, 70.0, 60.0, 90.0, 80.0, 70.0, 110.0], gx.black)
+ app.gg.draw_triangle(450, 142, 530, 280, 370, 280, gx.red)
+ app.gg.end()
+}
diff --git a/v_windows/v/examples/gg/random.v b/v_windows/v/examples/gg/random.v
new file mode 100644
index 0000000..f9e963b
--- /dev/null
+++ b/v_windows/v/examples/gg/random.v
@@ -0,0 +1,63 @@
+import gg
+import time
+
+const pwidth = 800
+
+const pheight = 600
+
+const pbytes = 4
+
+struct AppState {
+mut:
+ gg &gg.Context = 0
+ istream_idx int
+ pixels [pheight][pwidth]u32
+}
+
+[direct_array_access]
+fn (mut state AppState) update() {
+ mut rcolor := u64(state.gg.frame)
+ for {
+ for y in 0 .. pheight {
+ for x in 0 .. pwidth {
+ rcolor = rcolor * 1664525 + 1013904223
+ state.pixels[y][x] = u32(rcolor & 0x0000_0000_FFFF_FFFF) | 0x1010AFFF
+ }
+ }
+ time.sleep(33 * time.millisecond)
+ }
+}
+
+fn (mut state AppState) draw() {
+ mut istream_image := state.gg.get_cached_image_by_idx(state.istream_idx)
+ istream_image.update_pixel_data(&state.pixels)
+ size := gg.window_size()
+ state.gg.draw_image(0, 0, size.width, size.height, istream_image)
+}
+
+// gg callbacks:
+
+fn graphics_init(mut state AppState) {
+ state.istream_idx = state.gg.new_streaming_image(pwidth, pheight, pbytes, pixel_format: .rgba8)
+}
+
+fn graphics_frame(mut state AppState) {
+ state.gg.begin()
+ state.draw()
+ state.gg.end()
+}
+
+fn main() {
+ mut state := &AppState{}
+ state.gg = gg.new_context(
+ width: 800
+ height: 600
+ create_window: true
+ window_title: 'Random Static'
+ init_fn: graphics_init
+ frame_fn: graphics_frame
+ user_data: state
+ )
+ go state.update()
+ state.gg.run()
+}
diff --git a/v_windows/v/examples/gg/raven_text_rendering.v b/v_windows/v/examples/gg/raven_text_rendering.v
new file mode 100644
index 0000000..2007787
--- /dev/null
+++ b/v_windows/v/examples/gg/raven_text_rendering.v
@@ -0,0 +1,104 @@
+module main
+
+import gg
+import gx
+import os
+import math
+
+const (
+ win_width = 600
+ win_height = 700
+ bg_color = gx.white
+)
+
+const (
+ text = '
+Once upon a midnight dreary, while I pondered, weak and weary,
+Over many a quaint and curious volume of forgotten lore—
+ While I nodded, nearly napping, suddenly there came a tapping,
+As of some one gently rapping, rapping at my chamber door.
+“’Tis some visitor,” I muttered, “tapping at my chamber door—
+ Only this and nothing more.”
+
+ Ah, distinctly I remember it was in the bleak December;
+And each separate dying ember wrought its ghost upon the floor.
+ Eagerly I wished the morrow;—vainly I had sought to borrow
+ From my books surcease of sorrow—sorrow for the lost Lenore—
+For the rare and radiant maiden whom the angels name Lenore—
+ Nameless here for evermore.
+
+ And the silken, sad, uncertain rustling of each purple curtain
+Thrilled me—filled me with fantastic terrors never felt before;
+ So that now, to still the beating of my heart, I stood repeating
+ “’Tis some visitor entreating entrance at my chamber door—
+Some late visitor entreating entrance at my chamber door;—
+ This it is and nothing more.”
+
+ Presently my soul grew stronger; hesitating then no longer,
+“Sir,” said I, “or Madam, truly your forgiveness I implore;
+ But the fact is I was napping, and so gently you came rapping,
+ And so faintly you came tapping, tapping at my chamber door,
+That I scarce was sure I heard you”—here I opened wide the door;—
+ Darkness there and nothing more.
+
+Deep into that darkness peering, long I stood there wondering, fearing,
+Doubting, dreaming dreams no mortal ever dared to dream before;
+ But the silence was unbroken, and the stillness gave no token,
+ And the only word there spoken was the whispered word, “Lenore?”
+This I whispered, and an echo murmured back the word, “Lenore!”—
+ Merely this and nothing more.
+
+ Back into the chamber turning, all my soul within me burning,
+Soon again I heard a tapping somewhat louder than before.
+ “Surely,” said I, “surely that is something at my window lattice;
+ Let me see, then, what thereat is, and this mystery explore—
+Let my heart be still a moment and this mystery explore;—
+ ’Tis the wind and nothing more!”
+'
+ lines = text.split('\n')
+)
+
+struct App {
+mut:
+ gg &gg.Context
+}
+
+fn main() {
+ mut app := &App{
+ gg: 0
+ }
+ mut font_path := os.resource_abs_path(os.join_path('..', 'assets', 'fonts', 'RobotoMono-Regular.ttf'))
+ $if android {
+ font_path = 'fonts/RobotoMono-Regular.ttf'
+ }
+ app.gg = gg.new_context(
+ width: win_width
+ height: win_height
+ create_window: true
+ window_title: 'Empty window'
+ user_data: app
+ bg_color: bg_color
+ frame_fn: frame
+ font_path: font_path // window_user_ptr: ctx
+ // native_rendering: true
+ )
+ app.gg.run()
+}
+
+fn frame(mut app App) {
+ app.gg.begin()
+ width := gg.window_size().width
+ mut scale_factor := math.round(f32(width) / win_width)
+ if scale_factor <= 0 {
+ scale_factor = 1
+ }
+ text_cfg := gx.TextCfg{
+ size: 16 * int(scale_factor)
+ }
+ mut y := 10
+ for line in lines {
+ app.gg.draw_text(10, y, line, text_cfg)
+ y += 30
+ }
+ app.gg.end()
+}
diff --git a/v_windows/v/examples/gg/rectangles.v b/v_windows/v/examples/gg/rectangles.v
new file mode 100644
index 0000000..97dc222
--- /dev/null
+++ b/v_windows/v/examples/gg/rectangles.v
@@ -0,0 +1,63 @@
+module main
+
+import gg
+import gx
+import os
+import net.http
+
+const (
+ win_width = 600
+ win_height = 300
+)
+
+struct App {
+mut:
+ gg &gg.Context
+ image gg.Image
+}
+
+fn main() {
+ mut app := &App{
+ gg: 0
+ }
+ app.gg = gg.new_context(
+ bg_color: gx.white
+ width: win_width
+ height: win_height
+ create_window: true
+ window_title: 'Rectangles'
+ frame_fn: frame
+ user_data: app
+ init_fn: init_images
+ )
+ app.image = app.gg.create_image(os.resource_abs_path('logo.png'))
+
+ http.download_file_with_progress("http://retrowave.ru/artwork/8b1a28d7f9a9322f44fe5f98f87229f1d2f6b883.jpg","download.png", download_finished, downloading)
+ app.gg.run()
+}
+
+fn download_finished() {
+ println("Finished")
+}
+
+fn downloading() {
+ println("downloding")
+}
+
+fn init_images(mut app App) {
+ // app.image = gg.create_image('logo.png')
+}
+
+fn frame(app &App) {
+ app.gg.begin()
+ app.draw()
+ app.gg.end()
+}
+
+fn (app &App) draw() {
+ // app.gg.draw_text_def(200,20, 'hello world!')
+ // app.gg.draw_text_def(300,300, 'привет')
+ app.gg.draw_rect(10, 10, 100, 30, gx.blue)
+ app.gg.draw_empty_rect(110, 150, 80, 40, gx.black)
+ app.gg.draw_image(230, 30, app.image.width, app.image.height, app.image)
+}
diff --git a/v_windows/v/examples/gg/set_pixels.v b/v_windows/v/examples/gg/set_pixels.v
new file mode 100644
index 0000000..69dc53d
--- /dev/null
+++ b/v_windows/v/examples/gg/set_pixels.v
@@ -0,0 +1,39 @@
+module main
+
+import gg
+import gx
+
+struct App {
+mut:
+ gg &gg.Context
+}
+
+fn main() {
+ mut app := &App{
+ gg: 0
+ }
+ app.gg = gg.new_context(
+ bg_color: gx.rgb(174, 198, 255)
+ width: 100
+ height: 100
+ window_title: 'Set Pixels'
+ frame_fn: frame
+ user_data: app
+ )
+ app.gg.run()
+}
+
+fn frame(mut app App) {
+ mut pixels := []f32{}
+
+ for x in 30 .. 60 {
+ for y in 30 .. 60 {
+ pixels << f32(x)
+ pixels << f32(y)
+ }
+ }
+
+ app.gg.begin()
+ app.gg.set_pixels(pixels, gx.red)
+ app.gg.end()
+}
diff --git a/v_windows/v/examples/gg/stars.v b/v_windows/v/examples/gg/stars.v
new file mode 100644
index 0000000..815cfeb
--- /dev/null
+++ b/v_windows/v/examples/gg/stars.v
@@ -0,0 +1,134 @@
+module main
+
+import os
+import gg
+import gx
+import rand
+import sokol.sgl
+
+const (
+ win_width = 800
+ win_height = 600
+ max_stars = 5000
+ max_v_letters = 5
+)
+
+struct Star {
+mut:
+ x f32
+ y f32
+ z f32
+ r f32
+ g f32
+ b f32
+}
+
+struct VLetter {
+mut:
+ x f32
+ y f32
+ z f32
+ w f32
+ h f32
+ angle f32
+ dz f32
+ dangle f32
+}
+
+struct App {
+mut:
+ gg &gg.Context
+ image gg.Image
+ stars []Star
+ v_letters []VLetter
+}
+
+fn main() {
+ mut app := &App{
+ gg: 0
+ stars: []Star{len: max_stars}
+ v_letters: []VLetter{len: max_v_letters}
+ }
+ app.gg = gg.new_context(
+ bg_color: gx.black
+ width: win_width
+ height: win_height
+ create_window: true
+ window_title: 'Star Vield'
+ frame_fn: frame
+ init_fn: init_images
+ user_data: app
+ )
+ for i in 0 .. max_stars {
+ app.stars[i].x = rand.f32_in_range(-200.0, 200.0)
+ app.stars[i].y = rand.f32_in_range(-200.0, 200.0)
+ app.stars[i].z = rand.f32_in_range(-200.0, -100.0)
+ app.stars[i].r = rand.f32_in_range(0.1, 1.0)
+ app.stars[i].g = rand.f32_in_range(0.1, 1.0)
+ app.stars[i].b = rand.f32_in_range(0.1, 1.0)
+ }
+ for i in 0 .. max_v_letters {
+ app.v_letters[i].x = rand.f32_in_range(-20.0, 20.0)
+ app.v_letters[i].y = rand.f32_in_range(-20.0, 20.0)
+ app.v_letters[i].z = rand.f32_in_range(-5.0, -1.0)
+ app.v_letters[i].w = rand.f32_in_range(5, 20)
+ app.v_letters[i].h = app.v_letters[i].w
+ app.v_letters[i].angle = rand.f32_in_range(0, 6.283184)
+ app.v_letters[i].dangle = rand.f32_in_range(-0.05, 0.05)
+ app.v_letters[i].dz = rand.f32_in_range(-0.1, -0.01)
+ }
+ app.gg.run()
+}
+
+fn init_images(mut app App) {
+ app.image = app.gg.create_image(os.resource_abs_path('logo.png'))
+}
+
+fn frame(mut app App) {
+ app.gg.begin()
+ app.draw()
+ app.gg.end()
+}
+
+// fn C.glPointSize(size f32)
+fn (mut app App) draw() {
+ sgl.defaults()
+ sgl.perspective(sgl.rad(90), 1.0, 1.0, 100.0)
+ // C.glPointSize(3.0)
+ sgl.begin_points()
+ for i in 0 .. app.stars.len {
+ s := app.stars[i]
+ sgl.v3f_c3f(s.x, s.y, s.z, s.r, s.g, s.b)
+ app.stars[i].z += 0.3
+ if app.stars[i].z > -1.0 {
+ app.stars[i].x = rand.f32_in_range(-200.0, 200.0)
+ app.stars[i].y = rand.f32_in_range(-200.0, 200.0)
+ app.stars[i].z = rand.f32_in_range(-200.0, -100.0)
+ }
+ }
+ sgl.end()
+ // ////
+ for i in 0 .. app.v_letters.len {
+ v := app.v_letters[i]
+ sgl.defaults()
+ sgl.perspective(sgl.rad(90), 1.0, 1.0, 100.0)
+ sgl.rotate(v.angle, 0, 0, 1)
+ app.gg.draw_image_3d(v.x, v.y, v.z, v.w, v.h, app.image)
+ //
+ app.v_letters[i].z += app.v_letters[i].dz
+ app.v_letters[i].angle += app.v_letters[i].dangle
+ if app.v_letters[i].z > -60.0 {
+ app.v_letters[i].x += rand.f32_in_range(-0.05, 0.05)
+ app.v_letters[i].y += rand.f32_in_range(-0.05, 0.05)
+ }
+ if app.v_letters[i].z < -95.0 {
+ app.v_letters[i].h *= 0.8
+ app.v_letters[i].w *= 0.8
+ }
+ if app.v_letters[i].z < -100.0 {
+ app.v_letters[i].z = rand.f32_in_range(-5.0, -1.0)
+ app.v_letters[i].h = 10.0
+ app.v_letters[i].w = 10.0
+ }
+ }
+}
diff --git a/v_windows/v/examples/gg/worker_thread.v b/v_windows/v/examples/gg/worker_thread.v
new file mode 100644
index 0000000..a2c9852
--- /dev/null
+++ b/v_windows/v/examples/gg/worker_thread.v
@@ -0,0 +1,90 @@
+// Copyright(C) 2019 Lars Pontoppidan. All rights reserved.
+// Use of this source code is governed by an MIT license file distributed with this software package.
+module main
+
+// Example of how to send a value through a channel from a worker thread to the main/rendering thread.
+// This can be useful to do long running computations while keeping your framerate high (60 fps in this example).
+import gg
+import gx
+import math
+import time
+
+const (
+ win_width = 600
+ win_height = 700
+ bg_color = gx.white
+ count_color = gx.black
+)
+
+struct App {
+mut:
+ gg &gg.Context
+ ch chan i64
+ counter i64
+}
+
+fn main() {
+ mut app := &App{
+ gg: 0
+ }
+ app.gg = gg.new_context(
+ width: win_width
+ height: win_height
+ create_window: true
+ window_title: 'Counter'
+ user_data: app
+ bg_color: bg_color
+ frame_fn: frame
+ init_fn: init
+ font_path: gg.system_font_path()
+ )
+ app.gg.run()
+}
+
+fn init(mut app App) {
+ // Spawn a new worker thread.
+ go worker(mut app)
+}
+
+// worker simulates a workload. This should be run in a separate thread.
+fn worker(mut app App) {
+ stopwatch := time.new_stopwatch()
+ mut elapsed := stopwatch.elapsed()
+ // Do heavy operations here - like invoking a path finding algorithm, load an image or similar.
+ for {
+ now := stopwatch.elapsed()
+ // When done - send the result through a channel to the main/rendering thread.
+ app.ch <- (now - elapsed)
+ elapsed = now
+ time.sleep(1 * time.second)
+ }
+}
+
+fn frame(mut app App) {
+ app.gg.begin()
+ size := gg.window_size()
+ mut scale_factor := math.round(f32(size.width) / win_width)
+ if scale_factor <= 0 {
+ scale_factor = 1
+ }
+ text_cfg := gx.TextCfg{
+ size: 64 * int(scale_factor)
+ }
+
+ // Try a pop from the channel
+ mut count := i64(0)
+ if app.ch.try_pop(mut count) == .success {
+ // A value was assigned - increase the counter
+ app.counter += i64(f64(count) / time.second)
+ }
+
+ label := '$app.counter'
+ label_width := (f64(label.len * text_cfg.size) / 4.0)
+ label_height := (f64(1 * text_cfg.size) / 2.0)
+ mut x := f32(size.width) * 0.5 - label_width
+ mut y := f32(size.height) * 0.5 - label_height
+
+ app.gg.draw_text(int(x), int(y), label, text_cfg)
+
+ app.gg.end()
+}