If I account for every single possible errors when using SDL functions, the code needed to make a minimal window will look something like this:
#include <SDl2/SDL.h>
#include <stdbool.h>
int main(int argc, char *argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
// Error handling here
}
SDL_Window *window = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 500, 300, 0);
if (window == NULL) {
// Error handling here
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == NULL) {
// Error handling here
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // This can also fail, so should we handle it just in case?
bool active = true;
SDL_Event event;
Uint8 r, g, b, a;
while (active) {
while (SDL_PollEvent(&event) > 0) {
switch (event.type) {
case SDL_QUIT:
active = false;
break;
}
}
if (SDL_RenderClear(renderer) < 0) {
// More error handling
if (SDL_RenderPresent(renderer) < 0) {
// More error handling
}
}
// And also for these...
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
I don’t want to do the tedious act of writing the error handling part over and over again. Should I handle every error that can happen just in case? Thanks.