OSDN Git Service

[Refactor] monster_idxと0との比較を関数化する
[hengbandforosx/hengbandosx.git] / src / main-cap.cpp
index 411ec53..aae411a 100644 (file)
@@ -1,9 +1,11 @@
-/* File: main-cap.c */
+/* File: main-cap.c */
 
 /* Purpose: Support for "term.c" using "termcap" calls */
 
 #include "io/exit-panic.h"
 #include "system/angband.h"
+#include "term/gameterm.h"
+#include "term/z-form.h"
 
 #ifdef USE_CAP
 
@@ -150,16 +152,18 @@ static void ewrite(char *str)
         numwritten = write(1, str, numtowrite);
 
         /* Handle FIFOs and EINTR */
-        if (numwritten < 0)
+        if (numwritten < 0) {
             numwritten = 0;
+        }
 
         /* See what we completed */
         numtowrite -= numwritten;
         str += numwritten;
 
         /* Hack -- sleep if not done */
-        if (numtowrite > 0)
+        if (numtowrite > 0) {
             sleep(1);
+        }
     }
 }
 
@@ -204,8 +208,9 @@ static void tp(char *s)
  */
 static void do_cl(void)
 {
-    if (cl)
+    if (cl) {
         tp(cl);
+    }
 }
 
 /*
@@ -213,8 +218,9 @@ static void do_cl(void)
  */
 static void do_ce(void)
 {
-    if (ce)
+    if (ce) {
         tp(ce);
+    }
 }
 
 /*
@@ -232,8 +238,9 @@ static void curs_set(int vis)
         v = ve ? ve : vs;
     }
 
-    if (v)
+    if (v) {
         tp(v);
+    }
 }
 
 /*
@@ -243,13 +250,14 @@ static void do_cs(int y1, int y2)
 {
 
 #ifdef USE_TERMCAP
-    if (cs)
+    if (cs) {
         tp(tgoto(cs, y2, y1));
+    }
 #endif
 
 #ifdef USE_HARDCODE
     char temp[64];
-    sprintf(temp, cs, y1, y2);
+    strnfmt(temp, sizeof(temp), cs, y1, y2);
     tp(temp);
 #endif
 }
@@ -261,13 +269,14 @@ static void do_cm(int x, int y)
 {
 
 #ifdef USE_TERMCAP
-    if (cm)
+    if (cm) {
         tp(tgoto(cm, x, y));
+    }
 #endif
 
 #ifdef USE_HARDCODE
     char temp[64];
-    sprintf(temp, cm, y + 1, x + 1);
+    strnfmt(temp, sizeof(temp), cm, y + 1, x + 1);
     tp(temp);
 #endif
 }
@@ -280,24 +289,27 @@ static void do_cm(int x, int y)
 static void do_move(int x1, int y1, int x2, int y2)
 {
     /* Hack -- unknown start location */
-    if ((x1 == x2) && (y1 == y2))
+    if ((x1 == x2) && (y1 == y2)) {
         do_cm(x2, y2);
+    }
 
     /* Left edge */
     else if (x2 == 0) {
-        if ((y2 <= 0) && ho)
+        if ((y2 <= 0) && ho) {
             tp(ho);
-        else if ((y2 >= rows - 1) && ll)
+        } else if ((y2 >= rows - 1) && ll) {
             tp(ll);
-        else if ((y2 == y1) && cr)
+        } else if ((y2 == y1) && cr) {
             tp(cr);
-        else
+        } else {
             do_cm(x2, y2);
+        }
     }
 
     /* Default -- go directly there */
-    else
+    else {
         do_cm(x2, y2);
+    }
 }
 
 /*
@@ -310,23 +322,28 @@ errr init_cap_aux(void)
 
     /* Get the terminal name (if possible) */
     desc = getenv("TERM");
-    if (!desc)
+    if (!desc) {
         return 1;
+    }
 
     /* Get the terminal info */
-    if (tgetent(blob, desc) != 1)
+    if (tgetent(blob, desc) != 1) {
         return 2;
+    }
 
     /* Get the (initial) columns and rows, or default */
-    if ((cols = tgetnum("co")) == -1)
-        cols = 80;
-    if ((rows = tgetnum("li")) == -1)
-        rows = 24;
+    if ((cols = tgetnum("co")) == -1) {
+        cols = TERM_DEFAULT_COLS;
+    }
+    if ((rows = tgetnum("li")) == -1) {
+        rows = TERM_DEFAULT_ROWS;
+    }
 
     /* Find out how to move the cursor to a given location */
     cm = tgetstr("cm", &next);
-    if (!cm)
+    if (!cm) {
         return 10;
+    }
 
     /* Find out how to move the cursor to a given position */
     ch = tgetstr("ch", &next);
