me empecine con el makefile.... make stats :)
[seni.git] / engine / engine.cpp
blob7f71bc11649ac8926a6010f81ec7c86577b41947
1 /*
2 Copyright SSW Team 2010
3 */
5 /*SENI, Search for Extra Nibiru Intelligence*/
7 /*
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 Copyright SSW Team 2010
26 #include <SDL/SDL.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include "engine.hpp"
30 #include "menu.hpp"
32 void defaultEvent(SDL_Event *, Engine *);
34 int Engine::initVideo(int resx, int resy, int bitres, int flags)
36 if(SDL_Init(SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO)){
37 strcpy(this->engineError, "Error: SDL_Init(SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO): ");
38 strcat(this->engineError, SDL_GetError());
39 return ENGINE_ERROR;
41 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
42 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
43 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
44 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, bitres);
45 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
46 this->screen = SDL_SetVideoMode(resx, resy, bitres, flags);
47 if(!screen){
48 strcpy(this->engineError, "Error: SDL_SetVideoMode(sc.resx, sc.resy, sc.bitres, sc.sdlflags): ");
49 strcat(this->engineError, SDL_GetError());
50 return ENGINE_ERROR;
52 strcpy(this->engineError, "No hay errores");
53 return ENGINE_NO_ERROR;
56 void Engine::quit(void)
58 SDL_Quit();
59 if(this->onquit != 0)
60 this->onquit();
63 void Engine::setOnQuit(quit_event qe)
65 this->onquit = qe;
68 char *Engine::getError()
70 return this->engineError;
73 void Engine::setError(char *e)
75 strcpy(this->engineError, e);
78 Engine::Engine(void)
80 this->engineError[0] = 0;
81 this->setEventInAll(defaultEvent);
82 this->onquit = 0;
83 this->menu = 0;
86 void Engine::setEvent(int i, engine_event ee)
88 if(!ee)
89 ee = defaultEvent;
90 if(i < MAX_EVENTS)
91 this->events[i] = ee;
94 void Engine::setEventInAll(engine_event ee)
96 int i;
97 if(!ee)
98 ee = defaultEvent;
99 for(i = 0; i < MAX_EVENTS; i++)
100 this->events[i] = ee;
103 void Engine::startEvents(void)
105 SDL_Event ev;
106 int i;
107 while(SDL_WaitEvent(&ev)){
108 switch(ev.type){
109 case SDL_QUIT:
110 this->events[EVENT_QUIT](&ev, this);
111 break;
112 case SDL_MOUSEMOTION:
113 this->events[EVENT_MOUSEMOTION](&ev, this);
114 break;
115 case SDL_MOUSEBUTTONDOWN:
116 case SDL_MOUSEBUTTONUP:
117 if(ev.button.button == SDL_BUTTON_LEFT && this->menu != 0){
118 for(i = 0; i < MAX_OPTIONS; ++i){
119 Option *o = this->menu->getOption(i);
120 if(o != WITHOUT_OPTION){
121 int x = ev.button.x, y = ev.button.y;
122 if(x >= o->getX() && x <= o->getX() + o->getW()
123 && y >= o->getY() && y <= o->getY() + o->getH())
124 o->execEvent(&ev, this, this->menu, o);
128 this->events[EVENT_MOUSEBUTTON](&ev, this);
129 break;
130 case SDL_KEYDOWN:
131 case SDL_KEYUP:
132 this->events[EVENT_KEYBOARD](&ev, this);
133 break;
134 case SDL_VIDEORESIZE:
135 this->events[EVENT_RESIZE](&ev, this);
136 break;
137 default:
138 this->events[EVENT_DEFAULT](&ev, this);
143 void Engine::update(void)
145 SDL_GL_SwapBuffers();
148 void Engine::update(int xi, int yi, int xf, int yf)
150 SDL_UpdateRect(screen, xi, yi, xf, yf);
151 this->update();
154 int Engine::show(SDL_Surface *s, int x, int y, int w, int h)
156 SDL_Rect dest;
157 if(!s){
158 return ENGINE_ERROR;
160 dest.x = x;
161 dest.y = y;
162 dest.w = w;
163 dest.h = h;
164 if(SDL_BlitSurface(s, NULL, screen, &dest))
165 return ENGINE_ERROR;
166 return ENGINE_NO_ERROR;
169 int Engine::showMenu(Menu *m)
171 if(m->show() == MENU_ERROR){
172 strcpy(this->engineError, "Error: m->show(screen): ");
173 strcat(this->engineError, SDL_GetError());
174 return ENGINE_ERROR;
176 m->update();
177 this->menu = m;
178 strcpy(this->engineError, "No hay errores");
179 return ENGINE_NO_ERROR;
182 void defaultEvent(SDL_Event *e, Engine *engine)
184 switch(e->type){
185 case SDL_QUIT:
186 engine->quit();
187 break;
188 case SDL_VIDEOEXPOSE:
189 engine->update();
190 break;