font loading and drawing chars
This commit is contained in:
		
							
								
								
									
										28
									
								
								src/Game.cpp
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								src/Game.cpp
									
									
									
									
									
								
							@@ -12,6 +12,7 @@
 | 
			
		||||
const int CHAR_SIZE = 30;
 | 
			
		||||
const int STEP_SIZE = CHAR_SIZE + CHAR_SIZE / 2;
 | 
			
		||||
const int UNDERSCORE_DY = 10;
 | 
			
		||||
const SDL_Color TEXT_COLOR = {255, 255, 255};
 | 
			
		||||
 | 
			
		||||
void Game::Run() {
 | 
			
		||||
    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};
 | 
			
		||||
        SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
 | 
			
		||||
        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);
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,7 @@ private:
 | 
			
		||||
    std::vector<const char *> all_words;
 | 
			
		||||
    std::unique_ptr<GuessCorrector> guess_corrector;
 | 
			
		||||
    const char *word;
 | 
			
		||||
    const _TTF_Font *font;
 | 
			
		||||
    _TTF_Font *font;
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
    static void Run();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user