SBgl 0.1.0
A graphics framework in C99
Loading...
Searching...
No Matches
triangle_main.c
Go to the documentation of this file.
1#include <sbgl.h>
2#include <stdio.h>
3#include <time.h>
4#include "triangle_vert.h"
5#include "interactive_frag.h"
6
7typedef struct {
8 float mousePos[2];
9 float time;
10 float padding;
11} PushData;
12
13int main(void) {
14 sbgl_InitResult res = sbgl_Init(800, 600, "SBgl Unified Triangle Example");
15 if (res.error != SBGL_SUCCESS)
16 return 1;
17 sbgl_Context* ctx = res.ctx;
18
19 sbgl_Shader vert =
20 sbgl_LoadShader(ctx, SBGL_SHADER_STAGE_VERTEX, (const uint32_t*)triangle_vert_spv, triangle_vert_spv_len);
21 sbgl_Shader frag =
22 sbgl_LoadShader(ctx, SBGL_SHADER_STAGE_FRAGMENT, (const uint32_t*)interactive_frag_spv, interactive_frag_spv_len);
23
24 sbgl_Vertex vertices[] = {
25 { { 0, 16383, 0, 32767 }, 0xFF0000FF, 0 }, // 0: Top
26 { { -16383, -16383, 0, 32767 }, 0xFFFF0000, 0 }, // 1: Bottom-Left
27 { { 16383, -16383, 0, 32767 }, 0xFF00FF00, 0 } // 2: Bottom-Right
28 };
29
30 sbgl_Buffer vbo = sbgl_CreateBuffer(ctx, SBGL_BUFFER_USAGE_VERTEX, sizeof(vertices), vertices);
31
32 sbgl_VertexAttribute attributes[] = {
33 { 0, offsetof(sbgl_Vertex, position), SBGL_FORMAT_R16G16B16A16_SNORM },
34 { 1, offsetof(sbgl_Vertex, color), SBGL_FORMAT_R8G8B8A8_UNORM }
35 };
36
37 sbgl_PipelineConfig config = { .vertexShader = vert,
38 .fragmentShader = frag,
39 .vertexLayout = { sizeof(sbgl_Vertex), 2, attributes } };
40
41 sbgl_Pipeline pipeline = sbgl_CreatePipeline(ctx, &config);
42 double start_time = sbgl_GetTime(ctx);
43
44 printf("--- Triangle Controls ---\n");
45 printf("ESC: Exit\n");
46 printf("-------------------------\n");
47
48 while (!sbgl_WindowShouldClose(ctx)) {
49 const sbgl_InputState* input = sbgl_GetInputState(ctx);
50 if (input->keysDown[SBGL_KEY_ESCAPE]) break;
51
52 sbgl_Clear(ctx, 0.05f, 0.05f, 0.05f, 1.0f);
54
55 int width, height;
56 sbgl_GetWindowSize(ctx, &width, &height);
57
58 PushData push = {
59 .mousePos = { (float)input->mouseX / (float)width, (float)input->mouseY / (float)height },
60 .time = (float)(sbgl_GetTime(ctx) - start_time)
61 };
62
63 sbgl_BindPipeline(ctx, pipeline);
64 sbgl_PushConstants(ctx, sizeof(push), &push);
66 sbgl_Draw(ctx, 3, 0, 1);
67
68 sbgl_EndDrawing(ctx);
69 }
70
72
73 sbgl_DestroyPipeline(ctx, pipeline);
74 sbgl_DestroyShader(ctx, vert);
75 sbgl_DestroyShader(ctx, frag);
76 sbgl_DestroyBuffer(ctx, vbo);
77 sbgl_Shutdown(ctx);
78
79 return 0;
80}
API for the SiputBiru Graphics Library (SBgl).
sbgl_InitResult sbgl_Init(int w, int h, const char *title)
Initializes the engine and opens a window.
Definition sbgl_core.c:193
void sbgl_GetWindowSize(sbgl_Context *ctx, int *w, int *h)
Retrieves the current window dimensions.
Definition sbgl_core.c:252
bool sbgl_WindowShouldClose(sbgl_Context *ctx)
Checks if the user or OS has requested to close the window.
Definition sbgl_core.c:238
const sbgl_InputState * sbgl_GetInputState(sbgl_Context *ctx)
Retrieves the input state for the current frame.
Definition sbgl_core.c:413
sbgl_Pipeline sbgl_CreatePipeline(sbgl_Context *ctx, const sbgl_PipelineConfig *config)
Creates a graphics pipeline.
Definition sbgl_core.c:587
void sbgl_DestroyShader(sbgl_Context *ctx, sbgl_Shader shader)
Destroys a shader module.
Definition sbgl_core.c:565
sbgl_Shader sbgl_LoadShader(sbgl_Context *ctx, sbgl_ShaderStage stage, const uint32_t *bytecode, size_t size)
Loads a shader from SPIR-V bytecode.
Definition sbgl_core.c:523
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
void sbgl_EndDrawing(sbgl_Context *ctx)
Finalizes the current frame and presents it to the screen.
Definition sbgl_core.c:315
#define SBGL_KEY_ESCAPE
Definition sbgl.h:84
void sbgl_BindPipeline(sbgl_Context *ctx, sbgl_Pipeline pipeline)
Binds a graphics pipeline for subsequent draw calls.
Definition sbgl_core.c:673
#define sbgl_Clear
Backward compatibility alias for sbgl_SetClearColor.
Definition sbgl.h:229
void sbgl_DestroyBuffer(sbgl_Context *ctx, sbgl_Buffer buffer)
Destroys a GPU buffer.
Definition sbgl_core.c:457
void sbgl_DeviceWaitIdle(sbgl_Context *ctx)
Synchronizes the CPU with the GPU, waiting for all commands to complete.
Definition sbgl_core.c:391
double sbgl_GetTime(sbgl_Context *ctx)
Retrieves the current monotonic system time in seconds.
Definition sbgl_core.c:245
void sbgl_Draw(sbgl_Context *ctx, uint32_t vertexCount, uint32_t firstVertex, uint32_t instanceCount)
Submits a non-indexed draw command.
Definition sbgl_core.c:698
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
void sbgl_BindBuffer(sbgl_Context *ctx, sbgl_Buffer buffer, sbgl_BufferUsage usage)
Binds a buffer to the pipeline.
Definition sbgl_core.c:681
void sbgl_DestroyPipeline(sbgl_Context *ctx, sbgl_Pipeline pipeline)
Destroys a graphics pipeline.
Definition sbgl_core.c:597
sbgl_Buffer sbgl_CreateBuffer(sbgl_Context *ctx, sbgl_BufferUsage usage, size_t size, const void *data)
Creates a GPU buffer.
Definition sbgl_core.c:445
@ SBGL_BUFFER_USAGE_VERTEX
Definition sbgl_types.h:123
@ SBGL_SUCCESS
Definition sbgl_types.h:215
@ SBGL_FORMAT_R8G8B8A8_UNORM
Definition sbgl_types.h:160
@ SBGL_FORMAT_R16G16B16A16_SNORM
Definition sbgl_types.h:159
uint32_t sbgl_Buffer
Handle for a GPU-side buffer.
Definition sbgl_types.h:37
uint32_t sbgl_Shader
Handle for a shader module.
Definition sbgl_types.h:42
@ SBGL_SHADER_STAGE_FRAGMENT
Definition sbgl_types.h:135
@ SBGL_SHADER_STAGE_VERTEX
Definition sbgl_types.h:134
uint32_t sbgl_Pipeline
Handle for a graphics pipeline.
Definition sbgl_types.h:47
float time
float padding
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
Represents the real-time state of physical inputs.
Definition sbgl_input.h:165
bool keysDown[SBGL_SCANCODE_MAX]
Definition sbgl_input.h:166
Configuration for creating a graphics pipeline.
Definition sbgl_types.h:204
Vertex attribute definition.
Definition sbgl_types.h:166
Standard vertex structure for basic geometry rendering. Optimized for cache density (16 bytes).
Definition sbgl_types.h:28
int main(void)