a0199a56e45eff505999a278b54bb7e0c7f690ad
[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
10 static const struct menuent {
11         int x, y, len, height;
12 } menuent[] = {
13         {240, 300, 170, 48},
14         {230, 360, 184, 48},
15         {260, 424, 130, 48}
16 };
17
18 static int cur;
19
20 static uint16_t *bgpix;
21 static int bgwidth, bgheight;
22
23 int menu_init(void)
24 {
25         if(!(bgpix = img_load_pixels("data/menbg640.png", &bgwidth, &bgheight, IMG_FMT_RGB565))) {
26                 fprintf(stderr, "failed to load menu bg image\n");
27                 return -1;
28         }
29         return 0;
30 }
31
32 void menu_cleanup(void)
33 {
34         img_free_pixels(bgpix);
35 }
36
37 void menu_start(void)
38 {
39         draw = menu_draw;
40         key_event = menu_keyb;
41 }
42
43 void menu_stop(void)
44 {
45 }
46
47
48 #define BBW             256
49 #define BBH             64
50
51 void menu_draw(void)
52 {
53         static uint16_t blurbuf[2][BBW * BBH];
54         int fboffs, bboffs, tmp;
55         const struct menuent *ent = menuent + cur;
56
57         int blur_rad_x = (int)((sin(time_msec / 1000.0f) * 0.5f + 0.5f) * 50.0f);
58         int blur_rad_y = (int)((cos(time_msec / 1000.0f) * 0.5f + 0.5f) * 50.0f);
59
60         fboffs = (ent->y - ent->height / 2) * fb_width + ent->x;
61         bboffs = (BBH - ent->height) / 2 * BBW + BBW / 2;
62
63         memset(blurbuf[0], 0, sizeof blurbuf[0]);
64         blit(blurbuf[0] + bboffs, BBW, bgpix + fboffs, ent->len, ent->height, bgwidth);
65
66         blur_horiz(blurbuf[1], blurbuf[0], BBW, BBH, blur_rad_x + 3, 0x140);
67         blur_vert(blurbuf[0], blurbuf[1], BBW, BBH, blur_rad_y / 4 + 3, 0x140);
68
69         wait_vsync();
70
71         memcpy(fb_pixels, bgpix, fb_size);
72         tmp = fboffs;
73         fboffs -= 8 * fb_width + 32;
74         bboffs -= 8 * BBW + 32;
75         blit(fb_pixels + fboffs, fb_width, blurbuf[0] + bboffs, ent->len + 64, ent->height + 16, BBW);
76         fboffs = tmp;
77         blit_key(fb_pixels + fboffs, fb_width, bgpix + fboffs, ent->len, ent->height, bgwidth, 0);
78
79         blit_frame(fb_pixels, 0);
80 }
81
82 void menu_keyb(int key, int pressed)
83 {
84         if(!pressed) return;
85
86         switch(key) {
87         case 27:
88                 game_quit();
89                 break;
90
91         case KB_UP:
92                 if(cur > 0) cur--;
93                 break;
94
95         case KB_DOWN:
96                 if(cur < sizeof menuent / sizeof *menuent - 1) {
97                         cur++;
98                 }
99                 break;
100
101         case '\n':
102         case '\r':
103                 switch(cur) {
104                 case 0:
105                         /* enter game */
106                         break;
107
108                 case 1:
109                         //options_start();
110                         break;
111
112                 case 2:
113                         game_quit();
114                         break;
115                 }
116         }
117 }