backported fixes from rtxon: BSP tree construction and mouse handling
[dosdemo] / src / demo.c
index 6abcf67..2ff25a7 100644 (file)
@@ -11,6 +11,7 @@
 #include "music.h"
 #include "cfgopt.h"
 #include "tinyfps.h"
+#include "util.h"
 
 int fb_width = 320;
 int fb_height = 240;
@@ -23,6 +24,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)
 {
@@ -119,25 +121,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 +179,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 +211,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_LEFT) {
                                        float p = *phi;
                                        *theta += dx * 1.0;
                                        p += dy * 1.0;
@@ -181,7 +220,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_RIGHT) {
                                        *dist += dy * 0.5;
 
                                        if(*dist < 0) *dist = 0;