me empecine con el makefile.... make stats :)
[seni.git] / engine / menu.cpp
blob142d35023209d6048ceec0f7bfcfe7596ed93148
1 /*SENI, Search for Extra Nibiru Intelligence*/
3 /*
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 Copyright SSW Team 2010
22 #include <string.h>
23 #include "menu.hpp"
24 #include "engine.hpp"
26 Option::Option()
28 this->id = 0;
29 this->image[0] = 0;
30 this->Simage = 0;
31 this->event = 0;
32 this->engine = 0;
35 Option::Option(Engine *e)
37 this->id = 0;
38 this->image[0] = 0;
39 this->Simage = 0;
40 this->event = 0;
41 this->engine = e;
44 Option::Option(char *f, Engine *e)
46 this->id = 0;
47 this->engine = e;
48 this->setImage(f);
49 this->event = 0;
52 Option::Option(char *f, int i, Engine *e)
54 this->setId(i);
55 this->engine = e;
56 this->setImage(f);
57 this->event = 0;
60 int Option::getId(void)
62 return this->id;
65 void Option::setId(int i)
67 this->id = i;
70 int Option::setImage(char *f)
72 char err[255];
73 if(!this->engine)
74 return MENU_NO_ENGINE;
75 strcpy(this->image, f);
76 this->Simage = SDL_LoadBMP(f);
77 if(!this->Simage){
78 strcpy(err, "Error: SDL_LoadBMP(f): ");
79 strcat(err, SDL_GetError());
80 this->engine->setError(err);
81 return MENU_ERROR;
83 this->engine->setError((char *)"No hay errores");
84 return MENU_NO_ERROR;
87 void Option::setEvent(option_event oe)
89 event = oe;
92 int Option::execEvent(SDL_Event *se, Engine *e, Menu *m, Option *o)
94 if(!this->event)
95 return 0;
96 return this->event(se, e, m, o);
99 int Option::show(void)
101 if(!this->engine)
102 return MENU_NO_ENGINE;
103 if(!this->Simage){
104 engine->setError((char *)"Error: No se asigno imagen");
105 return MENU_ERROR;
107 if(this->engine->show(this->Simage, this->x, this->y, this->Simage->w, this->Simage->h) == ENGINE_ERROR)
108 return MENU_ERROR;
109 // this->engine->update(this->x, this->y, this->Simage->w, this->Simage->h);
110 return MENU_NO_ERROR;
114 int Option::update(void)
116 if(!engine)
117 return MENU_NO_ENGINE;
118 this->engine->update(this->x, this->y, this->Simage->w, this->Simage->h);
119 return MENU_NO_ERROR;
122 void Option::setXY(int x, int y)
124 this->x = x;
125 this->y = y;
128 int Option::getX(void)
130 return this->x;
133 int Option::getY(void)
135 return this->y;
138 int Option::getH(void)
140 return this->Simage ? this->Simage->h : 0;
143 int Option::getW(void)
145 return this->Simage ? this->Simage->w : 0;
148 void Option::setEngine(Engine *e)
150 this->engine = e;
153 Menu::Menu(void)
155 int i;
156 this->engine = 0;
157 for(i = 0; i < MAX_OPTIONS; i++)
158 this->options[i] = WITHOUT_OPTION;
159 this->wallpaper[0] = 0;
162 Menu::Menu(Engine *e)
164 int i;
165 this->engine = e;
166 for(i = 0; i < MAX_OPTIONS; i++)
167 this->options[i] = WITHOUT_OPTION;
168 this->wallpaper[0] = 0;
171 Menu::Menu(char *w, Engine *e)
173 int i;
174 this->engine = e;
175 for(i = 0; i < MAX_OPTIONS; i++)
176 this->options[i] = WITHOUT_OPTION;
177 this->setWallpaper(w);
180 Menu::Menu(char *w, Option **o, int im, Engine *e)
182 int i;
183 this->engine = e;
184 for(i = 0; i < im && i < MAX_OPTIONS; i++){
185 this->options[i] = o[i];
187 for(;i < MAX_OPTIONS; i++)
188 this->options[i] = WITHOUT_OPTION;
189 this->setWallpaper(w);
192 int Menu::setWallpaper(char *w)
194 char err[255];
195 strcpy(this->wallpaper, w);
196 this->Swallpaper = SDL_LoadBMP(w);
197 if(!this->Swallpaper){
198 strcpy(err, "Error: SDL_LoadBMP(w): ");
199 strcat(err, SDL_GetError());
200 this->engine->setError(err);
201 return MENU_ERROR;
203 this->engine->setError((char *)"No hay errores");
204 return MENU_NO_ERROR;
207 int Menu::addOption(Option *o)
209 int i;
210 for(i = 0; i < MAX_OPTIONS; i++){
211 if(this->options[i] == WITHOUT_OPTION){
212 this->options[i] = o;
213 return MENU_NO_ERROR;
216 return OPTIONS_FULL;
219 Option *Menu::getOptionById(int oid)
221 int i;
222 for(i = 0; i < MAX_OPTIONS; i++){
223 if(options[i] != WITHOUT_OPTION && options[i]->getId() == oid){
224 return options[i];
227 return 0;
230 Option *Menu::getOption(int i)
232 if(i >= MAX_OPTIONS)
233 return WITHOUT_OPTION;
234 return this->options[i];
237 int Menu::removeOption(int oid)
239 int i;
240 for(i = 0; i < MAX_OPTIONS; i++){
241 if(options[i] != WITHOUT_OPTION && options[i]->getId() == oid){
242 options[i] = WITHOUT_OPTION;
243 return MENU_NO_ERROR;
246 return NO_OPTION;
249 int Menu::show()
251 int i;
252 if(!this->engine)
253 return MENU_NO_ENGINE;
254 if(this->engine->show(this->Swallpaper, 0, 0, this->Swallpaper->w, this->Swallpaper->h) == ENGINE_ERROR)
255 return MENU_ERROR;
256 // this->engine->update(0, 0, 0, 0);
257 for(i = 0; i < MAX_OPTIONS; ++i){
258 if(this->options[i] != WITHOUT_OPTION)
259 this->options[i]->show();
261 return MENU_NO_ERROR;
264 int Menu::update(void)
266 if(!engine)
267 return MENU_NO_ENGINE;
268 this->engine->update(0, 0, 0, 0);
269 return MENU_NO_ERROR;
272 void Menu::setEngine(Engine *e)
274 this->engine = e;