I don't think I'll bother any more with this, so I'll just make it start
[ld42_outofspace] / src / menuscr.cc
1 #include <stdio.h>
2 #include "game.h"
3 #include "screen.h"
4 #include "opengl.h"
5 #include "goatkit/goatkit.h"
6
7 enum {
8         BN_START,
9         BN_EXIT
10 };
11
12 static void bn_handler(goatkit::Widget *w, const goatkit::Event &ev, void *cls);
13
14 extern GameScreen *scr_game;
15
16 static int virt_width = 800;
17 static int virt_height = 600;
18 static goatkit::Screen ui;
19 static goatkit::Button *bn_start, *bn_exit;
20 static bool start_pending, exit_pending;
21 static bool stopped;
22
23 bool MenuScreen::init()
24 {
25         ui.hide();
26
27         int bnwidth = 200;
28         int bnheight = 40;
29
30         int xpos = (virt_width - bnwidth) / 2;
31         int ypos = 100;
32         int vsep = 80;
33
34         bn_start = new goatkit::Button;
35         bn_start->set_position(xpos, ypos += vsep);
36         bn_start->set_size(bnwidth, bnheight);
37         bn_start->set_text("Don't Panic!");
38         bn_start->set_callback(goatkit::EV_CLICK, bn_handler, (void*)BN_START);
39         ui.add_widget(bn_start);
40
41         bn_exit = new goatkit::Button;
42         bn_exit->set_position(xpos, ypos += vsep);
43         bn_exit->set_size(bnwidth, bnheight);
44         bn_exit->set_text("Panic");
45         bn_exit->set_callback(goatkit::EV_CLICK, bn_handler, (void*)BN_EXIT);
46         ui.add_widget(bn_exit);
47
48         ui.set_visibility_transition(300);
49
50         if(!(goatkit::theme = goatkit::get_theme("simple"))) {
51                 return false;
52         }
53
54         return true;
55 }
56
57 void MenuScreen::destroy()
58 {
59 }
60
61
62 void MenuScreen::start()
63 {
64         ui.show();
65         stopped = false;
66 }
67
68 void MenuScreen::stop()
69 {
70         ui.hide();
71         stopped = true;
72 }
73
74 void MenuScreen::draw()
75 {
76         if(ui.get_visibility() == 0.0f) {
77                 if(start_pending) {
78                         start_pending = false;
79                         push_screen(scr_game);
80                         stop();
81                         return;
82                 }
83                 if(exit_pending) {
84                         exit_pending = false;
85                         game_quit();
86                         return;
87                 }
88         }
89
90         if(stopped) {
91                 start();
92         }
93
94         glClearColor(0, 0, 0, 1);
95         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
96
97         glMatrixMode(GL_PROJECTION);
98         glLoadIdentity();
99         glOrtho(0, virt_width, virt_height, 0, -1, 1);
100
101         glMatrixMode(GL_MODELVIEW);
102         glLoadIdentity();
103
104         ui.draw();
105
106 }
107
108 void MenuScreen::reshape(int x, int y)
109 {
110         ui.set_size(virt_width, virt_height);
111 }
112
113
114 void MenuScreen::keyboard(int key, bool pressed)
115 {
116         if(pressed) {
117                 switch(key) {
118                 case KEY_ESC:
119                         exit_pending = true;
120                         ui.hide();
121                         return;
122
123                 default:
124                         break;
125                 }
126         }
127
128         ui.sysev_keyboard(key, pressed);
129 }
130
131 void MenuScreen::mbutton(int bn, bool pressed, int x, int y)
132 {
133         x = x * virt_width / win_width;
134         y = y * virt_height / win_height;
135         ui.sysev_mouse_button(bn, pressed, x, y);
136 }
137
138 void MenuScreen::mmotion(int x, int y)
139 {
140         x = x * virt_width / win_width;
141         y = y * virt_height / win_height;
142         ui.sysev_mouse_motion(x, y);
143 }
144
145 static void bn_handler(goatkit::Widget *w, const goatkit::Event &ev, void *cls)
146 {
147         if(w == bn_start) {
148                 start_pending = true;
149                 ui.hide();
150         } else if(w == bn_exit) {
151                 exit_pending = true;
152                 ui.hide();
153         }
154 }