From 1f4e8254b9f20b025ce9d1682cbea773ecdb2181 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 26 Jan 2021 23:40:30 +0200 Subject: [PATCH] examples: enable maximization --- examples/fireworks/fireworks.v | 31 ++++++++++++++++--- .../fireworks/modules/objects/constants.v | 28 +++++++++++------ examples/fireworks/modules/objects/particle.v | 9 +++--- examples/fireworks/modules/objects/rocket.v | 9 +++--- 4 files changed, 55 insertions(+), 22 deletions(-) diff --git a/examples/fireworks/fireworks.v b/examples/fireworks/fireworks.v index 8c3847283d..0d742a34b7 100644 --- a/examples/fireworks/fireworks.v +++ b/examples/fireworks/fireworks.v @@ -3,13 +3,15 @@ import objects import gg import gx import rand +import sokol.sapp struct App { mut: - gg &gg.Context = 0 + gg &gg.Context = 0 + ui &objects.UIParams = 0 rockets []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 :) } @@ -54,6 +56,25 @@ fn on_frame(mut app App) { 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() { mut font_path := os.resource_abs_path(os.join_path('../assets/fonts/', 'RobotoMono-Regular.ttf')) $if android { @@ -61,15 +82,17 @@ fn main() { } mut app := &App{} + app.ui = objects.get_params() app.gg = gg.new_context( - width: objects.width - height: objects.height + width: app.ui.width + height: app.ui.height window_title: 'Fireworks!' bg_color: gx.black use_ortho: true user_data: app frame_fn: on_frame + event_fn: on_event font_path: font_path ) diff --git a/examples/fireworks/modules/objects/constants.v b/examples/fireworks/modules/objects/constants.v index 8160c57dea..a8e5abd8c0 100644 --- a/examples/fireworks/modules/objects/constants.v +++ b/examples/fireworks/modules/objects/constants.v @@ -1,12 +1,20 @@ module objects -const ( - width = 800 - height = 800 - gravity = Vector{0, -0.03} - age_rate = 1 - offspring_count = 100 - rocket_radius = 5 - particle_radius = 2.5 - drag = 0.98 -) +pub struct UIParams { +pub mut: + dpi_scale f32 = 1.0 + width int = 800 + height int = 800 + gravity Vector = Vector{0, -0.03} + age_rate int = 1 + offspring_count int = 100 + rocket_radius int = 5 + particle_radius f32 = 2.5 + drag f32 = 0.98 +} + +const params = &UIParams{} + +pub fn get_params() &UIParams { + return objects.params +} diff --git a/examples/fireworks/modules/objects/particle.v b/examples/fireworks/modules/objects/particle.v index 32fb82d8ad..cd4004d550 100644 --- a/examples/fireworks/modules/objects/particle.v +++ b/examples/fireworks/modules/objects/particle.v @@ -13,11 +13,12 @@ pub mut: } 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) { - particle.lifespan -= age_rate + particle.lifespan -= get_params().age_rate particle.color.a = byte(particle.lifespan) if particle.lifespan <= 0 { @@ -25,9 +26,9 @@ pub fn (mut particle Particle) tick(mut rocket Rocket, mut ctx gg.Context) { return } - particle.accel += gravity + particle.accel += get_params().gravity particle.vel += particle.accel - particle.vel = particle.vel.mult(drag) + particle.vel = particle.vel.mult(get_params().drag) particle.pos += particle.vel particle.draw(mut ctx) diff --git a/examples/fireworks/modules/objects/rocket.v b/examples/fireworks/modules/objects/rocket.v index 5257bc45d4..188c206822 100644 --- a/examples/fireworks/modules/objects/rocket.v +++ b/examples/fireworks/modules/objects/rocket.v @@ -16,13 +16,14 @@ pub mut: } 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() { rocket.exploded = true - for _ in 0 .. offspring_count { + for _ in 0 .. get_params().offspring_count { rocket.spawn_particle() } } @@ -33,7 +34,7 @@ pub fn (mut rocket Rocket) tick(mut ctx gg.Context) { rocket.explode() } - rocket.accel += gravity + rocket.accel += get_params().gravity rocket.vel += rocket.accel rocket.pos += rocket.vel rocket.draw(mut ctx) @@ -50,7 +51,7 @@ pub fn new_rocket() Rocket { return Rocket{ color: random_color() pos: { - x: rand.f32_in_range(50, width - 50) + x: rand.f32_in_range(50, get_params().width - 50) } vel: { x: rand.f32_in_range(-1.5, 1.5)