-
-
Save spytheman/b265ffa232aece10d676ae9897a6f6d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Build this example with | |
| // v -live bounce.v | |
| module main | |
| import gx | |
| import gg | |
| import time | |
| import sokol.gfx | |
| struct Game { | |
| mut: | |
| gg &gg.Context | |
| x int | |
| y int | |
| dy int | |
| dx int | |
| height int | |
| width int | |
| draw_fn voidptr | |
| bground gg.Image | |
| } | |
| const ( | |
| window_width = 400 | |
| window_height = 300 | |
| width = 50 | |
| ) | |
| fn main() { | |
| mut game := &Game{ | |
| gg: 0 | |
| 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 | |
| use_ortho: true | |
| user_data: game | |
| window_title: 'Hot code reloading demo' | |
| create_window: true | |
| frame_fn: frame | |
| bg_color: gx.white | |
| init_fn: init_fn | |
| }) | |
| // window.onkeydown(key_down) | |
| println('Starting the game loop...') | |
| go game.run() | |
| game.gg.run() | |
| } | |
| struct Mat32 { pub: data [6]f32 } | |
| fn layout_desc_make_default() C.sg_layout_desc { | |
| mut layout := C.sg_layout_desc {} | |
| layout.attrs[0] = C.sg_vertex_attr_desc{ | |
| format: .float2 // position | |
| } | |
| layout.attrs[1] = C.sg_vertex_attr_desc{ | |
| format: .float2 // tex coords | |
| } | |
| layout.attrs[2] = C.sg_vertex_attr_desc{ | |
| format: .ubyte4n // color | |
| } | |
| return layout | |
| } | |
| fn init_fn (mut game Game) { | |
| game.bground = gg.create_image2('/home/delian/.vmodules/ui/examples/logo.png') | |
| mut shader_desc := C.sg_shader_desc{ | |
| label: &byte(0) | |
| } | |
| shader_desc.set_frag_image(0, 'MainTex') | |
| shader_desc.set_vert_src(' | |
| void main() { | |
| gl_FrontColor = gl_FrontMaterial.diffuse; | |
| gl_Position = ftransform(); | |
| } | |
| ') | |
| shader_desc.set_frag_src(' | |
| void main() { | |
| gl_FragColor = gl_Color; | |
| } | |
| ') | |
| shader := gfx.make_shader(&shader_desc) | |
| pipdesc := C.sg_pipeline_desc{ | |
| layout: layout_desc_make_default() | |
| index_type: .uint16 | |
| label: &byte(0) | |
| shader: shader | |
| blend: C.sg_blend_state { | |
| enabled: true | |
| src_factor_rgb: .src_alpha | |
| dst_factor_rgb: .one_minus_src_alpha | |
| src_factor_alpha: .one | |
| dst_factor_alpha: .one_minus_src_alpha | |
| } | |
| } | |
| pip := C.sg_make_pipeline(&pipdesc) | |
| C.sg_apply_pipeline(pip) | |
| } | |
| // Try uncommenting or changing the lines inside the live functions. | |
| // Guess what will happen: | |
| [live] | |
| fn frame (mut game Game) { | |
| game.gg.begin() | |
| C.sgl_enable_texture() | |
| C.sgl_texture(game.bground.sokol_img) | |
| C.sgl_begin_quads() | |
| // | |
| awidth := 200 aheight := 200 | |
| dx := 0 dy := 0 dw := 200 dh := 200 | |
| sx := 0 sy := 0 sw := 200 sh := 200 | |
| u0 := f32(sx) / f32(awidth) | |
| v0 := f32(sy) / f32(aheight) | |
| u1 := f32(sx + sw) / f32(awidth) | |
| v1 := f32(sy + sh) / f32(aheight) | |
| x0 := f32(dx) | |
| y0 := f32(dy) | |
| x1 := f32(dx + dw) | |
| y1 := f32(dy + dh) | |
| // | |
| C.sgl_v2f_t2f_c4f(x0, y0, u0, v0, 1.0, 1.0, 1.0, 1.0) | |
| C.sgl_v2f_t2f_c4f(x1, y0, u1, v0, 0.0, 0.0, 1.0, 0.5) | |
| C.sgl_v2f_t2f_c4f(x1, y1, u1, v1, 0.0, 0.0, 1.0, 1.0) | |
| C.sgl_v2f_t2f_c4f(x0, y1, u0, v1, 0.0, 1.0, 1.0, 1.0) | |
| C.sgl_end() | |
| C.sgl_disable_texture() | |
| game.gg.draw_rect(game.x, game.y, width, width, gx.blue) | |
| game.gg.draw_rect(window_width - width - game.x + 10, 200 - game.y + width, width, width, gx.rgb(228, 10, 55)) | |
| game.gg.draw_rect(game.x - 25, 250 - game.y, width, width, gx.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() | |
| //glfw.post_empty_event() // Refresh | |
| time.sleep_ms(17) // 60fps | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment