feat: integrate TUI module and apply professional refactoring

This commit is contained in:
2026-04-16 20:39:48 +00:00
parent 42529be0f8
commit 72e0ef4022
18 changed files with 1340 additions and 2106 deletions

View File

@@ -1,10 +1,3 @@
/**
* @file config.h
* @brief Configuration constants for ASCII 3D Renderer
* @author ASCII3D Project
* @version 2.0.0
*/
#ifndef ASCII3D_CONFIG_H
#define ASCII3D_CONFIG_H
@@ -12,159 +5,73 @@
extern "C" {
#endif
/*============================================================================
* SCREEN CONFIGURATION
*============================================================================*/
#define SCREEN_WIDTH 120
#define SCREEN_HEIGHT 45
// Base viewport dimensions. Increase for monolithic monitors.
#define SCREEN_WIDTH 120
#define SCREEN_HEIGHT 45
/*============================================================================
* RENDERING QUALITY SETTINGS
*============================================================================*/
#define DEPTH_BUFFER_INIT 1e9f
#define EXTRUSION_DEPTH 4.0f
#define CHAR_SCALE 2.0f
/* Depth buffer initialization value */
#define DEPTH_BUFFER_INIT 1e9f
#define CAMERA_DISTANCE 30.0f
#define FIELD_OF_VIEW 50.0f
#define NEAR_PLANE 0.1f
#define FAR_PLANE 100.0f
/* Character extrusion depth (3D thickness) */
#define EXTRUSION_DEPTH 4.0f
// NxN jitter patterns for anti-aliasing passes
#define AA_SAMPLES 2
/* Base character scale */
#define CHAR_SCALE 2.0f
// Depth steps for voxel ray casting computation
#define VOXEL_STEP 0.15f
#define SMOOTH_PASSES 1
/* Camera settings */
#define CAMERA_DISTANCE 30.0f
#define FIELD_OF_VIEW 50.0f
#define NEAR_PLANE 0.1f
#define FAR_PLANE 100.0f
#define TARGET_FPS 60
#define FRAME_TIME_US (1000000 / TARGET_FPS)
/* Sub-pixel sampling for anti-aliasing (NxN samples per pixel) */
#define AA_SAMPLES 2
#define FONT_WIDTH 5
#define FONT_HEIGHT 7
#define FONT_CHAR_SPACING 2
/* Voxel rendering step (smaller = higher quality, slower) */
#define VOXEL_STEP 0.15f
// Physics-Based lighting configurations
#define AMBIENT_INTENSITY 0.15f
#define DIFFUSE_INTENSITY 0.70f
#define SPECULAR_INTENSITY 0.40f
#define SPECULAR_POWER 32.0f
#define MAX_LIGHTS 3
/* Surface smoothing iterations */
#define SMOOTH_PASSES 1
// Shader maps determining intensity mappings
#define SHADE_CHARS_STANDARD " .:-=+*#%@"
#define SHADE_COUNT_STANDARD 10
#define SHADE_CHARS_EXTENDED \
" .'`^\",:;Il!i><~+_-?][}{1)(|/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$"
#define SHADE_COUNT_EXTENDED 70
#define SHADE_CHARS_BLOCK " ░▒▓█"
#define SHADE_COUNT_BLOCK 5
#define SHADE_CHARS_MINIMAL " .:+#@"
#define SHADE_COUNT_MINIMAL 6
#define SHADE_CHARS SHADE_CHARS_EXTENDED
#define SHADE_COUNT SHADE_COUNT_EXTENDED
/*============================================================================
* ANIMATION PARAMETERS
*============================================================================*/
#define TARGET_FPS 60
#define FRAME_TIME_US (1000000 / TARGET_FPS)
/*============================================================================
* FONT CONFIGURATION
*============================================================================*/
/* Standard 5x7 font */
#define FONT_WIDTH 5
#define FONT_HEIGHT 7
#define FONT_CHAR_SPACING 2
/*============================================================================
* LIGHTING CONFIGURATION
*============================================================================*/
/* Ambient light intensity (0.0 - 1.0) */
#define AMBIENT_INTENSITY 0.15f
/* Diffuse light intensity */
#define DIFFUSE_INTENSITY 0.70f
/* Specular light intensity */
#define SPECULAR_INTENSITY 0.40f
/* Specular shininess exponent */
#define SPECULAR_POWER 32.0f
/* Number of light sources */
#define MAX_LIGHTS 3
/*============================================================================
* ASCII SHADING PALETTES
*============================================================================*/
/* Standard gradient (10 levels) */
#define SHADE_CHARS_STANDARD " .:-=+*#%@"
#define SHADE_COUNT_STANDARD 10
/* Extended gradient (16 levels) - more detail */
#define SHADE_CHARS_EXTENDED " .'`^\",:;Il!i><~+_-?][}{1)(|/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$"
#define SHADE_COUNT_EXTENDED 70
/* Block characters for solid look */
#define SHADE_CHARS_BLOCK " ░▒▓█"
#define SHADE_COUNT_BLOCK 5
/* Minimal gradient */
#define SHADE_CHARS_MINIMAL " .:+#@"
#define SHADE_COUNT_MINIMAL 6
/* Default palette */
#define SHADE_CHARS SHADE_CHARS_EXTENDED
#define SHADE_COUNT SHADE_COUNT_EXTENDED
/*============================================================================
* RENDER MODES
*============================================================================*/
typedef enum RenderMode {
RENDER_MODE_SOLID = 0, /* Filled solid rendering */
RENDER_MODE_WIREFRAME, /* Edge-only wireframe */
RENDER_MODE_POINTS, /* Point cloud */
RENDER_MODE_SHADED, /* Full Phong shading */
RENDER_MODE_COUNT
RENDER_MODE_SOLID = 0,
RENDER_MODE_WIREFRAME,
RENDER_MODE_POINTS,
RENDER_MODE_SHADED,
RENDER_MODE_COUNT
} RenderMode;
/*============================================================================
* COLOR MODES
*============================================================================*/
typedef enum ColorMode {
COLOR_MODE_MONO = 0, /* Monochrome ASCII */
COLOR_MODE_ANSI_16, /* 16-color ANSI */
COLOR_MODE_ANSI_256, /* 256-color ANSI */
COLOR_MODE_TRUECOLOR, /* 24-bit RGB */
COLOR_MODE_COUNT
COLOR_MODE_MONO = 0,
COLOR_MODE_ANSI_16,
COLOR_MODE_ANSI_256,
COLOR_MODE_TRUECOLOR,
COLOR_MODE_COUNT
} ColorMode;
/*============================================================================
* ANSI COLOR CODES
*============================================================================*/
#define ANSI_RESET "\033[0m"
#define ANSI_BOLD "\033[1m"
#define ANSI_DIM "\033[2m"
/* Foreground colors */
#define ANSI_FG_BLACK "\033[30m"
#define ANSI_FG_RED "\033[31m"
#define ANSI_FG_GREEN "\033[32m"
#define ANSI_FG_YELLOW "\033[33m"
#define ANSI_FG_BLUE "\033[34m"
#define ANSI_FG_MAGENTA "\033[35m"
#define ANSI_FG_CYAN "\033[36m"
#define ANSI_FG_WHITE "\033[37m"
/* Bright foreground colors */
#define ANSI_FG_BRIGHT_BLACK "\033[90m"
#define ANSI_FG_BRIGHT_RED "\033[91m"
#define ANSI_FG_BRIGHT_GREEN "\033[92m"
#define ANSI_FG_BRIGHT_YELLOW "\033[93m"
#define ANSI_FG_BRIGHT_BLUE "\033[94m"
#define ANSI_FG_BRIGHT_MAGENTA "\033[95m"
#define ANSI_FG_BRIGHT_CYAN "\033[96m"
#define ANSI_FG_BRIGHT_WHITE "\033[97m"
/* Background colors */
#define ANSI_BG_BLACK "\033[40m"
#define ANSI_BG_RED "\033[41m"
#define ANSI_BG_GREEN "\033[42m"
#define ANSI_BG_YELLOW "\033[43m"
#define ANSI_BG_BLUE "\033[44m"
#define ANSI_BG_MAGENTA "\033[45m"
#define ANSI_BG_CYAN "\033[46m"
#define ANSI_BG_WHITE "\033[47m"
#define ANSI_RESET "\033[0m"
#ifdef __cplusplus
}
#endif
#endif /* ASCII3D_CONFIG_H */
#endif

