*ฅ^•ﻌ•^ฅ* ✨✨  HWisnu's blog  ✨✨ о ฅ^•ﻌ•^ฅ

Bunnymarks in C + Raylib

Introduction

Bunnymark is a popular benchmarking tool used to measure the performance of a system's rendering capabilities. In this article, we will explore how to implement the Bunnymark program using C and the Raylib game development library.

What is Raylib?

Raylib is a simple and easy-to-use game development library that provides a wide range of features for creating games and other graphical applications. It is written in C and provides a simple API for handling tasks such as window creation, input handling, and graphics rendering. Raylib is a great choice for creating games and other graphical applications, especially for developers who are new to game development.

Implementing Bunnymark with C and Raylib

To implement the Bunnymark program with C and Raylib, we will need to create a window and render a large number of bunnies on the screen. We will use Raylib's sprite rendering functions to draw the bunnies, and we will use a loop to update and render the bunnies in real-time. We will also use Raylib's timing functions to measure the frame rate and update the bunnies accordingly.

The code

Section 1: Header Inclusions and Definitions

This section includes the necessary header files for the program, including Raylib for graphics and standard C libraries for input/output and memory management. It also defines several constants for the program, including the window dimensions, caption, number of bunnies, and bunny properties.

#include "raylib.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define WIDTH 800
#define HEIGHT 600
#define CAPTION "Bunnymark"

#define BUNNIES 100000
#define BUNNY_MAX_SPEED 10

#define BUNNY_WIDTH 10
#define BUNNY_HEIGHT 10

Section 2: Bunny Structure and Functions

Defines a Bunny structure to represent individual bunnies, with properties for position, speed, and color. It also defines several functions to initialize, update, paint, and destroy bunnies.

typedef struct {
    int x;
    int y;
    int speedx;
    int speedy;
    Color color;
} Bunny;

Bunny bunnies[BUNNIES];

void bunny_init(Bunny* b, int x, int y, int sx, int sy, Color c)
{
    b->x = x;
    b->y = y;
    b->speedx = sx;
    b->speedy = sy;
    b->color = c;
}

void bunny_update(Bunny* b)
{
    b->x += b->speedx;
    b->y += b->speedy;

    if (b->y  < 0 || b->y > HEIGHT) {
        b->speedy *= -1;
    }
    if (b->x  < 0 || b->x > WIDTH) {
        b->speedx *= -1;
    }
}

void bunny_paint(Bunny* b)
{
    DrawRectangle(b->x, b->y, BUNNY_WIDTH, BUNNY_HEIGHT, b->color);
}

void bunny_destroy(Bunny* b)
{
    free(b);
}

Section 3: Main Program Loop

Defines two functions to update and paint the bunnies. The update function updates the position of each bunny, while the paint function clears the background, paints each bunny, and displays the FPS and number of bunnies on the screen.

void update()
{
    for (int i=0; i<BUNNIES; i++) {
        bunny_update(&bunnies[i]);
    }
}

void paint()
{
    ClearBackground(RAYWHITE);

    for (int i=0; i<BUNNIES; i++) {
        bunny_paint(&bunnies[i]);
    }

    snprintf(FPS_message, sizeof(FPS_message), "FPS: %d", GetFPS());
    DrawText(FPS_message, 5, 5, 30, BLACK);

    snprintf(bunnies_message, sizeof(bunnies_message), "Bunnies: %d", BUNNIES);
    DrawText(bunnies_message, 5, 45, 30, BLACK);
}

Section 4: Main Function

The main function of the program. It initializes the window, seeds the random number generator, and initializes each bunny with random properties. It then enters a loop where it updates and paints the bunnies until the window is closed.

int main(void)
{
    srand(time(0));
    InitWindow(WIDTH, HEIGHT, CAPTION);

    for (int i=0; i<BUNNIES; i++) {
        bunny_init(&bunnies[i],
            rand() % WIDTH, 
            rand() % HEIGHT, 
            rand() % BUNNY_MAX_SPEED + 1, 
            rand() % BUNNY_MAX_SPEED + 1, 
            (Color) {
                rand() % 255,
                rand() % 255,
                rand() % 255,
                255
            });
    }

    while (!WindowShouldClose()) {
        update();
        
        BeginDrawing();
        { paint(); }
        EndDrawing();
    }

    CloseWindow();

    return 0;
}

Benchmark

This version runs at an 83 FPS in my system, your numbers might vary depending on the system.


Note: I wrote Zig + Raylib version of this Bunnymarks program, you can find the post here

#benchmark #bunnymarks #c #game development #low level #raylib