git.haldean.org qb / main launch.c
main

Tree @main (Download .tar.gz)

launch.c @mainraw · history · blame

//------------------------------------------------------------------------------
//  Simple C99 cimgui+sokol starter project for Win32, Linux and macOS.
//------------------------------------------------------------------------------
#include "sokol_app.h"
#include "sokol_gfx.h"
#include "sokol_time.h"
#include "sokol_glue.h"
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include "cimgui.h"
#include "sokol_imgui.h"

#include "qbshim.hpp"

static struct {
  uint64_t laptime;
  sg_pass_action pass_action;
  qb_context_ptr context;
} state;

static void init(void) {
  sg_setup(&(sg_desc){
      .context = sapp_sgcontext(),
    });
  stm_setup();
  simgui_setup(&(simgui_desc_t){
    .max_vertices = 524288,
  });

  // initial clear color
  state.pass_action = (sg_pass_action) {
    .colors[0] = { .action = SG_ACTION_CLEAR, .value = { 0.04f, 0.02f, 0.045f, 1.0 } }
  };
  state.context = qb_init();
}

static void frame(void) {
  const int width = sapp_width();
  const int height = sapp_height();
  const double delta_time = stm_sec(stm_round_to_common_refresh_rate(stm_laptime(&state.laptime)));
  simgui_new_frame(width, height, delta_time);

  qb_frame(state.context, sapp_width(), sapp_height());

  sg_begin_default_pass(&state.pass_action, width, height);
  simgui_render();
  sg_end_pass();
  sg_commit();
}

static void cleanup(void) {
  simgui_shutdown();
  sg_shutdown();
  qb_shutdown(state.context);
}

static void event(const sapp_event* ev) {
  simgui_handle_event(ev);
}

sapp_desc sokol_main(int argc, char* argv[]) {
  (void)argc;
  (void)argv;
  return (sapp_desc){
    .init_cb = init,
      .frame_cb = frame,
      .cleanup_cb = cleanup,
      .event_cb = event,
      .window_title = "qb",
      .width = 800,
      .height = 600,
      };
}