### Hello Triangle with sokol_app.h and sokol_gfx.h Source: https://github.com/floooh/sokol/blob/master/README.md A basic 'Hello Triangle' example demonstrating initialization, rendering, and cleanup using sokol_app.h and sokol_gfx.h. This snippet requires sokol_glue.h for environment setup and a shader compiled by sokol-shdc. ```c #include "sokol_app.h" #include "sokol_gfx.h" #include "sokol_log.h" #include "sokol_glue.h" #include "triangle-sapp.glsl.h" static struct { sg_pipeline pip; sg_bindings bind; sg_pass_action pass_action; } state; static void init(void) { sg_setup(&(sg_desc){ .environment = sglue_environment(), .logger.func = slog_func, }); float vertices[] = { 0.0f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f }; state.bind.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){ .data = SG_RANGE(vertices), }); state.pip = sg_make_pipeline(&(sg_pipeline_desc){ .shader = sg_make_shader(triangle_shader_desc(sg_query_backend())), .layout = { .attrs = { [ATTR_triangle_position].format = SG_VERTEXFORMAT_FLOAT3, [ATTR_triangle_color0].format = SG_VERTEXFORMAT_FLOAT4 } }, }); state.pass_action = (sg_pass_action) { .colors[0] = { .load_action=SG_LOADACTION_CLEAR, .clear_value={0.0f, 0.0f, 0.0f, 1.0f } } }; } void frame(void) { sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = sglue_swapchain() }); sg_apply_pipeline(state.pip); sg_apply_bindings(&state.bind); sg_draw(0, 3, 1); sg_end_pass(); sg_commit(); } void cleanup(void) { sg_shutdown(); } sapp_desc sokol_main(int argc, char* argv[]) { (void)argc; (void)argv; return (sapp_desc){ .init_cb = init, .frame_cb = frame, .cleanup_cb = cleanup, .width = 640, .height = 480, .window_title = "Triangle", .icon.sokol_default = true, .logger.func = slog_func, }; } ``` -------------------------------- ### Copy Example Assets Source: https://github.com/floooh/sokol/blob/master/tests/ext/CMakeLists.txt Copies example assets for the 'spineboy' demo to the binary directory. Ensure the source paths are correct. ```cmake file(COPY ${spineruntimes_dir}/examples/spineboy/export/spineboy-pro.skel DESTINATION ${CMAKE_BINARY_DIR}/Debug) ``` ```cmake file(COPY ${spineruntimes_dir}/examples/spineboy/export/spineboy.atlas DESTINATION ${CMAKE_BINARY_DIR}/Debug) ``` ```cmake file(COPY ${spineruntimes_dir}/examples/spineboy/export/spineboy.png DESTINATION ${CMAKE_BINARY_DIR}/Debug) ``` -------------------------------- ### Install Tools on Windows with Scoop Source: https://github.com/floooh/sokol/blob/master/bindgen/README.md Install clang (via llvm) and python on Windows using the Scoop package manager. ```bash > scoop install llvm > scoop install python ``` -------------------------------- ### Check Tool Versions Source: https://github.com/floooh/sokol/blob/master/bindgen/README.md Verify that clang and python3 are installed and accessible in your system's PATH. ```bash > clang --version > python3 --version ``` -------------------------------- ### Copy Spineboy Example Assets Source: https://github.com/floooh/sokol/blob/master/tests/ext/CMakeLists.txt This CMake command copies essential asset files for the Spineboy example from the runtimes directory to the build directory and its Debug subfolder. ```cmake file(COPY ${spineruntimes_dir}/examples/spineboy/export/spineboy-pro.json DESTINATION ${CMAKE_BINARY_DIR}) file(COPY ${spineruntimes_dir}/examples/spineboy/export/spineboy-pro.skel DESTINATION ${CMAKE_BINARY_DIR}) file(COPY ${spineruntimes_dir}/examples/spineboy/export/spineboy.atlas DESTINATION ${CMAKE_BINARY_DIR}) file(COPY ${spineruntimes_dir}/examples/spineboy/export/spineboy.png DESTINATION ${CMAKE_BINARY_DIR}) file(COPY ${spineruntimes_dir}/examples/spineboy/export/spineboy-pro.json DESTINATION ${CMAKE_BINARY_DIR}/Debug) ``` -------------------------------- ### Time Measurement with Sokol Time Source: https://github.com/floooh/sokol/blob/master/README.md Measures time intervals using high-resolution timestamps. Use stm_now() to get current time and stm_since() or stm_diff() to calculate elapsed time. ```c #include "sokol_time.h" ... /* initialize sokol_time */ stm_setup(); /* take start timestamp */ uint64_t start = stm_now(); ...some code to measure... /* compute elapsed time */ uint64_t elapsed = stm_since(start); /* convert to time units */ double seconds = stm_sec(elapsed); double milliseconds = stm_ms(elapsed); double microseconds = stm_us(elapsed); double nanoseconds = stm_ns(elapsed); /* difference between 2 time stamps */ uint64_t start = stm_now(); ... uint64_t end = stm_now(); uint64_t elapsed = stm_diff(end, start); /* compute a 'lap time' (e.g. for fps) */ uint64_t last_time = 0; while (!done) { ...render something... double frame_time_ms = stm_ms(stm_laptime(&last_time)); } ``` -------------------------------- ### Build and Run Zig Samples Source: https://github.com/floooh/sokol/blob/master/bindgen/README.md Navigate to the Zig bindings directory and build/run sample applications. ```bash > cd sokol/bindgen/sokol-zig > zig build run-clear > zig build run-triangle > zig build run-cube ... ``` -------------------------------- ### Run Shader Generator with Deno Source: https://github.com/floooh/sokol/blob/master/shdgen/readme.txt Execute the shader generation script using Deno. Ensure Deno is in your PATH. This command allows all necessary permissions for the script to run. ```bash deno run --allow-all shdgen.ts ``` -------------------------------- ### CMake Project Configuration Source: https://github.com/floooh/sokol/blob/master/tests/CMakeLists.txt Sets up the CMake build environment for the Sokol project. Specifies the minimum required CMake version, project name, runtime output directory, and C/C++ standards. ```cmake cmake_minimum_required(VERSION 3.20) project(sokol-test) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 11) ``` -------------------------------- ### Generate All Sokol Bindings Source: https://github.com/floooh/sokol/blob/master/bindgen/README.md Execute the Python script to generate all Sokol language bindings. ```bash > python3 gen_all.py ``` -------------------------------- ### Clone Sokol Language Binding Repositories Source: https://github.com/floooh/sokol/blob/master/bindgen/README.md Clone the necessary repositories for various language bindings of Sokol. ```bash > cd sokol/bindgen > git clone https://github.com/floooh/sokol-zig > git clone https://github.com/floooh/sokol-nim > git clone https://github.com/floooh/sokol-odin > git clone https://github.com/floooh/sokol-rust > git clone https://github.com/floooh/sokol-d > git clone https://github.com/colinbellino/sokol-jai > git clone https://github.com/floooh/sokol-c3 ``` -------------------------------- ### Asynchronous File Loading with Sokol Fetch Source: https://github.com/floooh/sokol/blob/master/README.md Loads a file into a pre-allocated buffer asynchronously. Ensure sfetch_dowork() is called every frame and handle responses in the callback. ```c #include "sokol_fetch.h" #include "sokol_log.h" static void response_callback(const sfetch_response*); #define MAX_FILE_SIZE (1024*1024) static uint8_t buffer[MAX_FILE_SIZE]; // application init static void init(void) { ... // setup sokol-fetch with default config: sfetch_setup(&(sfetch_desc_t){ .logger.func = slog_func }); // start loading a file into a statically allocated buffer: sfetch_send(&(sfetch_request_t){ .path = "hello_world.txt", .callback = response_callback .buffer_ptr = buffer, .buffer_size = sizeof(buffer) }); } // per frame... static void frame(void) { ... // need to call sfetch_dowork() once per frame to 'turn the gears': sfetch_dowork(); ... } // the response callback is where the interesting stuff happens: static void response_callback(const sfetch_response_t* response) { if (response->fetched) { // data has been loaded into the provided buffer, do something // with the data... const void* data = response->buffer_ptr; uint64_t data_size = response->fetched_size; } // the finished flag is set both on success and failure if (response->failed) { // oops, something went wrong switch (response->error_code) { SFETCH_ERROR_FILE_NOT_FOUND: ... SFETCH_ERROR_BUFFER_TOO_SMALL: ... ... } } } // application shutdown static void shutdown(void) { ... sfetch_shutdown(); ... } ``` -------------------------------- ### Fetch dcimgui Dependency Source: https://github.com/floooh/sokol/blob/master/tests/ext/CMakeLists.txt This snippet checks if the dcimgui directory exists and fetches it using git clone if not. It's a workaround for slow FetchContent. ```cmake set(dcimgui_dir ${CMAKE_BINARY_DIR}/../_deps/dcimgui) if (IS_DIRECTORY ${dcimgui_dir}) message("### ${dcimgui_dir} exists...") else() message("### Fetching dcimgui to ${dcimgui_dir} (this may take a while...)") execute_process(COMMAND git clone --depth=1 --recursive https://github.com/floooh/dcimgui ${dcimgui_dir}) endif() ``` -------------------------------- ### Configure C Target Settings (CMake) Source: https://github.com/floooh/sokol/blob/master/tests/CMakeLists.txt Configures a C target by applying common settings and C-specific compile options. Includes platform-specific configurations for macOS and iOS. ```cmake macro(configure_c target) configure_common(${target}) target_compile_options(${target} PRIVATE ${c_flags}) if (OSX_MACOS OR OSX_IOS) target_compile_options(${target} PRIVATE -x objective-c) configure_osx_properties(${target}) endif() endmacro() ``` -------------------------------- ### Add ImGui Library Source: https://github.com/floooh/sokol/blob/master/tests/ext/CMakeLists.txt This CMake command adds the ImGui library, compiling source files from the fetched dcimgui directory and setting include directories. ```cmake add_library(imgui ${dcimgui_dir}/src/cimgui.cpp ${dcimgui_dir}/src/imgui.cpp ${dcimgui_dir}/src/imgui_demo.cpp ${dcimgui_dir}/src/imgui_draw.cpp ${dcimgui_dir}/src/imgui_tables.cpp ${dcimgui_dir}/src/imgui_widgets.cpp) target_include_directories(imgui SYSTEM PUBLIC ${dcimgui_dir}/src) ``` -------------------------------- ### Mono Square-Wave Generator (Push Model) Source: https://github.com/floooh/sokol/blob/master/README.md Implements a mono square-wave generator using sokol_audio.h with the push model. Audio samples are generated in the main loop and pushed to the audio buffer. ```c #define BUF_SIZE (32) int main() { // init sokol-audio with default params, no callback saudio_setup(&(saudio_desc){ .logger.func = slog_func, }); assert(saudio_channels() == 1); // a small intermediate buffer so we don't need to push // individual samples, which would be quite inefficient float buf[BUF_SIZE]; int buf_pos = 0; uint32_t count = 0; // push samples from main loop bool done = false; while (!done) { // generate and push audio samples... int num_frames = saudio_expect(); for (int i = 0; i < num_frames; i++) { // simple square wave generator buf[buf_pos++] = (count++ & (1<<3)) ? 0.5f : -0.5f; if (buf_pos == BUF_SIZE) { buf_pos = 0; saudio_push(buf, BUF_SIZE); } } // handle other per-frame stuff... ... } // shutdown sokol-audio saudio_shutdown(); return 0; } ``` -------------------------------- ### Configure Common Target Settings (CMake) Source: https://github.com/floooh/sokol/blob/master/tests/CMakeLists.txt Applies common compile definitions, link options, libraries, and include directories to a target. Handles EGL forcing and backend-specific definitions. ```cmake macro(configure_common target) if (SOKOL_FORCE_EGL) target_compile_definitions(${target} PRIVATE SOKOL_FORCE_EGL) endif() target_compile_definitions(${target} PRIVATE ${SOKOL_BACKEND}) target_link_options(${target} PRIVATE ${link_flags}) target_link_libraries(${target} PRIVATE ${system_libs}) target_include_directories(${target} PRIVATE ../.. ../../util) target_include_directories(${target} PRIVATE ../ext) if (WINDOWS AND SOKOL_BACKEND STREQUAL SOKOL_VULKAN) target_include_directories(${target} PUBLIC $ENV{VULKAN_SDK}/Include) target_link_directories(${target} PUBLIC $ENV{VULKAN_SDK}/Lib) endif() endmacro() ``` -------------------------------- ### Argument Parsing with Sokol Args Source: https://github.com/floooh/sokol/blob/master/README.md Parses command-line arguments (native) or URL query parameters (web). Use sargs_exists() and sargs_equals() to check and compare argument values. ```c #include "sokol_args.h" int main(int argc, char* argv[]) { sargs_setup(&(sargs_desc){ .argc=argc, .argv=argv }); if (sargs_exists("type")) { if (sargs_equals("type", "kc85_4")) { // start as KC85/4 } else if (sargs_equals("type", "kc85_3")) { // start as KC85/3 } else { // start as KC85/2 } } sargs_shutdown(); return 0; } ``` -------------------------------- ### Run Shader Generator from a Feature Branch Source: https://github.com/floooh/sokol/blob/master/shdgen/readme.txt Execute the shader generation script using Deno, specifying a feature branch. This is useful for testing changes from a specific branch before merging. ```bash deno run --allow-all shdgen.ts --branch [name] ``` -------------------------------- ### Configure C++ Target Settings (CMake) Source: https://github.com/floooh/sokol/blob/master/tests/CMakeLists.txt Configures a C++ target by applying common settings and C++-specific compile options. Includes platform-specific configurations for macOS and iOS. ```cmake macro(configure_cxx target) configure_common(${target}) target_compile_options(${target} PRIVATE ${cxx_flags}) if (OSX_MACOS OR OSX_IOS) target_compile_options(${target} PRIVATE -x objective-c++) configure_osx_properties(${target}) endif() endmacro() ``` -------------------------------- ### Mono Square-Wave Generator (Callback Model) Source: https://github.com/floooh/sokol/blob/master/README.md Implements a mono square-wave generator using sokol_audio.h with the callback model. The `stream_cb` function runs in the audio thread to provide audio samples. ```c // the sample callback, running in audio thread static void stream_cb(float* buffer, int num_frames, int num_channels) { assert(1 == num_channels); static uint32_t count = 0; for (int i = 0; i < num_frames; i++) { buffer[i] = (count++ & (1<<3)) ? 0.5f : -0.5f; } } int main() { // init sokol-audio with default params saudio_setup(&(saudio_desc){ .stream_cb = stream_cb, .logger.func = slog_func, }); // run main loop ... // shutdown sokol-audio saudio_shutdown(); return 0; } ``` -------------------------------- ### Add Subdirectories (CMake) Source: https://github.com/floooh/sokol/blob/master/tests/CMakeLists.txt Includes external, compile, and functional subdirectories into the build system. ```cmake add_subdirectory(ext) add_subdirectory(compile) add_subdirectory(functional) ``` -------------------------------- ### Fetch Spine Runtimes Dependency Source: https://github.com/floooh/sokol/blob/master/tests/ext/CMakeLists.txt This snippet checks if the spineruntimes directory exists and fetches it using git clone if not. It specifies a branch for the spine-runtimes. ```cmake set(spineruntimes_dir ${CMAKE_BINARY_DIR}/../_deps/spineruntimes) if (IS_DIRECTORY ${spineruntimes_dir}) message("### ${spineruntimes_dir} exists...") else() message("### Fetching spine runtimes to ${spineruntimes_dir} (this may take a while...)") execute_process(COMMAND git clone --depth=1 --branch 4.2 --recursive https://github.com/EsotericSoftware/spine-runtimes ${spineruntimes_dir}) endif() ``` -------------------------------- ### Add Spine Library with Compiler Options Source: https://github.com/floooh/sokol/blob/master/tests/ext/CMakeLists.txt This CMake command adds the Spine library, compiling source files from the fetched spine-runtimes directory. It also configures specific compile options for MSVC and Clang compilers. ```cmake add_library(spine ${spineruntimes_dir}/spine-c/spine-c/src/spine/Animation.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/AnimationState.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/AnimationStateData.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/Array.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/Atlas.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/AtlasAttachmentLoader.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/Attachment.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/AttachmentLoader.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/Bone.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/BoneData.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/BoundingBoxAttachment.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/ClippingAttachment.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/Color.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/Debug.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/Event.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/EventData.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/IkConstraint.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/IkConstraintData.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/Json.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/Json.h ${spineruntimes_dir}/spine-c/spine-c/src/spine/MeshAttachment.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/PathAttachment.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/PathConstraint.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/PathConstraintData.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/PhysicsConstraint.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/PhysicsConstraintData.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/PointAttachment.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/RegionAttachment.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/Sequence.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/Skeleton.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/SkeletonBinary.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/SkeletonBounds.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/SkeletonClipping.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/SkeletonData.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/SkeletonJson.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/Skin.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/Slot.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/SlotData.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/TransformConstraint.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/TransformConstraintData.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/Triangulator.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/VertexAttachment.c ${spineruntimes_dir}/spine-c/spine-c/src/spine/extension.c) target_include_directories(spine SYSTEM PUBLIC ${spineruntimes_dir}/spine-c/spine-c/include) if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC") target_compile_options(spine PRIVATE /wd4267 /wd4244) # conversion from 'x' to 'y' possible loss of data endif() if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") target_compile_options(spine PRIVATE -Wno-shorten-64-to-32 -Wno-implicit-const-int-float-conversion) endif() ``` -------------------------------- ### Define Sokol Test Executable in CMake Source: https://github.com/floooh/sokol/blob/master/tests/functional/CMakeLists.txt Defines the test executable and links necessary libraries. This is conditional on not being on Android. ```cmake set(c_sources sokol_log_test.c sokol_args_test.c sokol_audio_test.c sokol_debugtext_test.c sokol_fetch_test.c sokol_gfx_test.c sokol_gl_test.c sokol_shape_test.c sokol_color_test.c sokol_spine_test.c sokol_test.c ) add_executable(sokol-test ${c_sources}) target_link_libraries(sokol-test PUBLIC spine) configure_c(sokol-test) ``` -------------------------------- ### Copy Assets in CMake Source: https://github.com/floooh/sokol/blob/master/tests/functional/CMakeLists.txt Copies assets to the binary directory. This is conditional on not being on Android. ```cmake if (NOT ANDROID) file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/assets/comsi.s3m DESTINATION ${CMAKE_BINARY_DIR}) file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/assets/comsi.s3m DESTINATION ${CMAKE_BINARY_DIR}/Debug) endif() ``` -------------------------------- ### Configure macOS/iOS Target Properties (CMake) Source: https://github.com/floooh/sokol/blob/master/tests/CMakeLists.txt Sets Xcode-specific build properties for macOS and iOS targets, including bundle identifiers and names. Also defines GLES_SILENCE_DEPRECATION for iOS. ```cmake macro(configure_osx_properties target) if (OSX_IOS) target_compile_definitions(${target} PRIVATE GLES_SILENCE_DEPRECATION) endif() set_target_properties(${target} PROPERTIES XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "${target}") set_target_properties(${target} PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER "${target}") set_target_properties(${target} PROPERTIES MACOSX_BUNDLE_PRODUCT_NAME "${target}") set_target_properties(${target} PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "${target}") endmacro() ``` -------------------------------- ### Add Nuklear Library Source: https://github.com/floooh/sokol/blob/master/tests/ext/CMakeLists.txt Adds the Nuklear library as a CMake library. Includes a conditional compile option for MSVC to suppress warning C5287. ```cmake add_library(nuklear nuklear.c) ``` ```cmake if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC") # NOTE: must propagate to upstream includers target_compile_options(nuklear PUBLIC /wd5287) endif() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.