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