Ascender: more fun tests with shapes and attributes
authorMatthew Mondor <mmondor@pulsar-zone.net>
Sun, 16 Apr 2023 07:07:26 +0000 (07:07 +0000)
committerMatthew Mondor <mmondor@pulsar-zone.net>
Sun, 16 Apr 2023 07:07:26 +0000 (07:07 +0000)
mmsoftware/ascender/src/main.c
mmsoftware/ascender/src/screen.c
mmsoftware/ascender/src/screen.h
mmsoftware/ascender/src/shapes.txt

index 91e98a2..3cf58e0 100644 (file)
@@ -35,6 +35,7 @@
 #include <screen.h>
 #include <nbrandom.h>
 #include <config.h>
+#include <collision.h>
 
 #include <mmrarray.h>
 
@@ -75,7 +76,7 @@ typedef struct aobject {
 int                    main(int, char **);
 static void            usage(void);
 
-static void            shape_draw(int, int, int);
+static void            shape_draw(int, int, int, bool);
 static void            anim_init(void);
 static void            anim_redraw(void);
 static void            anim_update(void);
@@ -90,8 +91,6 @@ static const char     *progname = "";
 /* Color related */
 static bool            cfg_use_color = true;
 static bool            cfg_use_attributes = true;
-static bool            use_color = false;
-static bool            use_attributes = false;
 static short           ncolpairs = -1;
 
 /* Loaded shapes */
@@ -261,10 +260,10 @@ usage(void)
 
 /* 2d text shape blitting routine */
 static void
-shape_draw(int shape, int y, int x)
+shape_draw(int shape, int y, int x, bool rev)
 {
 
-       screen_blitpad(spads[shape], y, x);
+       screen_blitpad(spads[shape], y, x, rev);
 }
 
 static void
@@ -307,6 +306,7 @@ static void
 anim_redraw(void)
 {
        int i;
+       chtype ch;
 
        /* Redraw screen with all objects */
        if (use_color)
@@ -315,9 +315,17 @@ anim_redraw(void)
        for (i = 0; i < NOBJECTS; i++) {
                aobject_t *o = &objects[i];
 
-               if (o->t == AOT_SHAPE)
-                       shape_draw(o->s, o->y, o->x);
-               else {
+               if (o->t == AOT_SHAPE) {
+                       bool rev = false;
+                       int y1, x1, y2, x2;
+
+                       /* Verify if collision with avatar */
+                       screen_blitpad_rect(spads[o->s], o->y, o->x,
+                           &y1, &x1, &y2, &x2);
+                       rev = point_in_rect(avatar.x, avatar.y,
+                           x1 - 1, y1 - 1, x2 + 1, y2 + 1);
+                       shape_draw(o->s, o->y, o->x, rev);
+               } else {
                        if (use_color && o->col != -1)
                                (void)attrset(COLOR_PAIR(o->col));
                        (void)mvaddch(o->y, o->x, o->c);
@@ -326,9 +334,10 @@ anim_redraw(void)
 
        /* Avatar */
        /* XXX */
-       (void)attr_on(A_REVERSE, NULL);
-       (void)mvaddch(avatar.y, avatar.x, avatar.c);
-       (void)attr_off(A_REVERSE, NULL);
+       ch = avatar.c;
+       if (use_attributes)
+               ch |= A_REVERSE;
+       (void)mvaddch(avatar.y, avatar.x, ch);
 
        (void)doupdate();
 }
index 4d395b7..b2033cb 100644 (file)
@@ -41,6 +41,7 @@
 
 static void            screen_cleanup(void);
 static inline int      int_min(int, int);
+static void            screen_blitfill(WINDOW *, int, int);
 
 
 /* Error to display after screen cleanup if any */
@@ -50,6 +51,9 @@ static int            error_errno = 0;
 static bool            curses_initialized = false;
 static WINDOW          *screen_window = NULL;
 
+bool                   use_color = false;
+bool                   use_attributes = false;
+
 
 void
 screen_init(void)
@@ -176,9 +180,30 @@ int_min(int a, int b)
        return (a < b ? a : b);
 }
 
-/* 2d text shape blitting routine */
 void
-screen_blitpad(WINDOW *pad, int y, int x)
+screen_blitpad_rect(WINDOW *pad, int y, int x,
+    int *y1, int *x1, int *y2, int *x2)
+{
+       int xs, ys, xo, yo;
+
+       getmaxyx(pad, ys, xs);
+       xo = xs / 2;
+       yo = ys / 2;
+
+       *y1 = y - yo;
+       *y2 = int_min((y - yo) + ys - 1, TERM_LINES - 1);
+       *x1 = x - xo;
+       *x2 = int_min((x - xo) + xs - 1, TERM_COLUMNS - 1);
+}
+
+/*
+ * 2d text shape blitting routine.
+ * XXX
+ * Currently the center of the object is used for its position, but perhaps we
+ * really want to keep the upper-left corner for this.
+ */
+void
+screen_blitpad(WINDOW *pad, int y, int x, bool reverse)
 {
        int xs, ys, xo, yo;
 
@@ -190,4 +215,40 @@ screen_blitpad(WINDOW *pad, int y, int x)
            int_min((y - yo) + ys - 1, TERM_LINES - 1),
            int_min((x - xo) + xs - 1, TERM_COLUMNS - 1),
            TRUE);
+       if (reverse)
+               screen_blitfill(pad, y, x);
+}
+
+/*
+ * Fills what would normally be the area of the pad to blit by reverse
+ * attribute.  Used for collision detection tests visual feedback.
+ */
+static void
+screen_blitfill(WINDOW *pad, int y, int x)
+{
+       int xs, ys, xo, yo, xp, yp, xt, yt;
+       chtype ch;
+
+       if (!use_attributes)
+               return;
+
+       getmaxyx(pad, ys, xs);
+       xo = xs / 2;
+       yo = ys / 2;
+
+       for (yp = y - yo,
+            yt = int_min((y - yo) + ys - 1, TERM_LINES - 1);
+            yp <= yt; yp++) {
+               if (yp < 0)
+                       continue;
+               for (xp = x - xo,
+                   xt = int_min((x - xo) + xs - 1, TERM_COLUMNS - 1);
+                   xp <= xt; xp++) {
+                       if (x < 0)
+                               continue;
+                       ch = mvwinch(screen_window, yp, xp);
+                       ch |= A_REVERSE;
+                       (void)mvwaddch(screen_window, yp, xp, ch);
+               }
+       }
 }
index bde8e2f..b52ac62 100644 (file)
@@ -32,6 +32,7 @@
 
 
 #include <curses.h>
+#include <stdbool.h>
 
 #include <config.h>
 
 void   screen_init(void);
 int    screen_getchar(void);
 void   screen_error(const char *, ...);
-void   screen_blitpad(WINDOW *, int, int);
+void   screen_blitpad_rect(WINDOW *, int, int, int *, int *, int *, int *);
+void   screen_blitpad(WINDOW *, int, int, bool);
+
+
+extern bool            use_color;
+extern bool            use_attributes;
 
 
 #endif
index 75be182..0ffd222 100644 (file)
@@ -34,7 +34,7 @@ T  |----\ (/ |    /|   |'\ /^;
 T \---*---Y--+-----+---+--/(
 T  \------*---*--*---*--/
 T   '~~ ~~~~~~~~~~~~~~~
-F                  24
+F                  22
 F            2     3
 F                  2    
 F                  2   2 
@@ -43,7 +43,7 @@ F   1111     2     2   2   2
 F  333333333333333333333332 
 F   33333333333333333333 
 F
-B                   6
+B                   1
 B                   
 B                       
 B                        
@@ -66,8 +66,8 @@ T _ _
 T  ^
 F 3 3
 F  3
-A EEE
-A EEE
+A BBB
+A  E 
 
 T o_O
 
@@ -80,18 +80,36 @@ T    T
 F   444
 F 5233325
 F    1
+A
+A    E
+A    B
 
 T Nooooo!
 F 1111111
 A BBBBBBB
 
-T      _
-T    _> >=
-T O-<_--<=
-T     >_>=
-T
+T      ____
+T    o<__  >=
+T  /\___/ <
+T <  ___   )
+T  \/  _\ <
+T    o<____>=
+F     11111
+F    5111  17
+F  311111 1
+F 3  111   1
+F  31  11 1
+F    51111117
+A
+A           B
+A
+A
+A
+A           B
 
 T    _
 T -=<_>=-
 F    7
 F 7777777
+A
+A B     B