cube

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

commit c3f3390c8e441ec44a9958758cceafb3137ae8a7
parent bd3414ca130fd1d702a009fd66693ad6e411e584
Author: Michael Neuper <michael@michaelneuper.com>
Date:   Thu,  9 Apr 2026 21:07:15 +0200

replace macros

replace macros with `static const`s and
`inline` functions

Diffstat:
Msrc/cube.c | 26+++++++++++++++-----------
Msrc/main.c | 38+++++++++++++++++++-------------------
2 files changed, 34 insertions(+), 30 deletions(-)

diff --git a/src/cube.c b/src/cube.c @@ -14,13 +14,17 @@ #include "debug.h" #include "ncdraw.h" -#define CAM_DISTANCE 50.0 -#define ZOOM 500.0 -#define SCALE_X 2.0 -#define SCALE_Y 1.0 +static const double cam_distance = 50.0; +static const double zoom = 500.0; +static const double scale_x = 2.0; +static const double scale_y = 1.0; -#define TRANS_X(x) ((x) + getmaxx(stdscr) / 2.0) -#define TRANS_Y(y) ((y) + getmaxy(stdscr) / 2.0) +static inline double trans_x(double x) { + return x + getmaxx(stdscr) / 2.0; +} +static inline double trans_y(double y) { + return y + getmaxy(stdscr) / 2.0; +} /** * Represents a projected point in 2D screen-space. @@ -97,8 +101,8 @@ void draw_cube(const Cube *cube) { } static Pixel point_to_pixel(Point2D p) { - float x = TRANS_X(SCALE_X * p.x); - float y = TRANS_Y(SCALE_Y * -p.y); + float x = trans_x(scale_x * p.x); + float y = trans_y(scale_y * -p.y); if (x < 0) x = 0; if (y < 0) y = 0; @@ -112,11 +116,11 @@ static Pixel point_to_pixel(Point2D p) { } static Point2D project_point(Point3D p) { - double depth = p.z + CAM_DISTANCE; + double depth = p.z + cam_distance; return (Point2D){ - .x = ZOOM * p.x / depth, - .y = ZOOM * p.y / depth, + .x = zoom * p.x / depth, + .y = zoom * p.y / depth, }; } diff --git a/src/main.c b/src/main.c @@ -6,19 +6,19 @@ #include "debug.h" #include "ncdraw.h" -#define KEY_QUIT 'q' -#define FRAME_DELAY_MS 100 -#define INITIAL_ANGLE_X 0.4 -#define INITIAL_ANGLE_Y 0.6 -#define ROTATION_SPEED_X (M_PI / 96) -#define ROTATION_SPEED_Y (M_PI / 64) -#define SLEEP(msecs) \ - do { \ - struct timespec ts; \ - ts.tv_sec = msecs / 1000; \ - ts.tv_nsec = (msecs % 1000) * 1000000; \ - nanosleep(&ts, NULL); \ - } while (0) +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); @@ -34,25 +34,25 @@ void loop(void) { ASSERT_NOT_NULL(cube); // initial rotation for better 3D view - rotate_cube(cube, X, INITIAL_ANGLE_X); - rotate_cube(cube, Y, INITIAL_ANGLE_Y); + 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) { + if ((ch) == key_quit) { destroy_cube(cube); return; } clear(); - rotate_cube(cube, Y, ROTATION_SPEED_Y); - rotate_cube(cube, X, ROTATION_SPEED_X); + rotate_cube(cube, Y, rotation_speed_y); + rotate_cube(cube, X, rotation_speed_x); draw_cube(cube); refresh(); - SLEEP(FRAME_DELAY_MS); + sleep(frame_delay_ms); } }