removed clang-format and clang_complete files from the repo
[dosdemo] / src / scr / cybersun.c
index 7a247d2..fde29ab 100644 (file)
@@ -1,16 +1,26 @@
 #include <stdio.h>
 #include <math.h>
+#include <assert.h>
 #include "demo.h"
 #include "3dgfx.h"
 #include "screen.h"
 #include "gfxutil.h"
 #include "mesh.h"
 #include "image.h"
+#include "util.h"
+#include "cgmath/cgmath.h"
+
+#define TM_RIPPLE_START                1.0f
+#define TM_RIPPLE_TRANS_LEN    3.0f
+
+#define VFOV   50.0f
+#define HFOV   (VFOV * 1.333333f)
 
 static int init(void);
 static void destroy(void);
 static void start(long trans_time);
 static void draw(void);
+static void draw_mountains(void);
 
 static struct screen scr = {
        "cybersun",
@@ -21,7 +31,7 @@ static struct screen scr = {
        draw
 };
 
-static float cam_theta = 0, cam_phi = 10;
+static float cam_theta = 0, cam_phi = 0;
 static float cam_dist = 0;
 
 static struct g3d_mesh gmesh;
@@ -29,6 +39,14 @@ static struct g3d_mesh gmesh;
 #define GMESH_SIZE             128
 static struct image gtex;
 
+#define MOUNTIMG_WIDTH 512
+#define MOUNTIMG_HEIGHT        64
+static struct image mountimg;
+static int mountimg_skip[MOUNTIMG_WIDTH];
+
+static long part_start;
+
+
 struct screen *cybersun_screen(void)
 {
        return &scr;
@@ -48,6 +66,24 @@ static int init(void)
        if(load_image(&gtex, "data/pgrid.png") == -1) {
                return -1;
        }
+       if(load_image(&mountimg, "data/cybmount.png") == -1) {
+               return -1;
+       }
+       assert(mountimg.width == MOUNTIMG_WIDTH);
+       assert(mountimg.height == MOUNTIMG_HEIGHT);
+
+       for(i=0; i<MOUNTIMG_WIDTH; i++) {
+               uint16_t *pptr = mountimg.pixels + i;
+               for(j=0; j<MOUNTIMG_HEIGHT; j++) {
+                       if(*pptr != 0x7e0) {
+                               mountimg_skip[i] = j;
+                               break;
+                       }
+                       pptr += MOUNTIMG_WIDTH;
+               }
+       }
+       destroy_image(&mountimg);
+       mountimg.pixels = 0;
 
        return 0;
 }
@@ -62,19 +98,23 @@ static void start(long trans_time)
 {
        g3d_matrix_mode(G3D_PROJECTION);
        g3d_load_identity();
-       g3d_perspective(50.0, 1.3333333, 0.5, 500.0);
+       g3d_perspective(VFOV, 1.3333333, 0.5, 500.0);
 
        g3d_enable(G3D_CULL_FACE);
 
-       g3d_clear_color(0, 0, 0);
+       g3d_clear_color(85, 70, 136);
+
+       part_start = time_msec;
 }
 
 static void update(void)
 {
        int i, j;
-       float t = time_msec / 1000.0f;
+       float t = (time_msec - part_start) / 1000.0f;
        struct g3d_vertex *vptr;
 
+       float ampl = cgm_smoothstep(TM_RIPPLE_START, TM_RIPPLE_START + TM_RIPPLE_TRANS_LEN, t);
+
        mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
 
        /* update mesh */
@@ -91,6 +131,7 @@ static void update(void)
                        vptr->z += cos(y * 0.5 + t);
                        vptr->z += sin(r + t) * 0.5f;
                        vptr->z *= r * 0.1f > 1.0f ? 1.0f : r * 0.1f;
+                       vptr->z *= ampl;
                        vptr++;
                }
        }
@@ -112,6 +153,7 @@ static void draw(void)
        }
 
        g3d_clear(G3D_COLOR_BUFFER_BIT | G3D_DEPTH_BUFFER_BIT);
+       draw_mountains();
 
        g3d_set_texture(gtex.width, gtex.height, gtex.pixels);
        g3d_enable(G3D_TEXTURE_2D);
@@ -127,3 +169,47 @@ static void draw(void)
 
        swap_buffers(fb_pixels);
 }
+
+/* XXX all the sptr calculations assume mountimg.width == 512 */
+static void draw_mountains(void)
+{
+       int i, j, horizon_y, y;
+       int32_t x, xstart, xend, dx;
+       uint16_t *dptr, *sptr;
+
+       /* 24.8 fixed point, 512 width, 90deg arc */
+       xstart = cround64(cam_theta * (256.0 * MOUNTIMG_WIDTH / 90.0));
+       xend = cround64((cam_theta + HFOV) * (256.0 * MOUNTIMG_WIDTH / 90.0));
+       dx = (xend - xstart) / FB_WIDTH;
+       x = xstart;
+
+       horizon_y = cround64(-cam_phi * (FB_HEIGHT / 45.0)) + FB_HEIGHT / 2;
+       y = horizon_y - MOUNTIMG_HEIGHT;
+
+       if(y >= FB_HEIGHT) {
+               /* TODO draw gradient for the sky */
+               return;
+       }
+       if(horizon_y < 0) {
+               memset(fb_pixels, 0, FB_WIDTH * FB_HEIGHT * 2);
+               return;
+       }
+
+       for(i=0; i<FB_WIDTH; i++) {
+               int skip = mountimg_skip[(x >> 8) & 0x1ff];
+               int vspan = MOUNTIMG_HEIGHT - skip;
+
+               dptr = fb_pixels + (y + skip) * FB_WIDTH + i;
+
+               for(j=0; j<vspan; j++) {
+                       *dptr = 0;      /* black mountains */
+                       dptr += FB_WIDTH;
+               }
+
+               x += dx;
+       }
+
+       if(horizon_y < FB_HEIGHT) {
+               memset(fb_pixels + horizon_y * FB_WIDTH, 0, (FB_HEIGHT - horizon_y) * FB_WIDTH * 2);
+       }
+}