1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

examples: enable maximization

This commit is contained in:
Delyan Angelov 2021-01-26 23:40:30 +02:00
parent 06b660666b
commit 1f4e8254b9
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
4 changed files with 55 additions and 22 deletions

View File

@ -3,13 +3,15 @@ import objects
import gg import gg
import gx import gx
import rand import rand
import sokol.sapp
struct App { struct App {
mut: mut:
gg &gg.Context = 0 gg &gg.Context = 0
ui &objects.UIParams = 0
rockets []objects.Rocket rockets []objects.Rocket
frames [][]objects.Rocket frames [][]objects.Rocket
// i thought about using a fixed fifo queue for the frames but the array // i thought about using a fixed fifo queue for the frames but the array
// seemed to work fine, if you'd like a challenge try implementing it with the queue :) // seemed to work fine, if you'd like a challenge try implementing it with the queue :)
} }
@ -54,6 +56,25 @@ fn on_frame(mut app App) {
app.gg.end() app.gg.end()
} }
fn on_event(e &sapp.Event, mut app App) {
match e.typ {
.resized, .restored, .resumed { app.resize() }
else {}
}
}
fn (mut app App) resize() {
mut s := sapp.dpi_scale()
if s == 0.0 {
s = 1.0
}
w := int(sapp.width() / s)
h := int(sapp.height() / s)
app.ui.dpi_scale = s
app.ui.width = w
app.ui.height = h
}
fn main() { fn main() {
mut font_path := os.resource_abs_path(os.join_path('../assets/fonts/', 'RobotoMono-Regular.ttf')) mut font_path := os.resource_abs_path(os.join_path('../assets/fonts/', 'RobotoMono-Regular.ttf'))
$if android { $if android {
@ -61,15 +82,17 @@ fn main() {
} }
mut app := &App{} mut app := &App{}
app.ui = objects.get_params()
app.gg = gg.new_context( app.gg = gg.new_context(
width: objects.width width: app.ui.width
height: objects.height height: app.ui.height
window_title: 'Fireworks!' window_title: 'Fireworks!'
bg_color: gx.black bg_color: gx.black
use_ortho: true use_ortho: true
user_data: app user_data: app
frame_fn: on_frame frame_fn: on_frame
event_fn: on_event
font_path: font_path font_path: font_path
) )

View File

@ -1,12 +1,20 @@
module objects module objects
const ( pub struct UIParams {
width = 800 pub mut:
height = 800 dpi_scale f32 = 1.0
gravity = Vector{0, -0.03} width int = 800
age_rate = 1 height int = 800
offspring_count = 100 gravity Vector = Vector{0, -0.03}
rocket_radius = 5 age_rate int = 1
particle_radius = 2.5 offspring_count int = 100
drag = 0.98 rocket_radius int = 5
) particle_radius f32 = 2.5
drag f32 = 0.98
}
const params = &UIParams{}
pub fn get_params() &UIParams {
return objects.params
}

View File

@ -13,11 +13,12 @@ pub mut:
} }
pub fn (particle Particle) draw(mut ctx gg.Context) { pub fn (particle Particle) draw(mut ctx gg.Context) {
ctx.draw_circle(particle.pos.x, height - particle.pos.y, particle_radius, particle.color) ctx.draw_circle(particle.pos.x, get_params().height - particle.pos.y, get_params().particle_radius,
particle.color)
} }
pub fn (mut particle Particle) tick(mut rocket Rocket, mut ctx gg.Context) { pub fn (mut particle Particle) tick(mut rocket Rocket, mut ctx gg.Context) {
particle.lifespan -= age_rate particle.lifespan -= get_params().age_rate
particle.color.a = byte(particle.lifespan) particle.color.a = byte(particle.lifespan)
if particle.lifespan <= 0 { if particle.lifespan <= 0 {
@ -25,9 +26,9 @@ pub fn (mut particle Particle) tick(mut rocket Rocket, mut ctx gg.Context) {
return return
} }
particle.accel += gravity particle.accel += get_params().gravity
particle.vel += particle.accel particle.vel += particle.accel
particle.vel = particle.vel.mult(drag) particle.vel = particle.vel.mult(get_params().drag)
particle.pos += particle.vel particle.pos += particle.vel
particle.draw(mut ctx) particle.draw(mut ctx)

View File

@ -16,13 +16,14 @@ pub mut:
} }
pub fn (rocket Rocket) draw(mut ctx gg.Context) { pub fn (rocket Rocket) draw(mut ctx gg.Context) {
ctx.draw_circle(rocket.pos.x, height - rocket.pos.y, rocket_radius, rocket.color) ctx.draw_circle(rocket.pos.x, get_params().height - rocket.pos.y, get_params().rocket_radius,
rocket.color)
} }
pub fn (mut rocket Rocket) explode() { pub fn (mut rocket Rocket) explode() {
rocket.exploded = true rocket.exploded = true
for _ in 0 .. offspring_count { for _ in 0 .. get_params().offspring_count {
rocket.spawn_particle() rocket.spawn_particle()
} }
} }
@ -33,7 +34,7 @@ pub fn (mut rocket Rocket) tick(mut ctx gg.Context) {
rocket.explode() rocket.explode()
} }
rocket.accel += gravity rocket.accel += get_params().gravity
rocket.vel += rocket.accel rocket.vel += rocket.accel
rocket.pos += rocket.vel rocket.pos += rocket.vel
rocket.draw(mut ctx) rocket.draw(mut ctx)
@ -50,7 +51,7 @@ pub fn new_rocket() Rocket {
return Rocket{ return Rocket{
color: random_color() color: random_color()
pos: { pos: {
x: rand.f32_in_range(50, width - 50) x: rand.f32_in_range(50, get_params().width - 50)
} }
vel: { vel: {
x: rand.f32_in_range(-1.5, 1.5) x: rand.f32_in_range(-1.5, 1.5)