foo
[dosdemo] / src / demo.c
index 6abcf67..a990d5f 100644 (file)
 #include "music.h"
 #include "cfgopt.h"
 #include "tinyfps.h"
+#include "util.h"
 
-int fb_width = 320;
-int fb_height = 240;
+#define FB_WIDTH       320
+#define FB_HEIGHT      240
+
+int fb_width = FB_WIDTH;
+int fb_height = FB_HEIGHT;
 int fb_bpp = 16;
-uint16_t *fb_pixels, *vmem_back, *vmem_front;
+uint16_t *fb_pixels, *vmem;
 unsigned long time_msec;
 int mouse_x, mouse_y;
 unsigned int mouse_bmask;
@@ -23,6 +27,7 @@ unsigned int mouse_bmask;
 float sball_matrix[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
 
 static unsigned long nframes;
+static int console_active;
 
 int demo_init(int argc, char **argv)
 {
@@ -97,9 +102,68 @@ void demo_draw(void)
        scr_update();
        scr_draw();
 
+       draw_mouse_pointer(vmem);
+
        ++nframes;
 }
 
+
+
+#define DEST(x, y)     dest[(y) * FB_WIDTH + (x)]
+void draw_mouse_pointer(uint16_t *fb)
+{
+       uint16_t *dest = fb + mouse_y * FB_WIDTH + mouse_x;
+       int ylines = FB_HEIGHT - mouse_y;
+
+       switch(ylines) {
+       default:
+       case 10:
+               DEST(0, 9) = 0xffff;
+       case 9:
+               DEST(0, 8) = 0xffff;
+               DEST(1, 8) = 0xffff;
+       case 8:
+               DEST(0, 7) = 0xffff;
+               DEST(2, 7) = 0xffff;
+               DEST(1, 7) = 0;
+       case 7:
+               DEST(6, 6) = 0xffff;
+               DEST(0, 6) = 0xffff;
+               DEST(3, 6) = 0xffff;
+               DEST(4, 6) = 0xffff;
+               DEST(5, 6) = 0xffff;
+               DEST(1, 6) = 0;
+               DEST(2, 6) = 0;
+       case 6:
+               DEST(5, 5) = 0xffff;
+               DEST(0, 5) = 0xffff;
+               DEST(1, 5) = 0;
+               DEST(2, 5) = 0;
+               DEST(3, 5) = 0;
+               DEST(4, 5) = 0;
+       case 5:
+               DEST(4, 4) = 0xffff;
+               DEST(0, 4) = 0xffff;
+               DEST(1, 4) = 0;
+               DEST(2, 4) = 0;
+               DEST(3, 4) = 0;
+       case 4:
+               DEST(3, 3) = 0xffff;
+               DEST(0, 3) = 0xffff;
+               DEST(1, 3) = 0;
+               DEST(2, 3) = 0;
+       case 3:
+               DEST(2, 2) = 0xffff;
+               DEST(0, 2) = 0xffff;
+               DEST(1, 2) = 0;
+       case 2:
+               DEST(1, 1) = 0xffff;
+               DEST(0, 1) = 0xffff;
+       case 1:
+               DEST(0, 0) = 0xffff;
+       }
+}
+
 static void change_screen(int idx)
 {
        printf("change screen %d\n", idx);
@@ -119,25 +183,54 @@ void demo_keyboard(int key, int press)
                switch(key) {
                case 27:
                        demo_quit();
+                       return;
+
+               case 127:
+                       debug_break();
+                       return;
+
+               case '`':
+                       console_active = !console_active;
+                       if(console_active) {
+                               printf("> ");
+                               fflush(stdout);
+                       } else {
+                               putchar('\n');
+                       }
+                       return;
+
+               case '\b':
+                       if(console_active) {
+                               if(wr != rd) {
+                                       printf("\b \b");
+                                       fflush(stdout);
+                                       wr = (wr + CBUF_SIZE - 1) & CBUF_MASK;
+                               }
+                               return;
+                       }
                        break;
 
                case '\n':
                case '\r':
-                       dptr = inp;
-                       while(rd != wr) {
-                               *dptr++ = cbuf[rd];
-                               rd = (rd + 1) & CBUF_MASK;
-                       }
-                       *dptr = 0;
-                       if(inp[0]) {
-                               printf("trying to match: %s\n", inp);
-                               nscr = scr_num_screens();
-                               for(i=0; i<nscr; i++) {
-                                       if(strstr(scr_screen(i)->name, inp)) {
-                                               change_screen(i);
-                                               break;
+                       if(console_active) {
+                               dptr = inp;
+                               while(rd != wr) {
+                                       *dptr++ = cbuf[rd];
+                                       rd = (rd + 1) & CBUF_MASK;
+                               }
+                               *dptr = 0;
+                               if(inp[0]) {
+                                       printf("\ntrying to match: %s\n", inp);
+                                       nscr = scr_num_screens();
+                                       for(i=0; i<nscr; i++) {
+                                               if(strstr(scr_screen(i)->name, inp)) {
+                                                       change_screen(i);
+                                                       break;
+                                               }
                                        }
                                }
+                               console_active = 0;
+                               return;
                        }
                        break;
 
@@ -148,15 +241,23 @@ void demo_keyboard(int key, int press)
                                change_screen(9);
                        }
 
-                       if(key < 256 && isprint(key)) {
-                               cbuf[wr] = key;
-                               wr = (wr + 1) & CBUF_MASK;
-                               if(wr == rd) { /* overflow */
-                                       rd = (rd + 1) & CBUF_MASK;
+                       if(console_active) {
+                               if(key < 256 && isprint(key)) {
+                                       putchar(key);
+                                       fflush(stdout);
+
+                                       cbuf[wr] = key;
+                                       wr = (wr + 1) & CBUF_MASK;
+                                       if(wr == rd) { /* overflow */
+                                               rd = (rd + 1) & CBUF_MASK;
+                                       }
                                }
+                               return;
                        }
                        break;
                }
+
+               scr_keypress(key);
        }
 }
 
@@ -172,7 +273,7 @@ void mouse_orbit_update(float *theta, float *phi, float *dist)
                        int dy = mouse_y - prev_my;
 
                        if(dx || dy) {
-                               if(mouse_bmask & 1) {
+                               if(mouse_bmask & MOUSE_BN_LEFT) {
                                        float p = *phi;
                                        *theta += dx * 1.0;
                                        p += dy * 1.0;
@@ -181,7 +282,7 @@ void mouse_orbit_update(float *theta, float *phi, float *dist)
                                        if(p > 90) p = 90;
                                        *phi = p;
                                }
-                               if(mouse_bmask & 4) {
+                               if(mouse_bmask & MOUSE_BN_RIGHT) {
                                        *dist += dy * 0.5;
 
                                        if(*dist < 0) *dist = 0;
@@ -189,6 +290,7 @@ void mouse_orbit_update(float *theta, float *phi, float *dist)
                        }
                }
        }
+
        prev_mx = mouse_x;
        prev_my = mouse_y;
        prev_bmask = mouse_bmask;