View File

@@ -1,10 +1,3 @@
/**
* @file font.h
* @brief Bitmap font data for ASCII 3D Renderer
* @author ASCII3D Project
* @version 1.0.0
*/
#ifndef ASCII3D_FONT_H
#define ASCII3D_FONT_H
@@ -14,31 +7,18 @@
extern "C" {
#endif
/**
* @brief Get the font glyph data for a character
* @param c Character to look up (A-Z, a-z, 0-9)
* @return Pointer to 7-byte glyph data, or NULL if not found
*/
// Returns a direct pointer to the 7-byte glyph row data if the char is present
// in our sprite sheet
const unsigned char *font_get_glyph(char c);
/**
* @brief Check if a pixel is set in a glyph
* @param glyph Pointer to glyph data
* @param x X coordinate (0-4)
* @param y Y coordinate (0-6)
* @return true if pixel is set, false otherwise
*/
// Validates the bitmap bounds and extracts a specific pixel bit
bool font_pixel_set(const unsigned char *glyph, int x, int y);
/**
* @brief Check if character is renderable
* @param c Character to check
* @return true if character can be rendered
*/
// Tests an ASCII character against our mapped glyph dictionary
bool font_is_renderable(char c);
#ifdef __cplusplus
}
#endif
#endif /* ASCII3D_FONT_H */
#endif

