SBgl 0.1.0
A graphics framework in C99
Loading...
Searching...
No Matches
SBgl Examples

The examples/ directory contains several applications demonstrating various features of the library.


Getting Started

Building the Examples

Examples are integrated into the main CMake build system. From the project root:

mkdir build
cd build
cmake ..
make

Running an Example

After building, binaries are located in the build/examples/ directory. For example:

./build/examples/hello_window

Core Examples

  • window/hello_window.c: Demonstrates the basic initialization of the SBgl context, window creation, and the main event loop.
  • triangle/triangle_main.c: Shows how to load shaders, create vertex buffers, and draw a basic static or interactive triangle.
  • camera/camera_main.c: A complete 3D example demonstrating the perspective camera, 3D transformations, depth buffering, and batch ray-casting collision math.
  • input/input_keyboard.c: Showcases the Data-Oriented input system, polling for key presses and held states.
  • input/input_mouse.c: Demonstrates mouse coordinate tracking and color mapping.

Batching & GPU Optimization

  • batching/batch_main.c: Demonstrates high-performance batching by rendering 10,000 instances using Multi-Draw Indirect (MDI) and Buffer Device Address (BDA).
  • voxels/voxel_main.c: Showcases "Pure Procedural" rendering. Generates an infinite 2.5D voxel world entirely on the GPU using gl_VertexIndex, achieving zero-bandwidth geometry submission.

Basic Example Pattern

Most SBgl applications follow this standard initialization and main loop pattern:

#include "sbgl.h"
#include <stdio.h>
int main() {
// Initialize the engine and create a window
sbgl_InitResult res = sbgl_Init(800, 600, "SBgl Example");
if (res.error != SBGL_SUCCESS) {
fprintf(stderr, "Failed to initialize SBgl\n");
return 1;
}
sbgl_Context* ctx = res.ctx;
// Main Execution Loop
while (!sbgl_WindowShouldClose(ctx)) {
// Prepare for drawing
// Clear screen with a color (RGBA)
sbgl_SetClearColor(ctx, 0.1f, 0.1f, 0.1f, 1.0f); // Sets color for next frame
// --- Custom Drawing Logic Goes Here ---
// Present to screen
}
// Graceful Shutdown
return 0;
}
int main(void)
Definition batch_main.c:14
API for the SiputBiru Graphics Library (SBgl).
void sbgl_SetClearColor(sbgl_Context *ctx, float r, float g, float b, float a)
Sets the clear color for the next frame.
Definition sbgl_core.c:401
sbgl_InitResult sbgl_Init(int w, int h, const char *title)
Initializes the engine and opens a window.
Definition sbgl_core.c:193
bool sbgl_WindowShouldClose(sbgl_Context *ctx)
Checks if the user or OS has requested to close the window.
Definition sbgl_core.c:238
void sbgl_EndDrawing(sbgl_Context *ctx)
Finalizes the current frame and presents it to the screen.
Definition sbgl_core.c:315
void sbgl_DeviceWaitIdle(sbgl_Context *ctx)
Synchronizes the CPU with the GPU, waiting for all commands to complete.
Definition sbgl_core.c:391
void sbgl_Shutdown(sbgl_Context *ctx)
Gracefully shuts down the engine and releases all resources.
Definition sbgl_core.c:204
void sbgl_BeginDrawing(sbgl_Context *ctx)
Prepares the engine for a new frame of drawing.
Definition sbgl_core.c:262
@ SBGL_SUCCESS
Definition sbgl_types.h:215
Engine context.
Definition sbgl_types.h:268
Result structure for initialization.
Definition sbgl_types.h:339
sbgl_Context * ctx
Definition sbgl_types.h:340
sbgl_Result error
Definition sbgl_types.h:341

Advanced: Interactive Shaders

To pass dynamic data to shaders without the overhead of uniform buffers, SBgl utilizes Push Constants.

C Code:

float time = get_current_time();
sbgl_PushConstants(ctx, sizeof(float), &time);
void sbgl_PushConstants(sbgl_Context *ctx, size_t size, const void *data)
Updates push constants for the currently bound pipeline.
Definition sbgl_core.c:733

GLSL Vertex Shader:

layout(push_constant) uniform Constants {
float time;
} push;
void main() {
// Use push.time to animate vertices
}