Nugget
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages Concepts
font.hh
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2022 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#pragma once
28
29#include <EASTL/array.h>
30#include <EASTL/functional.h>
31#include <EASTL/string_view.h>
32#include <stdarg.h>
33#include <stdint.h>
34
35#include "psyqo/fragments.hh"
36#include "psyqo/gpu.hh"
39
40namespace psyqo {
41
42template <size_t Fragments = 16>
43class Font;
44
57class FontBase {
58 public:
59 virtual ~FontBase() {}
60
71 void uploadSystemFont(GPU& gpu, Vertex location = {{.x = 960, .y = 464}});
72
84 void uploadKromFont(GPU& gpu, Vertex location = {{.x = 960, .y = 422}});
85
92 static void unpackFont(GPU& gpu, const uint8_t* data, Vertex location, Vertex textureSize);
93
103 void initialize(GPU& gpu, Vertex location, Vertex glyphSize);
104
113 void print(GPU& gpu, eastl::string_view text, Vertex pos, Color color);
114 void print(GPU& gpu, eastl::string_view text, Vertex pos, Color color, eastl::function<void()>&& callback,
115 DMA::DmaCallback dmaCallback);
116 void print(GPU& gpu, const char* text, Vertex pos, Color color);
117 void print(GPU& gpu, const char* text, Vertex pos, Color color, eastl::function<void()>&& callback,
118 DMA::DmaCallback dmaCallback);
119 void printf(GPU& gpu, Vertex pos, Color color, const char* format, ...) {
120 va_list args;
121 va_start(args, format);
122 vprintf(gpu, pos, color, format, args);
123 va_end(args);
124 }
125 void printf(GPU& gpu, Vertex pos, Color color, eastl::function<void()>&& callback, DMA::DmaCallback dmaCallback,
126 const char* format, ...) {
127 va_list args;
128 va_start(args, format);
129 vprintf(gpu, pos, color, eastl::move(callback), dmaCallback, format, args);
130 va_end(args);
131 }
132 void vprintf(GPU& gpu, Vertex pos, Color color, const char* format, va_list ap);
133 void vprintf(GPU& gpu, Vertex pos, Color color, eastl::function<void()>&& callback, DMA::DmaCallback dmaCallback,
134 const char* format, va_list ap);
135
143 void chainprint(GPU& gpu, eastl::string_view text, Vertex pos, Color color);
144 void chainprint(GPU& gpu, const char* text, Vertex pos, Color color);
145 void chainprintf(GPU& gpu, Vertex pos, Color color, const char* format, ...) {
146 va_list args;
147 va_start(args, format);
148 chainvprintf(gpu, pos, color, format, args);
149 va_end(args);
150 }
151 void chainvprintf(GPU& gpu, Vertex pos, Color color, const char* format, va_list ap);
152
153 protected:
161 virtual GlyphsFragment* getGlyphFragment(bool increment) = 0;
162 virtual void forEach(eastl::function<void(GlyphsFragment&)>&& cb) = 0;
163
164 void innerprint(GlyphsFragment* fragment, GPU& gpu, eastl::string_view text, Vertex pos, Color color);
165 void innerprint(GlyphsFragment* fragment, GPU& gpu, const char* text, Vertex pos, Color color);
166 void innervprintf(GlyphsFragment* fragment, GPU& gpu, Vertex pos, Color color, const char* format, va_list ap);
167
168 private:
169 struct XPrintfInfo;
170 GlyphsFragment& printToFragment(GPU& gpu, const char* text, Vertex pos, Color color);
171 eastl::array<PrimPieces::TexInfo, 224> m_lut;
172 Vertex m_glyphSize;
173
174 friend struct XPrintfInfo;
175};
176
177template <size_t N>
178class Font : public FontBase {
179 public:
180 virtual ~Font() { static_assert(N > 0, "Needs to have at least one fragment"); }
181
182 private:
183 virtual GlyphsFragment* getGlyphFragment(bool increment) override {
184 auto fragment = &m_fragments[m_index];
185 if (increment) {
186 if (++m_index == N) {
187 m_index = 0;
188 }
189 }
190 return fragment;
191 }
192 virtual void forEach(eastl::function<void(GlyphsFragment&)>&& cb) override {
193 for (auto& fragment : m_fragments) {
194 cb(fragment);
195 }
196 }
197 eastl::array<GlyphsFragment, N> m_fragments;
198 unsigned m_index = 0;
199};
200
201} // namespace psyqo
The Font drawing class.
Definition font.hh:57
void chainprintf(GPU &gpu, Vertex pos, Color color, const char *format,...)
Definition font.hh:145
void printf(GPU &gpu, Vertex pos, Color color, const char *format,...)
Definition font.hh:119
void print(GPU &gpu, eastl::string_view text, Vertex pos, Color color)
These method immediately print text to the screen.
Definition font.cpp:262
static void unpackFont(GPU &gpu, const uint8_t *data, Vertex location, Vertex textureSize)
Unpacks and uploads a font to VRAM.
Definition font.cpp:184
Fragments::FixedFragmentWithPrologue< GlyphsFragmentPrologue, Prim::Sprite, 48 > GlyphsFragment
Definition font.hh:160
virtual GlyphsFragment * getGlyphFragment(bool increment)=0
virtual void forEach(eastl::function< void(GlyphsFragment &)> &&cb)=0
void vprintf(GPU &gpu, Vertex pos, Color color, const char *format, va_list ap)
Definition font.cpp:373
virtual ~FontBase()
Definition font.hh:59
void uploadSystemFont(GPU &gpu, Vertex location={{.x=960,.y=464}})
Uploads the system font to VRAM, and initializes the object.
Definition font.cpp:38
void innervprintf(GlyphsFragment *fragment, GPU &gpu, Vertex pos, Color color, const char *format, va_list ap)
Definition font.cpp:410
void chainvprintf(GPU &gpu, Vertex pos, Color color, const char *format, va_list ap)
Definition font.cpp:395
void chainprint(GPU &gpu, eastl::string_view text, Vertex pos, Color color)
These methods use the DMA chaining system to print text to the screen.
Definition font.cpp:306
void innerprint(GlyphsFragment *fragment, GPU &gpu, eastl::string_view text, Vertex pos, Color color)
Definition font.cpp:318
void initialize(GPU &gpu, Vertex location, Vertex glyphSize)
Initializes the object for use.
Definition font.cpp:230
void uploadKromFont(GPU &gpu, Vertex location={{.x=960,.y=422}})
Uploads the Kernel rom font to VRAM, and initializes the object.
Definition font.cpp:43
void printf(GPU &gpu, Vertex pos, Color color, eastl::function< void()> &&callback, DMA::DmaCallback dmaCallback, const char *format,...)
Definition font.hh:125
Definition font.hh:178
virtual ~Font()
Definition font.hh:180
The singleton GPU class.
Definition gpu.hh:88
int format(const char *deviceName)
Definition filesystem.c:60
DmaCallback
Definition gpu.hh:52
Definition cdrom-loader.hh:39
uint32_t pixel
Definition font.hh:156
Prim::TPage tpage
Definition font.hh:158
Prim::FlushCache flushCache
Definition font.hh:157
Prim::VRAMUpload upload
Definition font.hh:155
Definition font.cpp:401
A maximum fixed sized fragment of similar primitives.
Definition fragments.hh:121
The FlushCache primitive.
Definition misc.hh:45
Definition control.hh:37
Initiates a VRAM upload.
Definition control.hh:168
void uint32_t(classId, spec)
Definition gpu.h:109
The Color struct.
Definition common.hh:91
The Vertex struct.
Definition common.hh:47
int16_t x
Definition common.hh:50