starting infcubes part
authorJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 13 Feb 2018 18:47:25 +0000 (20:47 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 13 Feb 2018 18:47:25 +0000 (20:47 +0200)
src/demo.c
src/demo.h
src/greets.c
src/infcubes.c [new file with mode: 0644]
src/screen.c

index f25610a..6abcf67 100644 (file)
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <math.h>
+#include <ctype.h>
 #include <errno.h>
 #include <limits.h>
 #include "demo.h"
@@ -99,21 +100,96 @@ void demo_draw(void)
        ++nframes;
 }
 
-void demo_keyboard(int key, int state)
+static void change_screen(int idx)
 {
-       if(state) {
+       printf("change screen %d\n", idx);
+       scr_change(scr_screen(idx), 4000);
+}
+
+#define CBUF_SIZE      64
+#define CBUF_MASK      (CBUF_SIZE - 1)
+void demo_keyboard(int key, int press)
+{
+       static char cbuf[CBUF_SIZE];
+       static int rd, wr;
+       char inp[CBUF_SIZE + 1], *dptr;
+       int i, nscr;
+
+       if(press) {
                switch(key) {
                case 27:
                        demo_quit();
                        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;
+                                       }
+                               }
+                       }
+                       break;
+
                default:
-                       if(key >= '1' && key <= '1' + scr_num_screens()) {
-                               int idx = key - '1';
-                               printf("change screen %d\n", idx);
-                               scr_change(scr_screen(idx), 4000);
+                       if(key >= '1' && key <= '9' && key <= '1' + scr_num_screens()) {
+                               change_screen(key - '1');
+                       } else if(key == '0' && scr_num_screens() >= 10) {
+                               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;
+                               }
                        }
                        break;
                }
        }
 }
+
+
+void mouse_orbit_update(float *theta, float *phi, float *dist)
+{
+       static int prev_mx, prev_my;
+       static unsigned int prev_bmask;
+
+       if(mouse_bmask) {
+               if((mouse_bmask ^ prev_bmask) == 0) {
+                       int dx = mouse_x - prev_mx;
+                       int dy = mouse_y - prev_my;
+
+                       if(dx || dy) {
+                               if(mouse_bmask & 1) {
+                                       float p = *phi;
+                                       *theta += dx * 1.0;
+                                       p += dy * 1.0;
+
+                                       if(p < -90) p = -90;
+                                       if(p > 90) p = 90;
+                                       *phi = p;
+                               }
+                               if(mouse_bmask & 4) {
+                                       *dist += dy * 0.5;
+
+                                       if(*dist < 0) *dist = 0;
+                               }
+                       }
+               }
+       }
+       prev_mx = mouse_x;
+       prev_my = mouse_y;
+       prev_bmask = mouse_bmask;
+}
index 60c7646..fdb238b 100644 (file)
@@ -20,7 +20,7 @@ void demo_cleanup(void);
 
 void demo_draw(void);
 
-void demo_keyboard(int key, int state);
+void demo_keyboard(int key, int press);
 
 
 /* defined in main_*.c */
@@ -34,4 +34,7 @@ void set_palette(int idx, int r, int g, int b);
  */
 void swap_buffers(void *pixels);
 
+/* call each frame to get 3D viewing spherical coordinates */
+void mouse_orbit_update(float *theta, float *phi, float *dist);
+
 #endif /* DEMO_H_ */
index 57f85e8..353d29c 100644 (file)
@@ -211,11 +211,10 @@ static void draw(void)
        g3d_matrix_mode(G3D_MODELVIEW);
        g3d_load_identity();
        g3d_translate(0, 0, -cam_dist);
+       g3d_rotate(cam_phi, 1, 0, 0);
+       g3d_rotate(cam_theta, 0, 1, 0);
        if(opt.sball) {
                g3d_mult_matrix(sball_matrix);
-       } else {
-               g3d_rotate(cam_phi, 1, 0, 0);
-               g3d_rotate(cam_theta, 0, 1, 0);
        }
 
        memcpy(cur_smokebuf, prev_smokebuf, smokebuf_size);
