04b36f39a58af60d5b179ac97aa4f54b57d6be6b
[eradicate] / src / menuscr.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "screens.h"
4 #include "imago2.h"
5 #include "gfx.h"
6 #include "gfxutil.h"
7 #include "game.h"
8
9 static const struct menuent {
10         int x, y, len, height;
11 } menuent[] = {
12         {240, 300, 170, 40},
13         {230, 360, 184, 40},
14         {260, 424, 130, 40}
15 };
16
17 static int cur;
18
19 static uint16_t *bgpix;
20 static int bgwidth, bgheight;
21
22 int menu_init(void)
23 {
24         if(!(bgpix = img_load_pixels("data/menbg640.png", &bgwidth, &bgheight, IMG_FMT_RGB565))) {
25                 fprintf(stderr, "failed to load menu bg image\n");
26                 return -1;
27         }
28         return 0;
29 }
30
31 void menu_cleanup(void)
32 {
33         img_free_pixels(bgpix);
34 }
35
36 void menu_start(void)
37 {
38         draw = menu_draw;
39         key_event = menu_keyb;
40 }
41
42 void menu_stop(void)
43 {
44 }
45
46
47
48 void menu_draw(void)
49 {
50         static uint16_t blurbuf[2][16384];
51         int y, offs;
52         int i, j;
53         const struct menuent *ent = menuent + cur;
54
55         y = ent->y - ent->height / 2;
56         offs = y * fb_width + ent->x;
57         blit(blurbuf[0], ent->len, bgpix + offs, ent->len, ent->height, bgwidth);
58
59         blur_grey_horiz(blurbuf[1], blurbuf[0], ent->len, ent->height, 7, 0x100);
60
61         wait_vsync();
62
63         memcpy(fb_pixels, bgpix, fb_size);
64         blit(fb_pixels + offs, fb_width, blurbuf[1], ent->len, ent->height, ent->len);
65
66         blit_frame(fb_pixels, 0);
67 }
68
69 void menu_keyb(int key, int pressed)
70 {
71         if(!pressed) return;
72
73         switch(key) {
74         case 27:
75                 game_quit();
76                 break;
77
78         case KB_UP:
79                 if(cur > 0) cur--;
80                 break;
81
82         case KB_DOWN:
83                 if(cur < sizeof menuent / sizeof *menuent - 1) {
84                         cur++;
85                 }
86                 break;
87         }
88 }