KTech 1.1.0
C++ 2D terminal game engine library
Loading...
Searching...
No Matches
output.hpp
1/*
2 KTech, Kaup's C++ 2D terminal game engine library.
3 Copyright (C) 2023-2025 Ethan Kaufman (AKA Kaup)
4
5 This file is part of KTech.
6
7 KTech is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 any later version.
11
12 KTech is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with KTech. If not, see <https://www.gnu.org/licenses/>.
19*/
20
21#pragma once
22
23#define KTECH_DEFINITION
24#include "../ktech.hpp"
25#undef KTECH_DEFINITION
26#include "../basic/point.hpp"
27#include "../basic/upoint.hpp"
28
29#include <limits>
30#include <string>
31#ifdef _WIN32
32#include <Windows.h>
33#else
34#include <termio.h>
35#endif
36#include <vector>
37
42{
43public:
45 std::vector<std::string> outputOnQuit;
46
47 static void Log(const std::string& text, RGB color);
48
49 void PrintStartupNotice(const std::string& title, const std::string& years, const std::string& author, const std::string& programName) const;
50
51 void Clear();
52
53 void Draw(const std::vector<Cell>& sourceImage, UPoint resolution, Point position = Point(0, 0), UPoint start = UPoint(0, 0), UPoint end = UPoint(0, 0), uint8_t alpha = std::numeric_limits<uint8_t>::max());
54 void Draw(const std::vector<CellA>& sourceImage, UPoint resolution, Point position = Point(0, 0), UPoint start = UPoint(0, 0), UPoint end = UPoint(0, 0), uint8_t alpha = std::numeric_limits<uint8_t>::max());
55
56 void Print();
57
58 [[nodiscard]] auto ShouldRenderThisTick() -> bool;
59 [[nodiscard]] auto ShouldPrintThisTick() const -> bool;
60
61private:
62 Engine& engine;
63#if _WIN32
64 HANDLE m_stdoutHandle;
65 DWORD m_oldMode;
66 CONSOLE_SCREEN_BUFFER_INFO m_csbi;
67 struct winsize {
68 size_t ws_row;
69 size_t ws_col;
70 } m_terminalSize;
71#else
72 winsize m_terminalSize;
73#endif
74 std::vector<Cell> m_image;
75 std::string m_stringImage;
76 static constexpr size_t printSequenceLength = 39;
77
78 Output(Engine& engine, UPoint imageResolution, bool noGameLoopMode);
79 ~Output();
80
81 void PopulateForegroundColor(size_t& dst, const RGB& src);
82 void PopulateBackgroundColor(size_t& dst, const RGB& src);
83 void PopulateCharacter(size_t& dst, size_t src);
84 void PopulateEndOfLine(size_t& dst);
85
86 friend class Engine;
87};
Complete engine containing all engine components.
Definition engine.hpp:41
Engine component responsible for outputting rendered images.
Definition output.hpp:42
const UPoint resolution
The size of the image buffer (viewport).
Definition output.hpp:44
void Print()
Print the internal image buffer.
Definition output.cpp:264
void PrintStartupNotice(const std::string &title, const std::string &years, const std::string &author, const std::string &programName) const
Print a copyright notice.
Definition output.cpp:87
void Clear()
Clear the image buffer.
Definition output.cpp:131
static void Log(const std::string &text, RGB color)
Print colored log text to the terminal.
Definition output.cpp:43
void Draw(const std::vector< Cell > &sourceImage, UPoint resolution, Point position=Point(0, 0), UPoint start=UPoint(0, 0), UPoint end=UPoint(0, 0), uint8_t alpha=std::numeric_limits< uint8_t >::max())
Draw a Cell-based image .
Definition output.cpp:153
auto ShouldRenderThisTick() -> bool
Check whether things changed and require a new render.
Definition output.cpp:357
auto ShouldPrintThisTick() const -> bool
Check whether the terminal changed and requires a new print.
Definition output.cpp:407
std::vector< std::string > outputOnQuit
Vector of strings to print when the game quits (specifically, in Output::~Output()).
Definition output.hpp:45
2D vector, mostly used to store positions and directions.
Definition point.hpp:30
24-bit color, able of representing 16,777,216 (2^24) different colors.
Definition rgb.hpp:29
Unsigned 2D vector, mostly used to store sizes and 2D indexes.
Definition upoint.hpp:29