Merge branch 'master' of mutantstargoat.com:/home/nuclear/git/dosdemo
authorMichael Georgoulopoulos <mgeorgoulopoulos@gmail.com>
Sun, 11 Sep 2016 23:06:02 +0000 (02:06 +0300)
committerMichael Georgoulopoulos <mgeorgoulopoulos@gmail.com>
Sun, 11 Sep 2016 23:06:02 +0000 (02:06 +0300)
Conflicts:
src/grise.c

14 files changed:
.gitignore
Makefile
libs/imago/Makefile
src/3dgfx.c
src/3dgfx.h
src/demo.c
src/demo.h
src/dos/main.c
src/fract.c
src/grise.c
src/polytest.c
src/screen.c
src/sdl/main.c
src/tunnel.c

index 27e5618..ced3325 100644 (file)
@@ -22,5 +22,6 @@ demo
 *.zip
 data
 *.a
-\r
-/SDL.dll\r
+*.dll
+*.occ
+*.OCC
index 3503834..e22c1fa 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,12 +1,13 @@
 baseobj = main.obj
-demoobj = demo.obj screen.obj gfxutil.obj
-scrobj = tunnel.obj fract.obj grise.obj
+demoobj = demo.obj screen.obj gfxutil.obj 3dgfx.obj polyfill.obj
+scrobj = tunnel.obj fract.obj grise.obj polytest.obj
 sysobj = gfx.obj vbe.obj dpmi.obj timer.obj keyb.obj mouse.obj logger.obj
-obj = $(baseobj) $(demoobj) $(sysobj)
+obj = $(baseobj) $(demoobj) $(sysobj) $(scrobj)
 bin = demo.exe
 
 libs = imago.lib
 
+def = -dM_PI=3.141592653589793
 opt = -5 -fp5 -otexan -oh -oi -ei
 dbg = -d1
 
@@ -22,12 +23,12 @@ AS = nasm
 CC = wcc386
 CXX = wpp386
 ASFLAGS = -fobj
-CFLAGS = $(dbg) $(opt) -zq -bt=dos $(incpath)
+CFLAGS = $(dbg) $(opt) $(def) -zq -bt=dos $(incpath)
 CXXFLAGS = $(CFLAGS)
 LDFLAGS = libpath $(libpath) library { $(libs) }
 LD = wlink
 
-$(bin): $(obj) libs/imago/imago.lib
+$(bin): cflags.occ $(obj) libs/imago/imago.lib
        %write objects.lnk $(obj)
        $(LD) debug all name $@ system dos4g file { @objects } $(LDFLAGS)
 
@@ -35,15 +36,23 @@ $(bin): $(obj) libs/imago/imago.lib
 .cc: src;src/dos
 .asm: src;src/dos
 
+cflags.occ: Makefile
+       %write $@ $(CFLAGS)
+
+cxxflags.occ: Makefile
+       %write $@ $(CXXFLAGS)
+
 .c.obj: .autodepend
