7e910c4c0eb988235471030609fa8e8f7f5d3189
[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("Start");
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("Exit");
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         ui.sysev_keyboard(key, pressed);
117 }
118
119 void MenuScreen::mbutton(int bn, bool pressed, int x, int y)
120 {
121         x = x * virt_width / win_width;
122         y = y * virt_height / win_height;
123         ui.sysev_mouse_button(bn, pressed, x, y);
124 }
125
126 void MenuScreen::mmotion(int x, int y)
127 {
128         x = x * virt_width / win_width;
129         y = y * virt_height / win_height;
130         ui.sysev_mouse_motion(x, y);
131 }
132
133 static void bn_handler(goatkit::Widget *w, const goatkit::Event &ev, void *cls)
134 {
135         if(w == bn_start) {
136                 start_pending = true;
137                 ui.hide();
138         } else if(w == bn_exit) {
139                 exit_pending = true;
140                 ui.hide();
141         }
142 }