View File

@@ -1,170 +1,81 @@
/**
* @file lighting.h
* @brief Advanced lighting system for ASCII 3D Renderer
* @author ASCII3D Project
* @version 2.0.0
*/
#ifndef ASCII3D_LIGHTING_H
#define ASCII3D_LIGHTING_H
#include "vec3.h"
#include "config.h"
#include "vec3.h"
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Light types
*/
typedef enum LightType {
LIGHT_DIRECTIONAL = 0, /* Parallel rays (sun-like) */
LIGHT_POINT, /* Point source with falloff */
LIGHT_SPOT /* Cone-shaped spotlight */
LIGHT_DIRECTIONAL = 0,
LIGHT_POINT,
LIGHT_SPOT
} LightType;
/**
* @brief RGB Color structure
*/
typedef struct Color {
float r, g, b;
float r, g, b;
} Color;
/**
* @brief Light source structure
*/
typedef struct Light {
LightType type;
Vec3 position; /* Position for point/spot lights */
Vec3 direction; /* Direction for directional/spot lights */
Color color; /* Light color */
float intensity; /* Light intensity multiplier */
float falloff; /* Attenuation for point lights */
float spot_angle; /* Cone angle for spotlights (radians) */
bool enabled;
LightType type;
Vec3 position;
Vec3 direction;
Color color;
float intensity;
float falloff;
float spot_angle;
bool enabled;
} Light;
/**
* @brief Material properties for surfaces
*/
// PBR surface components
typedef struct Material {
Color ambient; /* Ambient color */
Color diffuse; /* Diffuse color */
Color specular; /* Specular highlight color */
float shininess; /* Specular exponent */
float reflectivity; /* For future use */
Color ambient;
Color diffuse;
Color specular;
float shininess;
float reflectivity;
} Material;
/**
* @brief Lighting system state
*/
// Encapsulates the entire render context's virtual ecosystem
typedef struct LightingSystem {
Light lights[MAX_LIGHTS];
int light_count;
Color ambient_color;
float ambient_intensity;
Vec3 camera_position;
Light lights[MAX_LIGHTS];
int light_count;
Color ambient_color;
float ambient_intensity;
Vec3 camera_position;
} LightingSystem;
/**
* @brief Initialize the lighting system with default lights
* @param system Lighting system to initialize
*/
// Instantiates default 3-point portrait lighting scheme
void lighting_init(LightingSystem *system);
/**
* @brief Add a light to the system
* @param system Lighting system
* @param light Light to add
* @return Index of added light, or -1 if full
*/
// Registers a new light emitter into the system context
int lighting_add_light(LightingSystem *system, const Light *light);
/**
* @brief Create a directional light
* @param direction Light direction (will be normalized)
* @param color Light color
* @param intensity Light intensity
* @return Configured light structure
*/
Light lighting_create_directional(Vec3 direction, Color color, float intensity);
Light lighting_create_point(Vec3 position, Color color, float intensity,
float falloff);
/**
* @brief Create a point light
* @param position Light position
* @param color Light color
* @param intensity Light intensity
* @param falloff Attenuation factor
* @return Configured light structure
*/
Light lighting_create_point(Vec3 position, Color color, float intensity, float falloff);
/**
* @brief Calculate lighting at a surface point (Phong model)
* @param system Lighting system
* @param point Surface point position
* @param normal Surface normal (must be normalized)
* @param material Surface material
* @return Final illumination value (0.0 - 1.0+)
*/
float lighting_calculate(const LightingSystem *system, Vec3 point,
Vec3 normal, const Material *material);
/**
* @brief Calculate full color lighting
* @param system Lighting system
* @param point Surface point position
* @param normal Surface normal
* @param material Surface material
* @return Final color
*/
// Computes monochrome or RGB shading based on surface interactions
float lighting_calculate(const LightingSystem *system, Vec3 point, Vec3 normal,
const Material *material);
Color lighting_calculate_color(const LightingSystem *system, Vec3 point,
Vec3 normal, const Material *material);
/**
* @brief Create default material
* @return Default white material
*/
Material lighting_default_material(void);
/**
* @brief Create a color
* @param r Red (0-1)
* @param g Green (0-1)
* @param b Blue (0-1)
* @return Color structure
*/
// Floating point color combinators
Color color_create(float r, float g, float b);
/**
* @brief Multiply color by scalar
*/
Color color_scale(Color c, float s);
/**
* @brief Add two colors
*/
Color color_add(Color a, Color b);
/**
* @brief Multiply two colors component-wise
*/
Color color_multiply(Color a, Color b);
/**
* @brief Clamp color components to 0-1 range
*/
Color color_clamp(Color c);
/**
* @brief Convert color to grayscale intensity
*/
float color_to_intensity(Color c);
#ifdef __cplusplus
}
#endif
#endif /* ASCII3D_LIGHTING_H */
#endif

