Initial project import
This commit is contained in:
commit
23f2d29e32
11957 files changed
+1754577
No files matched your search
@@ -0,0 +1 @@
|
||||
!glfw3.dll
|
||||
@@ -0,0 +1,79 @@
|
||||
// Build this example with
|
||||
// v -live bounce.v
|
||||
module main
|
||||
|
||||
import gg
|
||||
import time
|
||||
|
||||
struct Game {
|
||||
mut:
|
||||
gg &gg.Context = unsafe { nil }
|
||||
x int
|
||||
y int
|
||||
dy int
|
||||
dx int
|
||||
height int
|
||||
width int
|
||||
draw_fn voidptr
|
||||
}
|
||||
|
||||
const window_width = 400
|
||||
const window_height = 300
|
||||
const width = 50
|
||||
|
||||
fn main() {
|
||||
mut game := &Game{
|
||||
dx: 2
|
||||
dy: 2
|
||||
height: window_height
|
||||
width: window_width
|
||||
draw_fn: 0
|
||||
}
|
||||
game.gg = gg.new_context(
|
||||
width: window_width
|
||||
height: window_height
|
||||
font_size: 20
|
||||
user_data: game
|
||||
window_title: 'Hot code reloading demo'
|
||||
create_window: true
|
||||
frame_fn: frame
|
||||
bg_color: gg.white
|
||||
)
|
||||
// window.onkeydown(key_down)
|
||||
println('Starting the game loop...')
|
||||
spawn game.run()
|
||||
game.gg.run()
|
||||
}
|
||||
|
||||
// Try uncommenting or changing the lines inside the live functions.
|
||||
// Guess what will happen:
|
||||
@[live]
|
||||
fn frame(mut game Game) {
|
||||
game.gg.begin()
|
||||
game.gg.draw_text_def(10, 5, 'Modify examples/hot_reload/bounce.v to get instant updates')
|
||||
game.gg.draw_rect_filled(game.x, game.y, width, width, gg.black)
|
||||
game.gg.draw_rect_filled(window_width - width - game.x + 10, 200 - game.y + width, width,
|
||||
width, gg.rgb(228, 10, 55))
|
||||
game.gg.draw_rect_filled(game.x - 25, 250 - game.y, width, width, gg.rgb(28, 240, 55))
|
||||
game.gg.end()
|
||||
}
|
||||
|
||||
@[live]
|
||||
fn (mut game Game) update_model() {
|
||||
speed := 2
|
||||
game.x += speed * game.dx
|
||||
game.y += speed * game.dy
|
||||
if game.y >= game.height - width || game.y <= 0 {
|
||||
game.dy = -game.dy
|
||||
}
|
||||
if game.x >= game.width - width || game.x <= 0 {
|
||||
game.dx = -game.dx
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut game Game) run() {
|
||||
for {
|
||||
game.update_model()
|
||||
time.sleep(16 * time.millisecond) // 60fps
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
module main
|
||||
|
||||
import gg
|
||||
import time
|
||||
import math
|
||||
|
||||
const size = 700
|
||||
const scale = 50.0
|
||||
|
||||
struct Context {
|
||||
mut:
|
||||
gg &gg.Context = unsafe { nil }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mut context := &Context{}
|
||||
context.gg = gg.new_context(
|
||||
width: size
|
||||
height: size
|
||||
font_size: 20
|
||||
user_data: context
|
||||
window_title: 'Graph builder'
|
||||
create_window: true
|
||||
frame_fn: frame
|
||||
resizable: true
|
||||
bg_color: gg.white
|
||||
)
|
||||
context.gg.run()
|
||||
}
|
||||
|
||||
fn frame(mut ctx Context) {
|
||||
ctx.gg.begin()
|
||||
ctx.draw()
|
||||
ctx.gg.end()
|
||||
}
|
||||
|
||||
@[live]
|
||||
fn (ctx &Context) draw() {
|
||||
s := gg.window_size()
|
||||
mut w := s.width
|
||||
mut h := s.height
|
||||
if gg.high_dpi() {
|
||||
w /= 2
|
||||
h /= 2
|
||||
}
|
||||
ctx.gg.draw_line(0, h / 2, w, h / 2, gg.gray) // x axis
|
||||
ctx.gg.draw_line(w / 2, 0, w / 2, h, gg.gray) // y axis
|
||||
atime := f64(time.ticks() / 10)
|
||||
stime := math.sin(2.0 * math.pi * f64(time.ticks() % 6000) / 6000)
|
||||
mut y := 0.0
|
||||
blue := gg.Color{
|
||||
r: 100
|
||||
g: 100
|
||||
b: 200
|
||||
}
|
||||
red := gg.Color{
|
||||
r: 200
|
||||
g: 100
|
||||
b: 100
|
||||
}
|
||||
y = 1.0
|
||||
max := f32(w) / (2 * scale)
|
||||
min := -max
|
||||
for x := min; x <= max; x += 0.01 {
|
||||
// y = x*x + 2
|
||||
// y = x * x + stime * stime
|
||||
// y = stime
|
||||
// y = stime * h
|
||||
y = stime * 1.0 * math.sin(x + stime + atime / 32) * ((h / 256) + x)
|
||||
// y = (stime * x) * x + stime
|
||||
// y = (x + 3) * (x + 3) / stime + stime*2.5
|
||||
// y = math.sqrt(30.0 - x * x) * stime
|
||||
// y -= (stime-0.5) + stime
|
||||
// ctx.gg.draw_rect_filled(f32((w/2) + x * scale), f32((h/2) - y * scale), 2, 2, blue)
|
||||
ctx.gg.draw_rect_filled(f32((w / 2) + x * scale), f32((h / 2) - y * scale), 2,
|
||||
(f32(y) * scale), blue)
|
||||
ctx.gg.draw_rect_filled(f32((w / 2) + x * scale), f32((h / 2) + y * scale), 2,
|
||||
|
||||
(f32(y) * scale) + 32, red)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
module main
|
||||
|
||||
// Build this example with `v -live message.v`
|
||||
import time
|
||||
import v.live
|
||||
|
||||
struct App {
|
||||
mut:
|
||||
x int
|
||||
c int
|
||||
}
|
||||
|
||||
@[live]
|
||||
fn print_message(mut app App) {
|
||||
i := live.info()
|
||||
println('Hello! Modify this message. OK reloads: ${i.reloads_ok:2d} | Total: ${i.reloads:2d} | app: ${voidptr(app)} | app.c: ${app.c:4} | app.x: ${app.x:12}')
|
||||
// app.x = app.x * 3 + 1 // try changing this to another value, while the program is running ...
|
||||
// app.x = 0
|
||||
app.c++
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unbuffer_stdout()
|
||||
println('=============================================================')
|
||||
println('== Modify the message below, while the program is running: ==')
|
||||
println('=============================================================')
|
||||
mut app := &App{}
|
||||
for {
|
||||
print_message(mut app)
|
||||
time.sleep(500 * time.millisecond)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
// Try commenting or changing the drawing functions, that are at the bottom of the file,
|
||||
// and that are inside functions marked with @[live] ...
|
||||
import gg
|
||||
import math
|
||||
|
||||
fn main() {
|
||||
mut state := init_state()
|
||||
gg.start(
|
||||
window_title: 'Tunnel'
|
||||
bg_color: gg.Color{255, 255, 255, 255}
|
||||
width: 1024
|
||||
height: 768
|
||||
frame_fn: unsafe { state.draw }
|
||||
event_fn: unsafe { state.on_event }
|
||||
)
|
||||
}
|
||||
|
||||
struct BigRect {
|
||||
mut:
|
||||
id int
|
||||
t f32
|
||||
x f32
|
||||
y f32
|
||||
w f32
|
||||
bwidth f32
|
||||
c gg.Color
|
||||
}
|
||||
|
||||
struct State {
|
||||
mut:
|
||||
t f32
|
||||
real_t f32
|
||||
drt f32 = 0.1
|
||||
tparams gg.DrawTextParams
|
||||
rects []BigRect
|
||||
}
|
||||
|
||||
const colors = []gg.Color{len: 1024, init: gg.Color{u8(100 +
|
||||
100 * math.sin(f32(index) / 50) * math.cos(f32(index) / 50)), u8(100 +
|
||||
100 * math.cos(f32(index) / 50)), 100 + u8(index % 50), 255}}
|
||||
|
||||
fn init_state() State {
|
||||
mut state := State{
|
||||
t: 0
|
||||
real_t: 120
|
||||
drt: 0.1
|
||||
tparams: gg.DrawTextParams{
|
||||
x: 40
|
||||
size: 20
|
||||
color: gg.Color{255, 255, 255, 255}
|
||||
}
|
||||
rects: [
|
||||
BigRect{
|
||||
x: 350
|
||||
y: 250
|
||||
w: 30
|
||||
bwidth: 10
|
||||
c: gg.Color{0, 0, 255, 255}
|
||||
},
|
||||
]
|
||||
}
|
||||
for i in 1 .. 50 {
|
||||
state.rects << BigRect{
|
||||
id: i
|
||||
}
|
||||
}
|
||||
return state
|
||||
}
|
||||
|
||||
fn (mut state State) on_event(e &gg.Event, _ctx voidptr) {
|
||||
if e.typ == .char {
|
||||
if e.char_code == `f` {
|
||||
gg.toggle_fullscreen()
|
||||
return
|
||||
}
|
||||
if e.char_code == `1` {
|
||||
state.real_t -= 100
|
||||
}
|
||||
if e.char_code == `2` {
|
||||
state.real_t += 100
|
||||
}
|
||||
if e.char_code == `3` {
|
||||
state.t -= 100
|
||||
}
|
||||
if e.char_code == `4` {
|
||||
state.t += 100
|
||||
}
|
||||
if e.char_code == `5` {
|
||||
state.rects[0].w += 10
|
||||
}
|
||||
if e.char_code == `6` {
|
||||
state.rects[0].w -= 10
|
||||
}
|
||||
if e.char_code == `r` {
|
||||
state.real_t = 120
|
||||
state.t = 0
|
||||
state.rects[0].w = 30
|
||||
}
|
||||
if e.char_code == ` ` {
|
||||
state.drt = if state.drt > 0.0001 { f32(0.0) } else { 0.1 }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut state State) draw(ctx &gg.Context) {
|
||||
update(mut state)
|
||||
ctx.begin()
|
||||
mut r0 := &state.rects[0]
|
||||
draw_big_rect(ctx, r0)
|
||||
for i in 1 .. state.rects.len {
|
||||
draw_big_rect(ctx, state.rects[i])
|
||||
}
|
||||
ctx.draw_text2(gg.DrawTextParams{
|
||||
...state.tparams
|
||||
y: 0
|
||||
text: 'rt[1,2]: ${state.real_t:08.3f}, t[3,4]: ${state.t:08.3f}, r0.x: ${r0.x:07.3f}, r0.y: ${r0.y:07.3f}, r0.w[5,6]: ${r0.w:4.1f}, [f]-toggle fs, [r]-reset, [ ]-pause'
|
||||
})
|
||||
draw_center_point(ctx, r0, state.t)
|
||||
ctx.show_fps()
|
||||
ctx.end()
|
||||
}
|
||||
|
||||
@[live]
|
||||
fn update(mut state State) {
|
||||
if state.drt <= 0.0001 {
|
||||
return
|
||||
}
|
||||
state.real_t += state.drt
|
||||
state.t += 0.5 * math.sinf(state.real_t / 1000)
|
||||
mut r0 := &state.rects[0]
|
||||
r0.t = state.t
|
||||
r0.x = 550 + 450 * math.sinf(state.t / 10) * math.sinf(state.t / 50)
|
||||
r0.y = 350 + 350 * math.cosf(state.t / 20) * math.cosf(-state.t / 50)
|
||||
r0.x += 5 * math.sinf(state.t)
|
||||
r0.y += 5 * math.cosf(state.t)
|
||||
r0.c = colors[int((50000 + state.t) / 200 * (1 + math.sin(state.t / 5))) % colors.len]
|
||||
for i := state.rects.len - 1; i > 0; i-- {
|
||||
state.rects[i] = state.rects[i - 1]
|
||||
state.rects[i].w *= 1.11 + f32(i) / 1000
|
||||
state.rects[i].bwidth *= 1.09
|
||||
}
|
||||
}
|
||||
|
||||
@[live]
|
||||
fn draw_center_point(ctx &gg.Context, br BigRect, t f32) {
|
||||
b := u8(128 + 128 * math.sin(t / 12))
|
||||
c := gg.Color{255 - b, b, b, 55}
|
||||
ctx.draw_circle_filled(br.x, br.y, 8, c)
|
||||
}
|
||||
|
||||
@[live]
|
||||
fn draw_big_rect(ctx &gg.Context, br BigRect) {
|
||||
radius := 20
|
||||
x := br.x
|
||||
y := br.y
|
||||
w := br.w
|
||||
c := br.c
|
||||
bwidth := br.bwidth
|
||||
base := gg.DrawRectParams{
|
||||
radius: radius
|
||||
is_rounded: true
|
||||
color: c
|
||||
}
|
||||
rleft := gg.DrawRectParams{
|
||||
...base
|
||||
x: x - (w / 2)
|
||||
y: y - (w / 2)
|
||||
w: bwidth
|
||||
h: w
|
||||
}
|
||||
rright := gg.DrawRectParams{
|
||||
...base
|
||||
x: x + (w / 2) - bwidth
|
||||
y: y - w + (w / 2)
|
||||
w: bwidth
|
||||
h: w
|
||||
}
|
||||
rtop := gg.DrawRectParams{
|
||||
...base
|
||||
x: x - (w / 2)
|
||||
y: y - (w / 2)
|
||||
w: w
|
||||
h: bwidth
|
||||
}
|
||||
rbottom := gg.DrawRectParams{
|
||||
...base
|
||||
x: x + (w / 2) - w
|
||||
y: y + (w / 2) - bwidth
|
||||
w: w
|
||||
h: bwidth
|
||||
}
|
||||
border := gg.DrawRectParams{
|
||||
x: x - (w / 2) + bwidth + 1
|
||||
y: y - (w / 2) + bwidth + 2
|
||||
w: w - 2 * bwidth - 2
|
||||
h: w - 2 * bwidth - 1
|
||||
color: gg.Color{255, 255, 255, 155}
|
||||
style: .stroke
|
||||
}
|
||||
ctx.draw_rect(rtop)
|
||||
ctx.draw_rect(rleft)
|
||||
ctx.draw_rect(rright)
|
||||
ctx.draw_rect(border)
|
||||
ctx.draw_rect(rbottom)
|
||||
}
|
||||
Reference in new issue
Block a user