share/gui: don't use less of widget width for truncation than available
[neverball] / share / mapc.c
index f32cf07..3117147 100644 (file)
@@ -23,6 +23,8 @@
 #include "solid.h"
 #include "base_image.h"
 #include "base_config.h"
+#include "fs.h"
+#include "common.h"
 
 #define MAXSTR 256
 #define MAXKEY 16
@@ -39,7 +41,8 @@
 
 /*---------------------------------------------------------------------------*/
 
-static int debug_output = 0;
+static const char *input_file;
+static int         debug_output = 0;
 
 /*---------------------------------------------------------------------------*/
 
@@ -353,8 +356,8 @@ static void size_image(const char *name, int *w, int *h)
     strcpy(jpg, name); strcat(jpg, ".jpg");
     strcpy(png, name); strcat(png, ".png");
 
-    if (size_load(config_data(png), w, h) ||
-        size_load(config_data(jpg), w, h))
+    if (size_load(png, w, h) ||
+        size_load(jpg, w, h))
     {
 
         if (image_n + 1 >= image_alloc)
@@ -388,10 +391,15 @@ static void size_image(const char *name, int *w, int *h)
 
 /* Read the given material file, adding a new material to the solid.  */
 
+#define scan_vec4(f, s, v)                                              \
+    if (fs_gets((s), sizeof (s), (f)))                                  \
+        sscanf((s), "%f %f %f %f", (v), (v) + 1, (v) + 2, (v) + 3)
+
 static int read_mtrl(struct s_file *fp, const char *name)
 {
+    static char line[MAXSTR];
     struct s_mtrl *mp;
-    FILE *fin;
+    fs_file fin;
     int mi;
 
     for (mi = 0; mi < fp->mc; mi++)
@@ -411,25 +419,32 @@ static int read_mtrl(struct s_file *fp, const char *name)
     mp->fl   = 0;
     mp->angle = 45.0f;
 
-    if ((fin = fopen(config_data(name), "r")))
+    if ((fin = fs_open(name, "r")))
     {
-        fscanf(fin,
-               "%f %f %f %f "
-               "%f %f %f %f "
-               "%f %f %f %f "
-               "%f %f %f %f "
-               "%f %d %f",
-               mp->d, mp->d + 1, mp->d + 2, mp->d + 3,
-               mp->a, mp->a + 1, mp->a + 2, mp->a + 3,
-               mp->s, mp->s + 1, mp->s + 2, mp->s + 3,
-               mp->e, mp->e + 1, mp->e + 2, mp->e + 3,
-               mp->h, &mp->fl, &mp->angle);
-        fclose(fin);
+        scan_vec4(fin, line, mp->d);
+        scan_vec4(fin, line, mp->a);
+        scan_vec4(fin, line, mp->s);
+        scan_vec4(fin, line, mp->e);
+
+        if (fs_gets(line, sizeof (line), fin))
+            mp->h[0] = strtod(line, NULL);
+
+        if (fs_gets(line, sizeof (line), fin))
+            mp->fl = strtol(line, NULL, 10);
+
+        if (fs_gets(line, sizeof (line), fin))
+            mp->angle = strtod(line, NULL);
+
+        fs_close(fin);
     }
+    else
+        fprintf(stderr, "%s: unknown material \"%s\"\n", input_file, name);
 
     return mi;
 }
 
+#undef scan_vec4
+
 /*---------------------------------------------------------------------------*/
 
 /*
@@ -564,15 +579,15 @@ static void read_obj(struct s_file *fp, const char *name, int mi)
 {
     char line[MAXSTR];
     char mtrl[MAXSTR];
-    FILE *fin;
+    fs_file fin;
 
     int v0 = fp->vc;
     int t0 = fp->tc;
     int s0 = fp->sc;
 
-    if ((fin = fopen(config_data(name), "r")))
+    if ((fin = fs_open(name, "r")))
     {
-        while (fgets(line, MAXSTR, fin))
+        while (fs_gets(line, MAXSTR, fin))
         {
             if (strncmp(line, "usemtl", 6) == 0)
             {
@@ -590,7 +605,7 @@ static void read_obj(struct s_file *fp, const char *name, int mi)
             else if (strncmp(line, "vn", 2) == 0) read_vn(fp, line + 2);
             else if (strncmp(line, "v",  1) == 0) read_v (fp, line + 1);
         }
-        fclose(fin);
+        fs_close(fin);
     }
 }
 
@@ -688,11 +703,11 @@ static void make_plane(int   pi, float x0, float y0, float      z0,
 #define T_END 4
 #define T_NOP 5
 
-static int map_token(FILE *fin, int pi, char key[MAXSTR], char val[MAXSTR])
+static int map_token(fs_file fin, int pi, char key[MAXSTR], char val[MAXSTR])
 {
     char buf[MAXSTR];
 
-    if (fgets(buf, MAXSTR, fin))
+    if (fs_gets(buf, MAXSTR, fin))
     {
         char c;
         float x0, y0, z0;
@@ -748,7 +763,7 @@ static int map_token(FILE *fin, int pi, char key[MAXSTR], char val[MAXSTR])
 
 /* Parse a lump from the given file and add it to the solid. */
 
-static void read_lump(struct s_file *fp, FILE *fin)
+static void read_lump(struct s_file *fp, fs_file fin)
 {
     char k[MAXSTR];
     char v[MAXSTR];
@@ -1140,7 +1155,10 @@ static void make_swch(struct s_file *fp,
             make_ref(v[i], &xp->pi);
 
         if (strcmp(k[i], "timer") == 0)
+        {
             sscanf(v[i], "%f", &xp->t0);
+            xp->t = xp->t0;
+        }
 
         if (strcmp(k[i], "state") == 0)
         {
@@ -1229,7 +1247,7 @@ static void make_ball(struct s_file *fp,
 
 /*---------------------------------------------------------------------------*/
 
-static void read_ent(struct s_file *fp, FILE *fin)
+static void read_ent(struct s_file *fp, fs_file fin)
 {
     char k[MAXKEY][MAXSTR];
     char v[MAXKEY][MAXSTR];
@@ -1269,7 +1287,7 @@ static void read_ent(struct s_file *fp, FILE *fin)
     if (!strcmp(v[i], "misc_model"))               make_body(fp, k, v, c, l0);
 }
 
-static void read_map(struct s_file *fp, FILE *fin)
+static void read_map(struct s_file *fp, fs_file fin)
 {
     char k[MAXSTR];
     char v[MAXSTR];
@@ -2432,50 +2450,65 @@ static void dump_file(struct s_file *p, const char *name)
 
 int main(int argc, char *argv[])
 {
-    char src[MAXSTR];
-    char dst[MAXSTR];
+    char src[MAXSTR] = "";
+    char dst[MAXSTR] = "";
     struct s_file f;
-    FILE *fin;
+    fs_file fin;
+
+    if (!fs_init(argv[0]))
+    {
+        fprintf(stderr, "Failure to initialize virtual file system\n");
+        return 1;
+    }
 
     if (argc > 2)
     {
+        input_file = argv[1];
+
         if (argc > 3 && strcmp(argv[3], "--debug") == 0)
             debug_output = 1;
 
-        if (config_data_path(argv[2], NULL))
-        {
-            strncpy(src,  argv[1], MAXSTR);
-            strncpy(dst,  argv[1], MAXSTR);
+        strncpy(src, argv[1], MAXSTR - 1);
+        strncpy(dst, argv[1], MAXSTR - 1);
 
-            if (strcmp(dst + strlen(dst) - 4, ".map") == 0)
-                strcpy(dst + strlen(dst) - 4, ".sol");
-            else
-                strcat(dst, ".sol");
+        if (strcmp(dst + strlen(dst) - 4, ".map") == 0)
+            strcpy(dst + strlen(dst) - 4, ".sol");
+        else
+            strcat(dst, ".sol");
 
-            if ((fin = fopen(src, "r")))
+        fs_add_path     (dir_name(src));
+        fs_set_write_dir(dir_name(dst));
+
+        if ((fin = fs_open(base_name(src, NULL), "r")))
+        {
+            if (!fs_add_path_with_archives(argv[2]))
             {
-                init_file(&f);
-                read_map(&f, fin);
+                fprintf(stderr, "Failure to establish data directory\n");
+                fs_close(fin);
+                fs_quit();
+                return 1;
+            }
 
-                resolve();
-                targets(&f);
+            init_file(&f);
+            read_map(&f, fin);
 
-                clip_file(&f);
-                move_file(&f);
-                uniq_file(&f);
-                smth_file(&f);
-                sort_file(&f);
-                node_file(&f);
-                dump_file(&f, dst);
+            resolve();
+            targets(&f);
 
-                sol_stor(&f, dst);
+            clip_file(&f);
+            move_file(&f);
+            uniq_file(&f);
+            smth_file(&f);
+            sort_file(&f);
+            node_file(&f);
+            dump_file(&f, dst);
 
-                fclose(fin);
+            sol_stor(&f, base_name(dst, NULL));
 
-                free_imagedata();
-            }
+            fs_close(fin);
+
+            free_imagedata();
         }
-        else fprintf(stderr, "Failure to establish data directory\n");
     }
     else fprintf(stderr, "Usage: %s <map> <data> [--debug]\n", argv[0]);