Added scrolling
[dosdemo] / src / mike.c
index f985886..cf1ed73 100644 (file)
@@ -7,12 +7,23 @@
 #include "demo.h"
 #include "screen.h"
 
+#define BG_FILENAME "data/grise.png"
+
+#define MIN_SCROLL 32
+#define MAX_SCROLL (backgroundW - fb_width - MIN_SCROLL)
+
 static int init(void);
 static void destroy(void);
 static void start(long trans_time);
 static void stop(long trans_time);
 static void draw(void);
 
+static void convert32To16(unsigned int *src32, unsigned short *dst16, unsigned int pixelCount);
+
+static unsigned short *background = 0;
+static unsigned int backgroundW = 0;
+static unsigned int backgroundH = 0;
+
 static struct screen scr = {
        "mike",
        init,
@@ -30,36 +41,55 @@ struct screen *mike_screen(void)
 
 static int init(void)
 {
+       if (!(background = img_load_pixels(BG_FILENAME, &backgroundW, &backgroundH, IMG_FMT_RGBA32))) {
+               fprintf(stderr, "failed to load image " BG_FILENAME "\n");
+               return -1;
+       }
+
+       /* Convert to 16bpp */
+       convert32To16((unsigned int*)background, background, backgroundW * backgroundH);
+
        return 0;
 }
 
 static void destroy(void)
 {
+       //img_free_pixels(background);
 }
 
 static void start(long trans_time)
 {
-       if(trans_time) {
-               
-       }
+
 }
 
 static void stop(long trans_time)
 {
-       if(trans_time) {
-
-       }
 }
 
 static void draw(void)
 {      
-       unsigned short *pixels = fb_pixels;
+       int scroll = MIN_SCROLL + (MAX_SCROLL - MIN_SCROLL) * mouse_x / fb_width;
+       unsigned short *dst = fb_pixels;
+       unsigned short *src = background + 2 * scroll;
+       int scanline = 0;
+       
+
+       for (scanline = 0; scanline < fb_height; scanline++) {
+               memcpy(dst, src, fb_width * 2);
+               src += backgroundW;
+               dst += fb_width;
+       }
+}
 
-       int j, i;
-       for (j = 0; j < fb_height; j++) {
-               for (i = 0; i < fb_width; i++) {
-                       *pixels++ = 0xF800;
-               }
+/* src and dst can be the same */
+static void convert32To16(unsigned int *src32, unsigned short *dst16, unsigned int pixelCount) {
+       unsigned int p;
+       while (pixelCount) {
+               p = *src32++;
+               *dst16++ =      ((p << 8) & 0xF800)             /* R */
+                       |               ((p >> 5) & 0x07E0)             /* G */
+                       |               ((p >> 19) & 0x001F);   /* B */
+               pixelCount--;
        }
 }