View File

@@ -1,10 +1,3 @@
/**
* @file renderer.h
* @brief Advanced rendering engine for ASCII 3D Renderer
* @author ASCII3D Project
* @version 2.0.0
*/
#ifndef ASCII3D_RENDERER_H
#define ASCII3D_RENDERER_H
@@ -16,164 +9,73 @@
extern "C" {
#endif
/**
* @brief Rotation state for animation
*/
typedef struct RotationState {
float angle_x;
float angle_y;
float angle_z;
bool enable_x;
bool enable_y;
bool enable_z;
float speed;
float angle_x;
float angle_y;
float angle_z;
bool enable_x;
bool enable_y;
bool enable_z;
float speed;
} RotationState;
/**
* @brief Render settings structure
*/
typedef struct RenderSettings {
RenderMode mode; /* Rendering mode */
ColorMode color_mode; /* Color output mode */
bool anti_aliasing; /* Enable AA */
bool show_fps; /* Display FPS counter */
bool auto_rotate; /* Auto rotation enabled */
float quality; /* Quality multiplier (0.5 - 2.0) */
int palette_index; /* Shading palette selection */
Color base_color; /* Base color for colored modes */
Color highlight_color; /* Highlight/specular color */
RenderMode mode;
ColorMode color_mode;
bool anti_aliasing;
bool show_fps;
bool auto_rotate;
float quality;
int palette_index;
Color base_color;
Color highlight_color;
} RenderSettings;
/**
* @brief Frame statistics
*/
// Diagnostics profile updated each tick
typedef struct FrameStats {
double frame_time; /* Last frame time in ms */
double fps; /* Current FPS */
double avg_fps; /* Average FPS */
int frame_count; /* Total frames rendered */
int triangles; /* Triangles/voxels rendered */
double frame_time;
double fps;
double avg_fps;
int frame_count;
int triangles;
} FrameStats;
/**
* @brief Initialize the renderer
* @return 0 on success, -1 on failure
*/
// Core Engine Lifecycles
int renderer_init(void);
/**
* @brief Cleanup renderer resources
*/
void renderer_cleanup(void);
/**
* @brief Clear the screen and depth buffers
*/
// Nulls out all raster buffers ready for drawing
void renderer_clear(void);
/**
* @brief Render a 3D text string
* @param text Text to render (A-Z, 0-9)
* @param rotation Current rotation state
*/
void renderer_draw_text(const char *text, const RotationState *rotation);
/**
* @brief Render with full settings control
* @param text Text to render
* @param rotation Rotation state
* @param settings Render settings
*/
void renderer_draw_text_ex(const char *text, const RotationState *rotation,
const RenderSettings *settings);
/**
* @brief Display the rendered frame to terminal
*/
// Flushes the rendering pipeline out to standard output
void renderer_present(void);
/**
* @brief Present with color support
* @param settings Render settings for color mode
*/
void renderer_present_color(const RenderSettings *settings);
/**
* @brief Update rotation angles based on time delta
* @param rotation Rotation state to update
* @param delta_time Time since last update in seconds
*/
void renderer_update_rotation(RotationState *rotation, double delta_time);
/**
* @brief Create default rotation state
* @return Default rotation state with Y-axis rotation enabled
*/
RotationState renderer_default_rotation(void);
/**
* @brief Create default render settings
* @return Default settings
*/
RenderSettings renderer_default_settings(void);
/**
* @brief Set the render mode
* @param mode New render mode
*/
// Dynamic rendering reconfiguration toggles
void renderer_set_mode(RenderMode mode);
/**
* @brief Set the color mode
* @param mode New color mode
*/
void renderer_set_color_mode(ColorMode mode);
/**
* @brief Get current frame statistics
* @return Frame stats structure
*/
FrameStats renderer_get_stats(void);
/**
* @brief Set the shading palette
* @param palette_index 0=standard, 1=extended, 2=block, 3=minimal
*/
void renderer_set_palette(int palette_index);
/**
* @brief Get the lighting system for modification
* @return Pointer to lighting system
*/
FrameStats renderer_get_stats(void);
LightingSystem *renderer_get_lighting(void);
/**
* @brief Hide terminal cursor
*/
// Console visual hacks
void renderer_hide_cursor(void);
/**
* @brief Show terminal cursor
*/
void renderer_show_cursor(void);
/**
* @brief Clear terminal screen
*/
void renderer_clear_terminal(void);
/**
* @brief Set terminal to alternate screen buffer
*/
void renderer_enter_alternate_screen(void);
/**
* @brief Restore terminal to main screen buffer
*/
void renderer_exit_alternate_screen(void);
#ifdef __cplusplus
}
#endif
#endif /* ASCII3D_RENDERER_H */
#endif

