-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
402 additions
and
987 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,61 @@ | ||
#include "BitmapFont.h" | ||
#include "Utils.h" | ||
|
||
#include <cstring> | ||
|
||
BitmapFont::BitmapFont(SDL_Renderer* rend, const std::string& path, int charWidth, const std::string& alphabet) | ||
: m_renderer(rend), | ||
m_bitmapCharWidth(charWidth) | ||
: m_renderer(rend) | ||
, m_bitmapCharWidth(charWidth) | ||
, m_bitmap(Utils::LoadTexture(rend, path, nullptr, &m_bitmapHeight)) | ||
{ | ||
m_bitmap = Utils::LoadTexture(rend, path, nullptr, &m_bitmapHeight); | ||
|
||
for (auto& p : m_alphabet) { | ||
p = -1; | ||
} | ||
|
||
int currPos = 0; | ||
for (const auto& c : alphabet) { | ||
if (InsideAlphabet(c)) { | ||
m_alphabet[c - ALPHABET_START] = currPos; | ||
currPos += charWidth; | ||
} | ||
} | ||
std::memset(m_alphabet.data(), -1, m_alphabet.size() * sizeof(m_alphabet.front())); | ||
|
||
int pos = 0; | ||
for (const auto& c : alphabet) { | ||
if (InsideAlphabet(c)) { | ||
m_alphabet[c - ALPHABET_START] = pos; | ||
pos += charWidth; | ||
} | ||
} | ||
} | ||
|
||
BitmapFont::~BitmapFont() | ||
{ | ||
SDL_DestroyTexture(m_bitmap); | ||
SDL_DestroyTexture(m_bitmap); | ||
} | ||
|
||
int BitmapFont::GetLineWidth(const std::string& line, unsigned int fontSize) const | ||
int BitmapFont::GetLineWidth(const std::string& line, int fontSize) const | ||
{ | ||
unsigned int width = 0; | ||
for (const auto& c : line) { | ||
if (InsideAlphabet(c)) { | ||
width += GetCharWidthFontSize(fontSize); | ||
} | ||
} | ||
return width; | ||
int width = 0; | ||
for (const auto& c : line) { | ||
if (InsideAlphabet(c)) { | ||
width += GetCharWidthFontSize(fontSize); | ||
} | ||
} | ||
return width; | ||
} | ||
|
||
void BitmapFont::DrawLine(const std::string& line, unsigned int fontSize, int posX, int posY, bool isCentered) const | ||
void BitmapFont::DrawLine(const std::string& line, int fontSize, int posX, int posY, bool isCentered) const | ||
{ | ||
Utils::PushColor(m_renderer); | ||
SDL_SetRenderDrawColor(m_renderer, 255, 255, 255, 255); | ||
|
||
SDL_Rect dest = { posX, posY, GetCharWidthFontSize(fontSize), static_cast<int>(fontSize) }; | ||
SDL_Rect src = { 0, 0, m_bitmapCharWidth, m_bitmapHeight }; | ||
|
||
for (const auto& c : line) { | ||
// In range and inside alphabet? | ||
if (InsideAlphabet(c) && m_alphabet[c - ALPHABET_START] != -1) { | ||
src.x = m_alphabet[c - ALPHABET_START]; | ||
SDL_RenderCopyEx(m_renderer, m_bitmap, &src, &dest, 0, nullptr, SDL_FLIP_NONE); | ||
dest.x += fontSize; | ||
} | ||
else if (c == ' ') { | ||
dest.x += fontSize; | ||
} | ||
else if (c == '\n') { | ||
dest.y += fontSize; | ||
dest.x = posX; | ||
} | ||
} | ||
|
||
Utils::PopColor(m_renderer); | ||
Utils::PushColor(m_renderer); | ||
SDL_SetRenderDrawColor(m_renderer, 255, 255, 255, 255); | ||
|
||
SDL_Rect dest = { posX, posY, GetCharWidthFontSize(fontSize), fontSize }; | ||
SDL_Rect src = { 0, 0, m_bitmapCharWidth, m_bitmapHeight }; | ||
|
||
for (const auto& c : line) { | ||
// In range and inside alphabet? | ||
if (InsideAlphabet(c) && m_alphabet[c - ALPHABET_START] != -1) { | ||
src.x = m_alphabet[c - ALPHABET_START]; | ||
SDL_RenderCopyEx(m_renderer, m_bitmap, &src, &dest, 0, nullptr, SDL_FLIP_NONE); | ||
dest.x += fontSize; | ||
} else if (c == ' ') { | ||
dest.x += fontSize; | ||
} else if (c == '\n') { | ||
dest.y += fontSize; | ||
dest.x = posX; | ||
} | ||
} | ||
|
||
Utils::PopColor(m_renderer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,58 @@ | ||
#pragma once | ||
|
||
#include <array> | ||
#include <SDL2/SDL.h> | ||
#include <array> | ||
|
||
// Simple bitmap font with variable alphabet loaded from "one-line" image | ||
// Simple bitmap font with alphabet loaded from "one-line" image | ||
class BitmapFont { | ||
private: | ||
|
||
static constexpr int CHAR_WIDTH_OFFSET = -8; | ||
static constexpr int ALPHABET_START = ' '; | ||
static constexpr int ALPHABET_END = '~'; | ||
static constexpr int ALPHABET_SIZE = ALPHABET_END - ALPHABET_START + 1; | ||
|
||
std::array<int, ALPHABET_SIZE> m_alphabet; | ||
int m_bitmapHeight; | ||
int m_bitmapCharWidth; | ||
SDL_Texture* m_bitmap; | ||
SDL_Renderer* m_renderer; | ||
|
||
bool InsideAlphabet(char c) const { return c >= ALPHABET_START && c <= ALPHABET_END; } | ||
int GetCharWidthFontSize(int fontSize) const { return fontSize + CHAR_WIDTH_OFFSET; } | ||
|
||
public: | ||
|
||
BitmapFont(SDL_Renderer* rend, const std::string& path, int charWidth, const std::string& alphabet); | ||
|
||
BitmapFont(const BitmapFont&) = delete; | ||
BitmapFont(BitmapFont&&) = delete; | ||
|
||
BitmapFont& operator=(const BitmapFont&) = delete; | ||
BitmapFont& operator=(BitmapFont&&) = delete; | ||
|
||
BitmapFont(SDL_Renderer* rend, const std::string& path, int charWidth, const std::string& alphabet); | ||
|
||
virtual ~BitmapFont(); | ||
|
||
int GetBitmapCharWidth() const { return m_bitmapCharWidth; } | ||
SDL_Renderer* GetRenderer() { return m_renderer; } | ||
void SetRenderer(SDL_Renderer* rend) { m_renderer = rend; } | ||
int GetBitmapCharWidth() const | ||
{ | ||
return m_bitmapCharWidth; | ||
} | ||
|
||
SDL_Renderer* GetRenderer() | ||
{ | ||
return m_renderer; | ||
} | ||
|
||
void SetRenderer(SDL_Renderer* rend) | ||
{ | ||
m_renderer = rend; | ||
} | ||
|
||
int GetLineWidth(const std::string& line, int fontSize) const; | ||
void DrawLine(const std::string& line, int fontSize, int posX, int posY, bool isCentered = false) const; | ||
|
||
private: | ||
static constexpr auto CHAR_WIDTH_OFFSET = -8; | ||
static constexpr auto ALPHABET_START = ' '; | ||
static constexpr auto ALPHABET_END = '~'; | ||
static constexpr auto ALPHABET_SIZE = ALPHABET_END - ALPHABET_START + 1u; | ||
|
||
std::array<int, ALPHABET_SIZE> m_alphabet; | ||
int m_bitmapHeight; | ||
int m_bitmapCharWidth; | ||
SDL_Texture* m_bitmap; | ||
SDL_Renderer* m_renderer; | ||
|
||
bool InsideAlphabet(char c) const | ||
{ | ||
return c >= ALPHABET_START && c <= ALPHABET_END; | ||
} | ||
|
||
int GetLineWidth(const std::string& line, unsigned int fontSize) const; | ||
void DrawLine(const std::string& line, unsigned int fontSize, int posX, int posY, bool isCentered = false) const; | ||
int GetCharWidthFontSize(int fontSize) const | ||
{ | ||
return fontSize + CHAR_WIDTH_OFFSET; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.