diff --git a/src/infcubes.c b/src/infcubes.c
new file mode 100644 (file)
index 0000000..5fe7df9
--- /dev/null
@@ -0,0 +1,109 @@
+#include <stdio.h>
+#include <math.h>
+#include "demo.h"
+#include "3dgfx.h"
+#include "screen.h"
+#include "cfgopt.h"
+
+static int init(void);
+static void destroy(void);
+static void start(long trans_time);
+static void draw(void);
+static void draw_cube(void);
+
+static struct screen scr = {
+       "infcubes",
+       init,
+       destroy,
+       start, 0,
+       draw
+};
+
+static float cam_theta, cam_phi;
+static float cam_dist = 5;
+
+struct screen *infcubes_screen(void)
+{
+       return &scr;
+}
+
+
+static int init(void)
+{
+       return 0;
+}
+
+static void destroy(void)
+{
+}
+
+static void start(long trans_time)
+{
+       g3d_matrix_mode(G3D_PROJECTION);
+       g3d_load_identity();
+       g3d_perspective(50.0, 1.3333333, 0.5, 100.0);
+
+       g3d_enable(G3D_LIGHTING);
+       g3d_enable(G3D_LIGHT0);
+}
+
+static void update(void)
+{
+       mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
+}
+
+static void draw(void)
+{
+       update();
+
+       g3d_matrix_mode(G3D_MODELVIEW);
+       g3d_load_identity();
+       g3d_translate(0, 0, -cam_dist);
+       g3d_rotate(cam_phi, 1, 0, 0);
+       g3d_rotate(cam_theta, 0, 1, 0);
+       if(opt.sball) {
+               g3d_mult_matrix(sball_matrix);
+       }
+
+       memset(fb_pixels, 0, fb_width * fb_height * 2);
+
+       draw_cube();
+
+       swap_buffers(fb_pixels);
+}
+
+static void draw_cube(void)
+{
+       g3d_begin(G3D_QUADS);
+       g3d_normal(0, 0, 1);
+       g3d_vertex(-1, -1, 1);
+       g3d_vertex(1, -1, 1);
+       g3d_vertex(1, 1, 1);
+       g3d_vertex(-1, 1, 1);
+       g3d_normal(1, 0, 0);
+       g3d_vertex(1, -1, 1);
+       g3d_vertex(1, -1, -1);
+       g3d_vertex(1, 1, -1);
+       g3d_vertex(1, 1, 1);
+       g3d_normal(0, 0, -1);
+       g3d_vertex(1, -1, -1);
+       g3d_vertex(-1, -1, -1);
+       g3d_vertex(-1, 1, -1);
+       g3d_vertex(1, 1, -1);
+       g3d_normal(-1, 0, 0);
+       g3d_vertex(-1, -1, -1);
+       g3d_vertex(-1, -1, 1);
+       g3d_vertex(-1, 1, 1);
+       g3d_vertex(-1, 1, -1);
+       g3d_normal(0, 1, 0);
+       g3d_vertex(-1, 1, 1);
+       g3d_vertex(1, 1, 1);
+       g3d_vertex(1, 1, -1);
+       g3d_vertex(-1, 1, -1);
+       g3d_normal(0, -1, 0);
+       g3d_vertex(1, -1, 1);
+       g3d_vertex(-1, -1, 1);
+       g3d_vertex(-1, -1, -1);
+       g3d_vertex(1, -1, -1);
+       g3d_end();
+}
index fb89206..5ba6d40 100644 (file)
@@ -14,6 +14,7 @@ struct screen *bump_screen(void);
 struct screen *thunder_screen(void);
 struct screen *metaballs_screen(void);
 struct screen *greets_screen(void);
+struct screen *infcubes_screen(void);
 
 #define NUM_SCR 32
 static struct screen *scr[NUM_SCR];
@@ -53,6 +54,9 @@ int scr_init(void)
        if(!(scr[idx++] = greets_screen())) {
                return -1;
        }
+       if(!(scr[idx++] = infcubes_screen())) {
+               return -1;
+       }
        num_screens = idx;
 
        assert(num_screens <= NUM_SCR);