Browse Source

Updated ansi-to-src to python3.

Steve Thielemann 3 years ago
parent
commit
3b647bd469
4 changed files with 119 additions and 10 deletions
  1. 77 0
      ansi-to-src.py
  2. 1 1
      build_images.sh
  3. 39 9
      main.cpp
  4. 2 0
      utils.h

+ 77 - 0
ansi-to-src.py

@@ -0,0 +1,77 @@
+#!/usr/bin/env python3
+
+import sys
+
+##
+## Convert ANSI art files to C++ header file.
+##
+
+filenames = sys.argv
+filenames.pop(0)
+
+if not filenames:
+    print("I need filename(s) to convert.")
+    sys.exit(0)
+
+
+
+def my_repr(line):
+    """ Given a latin1 line, output valid C++ string.
+
+    if the character is < 0x20 or > 0x7e, output in hex format.
+    if the character is ', " or ?, output \', \", or \?.
+
+    """
+    r = ""
+    for c in line:
+        o = ord(c)
+        if ((o< 0x20) or (o > 0x7e)):
+            r += "\\x{0:02x}".format(o)
+        else:
+            if c == "'":
+                r += "\\'"
+            elif c == '"':
+                r += '\\"'
+            elif c == '?':
+                r += '\\?'
+            else:
+                r += c
+    return r    
+
+def readfile(filename):
+    """ Reads the given ANSI file and outputs valid C++ code. """
+
+    # basename will be the name of the array defined in the headerfile.
+    basename = filename
+    pos = basename.find('.')
+    if pos != -1:
+        basename = basename[:pos]
+    pos = basename.rfind('/')
+    if pos != -1:
+        basename = basename[pos+1:]
+    
+    # Read the file into lines
+    lines = []
+    with open(filename, "r", encoding="latin1") as fp:
+        for line in fp:
+            line = line.rstrip("\r\n")
+            lines.append(line)
+
+    print("std::array<const char *,{1}> {0} = {{".format(basename, len(lines)))
+    first = True
+    for line in lines:
+        if not first:
+            print(",")
+        else:
+            first = False
+        print("    \"{0}\"".format(my_repr(line)), end='')
+    print(" };\n")
+
+# Begin the output process
+print("#include <array>\n")
+
+# Process each file given
+for filename in filenames:
+    readfile(filename)
+
+

+ 1 - 1
build_images.sh

@@ -3,7 +3,7 @@
 BASE=.$$images.h
 FINAL=images.h
 
-ansi-to-src ansi/*.ans > $BASE
+./ansi-to-src.py ansi/*.ans > $BASE
 
 if [ $? -eq 127 ]; then
   echo "using default, missing ansi-to-src"

+ 39 - 9
main.cpp

@@ -23,6 +23,7 @@ door::ANSIColor stringToANSIColor(std::string colorCode);
 
 std::function<std::ofstream &(void)> get_logger;
 std::function<void(void)> cls_display_starfield;
+std::function<int(void)> press_a_key;
 
 std::string return_current_time_and_date() {
   auto now = std::chrono::system_clock::now();
@@ -34,9 +35,37 @@ std::string return_current_time_and_date() {
   return ss.str();
 }
 
-int press_a_key(door::Door &door) {
+int press_any_key(door::Door &door) {
+  static std::default_random_engine generator;
+  static std::uniform_int_distribution<int> distribution(0, 1);
+  int use_set = distribution(generator);
   door << door::reset << "Press a key to continue...";
-  int r = door.sleep_key(door.inactivity);
+  int t = 0;
+  int loop = 0;
+
+  const char *loop_chars[3][4] = {{"/", "-", "\\", "|"},
+                                  {"\xde", "\xdc", "\xdd", "\xdf"},
+                                  {"\u2590", "\u2584", "\u258c", "\u2580"}};
+
+  if (door::unicode) {
+    if (use_set == 1)
+      use_set = 2;
+  }
+
+  int r;
+
+  door << loop_chars[use_set][loop % 4];
+  while ((r = door.sleep_key(1)) == -1) {
+    loop++;
+    t++;
+    if (t >= door.inactivity) {
+      door << "\b \b";
+      door << door::nl;
+      return -1;
+    }
+    door << "\b" << loop_chars[use_set][loop % 4];
+  }
+  door << "\b \b";
   door << door::nl;
   return r;
 }
@@ -120,7 +149,7 @@ int configure(door::Door &door, DBData &db) {
         // config_panel.set(1, 1);
         door << config_panel << door::reset << door::nl;
 
-        r = press_a_key(door);
+        r = press_a_key();
         if (r < 0)
           return r;
         door << door::reset << door::cls;
@@ -139,6 +168,7 @@ int main(int argc, char *argv[]) {
 
   // store the door log so we can easily access it.
   get_logger = [&door]() -> ofstream & { return door.log(); };
+  press_a_key = [&door]() -> int { return press_any_key(door); };
 
   std::random_device rd;
   std::mt19937 rng(rd());
@@ -252,8 +282,7 @@ int main(int argc, char *argv[]) {
         }
       }
     }
-    door << door::reset;
-    press_a_key(door);
+    press_a_key();
   }
 
   int mx, my; // Max screen width/height
@@ -310,24 +339,24 @@ int main(int argc, char *argv[]) {
 
         score.display_scores(door);
       }
-      r = press_a_key(door);
+      r = press_a_key();
       break;
 
     case 3: // configure
       r = configure(door, spacedb);
-      // r = press_a_key(door);
+      // r = press_a_key();
       break;
 
     case 4: // help
       display_starfield(door, rng);
       door << help << door::nl;
-      r = press_a_key(door);
+      r = press_a_key();
       break;
 
     case 5: // about
       display_starfield(door, rng);
       door << about << door::nl;
-      r = press_a_key(door);
+      r = press_a_key();
       break;
 
     case 6: // quit
@@ -359,6 +388,7 @@ int main(int argc, char *argv[]) {
   // Normal DOOR exit goes here...
   door << door::nl << "Returning you to the BBS, please wait..." << door::nl;
 
+  press_a_key = nullptr;
   get_logger = nullptr;
   cls_display_starfield = nullptr;
   return 0;

+ 2 - 0
utils.h

@@ -24,6 +24,8 @@ extern std::function<std::ofstream &(void)> get_logger;
 
 extern std::function<void(void)> cls_display_starfield;
 
+extern std::function<int(void)> press_a_key;
+
 // configuration settings access
 #include "yaml-cpp/yaml.h"
 extern YAML::Node config;