From 3639266117b8cf0f5886692ba920efde974ea9df Mon Sep 17 00:00:00 2001 From: xolatile Date: Tue, 28 Nov 2023 15:30:55 -0500 Subject: [PATCH] Added rectangle rendering... --- chapter/chapter_2.c | 8 ++++++++ chapter/chapter_2.h | 2 ++ compile.sh | 3 +++ program/program_2.c | 21 +++++++++++---------- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/chapter/chapter_2.c b/chapter/chapter_2.c index 9f40d13..43aa9b4 100644 --- a/chapter/chapter_2.c +++ b/chapter/chapter_2.c @@ -464,6 +464,14 @@ void curses_render_background (char character, int colour, int effect) { } } +void curses_render_rectangle (char character, int colour, int effect, int x, int y, int width, int height) { + for (int j = 0; j < height; ++j) { // Iterating through rows (by height) of our framebuffer ('curses_screen'). + for (int i = 0; i < width; ++i) { // Iterating through columns (by width) of our framebuffer. + curses_render_character (character, colour, effect, x + i, y + j); // Now, we can use function 'curses_render_character' to simplify our life... + } + } +} + /* We've mentioned before, in chapter zero, that you can implement 'string_*' functions by 'string_*_limit', and here's an example of that. Functions below are quite self explanatory, so you can do a "homework" of reading them and trying to understand what they'll do. Since I use very verbose naming style, I hope that won't be a problem... diff --git a/chapter/chapter_2.h b/chapter/chapter_2.h index e0432be..0a557c9 100644 --- a/chapter/chapter_2.h +++ b/chapter/chapter_2.h @@ -170,6 +170,8 @@ extern void curses_render_cursor (int x, int y); // Render terminal cursor at po extern void curses_render_character (char character, int colour, int effect, int x, int y); // Render single character at position X and Y. extern void curses_render_background (char character, int colour, int effect); // Render entire buffer with the same character. +extern void curses_render_rectangle (char character, int colour, int effect, int x, int y, int width, int height); // Guess what this function does...? + // Remember that in chapter zero, I've separated 'string_*' and 'string_*_limit' functions. Now, there's always more ways to logically organize your code, for example, as below: extern int curses_render_string (char * string, int colour, int effect, int x, int y); extern int curses_render_number (int number, int colour, int effect, int x, int y); diff --git a/compile.sh b/compile.sh index 37325c7..f175001 100644 --- a/compile.sh +++ b/compile.sh @@ -18,6 +18,9 @@ gcc -o xhartae xhartae.o chapter/chapter_0.o chapter/chapter_1.o chapter/chapter gcc -g -Wall -Wextra -Wpedantic -Werror -O0 -o program/program_0 program/program_0.c gcc -g -Wall -Wextra -Wpedantic -Werror -O0 -o program/program_1 program/program_1.c +gcc -g -Wall -Wextra -Wpedantic -Werror -O0 -o program/program_2 program/program_2.c + +gcc -g -Wall -Wextra -Wpedantic -Werror -Ofast -o play_game program/program_z.c #~splint -weak -warnposix -retvalother -syntax -type chapter/chapter_0.h #~splint -weak -warnposix -retvalother -syntax -type chapter/chapter_0.c diff --git a/program/program_2.c b/program/program_2.c index c5b7732..c866df8 100644 --- a/program/program_2.c +++ b/program/program_2.c @@ -22,23 +22,24 @@ int main (void) { terminal_show_cursor (FALSE); curses_configure (); -/* + curses_bind (SIGNAL_ARROW_UP, player_move_up); curses_bind (SIGNAL_ARROW_DOWN, player_move_down); curses_bind (SIGNAL_ARROW_LEFT, player_move_left); curses_bind (SIGNAL_ARROW_RIGHT, player_move_right); -*/ + while (curses_active) { - curses_render_background ('.', COLOUR_GREY, EFFECT_BOLD); + curses_render_background (' ', COLOUR_WHITE, EFFECT_NORMAL); + curses_render_rectangle ('.', COLOUR_GREY, EFFECT_BOLD, 0, 0, 80, 24); curses_render_character ('@', COLOUR_CYAN, EFFECT_BOLD, player_x, player_y); - switch (curses_character) { - case 'w': player_move_up (); break; - case 'a': player_move_down (); break; - case 's': player_move_left (); break; - case 'd': player_move_right (); break; - default: break; - } + //~switch (curses_character) { + //~case 'w': player_move_up (); break; + //~case 's': player_move_down (); break; + //~case 'a': player_move_left (); break; + //~case 'd': player_move_right (); break; + //~default: break; + //~} curses_synchronize (); }