formatting of the second message
This commit is contained in:
parent
65f1c203a7
commit
57adcdf00d
1 changed files with 58 additions and 26 deletions
78
src/main.c
78
src/main.c
|
|
@ -104,7 +104,7 @@ bool init_game(Game *game) {
|
||||||
game->message1 = "Hello, human.";
|
game->message1 = "Hello, human.";
|
||||||
game->visible_chars1 = 0;
|
game->visible_chars1 = 0;
|
||||||
game->last_char_time1 = SDL_GetTicks();
|
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->visible_chars2 = 0;
|
||||||
game->last_char_time2 = 0; // Not used until we switch to message2
|
game->last_char_time2 = 0; // Not used until we switch to message2
|
||||||
game->message_stage = 0; // Start with message1
|
game->message_stage = 0; // Start with message1
|
||||||
|
|
@ -252,33 +252,65 @@ void render(SDL_Renderer *renderer, Game *game) {
|
||||||
visible_chars_to_render = game->visible_chars2;
|
visible_chars_to_render = game->visible_chars2;
|
||||||
}
|
}
|
||||||
|
|
||||||
char buffer[256] = {0}; // Increased buffer size for safety
|
|
||||||
if (visible_chars_to_render > 0) {
|
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_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;
|
||||||
|
|
||||||
if (surface) {
|
int total_height = line_count * line_height;
|
||||||
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
|
int start_y = (SCREEN_HEIGHT - total_height) / 2;
|
||||||
|
|
||||||
// CENTER THE TEXT WITH SHAKE:
|
// Second pass: render each line
|
||||||
// Subtract half the text width/height from center screen coordinates.
|
i = 0;
|
||||||
// Add shake offset for text jitter effect
|
int line_y = 0;
|
||||||
int shake_offset_x = (rand() % (int)(game->shake_intensity * 2 + 1)) - game->shake_intensity;
|
while (i < visible_chars_to_render) {
|
||||||
int shake_offset_y = (rand() % (int)(game->shake_intensity * 2 + 1)) - game->shake_intensity;
|
const char* line_start = message_to_render + i;
|
||||||
SDL_Rect text_rect = {
|
int line_len = 0;
|
||||||
(SCREEN_WIDTH - surface->w) / 2 + shake_offset_x,
|
while (i + line_len < visible_chars_to_render && message_to_render[i + line_len] != '\n') {
|
||||||
(SCREEN_HEIGHT - surface->h) / 2 + shake_offset_y,
|
line_len++;
|
||||||
surface->w,
|
}
|
||||||
surface->h
|
// Create temporary buffer for this line
|
||||||
};
|
char line_buf[256] = {0};
|
||||||
|
strncpy(line_buf, line_start, line_len);
|
||||||
|
line_buf[line_len] = '\0';
|
||||||
|
|
||||||
SDL_RenderCopy(renderer, texture, NULL, &text_rect);
|
SDL_Surface* surface = TTF_RenderText_Blended(game->current_font, line_buf, white);
|
||||||
SDL_FreeSurface(surface);
|
if (surface) {
|
||||||
SDL_DestroyTexture(texture);
|
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||||
|
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,
|
||||||
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue