Load and display background image for "galaxy rise" effect
[dosdemo] / src / mike.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <assert.h>
6 #include "imago2.h"
7 #include "demo.h"
8 #include "screen.h"
9
10 #define BG_FILENAME "data/grise.png"
11
12 static int init(void);
13 static void destroy(void);
14 static void start(long trans_time);
15 static void stop(long trans_time);
16 static void draw(void);
17
18 static void convert32To16(unsigned int *src32, unsigned short *dst16, unsigned int pixelCount);
19
20 static unsigned short *background = 0;
21 static unsigned int backgroundW = 0;
22 static unsigned int backgroundH = 0;
23
24 static struct screen scr = {
25         "mike",
26         init,
27         destroy,
28         start,
29         stop,
30         draw
31 };
32
33 struct screen *mike_screen(void)
34 {
35         return &scr;
36 }
37
38
39 static int init(void)
40 {
41         if (!(background = img_load_pixels(BG_FILENAME, &backgroundW, &backgroundH, IMG_FMT_RGBA32))) {
42                 fprintf(stderr, "failed to load image " BG_FILENAME "\n");
43                 return -1;
44         }
45
46         /* Convert to 16bpp */
47         convert32To16((unsigned int*)background, background, backgroundW * backgroundH);
48
49         return 0;
50 }
51
52 static void destroy(void)
53 {
54         img_free_pixels(background);
55 }
56
57 static void start(long trans_time)
58 {
59
60 }
61
62 static void stop(long trans_time)
63 {
64 }
65
66 static void draw(void)
67 {       
68         unsigned short *pixels = fb_pixels;
69
70         int j, i;
71         for (j = 0; j < fb_height; j++) {
72                 for (i = 0; i < fb_width; i++) {
73                         *pixels++ = 0x0000;
74                 }
75         }
76
77         memcpy(fb_pixels, background, backgroundW * backgroundH * 2);
78 }
79
80 /* src and dst can be the same */
81 static void convert32To16(unsigned int *src32, unsigned short *dst16, unsigned int pixelCount) {
82         unsigned int p;
83         while (pixelCount) {
84                 p = *src32++;
85                 *dst16++ =      ((p << 8) & 0xF800)             /* R */
86                         |               ((p >> 5) & 0x07E0)             /* G */
87                         |               ((p >> 19) & 0x001F);   /* B */
88                 pixelCount--;
89         }
90 }
91
92