formatting of the second message

This commit is contained in:
dvorakmortonxd 2026-08-30 13:31:20 +02:00
commit 57adcdf00d

View file

@ -104,7 +104,7 @@ bool init_game(Game *game) {
game->message1 = "Hello, human.";
game->visible_chars1 = 0;
game->last_char_time1 = SDL_GetTicks();
game->message2 = "It is a known fact that C, is superior to every other programming language.";
game->message2 = "It is a known fact that C,\nis superior to every other programming language.";
game->visible_chars2 = 0;
game->last_char_time2 = 0; // Not used until we switch to message2
game->message_stage = 0; // Start with message1
@ -252,34 +252,66 @@ void render(SDL_Renderer *renderer, Game *game) {
visible_chars_to_render = game->visible_chars2;
}
char buffer[256] = {0}; // Increased buffer size for safety
if (visible_chars_to_render > 0) {
// Copy only the number of characters currently 'revealed'
strncpy(buffer, message_to_render, visible_chars_to_render);
buffer[visible_chars_to_render] = '\0';
SDL_Color white = {255, 255, 255, 255};
SDL_Surface* surface = TTF_RenderText_Blended(game->current_font, buffer, white);
int line_height = TTF_FontLineSkip(game->current_font);
int line_count = 0;
// First pass: count lines to compute total height
int i = 0;
while (i < visible_chars_to_render) {
const char* line_start = message_to_render + i;
// Find end of line (newline or end of visible chars)
int line_len = 0;
while (i + line_len < visible_chars_to_render && message_to_render[i + line_len] != '\n') {
line_len++;
}
line_count++;
i += line_len;
if (i < visible_chars_to_render && message_to_render[i] == '\n') {
i++; // skip newline
}
}
// If no newline encountered, line_count is at least 1
if (line_count == 0) line_count = 1;
int total_height = line_count * line_height;
int start_y = (SCREEN_HEIGHT - total_height) / 2;
// Second pass: render each line
i = 0;
int line_y = 0;
while (i < visible_chars_to_render) {
const char* line_start = message_to_render + i;
int line_len = 0;
while (i + line_len < visible_chars_to_render && message_to_render[i + line_len] != '\n') {
line_len++;
}
// Create temporary buffer for this line
char line_buf[256] = {0};
strncpy(line_buf, line_start, line_len);
line_buf[line_len] = '\0';
SDL_Surface* surface = TTF_RenderText_Blended(game->current_font, line_buf, white);
if (surface) {
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
// CENTER THE TEXT WITH SHAKE:
// Subtract half the text width/height from center screen coordinates.
// Add shake offset for text jitter effect
int shake_offset_x = (rand() % (int)(game->shake_intensity * 2 + 1)) - game->shake_intensity;
int shake_offset_y = (rand() % (int)(game->shake_intensity * 2 + 1)) - game->shake_intensity;
SDL_Rect text_rect = {
(SCREEN_WIDTH - surface->w) / 2 + shake_offset_x,
(SCREEN_HEIGHT - surface->h) / 2 + shake_offset_y,
start_y + line_y + shake_offset_y,
surface->w,
surface->h
};
SDL_RenderCopy(renderer, texture, NULL, &text_rect);
SDL_FreeSurface(surface);
SDL_DestroyTexture(texture);
}
line_y += line_height;
i += line_len;
if (i < visible_chars_to_render && message_to_render[i] == '\n') {
i++; // skip newline
}
}
}
}