cube

A rotating 3D wireframe cube rendered in the terminal
git clone https://github.com/michaelneuper/cube.git
README | Files | Log | Refs | LICENSE

cube/src/main.c (1217B)


#include <math.h>
#include <ncurses.h>
#include <time.h>

#include "cube.h"
#include "debug.h"
#include "ncdraw.h"

static const char   key_quit         = 'q';
static const int    frame_delay_ms   = 100;
static const double initial_angle_x  = 0.4;
static const double initial_angle_y  = 0.6;
static const double rotation_speed_x = M_PI / 96;
static const double rotation_speed_y = M_PI / 64;

inline void sleep(int msecs) {
    struct timespec ts;
    ts.tv_sec  = msecs / 1000;
    ts.tv_nsec = (msecs % 1000) * 1000000;
    nanosleep(&ts, NULL);
}

void loop(void);

int main(void) {
    init_canvas();
    loop();

    return 0;
}

void loop(void) {
    Cube *cube = create_cube();
    ASSERT_NOT_NULL(cube);

    // initial rotation for better 3D view
    rotate_cube(cube, X, initial_angle_x);
    rotate_cube(cube, Y, initial_angle_y);

    draw_cube(cube);
    refresh();

    while (TRUE) {
        int ch = getch();
        if ((ch) == key_quit) {
            destroy_cube(cube);
            return;
        }

        clear();
        rotate_cube(cube, Y, rotation_speed_y);
        rotate_cube(cube, X, rotation_speed_x);
        draw_cube(cube);
        refresh();

        sleep(frame_delay_ms);
    }
}