Skip to content

Instantly share code, notes, and snippets.

@spytheman
Created September 16, 2022 23:55
Show Gist options
  • Save spytheman/ea65c7ece31f398923664b15ddee2ee7 to your computer and use it in GitHub Desktop.
Save spytheman/ea65c7ece31f398923664b15ddee2ee7 to your computer and use it in GitHub Desktop.
module main
#define SOKOL_IMPL
#define SOKOL_GLCORE33
#flag -I @VMODROOT
#flag -lX11 -lGL -lXcursor -lXi -lpthread
#flag -lm
#include "thirdparty/sokol_gfx.h"
#include "sokol_gp.h"
#include "thirdparty/sokol_app.h"
#include "thirdparty/sokol_glue.h"
pub struct C.sg_pass_action {}
pub struct C.sg_context_desc {}
pub struct C.sapp_desc {
pub:
init_cb fn () // these are the user-provided callbacks without user data
frame_cb fn ()
cleanup_cb fn ()
width int // the preferred width of the window / canvas
height int // the preferred height of the window / canvas
sample_count int // MSAA sample count
window_title &char // the window title as UTF-8 encoded string
}
pub struct C.sg_desc {
context C.sg_context_desc
}
pub struct C.sg_context_desc {}
pub struct C.sgp_desc {}
fn C.sapp_width() int
fn C.sapp_height() int
fn C.sapp_frame_count() u64
fn C.sapp_frame_duration() f64
fn C.sapp_sgcontext() C.sg_context_desc
fn C.sg_begin_default_pass(&C.sg_pass_action, int, int)
fn C.sg_end_pass()
fn C.sg_commit()
fn C.sg_shutdown()
fn C.sg_setup(&C.sg_desc)
fn C.sg_isvalid() bool
fn C.sgp_begin(int, int)
fn C.sgp_viewport(int, int, int, int)
fn C.sgp_project(f32, f32, f32, f32)
fn C.sgp_set_color(f32, f32, f32, f32)
fn C.sgp_clear()
fn C.sgp_rotate_at(f32, f32, f32)
fn C.sgp_draw_filled_rect(f32, f32, f32, f32)
fn C.sgp_flush()
fn C.sgp_end()
fn C.sgp_shutdown()
fn C.sgp_setup(&C.sgp_desc)
fn C.sgp_is_valid() bool
enum Sgp_error {
no_error = 0
}
fn C.sgp_get_error_message(Sgp_error) &char
fn C.sgp_get_last_error() Sgp_error
// a dummy main function, to keep V happy
fn main() {}
module main
import math
fn frame() {
width, height := C.sapp_width(), C.sapp_height()
ratio := width / f32(height)
C.sgp_begin(width, height)
C.sgp_viewport(0, 0, width, height)
C.sgp_project(-ratio, ratio, 1.0, -1.0)
C.sgp_set_color(0.1, 0.1, 0.1, 1.0)
C.sgp_clear()
time := f32(C.sapp_frame_count() * C.sapp_frame_duration())
r, g := math.sinf(time) * 0.5 + 0.5, math.cosf(time) * 0.5 + 0.5
C.sgp_set_color(r, g, 0.3, 1.0)
C.sgp_rotate_at(time, 0.0, 0.0)
C.sgp_draw_filled_rect(-0.5, -0.5, 1.0, 1.0)
pass_action := C.sg_pass_action{}
C.sg_begin_default_pass(&pass_action, width, height)
C.sgp_flush()
C.sgp_end()
C.sg_end_pass()
C.sg_commit()
}
fn init() {
sgdesc := C.sg_desc{
context: C.sapp_sgcontext()
}
C.sg_setup(&sgdesc)
if !C.sg_isvalid() {
eprintln('Failed to create Sokol GFX context!')
exit(-1)
}
sgpdesc := C.sgp_desc{}
C.sgp_setup(&sgpdesc)
if !C.sgp_is_valid() {
eprintln('Failed to create Sokol GP context: ' +
unsafe { cstring_to_vstring(C.sgp_get_error_message(C.sgp_get_last_error())) })
exit(-1)
}
}
fn cleanup() {
C.sgp_shutdown()
C.sg_shutdown()
}
[export: 'sokol_main']
fn sokol_main(argc int, argv &&char) C.sapp_desc {
return C.sapp_desc{
init_cb: init
frame_cb: frame
cleanup_cb: cleanup
window_title: c'Triangle (Sokol GP)'
sample_count: 4 // Enable anti aliasing.
}
}
@spytheman
Copy link
Author

Steps to run the ported example:

  1. Clone the repo with: git clone https://github.com/edubart/sokol_gp .
  2. Change your current directory to it: cd sokol_gp .
  3. Save the above 2 files in that same folder.
  4. Create an empty v.mod file there too: touch v.mod .
  5. Compile with v -cmain no_main run . .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment