Quellcode durchsuchen

Now with little editing, it is executable.

master
Ognjen Milan Robovic vor 6 Monaten
Ursprung
Commit
af5b4f7a75
2 geänderte Dateien mit 35 neuen und 24 gelöschten Zeilen
  1. +31
    -17
      chapter/chapter_2.c
  2. +4
    -7
      chapter/chapter_5.c

+ 31
- 17
chapter/chapter_2.c Datei anzeigen

@@ -160,7 +160,7 @@ int curses_character = 0;
int curses_signal = SIGNAL_NONE; int curses_signal = SIGNAL_NONE;
int curses_screen_width = 0; int curses_screen_width = 0;
int curses_screen_height = 0; int curses_screen_height = 0;
int curses_active = 0;
int curses_active = FALSE;


/* /*
I need to quickly explain how I'm structuring this subprogram. You can think of functions and variables starting with 'curses_*' as tiny standalone library if it's easier. They I need to quickly explain how I'm structuring this subprogram. You can think of functions and variables starting with 'curses_*' as tiny standalone library if it's easier. They
@@ -190,8 +190,12 @@ C can efficiently write good programs using those libraries, please don't misund
our subprogram called 'curses'. our subprogram called 'curses'.
*/ */


static void curses_idle (void) { return; } // If you have a lot of short functions that are intended to be in array of function pointers, you can align them like this.
static void curses_exit (void) { curses_active = 0; } // And this is our main function for quitting main loop in curses.

static void curses_initialize (void) { // This function will be called when 'curses_configure' is called, automatically. static void curses_initialize (void) { // This function will be called when 'curses_configure' is called, automatically.
struct winsize screen_dimension;
struct winsize screen_dimension; // We need this ugly structure for our 'ioctl' function to get the dimensions.
int screen_memory; // And you can use local variables to shorten some lines of code if you want.


fatal_failure (ioctl (STDOUT_FILENO, TIOCGWINSZ, & screen_dimension) == -1, // If function 'ioctl' failed, we immediately aborting the entire program. fatal_failure (ioctl (STDOUT_FILENO, TIOCGWINSZ, & screen_dimension) == -1, // If function 'ioctl' failed, we immediately aborting the entire program.
"ioctl: Failed to get terminal dimensions."); // I split those error messages, you can find your own formatting style. "ioctl: Failed to get terminal dimensions."); // I split those error messages, you can find your own formatting style.
@@ -204,7 +208,7 @@ static void curses_initialize (void) {


curses_new_terminal = curses_old_terminal; // Here we set our raw terminal to be the same as the non-raw one. curses_new_terminal = curses_old_terminal; // Here we set our raw terminal to be the same as the non-raw one.


curses_new_terminal.c_cc [VMIN] = (unsigned char) 0; // Now it's time to modify it to be raw, this essentially means no-echo.
curses_new_terminal.c_cc [VMIN] = (unsigned char) 0; // Now it's time to modify it to be raw.
curses_new_terminal.c_cc [VTIME] = (unsigned char) 1; curses_new_terminal.c_cc [VTIME] = (unsigned char) 1;


curses_new_terminal.c_iflag &= (unsigned int) ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); curses_new_terminal.c_iflag &= (unsigned int) ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
@@ -214,6 +218,16 @@ static void curses_initialize (void) {


fatal_failure (tcsetattr (STDIN_FILENO, TCSAFLUSH, & curses_new_terminal) == -1, // Finally, we're passing intormations to our terminal, and it becomes raw. fatal_failure (tcsetattr (STDIN_FILENO, TCSAFLUSH, & curses_new_terminal) == -1, // Finally, we're passing intormations to our terminal, and it becomes raw.
"tcsetattr: Failed to set reverse terminal attributes."); "tcsetattr: Failed to set reverse terminal attributes.");

screen_memory = CURSES_FORMAT * curses_screen_width * curses_screen_height; // This is square area of our terminal, and multiplied by 12, size of FORMAT.

curses_screen = allocate (CURSES_REVERT + screen_memory + CURSES_CURSOR + 1); // We're requesting new memory for framebuffer.

curses_bind (SIGNAL_ESCAPE, curses_exit); // Binding universal exit key (signal).

string_copy (& curses_screen [0], "\033[H"); // ASCII black magic to always clear screen.

terminal_clear ();
} }


