My first commit in this project, just a lame plasma test effect and my tinyfps with...
[dosdemo] / src / grise.c
index e6debce..ed15de0 100644 (file)
@@ -99,7 +99,7 @@ static int init(void)
        int tmpBitmapW, tmpBitmapH;
 
        /* Allocate back buffer */
-       backBuffer = (unsigned short*) malloc(BB_SIZE * BB_SIZE * sizeof(unsigned short));
+       backBuffer = (unsigned short*) calloc(BB_SIZE * BB_SIZE, sizeof(unsigned short));
 
        /* grise.png contains the background (horizon), baked reflection and normalmap for displacement */
        if (!(background = img_load_pixels(BG_FILENAME, &backgroundW, &backgroundH, IMG_FMT_RGBA32))) {
@@ -150,6 +150,73 @@ static void stop(long trans_time)
 {
 }
 
+
+struct {
+       int circleX[3];
+       int circleY[3];
+} wheelState;
+
+static void updateWheel(float t) {
+       float x = 0.0f;
+       float y = 18.0f;
+       float nx, ny;
+       float cost, sint;
+       static float sin120 = 0.86602540378f;
+       static float cos120 = -0.5f;
+
+       /* Rotate */
+       sint = sin(t);
+       cost = cos(t);
+       nx = x * cost - y * sint;
+       ny = y * cost + x * sint;
+       x = nx;
+       y = ny;
+       wheelState.circleX[0] = (int)(x + 0.5f) + 16;
+       wheelState.circleY[0] = (int)(y + 0.5f) + 16;
+
+       /* Rotate by 120 degrees, for the second circle */
+       nx = x * cos120 - y * sin120;
+       ny = y * cos120 + x * sin120;
+       x = nx;
+       y = ny;
+       wheelState.circleX[1] = (int)(x + 0.5f) + 16;
+       wheelState.circleY[1] = (int)(y + 0.5f) + 16;
+
+       /* 3rd circle */
+       nx = x * cos120 - y * sin120;
+       ny = y * cos120 + x * sin120;
+       x = nx;
+       y = ny;
+       wheelState.circleX[2] = (int)(x + 0.5f) + 16;
+       wheelState.circleY[2] = (int)(y + 0.5f) + 16;
+}
+
+#define WHEEL_CIRCLE_RADIUS 18
+#define WHEEL_CIRCLE_RADIUS_SQ (WHEEL_CIRCLE_RADIUS * WHEEL_CIRCLE_RADIUS)
+
+static unsigned short wheel(int x, int y) {
+       int cx, cy, count=0;
+
+       /* First circle */
+       cx = wheelState.circleX[0] - x;
+       cy = wheelState.circleY[0] - y;
+       if (cx*cx + cy*cy < WHEEL_CIRCLE_RADIUS_SQ) count++;
+
+       /* 2nd circle */
+       cx = wheelState.circleX[1] - x;
+       cy = wheelState.circleY[1] - y;
+       if (cx*cx + cy*cy < WHEEL_CIRCLE_RADIUS_SQ) count++;
+
+       /* 3rd circle */
+       cx = wheelState.circleX[2] - x;
+       cy = wheelState.circleY[2] - y;
+       if (cx*cx + cy*cy < WHEEL_CIRCLE_RADIUS_SQ) count++;
+
+       if (count >= 2) return 0xFFFF;
+
+       return 0x000F;
+}
+
 static void draw(void)
 {      
        int scroll = MIN_SCROLL + (MAX_SCROLL - MIN_SCROLL) * mouse_x / fb_width;
@@ -163,6 +230,9 @@ static void draw(void)
        lastFrameDuration = (time_msec - lastFrameTime) / 1000.0f;
        lastFrameTime = time_msec;
 
+       /* Update mini-effects here */
+       updateWheel(time_msec / 1000.0f);
+
        /* First, render the horizon */
        for (scanline = 0; scanline < HORIZON_HEIGHT; scanline++) {
                memcpy(dst, src, fb_width * 2);
@@ -202,19 +272,24 @@ static void draw(void)
        }
 
        /* Then after displacement, blit the objects */
-       //for (i = 0; i < 5; i++) rleBlit(backBuffer + PIXEL_PADDING, fb_width, fb_height, BB_SIZE, grobj, 134 + (i-3) * 60, 100);
-       for (i = 0; i < 5; i++) rleBlitScale(backBuffer + PIXEL_PADDING, fb_width, fb_height, BB_SIZE, grobj, 134 + (i - 3) * 120, 0, 1.8f, 1.8f);
+       for (i = 0; i < 5; i++) rleBlit(backBuffer + PIXEL_PADDING, fb_width, fb_height, BB_SIZE, grobj, 134 + (i-3) * 60, 100);
+
+       for (scanline = 0; scanline < 32; scanline++) {
+               for (i = 0; i < 32; i++) {
+                       backBuffer[PIXEL_PADDING + scanline * BB_SIZE + i] = wheel(i, scanline);
+               }
+       }
 
        /* Blit effect to framebuffer */
        src = backBuffer + PIXEL_PADDING;
-       dst = fb_pixels;
+       dst = vmem_back;
        for (scanline = 0; scanline < fb_height; scanline++) {
                memcpy(dst, src, fb_width * 2);
                src += BB_SIZE; 
                dst += fb_width;
        }
 
-       swap_buffers(fb_pixels);
+       swap_buffers(0);
 }
 
 /* src and dst can be the same */
@@ -314,7 +389,7 @@ static void updateScrollTables(float dt) {
 #define RLE_STREAKS_PER_SCANLINE 4
 /* Every streak is encoded by 2 bytes: offset and count of black pixels in the streak */
 #define RLE_BYTES_PER_SCANLINE RLE_STREAKS_PER_SCANLINE * 2
-#define RLE_FILL_COLOR 0xFF00 
+#define RLE_FILL_COLOR 0
 #define RLE_FILL_COLOR_32 ((RLE_FILL_COLOR << 16) | RLE_FILL_COLOR)
 
 #define RLE_FIXED_BITS 16
@@ -566,4 +641,4 @@ static void rleBlitScaleInv(unsigned short *dst, int dstW, int dstH, int dstStri
 
                dst -= dstStride;
        }
-}
\ No newline at end of file
+}