@@ -340,13 +357,15 @@ errr init_cap_aux(void)
 
     /* Find out how to do a "carriage return" */
     cr = tgetstr("cr", &next);
-    if (!cr)
+    if (!cr) {
         cr = "\r";
+    }
 
     /* Find out how to clear the screen */
     cl = tgetstr("cl", &next);
-    if (!cl)
+    if (!cl) {
         return 11;
+    }
 
     /* Find out how to clear to the end of display */
     cd = tgetstr("cd", &next);
@@ -360,14 +379,16 @@ errr init_cap_aux(void)
     /* Find out how to hilite */
     so = tgetstr("so", &next);
     se = tgetstr("se", &next);
-    if (!so || !se)
+    if (!so || !se) {
         so = se = nullptr;
+    }
 
     /* Find out how to bold */
     md = tgetstr("md", &next);
     me = tgetstr("me", &next);
-    if (!md || !me)
+    if (!md || !me) {
         md = me = nullptr;
+    }
 
     /* Check the cursor visibility stuff */
     vi = tgetstr("vi", &next);
@@ -379,8 +400,8 @@ errr init_cap_aux(void)
 #ifdef USE_HARDCODE
 
     /* Assume some defualt information */
-    rows = 24;
-    cols = 80;
+    rows = TERM_DEFAULT_ROWS;
+    cols = TERM_DEFAULT_COLS;
 
     /* Clear screen */
     cl = "\033[2J\033[H"; /* --]--]-- */
@@ -654,12 +675,13 @@ static void keymap_game_prepare(void)
 /*
  * Suspend/Resume
  */
