Skip to content

Commit

Permalink
a bit optimized rendering of already typed words
Browse files Browse the repository at this point in the history
  • Loading branch information
Iipal committed Apr 29, 2021
1 parent b918015 commit 93a4fc6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
1 change: 1 addition & 0 deletions includes/Print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion includes/Typing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 14 additions & 7 deletions srcs/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion srcs/Typing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 5 additions & 1 deletion srcs/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 93a4fc6

Please sign in to comment.