View File

@@ -1,10 +1,3 @@
/**
* @file timing.h
* @brief High-precision timing utilities
* @author ASCII3D Project
* @version 1.0.0
*/
#ifndef ASCII3D_TIMING_H
#define ASCII3D_TIMING_H
@@ -12,27 +5,18 @@
extern "C" {
#endif
/**
* @brief Get current time in seconds (monotonic clock)
* @return Time in seconds with nanosecond precision
*/
// Returns the monotonic wall clock time in seconds, suitable for frame timing.
double timing_get_seconds(void);
/**
* @brief Sleep for specified microseconds
* @param microseconds Duration to sleep
*/
// Microsecond-scale thread suspension layer.
void timing_sleep_us(unsigned int microseconds);
/**
* @brief Frame rate limiter - sleeps to maintain target FPS
* @param frame_start_time Time when frame started (from timing_get_seconds)
* @param target_fps Target frames per second
*/
// Calculates dynamic sleep padding against a target FPS to prevent high CPU
// utilization
void timing_limit_fps(double frame_start_time, int target_fps);
#ifdef __cplusplus
}
#endif
#endif /* ASCII3D_TIMING_H */
#endif

29
include/tui.h Normal file
View File

@@ -0,0 +1,29 @@
#ifndef ASCII3D_TUI_H
#define ASCII3D_TUI_H
#include "renderer.h"
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
// Reconfigures standard input to be non-blocking and disables canonical mode.
// This allows us to poll the keyboard gracefully in the render loop.
int tui_init(void);
// Restores terminal to its initial state to avoid leaving users with a broken
// prompt.
void tui_cleanup(void);
// Processes pending keys and updates application state.
// Standard typing appends to the text_buffer, while specific commands alter
// settings. Returns non-zero when an exit is requested.
int tui_process_input(RotationState *rotation, RenderSettings *settings,
char *text_buffer, size_t max_text_len);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,10 +1,3 @@
/**
* @file vec3.h
* @brief 3D Vector mathematics for ASCII 3D Renderer
* @author ASCII3D Project
* @version 1.0.0
*/
#ifndef ASCII3D_VEC3_H
#define ASCII3D_VEC3_H
@@ -12,104 +5,29 @@
extern "C" {
#endif
/**
* @brief 3D Vector structure
*/
// A standard 3-component float vector
typedef struct Vec3 {
float x;
float y;
float z;
float x;
float y;
float z;
} Vec3;
/**
* @brief Create a new Vec3
* @param x X component
* @param y Y component
* @param z Z component
* @return New Vec3 instance
*/
Vec3 vec3_create(float x, float y, float z);
/**
* @brief Add two vectors
* @param a First vector
* @param b Second vector
* @return Result of a + b
*/
Vec3 vec3_add(Vec3 a, Vec3 b);
/**
* @brief Subtract two vectors
* @param a First vector
* @param b Second vector
* @return Result of a - b
*/
Vec3 vec3_sub(Vec3 a, Vec3 b);
/**
* @brief Scale a vector by a scalar
* @param v Vector to scale
* @param s Scalar value
* @return Scaled vector
*/
Vec3 vec3_scale(Vec3 v, float s);
/**
* @brief Calculate dot product of two vectors
* @param a First vector
* @param b Second vector
* @return Dot product
*/
float vec3_dot(Vec3 a, Vec3 b);
/**
* @brief Calculate cross product of two vectors
* @param a First vector
* @param b Second vector
* @return Cross product
*/
Vec3 vec3_cross(Vec3 a, Vec3 b);
/**
* @brief Calculate length of a vector
* @param v Vector
* @return Length
*/
float vec3_length(Vec3 v);
/**
* @brief Normalize a vector
* @param v Vector to normalize
* @return Normalized vector
*/
Vec3 vec3_normalize(Vec3 v);
/**
* @brief Rotate vector around X axis
* @param v Vector to rotate
* @param angle Angle in radians
* @return Rotated vector
*/
// Rotating points through standard Euler coordinate transformations
Vec3 vec3_rotate_x(Vec3 v, float angle);
/**
* @brief Rotate vector around Y axis
* @param v Vector to rotate
* @param angle Angle in radians
* @return Rotated vector
*/
Vec3 vec3_rotate_y(Vec3 v, float angle);
/**
* @brief Rotate vector around Z axis
* @param v Vector to rotate
* @param angle Angle in radians
* @return Rotated vector
*/
Vec3 vec3_rotate_z(Vec3 v, float angle);
#ifdef __cplusplus
}
#endif
#endif /* ASCII3D_VEC3_H */
#endif