static void curses_deinitialize (void) { // This function will be only called once, automatically, at the program exit. static void curses_deinitialize (void) { // This function will be only called once, automatically, at the program exit.
@@ -221,6 +235,13 @@ static void curses_deinitialize (void) {
curses_activator = deallocate (curses_activator); curses_activator = deallocate (curses_activator);
curses_action = deallocate (curses_action); curses_action = deallocate (curses_action);


curses_action_count = 0;
curses_character = 0;
curses_signal = SIGNAL_NONE;
curses_screen_width = 0;
curses_screen_height = 0;
curses_active = FALSE;

terminal_clear (); // This only make things look prettier, we don't have mess when we exit the program. terminal_clear (); // This only make things look prettier, we don't have mess when we exit the program.


fatal_failure (tcsetattr (STDIN_FILENO, TCSAFLUSH, & curses_old_terminal) == -1, // Again, if this fails, we're doomed, we're aborting. fatal_failure (tcsetattr (STDIN_FILENO, TCSAFLUSH, & curses_old_terminal) == -1, // Again, if this fails, we're doomed, we're aborting.
@@ -308,27 +329,16 @@ static char * curses_format_character (char character, int colour, int effect) {
return (curses_format); // And we return the value (pointer to character) of formatted internal variables. return (curses_format); // And we return the value (pointer to character) of formatted internal variables.
} }


static void curses_idle (void) { return; } // If you have a lot of short functions that are intended to be in array of function pointers, you can align them like this.
static void curses_exit (void) { curses_active = 0; } // And this is our main function for quitting main loop in curses.

/* /*
External function definitions, those found in "chapter_2.h" header file. External function definitions, those found in "chapter_2.h" header file.
*/ */


void curses_configure (void) { void curses_configure (void) {
curses_active = TRUE;

atexit (curses_deinitialize); // Deinitialization is automatically on exit.

curses_initialize (); // Initializing curses, yaay.
curses_active = TRUE; // This is important part, and something I like to do in my standalone libraries.


curses_screen = allocate (CURSES_REVERT + CURSES_FORMAT * curses_screen_width * curses_screen_height + CURSES_CURSOR + 1); // We're requesting new memory for framebuffer.
atexit (curses_deinitialize); // Deinitialization is automatically executed on exit point of the program, since we called 'atexit' function.


curses_bind (SIGNAL_ESCAPE, curses_exit); // Binding universal exit key (signal).

terminal_clear ();

string_copy (& curses_screen [0], "\033[H"); // ASCII black magic to always clear screen.
curses_initialize (); // Initializing curses, finally, yaay.
} }


void curses_synchronize (void) { void curses_synchronize (void) {
@@ -360,6 +370,10 @@ void curses_synchronize (void) {
curses_action [signal] (); // We execute corresponding action (function). curses_action [signal] (); // We execute corresponding action (function).
} }
} }

if (curses_active == FALSE) { // Lastly, if we exited curses, we want to deinitialize.
curses_deinitialize (); // It's no problem if we do it more than once...
}
} }


/* /*


+ 4
- 7
chapter/chapter_5.c Datei anzeigen

@@ -10,11 +10,10 @@ It is distributed in the hope that it will be useful or harmful, it really depen
#define CHAPTER_5_SOURCE #define CHAPTER_5_SOURCE


#include "chapter_5.h" #include "chapter_5.h"
/*
static void (* game_action [GAME_ACTION_COUNT]) (game_t * game, player_t * player); static void (* game_action [GAME_ACTION_COUNT]) (game_t * game, player_t * player);

static number_t game_is_active (game_t * game) { return (game->active); }
static number_t game_exit (game_t * game) { return (game->active = FALSE); }/*
*/
static number_t game_is_active (game_t * game) { return (game->active = curses_active); }/*
static number_t game_get_screen_width (game_t * game) { return (game->screen_width); } static number_t game_get_screen_width (game_t * game) { return (game->screen_width); }
static number_t game_get_screen_height (game_t * game) { return (game->screen_height); } static number_t game_get_screen_height (game_t * game) { return (game->screen_height); }
static number_t game_set_screen_width (game_t * game, number_t width) { return (game->screen_width = width); } static number_t game_set_screen_width (game_t * game, number_t width) { return (game->screen_width = width); }
@@ -23,9 +22,7 @@ static number_t game_set_screen_height (game_t * game, number_t height) { return
static void game_configure (game_t * game, player_t * player) { static void game_configure (game_t * game, player_t * player) {
curses_configure (); curses_configure ();


curses_bind (SIGNAL_Q, game_exit);

game->active = TRUE;
game->active = curses_active;
game->screen_width = curses_screen_width; game->screen_width = curses_screen_width;
game->screen_height = curses_screen_height; game->screen_height = curses_screen_height;




Laden…
Abbrechen
Speichern