Load and display background image for "galaxy rise" effect
authorMichael Georgoulopoulos <mgeorgoulopoulos@gmail.com>
Thu, 1 Sep 2016 20:29:00 +0000 (23:29 +0300)
committerMichael Georgoulopoulos <mgeorgoulopoulos@gmail.com>
Thu, 1 Sep 2016 20:29:00 +0000 (23:29 +0300)
src/mike.c

index f985886..719121e 100644 (file)
@@ -7,12 +7,20 @@
 #include "demo.h"
 #include "screen.h"
 
+#define BG_FILENAME "data/grise.png"
+
 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,25 +38,29 @@ 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)
@@ -58,9 +70,23 @@ static void draw(void)
        int j, i;
        for (j = 0; j < fb_height; j++) {
                for (i = 0; i < fb_width; i++) {
-                       *pixels++ = 0xF800;
+                       *pixels++ = 0x0000;
                }
        }
+
+       memcpy(fb_pixels, background, backgroundW * backgroundH * 2);
+}
+
+/* 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--;
+       }
 }