用于 C++ Windows 的 ASCII 和 UNICODE 图形库
Fazen2d is a lightweight ASCII & Unicode graphics library for Windows consoles. It provides basic geometry primitives, console rendering, input handling, state management, and timing services. Users can draw shapes (points, lines, boxes, circles, text), handle keyboard and mouse events, and run a continuous render loop in a structured framework.
This engine fits into applications that require simple graphical output without external libraries beyond the Windows API. It centralizes console setup, buffer management, and coordinate transformations to simplify 2D console graphics development.
…include/headers/Shape.h)The Shape class defines a common interface for all drawable primitives. It enforces:
draw() method.translate(float dx, float dy) method.class Shape {
public:
virtual void draw() = 0;
virtual void translate(float dx, float dy) = 0;
virtual ~Shape() = default;
};include/headers/Point.h / include/Point.cpp)Represents a single console cell at (x, y) with a character and color.
x, y, color, characterdraw() writes the character into the console buffer.translate(dx, dy) moves the point within console bounds.explicit Point(float x, float y, short color, short character = 0x2588);
void draw() override;
void translate(float dx, float dy) override;include/headers/Line.h / include/Line.cpp)Draws a Bresenham line between two points.
x1, y1, x2, y2, color, characterdraw() implements Bresenham’s algorithm.translate(dx, dy) shifts both endpoints.Line(float x1, float y1, float x2, float y2, short color, short character = 0x2588);
void draw() override;
void translate(float dx, float dy) override;include/headers/Box.h / include/Box.cpp)Renders a filled rectangle by drawing multiple points.
x, y, width, height, color, characterdraw() loops over width/height to place points.translate(dx, dy) moves the box if within console bounds.explicit Box(float x, float y, float width, float height, short color, short character = 0x2588);
void draw() override;
void translate(float dx, float dy) override;include/headers/Circle.h / include/Circle.cpp)Draws a filled circle by checking each cell within a bounding square.
xc, yc, radius, color, characterdraw() uses the distance formula for each cell.translate(dx, dy) shifts center within valid range.explicit Circle(float xc, float yc, float radius, short color, short character = 0x2588);
void draw() override;
void translate(float dx, float dy) override;include/headers/Text.h / include/Text.cpp)Renders a Unicode string at a given location.
x, y, wstring value, colordraw() writes each wide character into the buffer.setValue overloads accept wstring or int.Text(float x, float y, wstring s, short color);
void draw() override;include/headers/Colors.h / include/Colors.cpp)Defines console color constants and a Color utility class.
ConsoleColor maps to Windows attributes.Color holds fgColor, bgColor, and computes combined value.enum ConsoleColor { redF = FOREGROUND_RED, greenF = FOREGROUND_GREEN, /*…*/ };
class Color {
short fgColor, bgColor, value;
// setters/getters
};include/headers/GraphicsRenderer.h / include/GraphicsRenderer.cpp)Handles console buffer display and draws shapes.
background(short col) clears buffer with a color.draw(Shape &s) invokes s.draw().display() writes buffer to the console via WriteConsoleOutputW.void background(short col);
void draw(Shape& s);
void display();include/headers/KeyboardHandler.h / include/KeyboardHandler.cpp)Wraps Windows key state functions.
IsKeyPressed(int keyCode)IsKeyReleased(int keyCode)CheckForUserExit() checks VK_ESCAPE.bool IsKeyPressed(int keyCode);
bool CheckForUserExit();include/headers/MouseHandler.h / include/MouseHandler.cpp)Tracks mouse position and click states.
leftButtonPressed, rightButtonPressedGetMouseX(), GetMouseY() map screen to console coords.UpdateMouseState(), IsLeftMousePressed(), IsLeftMouseClicked(), etc.float GetMouseX();
float GetMouseY();
void UpdateMouseState();
bool IsLeftMouseClicked();include/headers/StateManager.h / include/StateManager.cpp)Maintains a global center offset for coordinate translation.
saveState() / restoreState() to snapshot/restore center.setCenter(x, y), translateCoordinates(x&, y&).static void setCenter(float x, float y);
static void translateCoordinates(float& x, float& y);include/headers/TimeManager.h / include/TimeManager.cpp)Measures frame-to-frame elapsed time.
Reset() resets the internal clock.GetElapsedTime() returns seconds since last call.void Reset();
float GetElapsedTime();include/headers/MathUtils.h / include/MathUtils.cpp)Utility for numeric transformations.
static constexpr double piMap(value, x1, x2, y1, y2) scales a value linearly between ranges.static float Map(float value, float x1, float x2, float y1, float y2);include/headers/ConsoleHandler.h / include/ConsoleHandler.cpp)Sets up and manages a 2D console buffer.
ConsoleHandler(int s_width, int s_height);
static CHAR_INFO* GetBuffScreen();
static int GetConsoleWidth();
static int GetConsoleHeight();include/headers/Fazen.h / include/Fazen.cpp)Aggregates all subsystems:
console, graphics, keyboardHandler, mouseHandler, timerConsoleHandler.Fazen(int s_width, int s_height);main.cpp)Entry point demonstrating the engine loop:
Fazen game(100, 100).game.graphics.display(), exit on ESC.while (true) {
game.graphics.background(whiteB);
game.graphics.draw(b);
// ...
if (game.keyboardHandler.CheckForUserExit()) break;
}Makefile)Compiles all .cpp under include/ and main.cpp with g++ -std=c++11.
CC = g++
CFLAGS = -std=c++11
SRC_DIR = include
# ...
all: a| Class | Location | Responsibility |
|---|---|---|
| Shape | include/headers/Shape.h | Base interface for all drawable shapes |
| Point | include/headers/Point.h | Represents a single console cell |
| Line | include/headers/Line.h | Draws lines using Bresenham’s algorithm |
| Box | include/headers/Box.h | Renders filled rectangles |
| Circle | include/headers/Circle.h | Renders filled circles |
| Text | include/headers/Text.h | Draws wide-character strings |
| Colors / Color | include/headers/Colors.h | Defines color constants and utility class |
| GraphicsRenderer | include/headers/GraphicsRenderer.h | Manages buffer clearing and shape rendering |
| ConsoleHandler | include/headers/ConsoleHandler.h | Sets up and provides access to the console buffer |
| KeyboardHandler | include/headers/KeyboardHandler.h | Wraps keyboard state queries |
| MouseHandler | include/headers/MouseHandler.h | Tracks mouse position and click states |
| StateManager | include/headers/StateManager.h | Global coordinate offset manager |
| TimeManager | include/headers/TimeManager.h | Frame timing calculations |
| MathUtils | include/headers/MathUtils.h | Numeric mapping and constants |
| Fazen | include/headers/Fazen.h | Aggregates all subsystems into one engine object |
| Main | main.cpp | Demonstrates engine initialization and loop |
| Build | Makefile | Compilation instructions |
The frame loop in Fazen2d embodies the classic clear–draw–display cycle, enabling smooth, flicker-free rendering in the Windows console. Each iteration clears the off-screen buffer to a uniform background, issues draw commands for all visible shapes, and then flushes the completed buffer to the console in one atomic operation. This double-buffering strategy decouples per-cell updates from on-screen presentation, ensuring consistent frame timing and a tear-free visual experience.
By centralizing buffer management and draw dispatch in the GraphicsRenderer class, application code remains focused on high-level scene composition. Whether you’re animating geometry primitives or outputting text, the same three-step loop applies, making day-to-day use of the library both intuitive and efficient.
Call GraphicsRenderer::background(col) to fill the entire back-buffer with spaces and a uniform color attribute.
For each shape or text object, invoke GraphicsRenderer::draw(shape), which in turn calls the shape’s own draw() method to write characters into the back-buffer.
Flush the back-buffer to the visible console window via GraphicsRenderer::display(), which wraps the Win32 WriteConsoleOutputW API.
flowchart LR
A[Application] --> B[GraphicsRenderer background]
B --> C[ConsoleHandler BuffScreen]
A --> D[GraphicsRenderer draw(Shape)]
D --> E[Shape draw]
E --> C
A --> F[GraphicsRenderer display]
F --> G[WriteConsoleOutputW]
G --> H[Windows Console]Path: include/headers/GraphicsRenderer.h / include/GraphicsRenderer.cpp
Manages the off-screen buffer lifecycle and delegates draw calls to shapes.
| Method | Description |
|---|---|
| display() | Flushes the back-buffer to the console via WriteConsoleOutputW . |
| background(short col) | Iterates over every cell in ConsoleHandler::GetBuffScreen() to set a space character and the given attribute . |
| draw(Shape &s) | Invokes s.draw(), allowing each shape to write into the back-buffer . |
*Pat
暂无开放 Issues,或尚未同步最近议题。