-       $(CC) -fo=$@ $(CFLAGS) $[*
+       $(CC) -fo=$@ @cflags.occ $[*
 
 .cc.obj: .autodepend
-       $(CXX) -fo=$@ $(CXXFLAGS) $[*
+       $(CXX) -fo=$@ @cxxflags.occ $[*
 
 .asm.obj:
        $(AS) $(ASFLAGS) -o $@ $[*.asm
 
 clean: .symbolic
        del *.obj
+       del *.occ
+       del *.lnk
        del $(bin)
index ada8222..6900d25 100644 (file)
@@ -25,15 +25,20 @@ def = -DPNG_NO_SNPRINTF
 CC = wcc386
 CFLAGS = $(dbg) $(opt) $(def) -zq -bt=dos -Ilibpng -Izlib -Ijpeglib
 
-$(alib): $(obj)
+$(alib): cflags.occ $(obj)
        %write objects.lbc $(obj)
        wlib -b -n $@ @objects
 
 .c: src;libpng;jpeglib;zlib
 
+cflags.occ: Makefile
+       %write $@ $(CFLAGS)
+
 .c.obj: .autodepend
-       $(CC) -fo=$@ $(CFLAGS) $[*
+       $(CC) -fo=$@ @cflags.occ $[*
 
 clean: .symbolic
        del *.obj
+       del *.occ
+       del *.lbc
        del $(alib)
index 49b4c2c..31e649d 100644 (file)
@@ -251,16 +251,25 @@ void g3d_perspective(float vfov_deg, float aspect, float znear, float zfar)
        g3d_mult_matrix(m);
 }
 
-#define CROSS(res, a, b) \
-       do { \
-               (res)[0] = (a)[1] * (b)[2] - (a)[2] * (b)[1]; \
-               (res)[1] = (a)[2] * (b)[0] - (a)[0] * (b)[2]; \
-               (res)[2] = (a)[0] * (b)[1] - (a)[1] * (b)[0]; \
-       } while(0)
+const float *g3d_get_matrix(int which, float *m)
+{
+       int top = st->mtop[which];
+
+       if(m) {
+               memcpy(m, st->mat[which][top], 16 * sizeof(float));
+       }
+       return st->mat[which][top];
+}
 
 void g3d_draw(int prim, const struct g3d_vertex *varr, int varr_size)
 {
-       int i;
+       g3d_draw_indexed(prim, varr, varr_size, 0, 0);
+}
+
+void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
+               const int16_t *iarr, int iarr_size)
+{
+       int i, j, nfaces;
        struct pvertex pv[4];
        struct g3d_vertex v[4];
        int vnum = prim; /* primitive vertex counts correspond to enum values */
@@ -271,11 +280,12 @@ void g3d_draw(int prim, const struct g3d_vertex *varr, int varr_size)
        memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
        st->norm_mat[12] = st->norm_mat[13] = st->norm_mat[14] = 0.0f;
 
-       while(varr_size >= vnum) {
-               varr_size -= vnum;
+       nfaces = (iarr ? iarr_size : varr_size) / vnum;
+
+       for(j=0; j<nfaces; j++) {
 
                for(i=0; i<vnum; i++) {
-                       v[i] = *varr++;
+                       v[i] = iarr ? varr[*iarr++] : *varr++;
 
                        xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &v[i].x);
                        xform3_vec3(st->norm_mat, &v[i].nx);
index 9e4b140..c80ded1 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef THREEDGFX_H_
 #define THREEDGFX_H_
 
+#include "inttypes.h"
+
 struct g3d_vertex {
        float x, y, z, w;
        float nx, ny, nz;
@@ -63,6 +65,10 @@ void g3d_ortho(float left, float right, float bottom, float top, float znear, fl
 void g3d_frustum(float left, float right, float bottom, float top, float znear, float zfar);
 void g3d_perspective(float vfov, float aspect, float znear, float zfar);
 
+const float *g3d_get_matrix(int which, float *m);
+
 void g3d_draw(int prim, const struct g3d_vertex *varr, int varr_size);
+void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
+               const int16_t *iarr, int iarr_size);
 
 #endif /* THREEDGFX_H_ */
index fbcc4be..ce06a44 100644 (file)
@@ -11,7 +11,7 @@
 int fb_width = 320;
 int fb_height = 240;
 int fb_bpp = 16;
-void *fb_pixels;
+uint16_t *fb_pixels, *vmem_back, *vmem_front;
 unsigned long time_msec;
 int mouse_x, mouse_y;
 unsigned int mouse_bmask;
@@ -23,6 +23,7 @@ int demo_init(int argc, char **argv)
 {
        struct screen *scr;
 
+       start_scr_name = getenv("START_SCR");
        if(argv[1]) {
                start_scr_name = argv[1];
        }
index 530e3de..1b246ed 100644 (file)
@@ -1,8 +1,14 @@
 #ifndef DEMO_H_
 #define DEMO_H_
 
+#include "inttypes.h"
+
 extern int fb_width, fb_height, fb_bpp;
-extern void *fb_pixels;
+extern uint16_t *fb_pixels;    /* system-RAM pixel buffer: use swap_buffers(fb_pixels) */
+/* video memory pointers. might both point to the front buffer if there is not
+ * enough memory for page flipping. use swap_buffers(0) to flip. */
+extern uint16_t *vmem_back, *vmem_front;
+
 extern unsigned long time_msec;
 extern int mouse_x, mouse_y;
 extern unsigned int mouse_bmask;
@@ -14,9 +20,16 @@ void demo_draw(void);
 
 void demo_keyboard(int key, int state);
 
+
 /* defined in main_*.c */
 void demo_quit(void);
 unsigned long get_msec(void);
 void set_palette(int idx, int r, int g, int b);
 
+/* pass 0 to just swap vmem_back/vmem_front with page flipping
+ * pass a pointer to a system-ram pixel buffer to copy it to vmem_front,
+ * instead of flipping.
+ */
+void swap_buffers(void *pixels);
+
 #endif /* DEMO_H_ */
index 06fd0bb..10492fe 100644 (file)
 
 static int quit;
 static int use_mouse;
+static long fbsize;
 
 int main(int argc, char **argv)
 {
-       void *vmem;
-       long fbsize = fb_width * fb_height * fb_bpp / CHAR_BIT;
+       fbsize = fb_width * fb_height * fb_bpp / CHAR_BIT;
 
        init_timer(100);
        kb_init(32);
@@ -29,9 +29,11 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       if(!(vmem = set_video_mode(fb_width, fb_height, fb_bpp))) {
+       if(!(vmem_front = set_video_mode(fb_width, fb_height, fb_bpp))) {
                return 1;
        }
+       /* TODO implement multiple video memory pages for flipping */
+       vmem_back = vmem_front;
 
        if(demo_init(argc, argv) == -1) {
                set_text_mode();
@@ -52,9 +54,6 @@ int main(int argc, char **argv)
 
                time_msec = get_msec();
                demo_draw();
-
-               /*wait_vsync();*/
-               memcpy(vmem, fb_pixels, fbsize);
        }
 
 break_evloop:
@@ -68,3 +67,12 @@ void demo_quit(void)
 {
        quit = 1;
 }
+
+void swap_buffers(void *pixels)
+{
+       /* TODO implement page flipping */
+       if(pixels) {
+               /*wait_vsync();*/
+               memcpy(vmem_front, pixels, fbsize);
+       }
+}
index 0245b72..15da79d 100644 (file)
@@ -22,7 +22,7 @@ static struct screen scr = {
        draw
 };
 
-static long aspect_24x8 = (long)(1.3333333 * 256.0);
+/*static long aspect_24x8 = (long)(1.3333333 * 256.0);*/
 static long xscale_24x8 = (long)(1.3333333 * 1.2 * 256.0);
 static long yscale_24x8 = (long)(1.2 * 256.0);
 static int cx, cy;
@@ -84,6 +84,8 @@ static void draw(void)
        }
 
        pixels[mouse_y * fb_width + mouse_x] = 0xffe;
+
+       swap_buffers(fb_pixels);
 }
 
 static long normalize_coord(long x, long range)
index 5d2068b..896b7c0 100644 (file)
@@ -213,7 +213,7 @@ static void draw(void)
                dst += fb_width;
        }
 
-       
+       swap_buffers(fb_pixels);
 }
 
 /* src and dst can be the same */
index 5b43eea..fc38c16 100644 (file)
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <math.h>
 #include "screen.h"
 #include "demo.h"
 #include "3dgfx.h"
@@ -8,7 +9,7 @@
 struct mesh {
        int prim;
        struct g3d_vertex *varr;
-       unsigned int *iarr;
+       int16_t *iarr;
        int vcount, icount;
 };
 
@@ -18,6 +19,8 @@ static void start(long trans_time);
 static void draw(void);
 static void draw_mesh(struct mesh *mesh);
 static int gen_cube(struct mesh *mesh, float sz);
+static int gen_torus(struct mesh *mesh, float rad, float ringrad, int usub, int vsub);
+static void zsort(struct mesh *m);
 
 static struct screen scr = {
        "polytest",
@@ -27,7 +30,7 @@ static struct screen scr = {
        draw
 };
 
-static struct mesh cube;
+static struct mesh cube, torus;
 
 struct screen *polytest_screen(void)
 {
@@ -37,12 +40,15 @@ struct screen *polytest_screen(void)
 static int init(void)
 {
        gen_cube(&cube, 1.0);
+       gen_torus(&torus, 1.0, 0.25, 24, 12);
        return 0;
 }
 
 static void destroy(void)
 {
        free(cube.varr);
+       free(torus.varr);
+       free(torus.iarr);
 }
 
 static void start(long trans_time)
@@ -82,13 +88,16 @@ static void draw(void)
        g3d_rotate(phi, 1, 0, 0);
        g3d_rotate(theta, 0, 1, 0);
 
-       draw_mesh(&cube);
+       zsort(&torus);
+       draw_mesh(&torus);
+
+       swap_buffers(fb_pixels);
 }
 
 static void draw_mesh(struct mesh *mesh)
 {
        if(mesh->iarr) {
-               /*g3d_draw_indexed(mesh->prim, mesh->iarr, mesh->icount, mesh->varr);*/
+               g3d_draw_indexed(mesh->prim, mesh->varr, mesh->vcount, mesh->iarr, mesh->icount);
        } else {
                g3d_draw(mesh->prim, mesh->varr, mesh->vcount);
        }
@@ -163,3 +172,119 @@ static int gen_cube(struct mesh *mesh, float sz)
 
        return 0;
 }
+
+static void torusvec(float *res, float theta, float phi, float mr, float rr)
+{
+       float rx, ry, rz;
+       theta = -theta;
+
+       rx = -cos(phi) * rr + mr;
+       ry = sin(phi) * rr;
+       rz = 0.0f;
+
+       res[0] = rx * sin(theta) + rz * cos(theta);
+       res[1] = ry;
+       res[2] = -rx * cos(theta) + rz * sin(theta);
+}
+
+static int gen_torus(struct mesh *mesh, float rad, float ringrad, int usub, int vsub)
+{
+       int i, j;
+       int nfaces, uverts, vverts;
+       struct g3d_vertex *vptr;
+       int16_t *iptr;
+
+       mesh->prim = G3D_QUADS;
+
+       if(usub < 4) usub = 4;
+       if(vsub < 2) vsub = 2;
+
+       uverts = usub + 1;
+       vverts = vsub + 1;
+
+       mesh->vcount = uverts * vverts;
+       nfaces = usub * vsub;
+       mesh->icount = nfaces * 4;
+
+       if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
+               return -1;
+       }
+       if(!(mesh->iarr = malloc(mesh->icount * sizeof *mesh->iarr))) {
+               return -1;
+       }
+       vptr = mesh->varr;
+       iptr = mesh->iarr;
+
+       for(i=0; i<uverts; i++) {
+               float u = (float)i / (float)(uverts - 1);
+               float theta = u * 2.0 * M_PI;
+               float rcent[3];
+
+               torusvec(rcent, theta, 0, rad, 0);
+
+               for(j=0; j<vverts; j++) {
+                       float v = (float)j / (float)(vverts - 1);
+                       float phi = v * 2.0 * M_PI;
+                       int chess = (i & 1) == (j & 1);
+
+                       torusvec(&vptr->x, theta, phi, rad, ringrad);
+
+                       vptr->nx = (vptr->x - rcent[0]) / ringrad;
+                       vptr->ny = (vptr->y - rcent[1]) / ringrad;
+                       vptr->nz = (vptr->z - rcent[2]) / ringrad;
+                       vptr->u = u;
+                       vptr->v = v;
+                       vptr->r = chess ? 255 : 64;
+                       vptr->g = 128;
+                       vptr->b = chess ? 64 : 255;
+                       ++vptr;
+
+                       if(i < usub && j < vsub) {
+                               int idx = i * vverts + j;
+                               *iptr++ = idx;
+                               *iptr++ = idx + 1;
+                               *iptr++ = idx + vverts + 1;
+                               *iptr++ = idx + vverts;
+                       }
+               }
+       }
+       return 0;
+}
+
+
+static struct {
+       struct g3d_vertex *varr;
+       const float *xform;
+} zsort_cls;
+
+static int zsort_cmp(const void *aptr, const void *bptr)
+{
+       const int16_t *a = (const int16_t*)aptr;
+       const int16_t *b = (const int16_t*)bptr;
+
+       const float *m = zsort_cls.xform;
+
+       const struct g3d_vertex *va = zsort_cls.varr + a[0];
+       const struct g3d_vertex *vb = zsort_cls.varr + b[0];
+
+       float za = m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
+       float zb = m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
+
+       va = zsort_cls.varr + a[2];
+       vb = zsort_cls.varr + b[2];
+
+       za += m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14];
+       zb += m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14];
+
+       return za - zb;
+}
+
+static void zsort(struct mesh *m)
+{
+       int nfaces = m->icount / m->prim;
+
+       zsort_cls.varr = m->varr;
+       zsort_cls.xform = g3d_get_matrix(G3D_MODELVIEW, 0);
+
+       qsort(m->iarr, nfaces, m->prim * sizeof *m->iarr, zsort_cmp);
+}
index 54cd3e2..927903b 100644 (file)
@@ -117,11 +117,8 @@ int scr_change(struct screen *s, long trans_time)
                trans_dur = 0;
        }
 
-       if(cur) {
-               if(cur->stop) {
-                       cur->stop(trans_dur);
-               }
-
+       if(cur && cur->stop) {
+               cur->stop(trans_dur);
                prev = cur;
                next = s;
        } else {
index b3ff905..e5eda65 100644 (file)
@@ -32,6 +32,7 @@ int main(int argc, char **argv)
                fprintf(stderr, "failed to allocate virtual framebuffer\n");
                return 1;
        }
+       vmem_front = vmem_back = fb_pixels;
 
        SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
        if(!(fbsurf = SDL_SetVideoMode(xsz, ysz, fb_bpp, sdl_flags))) {
@@ -98,6 +99,11 @@ void demo_quit(void)
        quit = 1;
 }
 
+void swap_buffers(void *pixels)
+{
+       /* do nothing, all pointers point to the same buffer */
+}
+
 static void handle_event(SDL_Event *ev)
 {
        switch(ev->type) {
index 13eea70..63f0f9f 100644 (file)
@@ -27,8 +27,6 @@ static void draw_tunnel_range(unsigned short *pixels, int xoffs, int yoffs, int
 static int count_bits(unsigned int x);
 static int count_zeros(unsigned int x);
 
-static unsigned int *gen_test_image(int *wptr, int *hptr);
-
 static struct screen scr = {
        "tunnel",
        init,
@@ -184,11 +182,13 @@ static void draw(void)
                int starty = i * num_lines;
                int resty = starty + draw_lines;
                int rest_lines = num_lines - draw_lines;
-               draw_tunnel_range((unsigned short*)fb_pixels, xoffs, yoffs, starty, draw_lines, time_msec);
+               draw_tunnel_range(vmem_back, xoffs, yoffs, starty, draw_lines, time_msec);
                if(rest_lines) {
-                       memset((unsigned short*)fb_pixels + resty * fb_width, 0, rest_lines * fb_width * 2);
+                       memset(vmem_back + resty * fb_width, 0, rest_lines * fb_width * 2);
                }
        }
+
+       swap_buffers(0);
 }
 
 static void tunnel_color(int *rp, int *gp, int *bp, long toffs, unsigned int tpacked, int fog)
@@ -264,6 +264,7 @@ static int count_zeros(unsigned int x)
        return num;
 }
 
+/*
 static unsigned int *gen_test_image(int *wptr, int *hptr)
 {
        int i, j;
@@ -287,3 +288,4 @@ static unsigned int *gen_test_image(int *wptr, int *hptr)
        *hptr = ysz;
        return pixels;
 }
+*/