kisskit: Simplifying Game Dev in C
Tags: programming, c, sdl, gamedev
Published: 2022-04-08 09:06:07

I grew up programming in BASIC in the 1980s, and enjoyed a very simple API for graphics and sound. For example, this draws a line and a circle:

```BASIC
MOVE 100, 100
DRAW 300, 200
CIRCLE 300, 200, 50
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ef7hrid9ulk78365m9r.png)

To do the same in modern JavaScript with HTML in the browser, without any helper functions, we would have to write:

```HTML
<canvas id="myCanvas" width="800" height="600"
 style="width: 800px; height: 600px; background: black;"></canvas>
<script>
const canvas = document.getElementById('myCanvas');
const context = canvas.getContext('2d');

context.strokeStyle = 'white';

context.beginPath();
context.moveTo(100, 100);
context.lineTo(300, 200);
context.stroke();

context.beginPath();
context.arc(300, 200, 50, 0, 2 * Math.PI, false);
context.stroke();
</script>
```

And in C with SDL2_gfx, we would need something like this:

```C
#include <SDL.h>
#include <SDL2/SDL2_gfxPrimitives.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

int main(void)
{
        int exit_status = EXIT_FAILURE;
        char *title = "SDL Demo";
        SDL_Window *window;
        SDL_Renderer *renderer;
        bool quit = 0;
        SDL_Event e;

        SDL_Init(SDL_INIT_VIDEO);
        window = SDL_CreateWindow(title, 50, 50, 800, 600, 0);
        renderer = SDL_CreateRenderer(window, -1,
                SDL_RENDERER_ACCELERATED |
                SDL_RENDERER_PRESENTVSYNC);
        if (!renderer)
                goto error;

        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
        SDL_RenderClear(renderer);
        lineRGBA(renderer, 100, 100, 300, 200,
                 255, 255, 255, 255);
        circleRGBA(renderer, 300, 200, 50,
                   255, 255, 255, 255);

        SDL_RenderPresent(renderer);

        while (!quit) {
                SDL_WaitEvent(&e);
                if (e.type == SDL_QUIT)
                        quit = 1;
        }

        exit_status = EXIT_SUCCESS;
error:
        SDL_DestroyWindow(window);
        SDL_Quit();
        exit(exit_status);
}
```

I don't want my kids to suffer so very much more than I did, so I'm putting together [kisskit](https://github.com/gamedevfromscratch/kisskit), a set of tools to help simplify C programming and game dev as much as possible.

KISS stands for "Keep it simple, stupid!" and this is [very good advice](https://www.google.com/search?q=the+practice+of+programming) for programming.

Kisskit is a work in progress. So far, it includes three main features:

1. macros for logging and debugging
2. macros to wrap library functions and check for errors
3. a library called "basic" to enable writing simple games using SDL behind the scenes

Here is a [comparison](https://sam.ucm.dev/code/kisskit/eg/compare.html) of a simple program written with and without our `kiss.h` macros for logging and error handling.

Returning to our original graphics challenge, here is the same program to draw a line and a circle using our `basic` library. All of the SDL boilerplate code is hidden behind the scenes, and our main program is simple enough that a child could write it.

The library uses a linking trick so that we can override only the functions that we need. In this case, just `draw()`:

```C
#include "basic.h"

void draw(void)
{
        line(100, 100, 300, 200);
        circle0(300, 200, 50);
}
```

We are able to write simple code similar to BASIC, the [Processing](https://processing.org/) language, or [p5.js](https://p5js.org/), but in plain C.

In the next few posts, we'll show how we can draw pictures and make a simple game using this library.

Next post: [Creating Drawings](https://dev.to/gamedev/creating-drawings-229e), by Thalia
Previous post: [Basic Programming in C](https://dev.to/gamedev/basic-programming-in-c-243b), by Sam
Contents: [Game Dev From Scratch](https://dev.to/gamedev/game-dev-from-scratch-2do1)