fixed menu blur
[eradicate] / src / menuscr.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <math.h>
4 #include "screens.h"
5 #include "imago2.h"
6 #include "gfx.h"
7 #include "gfxutil.h"
8 #include "game.h"
9 #include "util.h"
10
11 #define PADX    42
12 #define PADX2   (PADX * 2)
13 #define PADY    16
14 #define PADY2   (PADY * 2)
15
16 static const struct menuent {
17         int x, y, len, height;
18 } menuent[] = {
19         {252 - PADX, 281 - PADY, 147 + PADX2, 37 + PADY2},
20         {244 - PADX, 344 - PADY, 161 + PADX2, 37 + PADY2},
21         {276 - PADX, 407 - PADY, 102 + PADX2, 38 + PADY2}
22 };
23
24 static int cur;
25
26 static uint16_t *bgpix;
27 static int bgwidth, bgheight;
28
29 int menu_init(void)
30 {
31         if(!(bgpix = img_load_pixels("data/menbg640.png", &bgwidth, &bgheight, IMG_FMT_RGB565))) {
32                 fprintf(stderr, "failed to load menu bg image\n");
33                 return -1;
34         }
35         return 0;
36 }
37
38 void menu_cleanup(void)
39 {
40         img_free_pixels(bgpix);
41 }
42
43 void menu_start(void)
44 {
45         draw = menu_draw;
46         key_event = menu_keyb;
47
48         memcpy(fb_pixels, bgpix, fb_size);
49 }
50
51 void menu_stop(void)
52 {
53 }
54
55
56 #define BBW             256
57 #define BBH             64
58
59 void menu_draw(void)
60 {
61         static uint16_t blurbuf[2][BBW * BBH];
62         int fboffs, tmp, cleartop;
63         const struct menuent *ent = menuent + cur;
64
65         int blur_rad_x = (int)((sin(time_msec / 1000.0f) * 0.5f + 0.5f) * 50.0f);
66         int blur_rad_y = (int)((cos(time_msec / 1000.0f) * 0.5f + 0.5f) * 50.0f);
67
68         fboffs = ent->y * fb_width + ent->x;
69
70         memset(blurbuf[0], 0, sizeof blurbuf[0]);
71         blit(blurbuf[0], BBW, bgpix + fboffs, ent->len, ent->height, bgwidth);
72
73         blur_horiz(blurbuf[1], blurbuf[0], BBW, BBH, blur_rad_x + 3, 0x140);
74         blur_vert(blurbuf[0], blurbuf[1], BBW, BBH, blur_rad_y / 4 + 3, 0x140);
75
76         //wait_vsync();
77
78         cleartop = 280 * fb_width;
79         memcpy(fb_pixels + cleartop, bgpix + cleartop, (fb_height - 280) * fb_width << 1);
80
81         blit(fb_pixels + fboffs, fb_width, blurbuf[0], ent->len, ent->height, BBW);
82         blit_key(fb_pixels + fboffs, fb_width, bgpix + fboffs, ent->len, ent->height, bgwidth, 0);
83
84         if(show_fps) {
85                 blit(fb_pixels, fb_width, bgpix, 64, 16, bgwidth);
86         }
87
88         blit_frame(fb_pixels, 0);
89 }
90
91 void menu_keyb(int key, int pressed)
92 {
93         if(!pressed) return;
94
95         switch(key) {
96         case 27:
97                 game_quit();
98                 break;
99
100         case KB_UP:
101                 if(cur > 0) cur--;
102                 break;
103
104         case KB_DOWN:
105                 if(cur < sizeof menuent / sizeof *menuent - 1) {
106                         cur++;
107                 }
108                 break;
109
110         case '\n':
111         case '\r':
112                 switch(cur) {
113                 case 0:
114                         /* enter game */
115                         break;
116
117                 case 1:
118                         //options_start();
119                         break;
120
121                 case 2:
122                         game_quit();
123                         break;
124                 }
125         }
126 }