DEV Community

Sam Watkins for Game Dev From Scratch

Posted on • Updated on

kisskit: Simplifying Game Dev in C

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:

MOVE 100, 100
DRAW 300, 200
CIRCLE 300, 200, 50
Enter fullscreen mode Exit fullscreen mode

Image description

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

<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>
Enter fullscreen mode Exit fullscreen mode

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

#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);
}
Enter fullscreen mode Exit fullscreen mode

I don't want my kids to suffer so very much more than I did, so I'm putting together 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 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 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():

#include "basic.h"

void draw(void)
{
        line(100, 100, 300, 200);
        circle0(300, 200, 50);
}
Enter fullscreen mode Exit fullscreen mode

We are able to write simple code similar to BASIC, the Processing language, or p5.js, 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, by Thalia
Previous post: Basic Programming in C, by Sam
Contents: Game Dev From Scratch

Top comments (0)