@@ -59,68 +59,69 @@ static const Vertex vertexes[] = {
5959 { -0.5f , -0.5f , -0.5f , 0 , 1 , 0 }, { 0.5f , -0.5f , -0.5f , 0 , 0 , 1 }, { 0.5f , -0.5f , 0.5f , 1 , 0 , 1 },
6060};
6161
62+ /**
63+ * @brief SDL application state passed via pointer to callbacks.
64+ */
6265typedef struct {
6366 SDL_Window * window ;
6467 RenderDevice * renderDevice ;
6568 Framebuffer * framebuffer ;
6669 SDL_GPUBuffer * vertexBuffer ;
6770 SDL_GPUGraphicsPipeline * pipeline ;
6871 vec2 angles ;
69- Uint64 lastTicks ;
70- } App ;
72+ Uint64 ticks ;
73+ } AppState ;
7174
72- static App app ;
75+ static AppState app ;
7376
77+ /**
78+ * @brief SDL3 application initialization callback.
79+ */
7480SDL_AppResult SDL_AppInit (void * * unused , int argc , char * argv []) {
75- (void ) unused ; (void ) argc ; (void ) argv ;
76-
77- if (!SDL_Init (SDL_INIT_VIDEO )) {
78- SDL_LogCritical (SDL_LOG_CATEGORY_APPLICATION , "SDL_Init: %s" , SDL_GetError ());
79- return SDL_APP_FAILURE ;
80- }
8181
82- SDL_memset ( & app , 0 , sizeof ( app ) );
82+ GPU_Assert ( SDL_Init ( SDL_INIT_VIDEO ), "SDL_Init" );
8383
8484#ifdef EXAMPLES
8585 $$ (Resource , addResourcePath , EXAMPLES );
8686#endif
87+
8788 const char * basePath = SDL_GetBasePath ();
8889 if (basePath ) {
8990 $$ (Resource , addResourcePath , basePath );
9091 }
9192
92- app .window = SDL_CreateWindow ("Hello ObjectivelyGPU" ,
93- HELLO_WINDOW_W , HELLO_WINDOW_H , HELLO_WINDOW_FLAGS );
94- if (!app .window ) {
95- SDL_LogCritical (SDL_LOG_CATEGORY_APPLICATION , "SDL_CreateWindow: %s" , SDL_GetError ());
96- return SDL_APP_FAILURE ;
97- }
93+ SDL_Window * window = SDL_CreateWindow ("Hello ObjectivelyGPU" , HELLO_WINDOW_W , HELLO_WINDOW_H , HELLO_WINDOW_FLAGS );
94+ GPU_Assert (window , "SDL_CreateWindow" );
9895
99- app . renderDevice = $ (alloc (RenderDevice ), initWithWindow , app . window );
96+ RenderDevice * renderDevice = $ (alloc (RenderDevice ), initWithWindow , window );
10097
10198 int w = 0 , h = 0 ;
102- SDL_GetWindowSizeInPixels (app .window , & w , & h );
103- app .framebuffer = $ (alloc (Framebuffer ), initWithDevice , app .renderDevice ,
99+ SDL_GetWindowSizeInPixels (window , & w , & h );
100+
101+ Framebuffer * framebuffer = $ (alloc (Framebuffer ), initWithDevice , renderDevice ,
104102 & MakeSize (w , h ),
105103 SDL_GPU_TEXTUREFORMAT_INVALID ,
106104 SDL_GPU_TEXTUREFORMAT_D16_UNORM );
107105
108- SDL_GPUShader * vertexShader = $ (app . renderDevice , loadShader , "Hello.vert" , & (SDL_GPUShaderCreateInfo ) {
106+ SDL_GPUShader * vertexShader = $ (renderDevice , loadShader , "Hello.vert" , & (SDL_GPUShaderCreateInfo ) {
109107 .stage = SDL_GPU_SHADERSTAGE_VERTEX ,
110108 .num_uniform_buffers = 1 ,
111109 });
112- SDL_GPUShader * fragmentShader = $ (app .renderDevice , loadShader , "Hello.frag" , & (SDL_GPUShaderCreateInfo ) {
110+
111+ SDL_GPUShader * fragmentShader = $ (renderDevice , loadShader , "Hello.frag" , & (SDL_GPUShaderCreateInfo ) {
113112 .stage = SDL_GPU_SHADERSTAGE_FRAGMENT ,
114113 });
115114
116115 SDL_GPUColorTargetDescription colorTargetDescription = {
117- .format = $ (app . renderDevice , getSwapchainTextureFormat , app . window ),
116+ .format = $ (renderDevice , getSwapchainTextureFormat , window ),
118117 };
118+
119119 SDL_GPUVertexBufferDescription vertexBufferDescription = {
120120 .slot = 0 ,
121121 .pitch = sizeof (Vertex ),
122122 .input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX ,
123123 };
124+
124125 SDL_GPUVertexAttribute vertexAttributes [] = {
125126 {
126127 .location = 0 ,
@@ -136,7 +137,7 @@ SDL_AppResult SDL_AppInit(void **unused, int argc, char *argv[]) {
136137 },
137138 };
138139
139- app . pipeline = $ (app . renderDevice , createGraphicsPipeline , & (SDL_GPUGraphicsPipelineCreateInfo ) {
140+ SDL_GPUGraphicsPipeline * pipeline = $ (renderDevice , createGraphicsPipeline , & (SDL_GPUGraphicsPipelineCreateInfo ) {
140141 .vertex_shader = vertexShader ,
141142 .fragment_shader = fragmentShader ,
142143 .vertex_input_state = {
@@ -168,45 +169,49 @@ SDL_AppResult SDL_AppInit(void **unused, int argc, char *argv[]) {
168169 },
169170 });
170171
171- $ (app . renderDevice , releaseShader , vertexShader );
172- $ (app . renderDevice , releaseShader , fragmentShader );
172+ $ (renderDevice , releaseShader , vertexShader );
173+ $ (renderDevice , releaseShader , fragmentShader );
173174
174- app .vertexBuffer = $ (app .renderDevice , createBufferWithConstMem ,
175- SDL_GPU_BUFFERUSAGE_VERTEX , vertexes , sizeof (vertexes ));
175+ SDL_GPUBuffer * vertexBuffer = $ (renderDevice , createBufferWithConstMem , SDL_GPU_BUFFERUSAGE_VERTEX , vertexes , sizeof (vertexes ));
176+
177+ app = (AppState ) {
178+ .window = window ,
179+ .renderDevice = renderDevice ,
180+ .framebuffer = framebuffer ,
181+ .vertexBuffer = vertexBuffer ,
182+ .pipeline = pipeline ,
183+ };
176184
177- app .lastTicks = SDL_GetTicks ();
178185 return SDL_APP_CONTINUE ;
179186}
180187
188+ /**
189+ * @brief SDL3 frame function callback.
190+ */
181191SDL_AppResult SDL_AppIterate (void * unused ) {
182- (void ) unused ;
183192
184193 Uint64 ticks = SDL_GetTicks ();
185- float dt = (float ) (ticks - app .lastTicks ) / 1000.0f ;
186- app .lastTicks = ticks ;
194+ float dt = (float ) (ticks - app .ticks ) / 1000.0f ;
195+ app .ticks = ticks ;
187196
188197 app .angles .x += dt * 30.0f ;
189198 app .angles .y += dt * 60.0f ;
199+
190200 while (app .angles .x >= 360.0f ) app .angles .x -= 360.0f ;
191201 while (app .angles .y >= 360.0f ) app .angles .y -= 360.0f ;
192202
193203 CommandBuffer * cmd = $ (app .renderDevice , acquireCommandBuffer );
194204
195205 SwapchainTexture swapchain = { 0 };
196- if (!$ (cmd , acquireSwapchainTexture , & swapchain )) {
197- $ (cmd , cancel );
198- release (cmd );
199- return SDL_APP_CONTINUE ;
200- }
206+ $ (cmd , waitAndAcquireSwapchainTexture , & swapchain );
201207
202208 $ (app .framebuffer , resize , & swapchain .size );
203209
204210 mat4 modelView = mat4_rotation (app .angles .x , vec3_new (1.f , 0.f , 0.f ));
205211 modelView = mat4_mul (mat4_rotation (app .angles .y , vec3_new (0.f , 1.f , 0.f )), modelView );
206212 modelView = mat4_mul (mat4_translation (vec3_new (0.f , 0.f , -2.5f )), modelView );
207213
208- const mat4 projection = mat4_perspective (45.f ,
209- (float ) swapchain .size .w / (float ) swapchain .size .h , 0.01f , 100.f );
214+ const mat4 projection = mat4_perspective (45.f , (float ) swapchain .size .w / (float ) swapchain .size .h , 0.01f , 100.f );
210215 const mat4 modelViewProjection = mat4_mul (projection , modelView );
211216
212217 SDL_GPUColorTargetInfo colorTarget = {
@@ -215,8 +220,8 @@ SDL_AppResult SDL_AppIterate(void *unused) {
215220 .load_op = SDL_GPU_LOADOP_CLEAR ,
216221 .store_op = SDL_GPU_STOREOP_STORE ,
217222 };
218- SDL_GPUDepthStencilTargetInfo depthTarget = $ ( app . framebuffer , depthTargetInfo ,
219- SDL_GPU_LOADOP_CLEAR , SDL_GPU_STOREOP_DONT_CARE , 1.f );
223+
224+ SDL_GPUDepthStencilTargetInfo depthTarget = $ ( app . framebuffer , depthTargetInfo , SDL_GPU_LOADOP_CLEAR , SDL_GPU_STOREOP_DONT_CARE , 1.f );
220225
221226 RenderPass * renderPass = $ (cmd , beginRenderPass , & colorTarget , 1 , & depthTarget );
222227 $ (renderPass , bindPipeline , app .pipeline );
@@ -233,8 +238,11 @@ SDL_AppResult SDL_AppIterate(void *unused) {
233238 return SDL_APP_CONTINUE ;
234239}
235240
241+ /**
242+ * @brief SDL3 event callback.
243+ */
236244SDL_AppResult SDL_AppEvent (void * unused , SDL_Event * event ) {
237- ( void ) unused ;
245+
238246 switch (event -> type ) {
239247 case SDL_EVENT_QUIT :
240248 case SDL_EVENT_WINDOW_CLOSE_REQUESTED :
@@ -244,26 +252,24 @@ SDL_AppResult SDL_AppEvent(void *unused, SDL_Event *event) {
244252 }
245253}
246254
255+ /**
256+ * @brief SDL3 quit callback.
257+ */
247258void SDL_AppQuit (void * unused , SDL_AppResult result ) {
248- (void ) unused ; (void ) result ;
249259
250260 if (app .renderDevice ) {
251261 $ (app .renderDevice , waitForIdle );
262+
263+ if (app .pipeline ) {
264+ $ (app .renderDevice , releaseGraphicsPipeline , app .pipeline );
265+ }
266+ if (app .vertexBuffer ) {
267+ $ (app .renderDevice , releaseBuffer , app .vertexBuffer );
268+ }
252269 }
253- if (app .pipeline ) {
254- $ (app .renderDevice , releaseGraphicsPipeline , app .pipeline );
255- }
256- if (app .vertexBuffer ) {
257- $ (app .renderDevice , releaseBuffer , app .vertexBuffer );
258- }
259- if (app .framebuffer ) {
260- release (app .framebuffer );
261- }
262- if (app .renderDevice ) {
263- release (app .renderDevice );
264- }
265- if (app .window ) {
266- SDL_DestroyWindow (app .window );
267- }
270+
271+ release (app .framebuffer );
272+ release (app .renderDevice );
273+
268274 SDL_Quit ();
269275}
0 commit comments