-static errr Term_xtra_cap_alive(int v)
+static errr game_term_xtra_cap_alive(int v)
 {
     /* Suspend */
     if (!v) {
-        if (!active)
+        if (!active) {
             return 1;
+        }
 
         /* Hack -- make sure the cursor is visible */
         curs_set(1);
@@ -676,8 +698,9 @@ static errr Term_xtra_cap_alive(int v)
 
     /* Resume */
     else {
-        if (active)
+        if (active) {
             return 1;
+        }
 
         /* Hack -- restore the cursor location */
         do_move(curx, cury, curx, cury);
@@ -699,7 +722,7 @@ static errr Term_xtra_cap_alive(int v)
 /*
  * Process an event
  */
-static errr Term_xtra_cap_event(int v)
+static errr game_term_xtra_cap_event(int v)
 {
     int i, arg;
     char buf[2];
@@ -710,34 +733,39 @@ static errr Term_xtra_cap_event(int v)
         i = read(0, buf, 1);
 
         /* Hack -- Handle "errors" */
-        if ((i <= 0) && (errno != EINTR))
+        if ((i <= 0) && (errno != EINTR)) {
             exit_game_panic(p_ptr);
+        }
     }
 
     /* Do not wait */
     else {
         /* Get the current flags for stdin */
-        if ((arg = fcntl(0, F_GETFL, 0)) < 1)
+        if ((arg = fcntl(0, F_GETFL, 0)) < 1) {
             return 1;
+        }
 
         /* Tell stdin not to block */
-        if (fcntl(0, F_SETFL, arg | O_NDELAY) < 0)
+        if (fcntl(0, F_SETFL, arg | O_NDELAY) < 0) {
             return 1;
+        }
 
         /* Read one byte, if possible */
         i = read(0, buf, 1);
 
         /* Replace the flags for stdin */
-        if (fcntl(0, F_SETFL, arg))
+        if (fcntl(0, F_SETFL, arg)) {
             return 1;
+        }
     }
 
     /* No keys ready */
-    if ((i != 1) || (!buf[0]))
+    if ((i != 1) || (!buf[0])) {
         return 1;
+    }
 
     /* Enqueue the keypress */
-    Term_keypress(buf[0]);
+    game_term_keypress(buf[0]);
 
     /* Success */
     return 0;
@@ -746,7 +774,7 @@ static errr Term_xtra_cap_event(int v)
 /*
  * Actually move the hardware cursor
  */
-static errr Term_curs_cap(int x, int y)
+static errr game_term_curs_cap(int x, int y)
 {
     /* Literally move the cursor */
     do_move(curx, cury, x, y);
@@ -766,15 +794,15 @@ static errr Term_curs_cap(int x, int y)
  * bottom line all the way to the bottom right edge, since we
  * have set the "avoid the bottom right corner" flag.
  */
-static errr Term_wipe_cap(int x, int y, int n)
+static errr game_term_wipe_cap(int x, int y, int n)
 {
     int dx;
 
     /* Place the cursor */
-    Term_curs_cap(x, y);
+    game_term_curs_cap(x, y);
 
     /* Wipe to end of line */
-    if (x + n >= 80) {
+    if (x + n >= MAIN_TERM_MIN_COLS) {
         do_ce();
     }
 
@@ -793,12 +821,12 @@ static errr Term_wipe_cap(int x, int y, int n)
 /*
  * Place some text on the screen using an attribute
  */
-static errr Term_text_cap(int x, int y, int n, byte a, concptr s)
+static errr game_term_text_cap(int x, int y, int n, byte a, concptr s)
 {
     int i;
 
     /* Move the cursor */
-    Term_curs_cap(x, y);
+    game_term_curs_cap(x, y);
 
     /* Dump the text, advance the cursor */
     for (i = 0; s[i]; i++) {
@@ -811,8 +839,9 @@ static errr Term_text_cap(int x, int y, int n, byte a, concptr s)
             curx = 0;
 
             /* Hack -- Advance cursor 'Y', and wrap */
-            if (++cury == rows)
+            if (++cury == rows) {
                 cury = 0;
+            }
         }
     }
 
@@ -823,7 +852,7 @@ static errr Term_text_cap(int x, int y, int n, byte a, concptr s)
 /*
  * Handle a "special request"
  */
-static errr Term_xtra_cap(int n, int v)
+static errr game_term_xtra_cap(int n, int v)
 {
     /* Analyze the request */
     switch (n) {
@@ -846,16 +875,17 @@ static errr Term_xtra_cap(int n, int v)
 
     /* Suspend/Resume */
     case TERM_XTRA_ALIVE:
-        return Term_xtra_cap_alive(v);
+        return game_term_xtra_cap_alive(v);
 
     /* Process events */
     case TERM_XTRA_EVENT:
-        return Term_xtra_cap_event(v);
+        return game_term_xtra_cap_event(v);
 
     /* Flush events */
     case TERM_XTRA_FLUSH:
-        while (!Term_xtra_cap_event(false))
+        while (!game_term_xtra_cap_event(false)) {
             ;
+        }
         return 0;
 
     /* Delay */
@@ -871,10 +901,11 @@ static errr Term_xtra_cap(int n, int v)
 /*
  * Init a "term" for this file
  */
-static void Term_init_cap(term *t)
+static void game_term_init_cap(term *t)
 {
-    if (active)
+    if (active) {
         return;
+    }
 
     /* Assume cursor at top left */
     curx = 0;
@@ -896,10 +927,11 @@ static void Term_init_cap(term *t)
 /*
  * Nuke a "term" for this file
  */
-static void Term_nuke_cap(term *t)
+static void game_term_nuke_cap(term *t)
 {
-    if (!active)
+    if (!active) {
         return;
+    }
 
     /* Hack -- make sure the cursor is visible */
     curs_set(1);
@@ -924,12 +956,14 @@ errr init_cap(void)
     /*** Initialize ***/
 
     /* Initialize the screen */
-    if (init_cap_aux())
+    if (init_cap_aux()) {
         return -1;
+    }
 
     /* Hack -- Require large screen, or Quit with message */
-    if ((rows < 24) || (cols < 80))
+    if ((rows < MAIN_TERM_MIN_ROWS) || (cols < MAIN_TERM_MIN_COLS)) {
         quit("Screen too small!");
+    }
 
     /*** Prepare to play ***/
 
@@ -948,7 +982,7 @@ errr init_cap(void)
     /*** Now prepare the term ***/
 
     /* Initialize the term */
-    term_init(t, 80, 24, 256);
+    term_init(t, TERM_DEFAULT_COLS, TERM_DEFAULT_ROWS, 256);
 
     /* Avoid the bottom right corner */
     t->icky_corner = true;
@@ -958,20 +992,20 @@ errr init_cap(void)
     t->char_blank = ' ';
 
     /* Set some hooks */
-    t->init_hook = Term_init_cap;
-    t->nuke_hook = Term_nuke_cap;
+    t->init_hook = game_term_init_cap;
+    t->nuke_hook = game_term_nuke_cap;
 
     /* Set some more hooks */
-    t->text_hook = Term_text_cap;
-    t->wipe_hook = Term_wipe_cap;
-    t->curs_hook = Term_curs_cap;
-    t->xtra_hook = Term_xtra_cap;
+    t->text_hook = game_term_text_cap;
+    t->wipe_hook = game_term_wipe_cap;
+    t->curs_hook = game_term_curs_cap;
+    t->xtra_hook = game_term_xtra_cap;
 
     /* Save the term */
     term_screen = t;
 
     /* Activate it */
-    Term_activate(term_screen);
+    game_term_activate(term_screen);
 
     /* Success */
     return 0;