font loading and drawing chars
This commit is contained in:
parent
cf538f2cf5
commit
7c62375761
28
src/Game.cpp
28
src/Game.cpp
@ -12,6 +12,7 @@
|
|||||||
const int CHAR_SIZE = 30;
|
const int CHAR_SIZE = 30;
|
||||||
const int STEP_SIZE = CHAR_SIZE + CHAR_SIZE / 2;
|
const int STEP_SIZE = CHAR_SIZE + CHAR_SIZE / 2;
|
||||||
const int UNDERSCORE_DY = 10;
|
const int UNDERSCORE_DY = 10;
|
||||||
|
const SDL_Color TEXT_COLOR = {255, 255, 255};
|
||||||
|
|
||||||
void Game::Run() {
|
void Game::Run() {
|
||||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||||
@ -121,6 +122,33 @@ void Game::draw_guesses(SDL_Renderer *renderer) {
|
|||||||
const SDL_Rect rect = {here, char_y + UNDERSCORE_DY, CHAR_SIZE, 5};
|
const SDL_Rect rect = {here, char_y + UNDERSCORE_DY, CHAR_SIZE, 5};
|
||||||
SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
|
SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
|
||||||
SDL_RenderFillRect(renderer, &rect);
|
SDL_RenderFillRect(renderer, &rect);
|
||||||
|
|
||||||
|
std::optional<char> current_char = guess_corrector->guessed().lock()[i];
|
||||||
|
if (current_char) {
|
||||||
|
const char *text_to_write = &*current_char;
|
||||||
|
|
||||||
|
SDL_Surface *surface = TTF_RenderText_Solid(font, text_to_write, TEXT_COLOR);
|
||||||
|
if (surface == nullptr) {
|
||||||
|
std::cerr << "Failed to create surface: " << TTF_GetError() << std::endl;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Texture *txt = SDL_CreateTextureFromSurface(renderer, surface);
|
||||||
|
if (txt == nullptr) {
|
||||||
|
std::cerr << "Failed to create texture: " << SDL_GetError() << std::endl;
|
||||||
|
SDL_FreeSurface(surface);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int text_width = surface->w;
|
||||||
|
int text_height = surface->h;
|
||||||
|
SDL_Rect text_rect = {here + (CHAR_SIZE - text_width) / 2, char_y - text_height, text_width, text_height};
|
||||||
|
|
||||||
|
SDL_RenderCopy(renderer, txt, nullptr, &text_rect);
|
||||||
|
|
||||||
|
SDL_DestroyTexture(txt);
|
||||||
|
SDL_FreeSurface(surface);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
|
@ -15,7 +15,7 @@ private:
|
|||||||
std::vector<const char *> all_words;
|
std::vector<const char *> all_words;
|
||||||
std::unique_ptr<GuessCorrector> guess_corrector;
|
std::unique_ptr<GuessCorrector> guess_corrector;
|
||||||
const char *word;
|
const char *word;
|
||||||
const _TTF_Font *font;
|
_TTF_Font *font;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static void Run();
|
static void Run();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user