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:
parent
06b660666b
commit
1f4e8254b9
@ -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
|
||||
)
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user