diff --git a/includes/Print.hpp b/includes/Print.hpp index df4a4ee..ad35778 100644 --- a/includes/Print.hpp +++ b/includes/Print.hpp @@ -7,6 +7,7 @@ class Print { static void render_all(const Typing &text, int input); static void text(const Typing &text); + static void text(const Typing &text, size_t n_words); static void text_delimiter(void); static void timer(int seconds); diff --git a/includes/Typing.hpp b/includes/Typing.hpp index 968aea5..fa42d60 100644 --- a/includes/Typing.hpp +++ b/includes/Typing.hpp @@ -22,7 +22,7 @@ class Typing : public TypingStats { void reset(void); TypingWord **get_words(void) const; - size_t get_words_length(void) const; + size_t get_length(void) const; size_t get_current_word_pos(void) const; TypingWord *get_word(void) const; TypingWord *get_word(size_t pos) const; diff --git a/srcs/Print.cpp b/srcs/Print.cpp index ae1bee2..8ae642b 100644 --- a/srcs/Print.cpp +++ b/srcs/Print.cpp @@ -11,19 +11,21 @@ void Print::render_all(const Typing &text, int input) { Print::input_word(text.get_word(), input); } -void Print::text(const Typing &text) { - curs_set(0); +void Print::text(const Typing &text) { Print::text(text, text.get_length()); } +void Print::text(const Typing &text, size_t n_words) { + static bool is_text_y_set = false; - const size_t max_x = stdscr->_maxx - 2; + curs_set(0); TypingWord **words = text.get_words(); const size_t current_word_pos = text.get_current_word_pos(); const TypingWord *current_word = text.get_word(); const size_t current_ch_pos = current_word->get_current_pos(); - Print::reset_text_y(); + const size_t max_x = stdscr->_maxx - 2; + int text_y = Print::_text_y_default; - for (size_t i = 0, start_print_pos_x = 0; words[i]; ++i) { + for (size_t i = 0, start_print_pos_x = 0; words[i] && n_words > i; ++i) { const TypingWord *word = words[i]; const std::string str = word->get_string(); @@ -35,11 +37,11 @@ void Print::text(const Typing &text) { } if (start_print_pos_x + word->get_length() > max_x) { - Print::inc_text_y(); + ++text_y; start_print_pos_x = 0; } - const int y = Print::get_text_y(); + const int y = text_y; const int x = Print::get_text_x(); if (current_word_pos == i) { @@ -60,6 +62,11 @@ void Print::text(const Typing &text) { start_print_pos_x += word->get_length() + 1; } + + if (!is_text_y_set) { + is_text_y_set = true; + Print::_text_y = text_y; + } } void Print::text_delimiter(void) { diff --git a/srcs/Typing.cpp b/srcs/Typing.cpp index fc674e4..a03d394 100644 --- a/srcs/Typing.cpp +++ b/srcs/Typing.cpp @@ -138,7 +138,7 @@ void Typing::reset(void) { } TypingWord **Typing::get_words(void) const { return this->words; } -size_t Typing::get_words_length(void) const { return this->length; } +size_t Typing::get_length(void) const { return this->length; } size_t Typing::get_current_word_pos(void) const { return this->current_word_pos; } diff --git a/srcs/main.cpp b/srcs/main.cpp index 65bc1a3..00ffe13 100644 --- a/srcs/main.cpp +++ b/srcs/main.cpp @@ -72,12 +72,16 @@ int main(int argc, char *argv[]) { Typing test_typing = Typing(__test_strings, __test_strings_length); g_Typing = &test_typing; + Print::text(test_typing); + Print::text_delimiter(); + while (!stop) { box(win, 0, 0); const TypingWord *current_word = test_typing.get_word(); const char current_ch = current_word->get_char(); - Print::render_all(test_typing, input); + Print::text(test_typing, test_typing.get_current_word_pos() + 1); + Print::input_word(current_word, input); input = Typing::get_input(); is_input_ok = false;