Added a ton of stuff, including the beginnings of a new UI system
[ne.git] / src / mods / space / run.cpp
blob3fa04213cbc7ed23aa4052620b4cefd47998d1d9
1 /************************************************************************
2 This file is part of NE.
4 NE 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 NE 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 NE. If not, see <http://www.gnu.org/licenses/>.
16 ************************************************************************/
18 #include "backend/video.h"
19 #include "backend/input.h"
20 #include "backend/texture.h"
21 //#include "backend/text.h"
23 #include "phys/camera.h"
24 #include "phys/world.h"
25 #include "phys/generic_objects.h"
26 #include "phys/flat.h"
28 #include "mods/mod.h"
30 #include "planet.h"
32 #include <GL/gl.h>
33 #include <GL/glu.h>
34 #include <SDL/SDL.h>
35 #include <vector>
37 int space_run(void *);
39 /* see mods/list.h for what this is for... */
40 struct mod space_mod = {
41 space_run,
42 "Space Demo",
43 "A simple demo with planets and satellites flying about"
46 static GLuint make_stars(int,int);
47 static int gl_init();
49 void show_message(const char *message)
51 static int duration = 2000;
52 static int time;
53 static const char *m = 0;
54 if (message) {
55 time = SDL_GetTicks();
56 m = message;
58 else if (m) {
59 int now = SDL_GetTicks();
60 glPushMatrix();
61 glLoadIdentity();
62 float alpha = 1 - (float)(now-time)/(float)duration;
63 glColor4f(1,1,1,alpha);
64 //text_draw(-1,0.75,-2,SIMPLE,m);
65 glPopMatrix();
66 if (now-time > duration) m = 0;
70 int space_run(void *)
72 Camera cam;
74 int width = v_info()->width;
75 int height = v_info()->height;
77 Texture sat_tex("data/sprites/satellite.png");
78 sat_tex.set(GL_TEXTURE_MAG_FILTER, GL_NEAREST);
80 Texture moon_tex("data/material/moon.jpg");
81 Texture earth_tex("data/material/earth.jpg");
83 float scale = 30;
85 if (!sat_tex.isReady() || !moon_tex.isReady() || !earth_tex.isReady()) {
86 return 1;
89 float speed = 1;
91 double zoom = 0.35;
92 float x = 0, y = 0, _x, _y;
94 int *pos=0;
95 double lpos[2] = {0,0};
96 double *rpos=lpos;
98 gl_init();
99 GLuint star_list = make_stars(width,height);
101 World *w = World::Instance();
103 //dWorldSetAutoDisableLinearThreshold(w->getWorld()->id(),0.25);
104 //dWorldSetAutoDisableAngularThreshold(w->getWorld()->id(),0.25);
105 //dWorldSetAutoDisableFlag(w->getWorld->id(),1);
107 std::vector<Planet*> planets;
109 Planet *earth = new Planet(&earth_tex,63.71,.055);
110 earth->setStatic(true);
111 planets.push_back(earth);
113 Planet *moon = new Planet(&moon_tex,17.37,5*.034);
114 moon->setPosition(150,150,0);
115 moon->setStatic(true);
116 planets.push_back(moon);
118 std::vector<Object*> objects;
119 Object *current_object = 0, *last_object = 0;
120 bool will_follow = false;
122 std::vector<Object*> particles;
124 int done = 0;
125 while (!done)
127 ////////////////
128 // Frame init //
129 ////////////////
131 // this stuff is for the fading background...
133 static float d = 0.0005;
134 static float c = 0.0;
135 glClearColor(c,0,c,0);
136 c += d;
137 if (c >= 0.2 || c <= 0.0) {
138 d = -d;
139 c += d;
143 w->step();
145 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
147 i_pump();
149 keysym_t *key = i_keystate();
151 if (key[K_ESCAPE]) {
152 done = 1;
155 if (key[K_RIGHT]) x -= speed;
156 if (key[K_LEFT]) x += speed;
157 if (key[K_UP]) y += speed;
158 if (key[K_DOWN]) y -= speed;
160 pos = i_mpos();
162 ///////////////////////
163 // Camera transforms //
164 ///////////////////////
166 if (i_mbutton(M_RIGHT)) {
167 int *rpos = i_mrelpos();
168 zoom += (float)rpos[1] * zoom*0.01;
171 if (zoom > 1.0) zoom = 1.0;
172 if (zoom < 0.1) zoom = 0.1;
174 glMatrixMode(GL_MODELVIEW);
175 glLoadIdentity();
177 glTranslatef(0,0,-zoom*1000);
179 rpos = v_unproject(pos[0],pos[1],1);
180 rpos[0] *= zoom;
181 rpos[1] *= zoom;
183 rpos[0] -= x;
184 rpos[1] += y;
186 cam.setTarget(-x,y,-10);
187 cam.setPosition(-x,y,0);
188 cam.update();
189 cam.apply();
190 //glScalef(zoom,zoom,zoom);
192 ///////////
193 // Logic //
194 ///////////
196 static int dropping = 0;
197 static int following = 0;
199 static bool operating_camera = false;
200 if (key[K_SPACE] && !operating_camera) {
201 will_follow = !will_follow;
202 if (will_follow)
203 show_message("Object tracking activated!");
204 else
205 show_message("Object tracking deactivated!");
206 operating_camera = true;
207 if (following) // go back to origin
209 following = 0;
210 cam.follow(0);
211 x = _x;
212 y = _y;
214 else if (last_object) // go back to object
216 following = 1;
217 cam.follow(last_object);
218 _x = x;
219 _y = y;
220 x = y = 0;
223 else if (!key[K_SPACE]) {
224 operating_camera = false;
227 if (i_mbutton(M_MIDDLE) || key[K_SPACE]) {
228 int *rpos = i_mrelpos();
229 x += rpos[0]*zoom;
230 y += rpos[1]*zoom;
232 else if (i_mbutton(M_LEFT)) {
233 if (following && dropping == 0) {
234 cam.follow(0);
235 following = 0;
236 x = _x;
237 y = _y;
239 else if (dropping == 0) {
240 // ready
241 float r = (float)(rand()%10+4)/scale;
242 float x = (float)sat_tex.getWidth()*r;
243 float y = (float)sat_tex.getHeight()*r;
244 Flat *flat = new Flat(x,y);
245 flat->setTexture(&sat_tex);
246 current_object = flat;
247 current_object->setPosition(rpos[0],rpos[1],0);
248 dropping = 2;
249 lpos[0] = rpos[0];
250 lpos[1] = rpos[1];
251 cam.follow(0);
253 else if (dropping == 2) {
254 // aim
255 glDisable(GL_TEXTURE_2D);
256 glBegin(GL_LINES);
257 glColor3f(1,1,0);
258 glVertex2f(lpos[0],lpos[1]);
259 glColor3f(1,0,0);
260 glVertex2f(rpos[0],rpos[1]);
261 glEnd();
262 current_object->setPosition(rpos[0],rpos[1],0);
265 else if (dropping == 2) {
266 // fire
267 float cx = rpos[0]-lpos[0];
268 float cy = rpos[1]-lpos[1];
269 float n = 1;// / sqrt(x*x + y*y);
270 float pow = 25*current_object->getMass();
271 current_object->getBody()->addForce(-n*cx*pow,-n*cy*pow,0);
272 objects.push_back(current_object);
274 if (will_follow)
276 cam.follow(current_object);
278 following = 1;
279 _x = x;
280 _y = y;
281 x = y = 0;
284 last_object = current_object;
285 current_object = 0;
286 dropping = 3;
288 else {
289 dropping = 0;
292 ////////////////////
293 // Physics update //
294 ////////////////////
296 earth->update();
297 for (size_t i=0; i<objects.size(); i++) {
298 for (size_t j=0; j<planets.size(); j++)
299 planets[j]->applyGravity(objects[i]);
300 objects[i]->update();
303 /////////////
304 // Drawing //
305 /////////////
307 glCallList(star_list);
309 static float deg = 0;
310 deg += 5;
313 glPushMatrix();
314 //glColor3f(0,1,0);
315 //glScalef(radius,radius,1);
316 //draw_circle();
317 glRotatef(deg,1,-1,1);
318 glutWireSphere(radius,64,64);
319 //glutSolidSphere(radius,64,64);
320 glPopMatrix();
323 glEnable(GL_TEXTURE_2D);
324 glColor3f(1,1,1);
326 static float rotation = 0;
327 rotation += 0.1;
329 glPushMatrix();
330 glRotatef(rotation,0,1,0);
331 earth->draw();
332 glPopMatrix();
334 moon->draw();
336 if (current_object) {
337 current_object->draw();
340 for (size_t i=0; i<objects.size(); i++) {
341 objects[i]->draw();
344 /******************
345 * Render the GUI *
346 ******************/
347 glDisable(GL_TEXTURE_2D);
348 glDisable(GL_DEPTH_TEST);
349 if (!following) {
350 glPushMatrix();
351 glTranslatef(rpos[0],rpos[1],0);
352 glColor3f(1,0,0);
353 glBegin(GL_LINES);
354 glVertex2f(-10,-10);
355 glVertex2f(10,10);
356 glVertex2f(-10,10);
357 glVertex2f(10,-10);
358 glEnd();
359 glPopMatrix();
361 show_message(0); // this will update the text if it was set
362 glEnable(GL_DEPTH_TEST);
364 SDL_Delay(10);
366 v_flip();
369 return 0;
372 GLuint make_stars(int width, int height)
374 GLuint star_list = glGenLists(1);
375 glNewList(star_list,GL_COMPILE);
377 float n = width*height/1000;
378 glDisable(GL_TEXTURE_2D);
379 glPushMatrix();
380 glLoadIdentity();
381 glTranslatef(0,0,-1000);
382 glScalef(2,2,2);
383 glBegin(GL_POINTS);
384 glColor3f(0.3,0.3,0.3);
385 for (int i=0; i<n; i++)
386 glVertex2f((float)(rand()%width)-width/2,(float)(rand()%height)-height/2);
387 glColor3f(0.6,0.6,0.6);
388 for (int i=0; i<n; i++)
389 glVertex2f((float)(rand()%width)-width/2,(float)(rand()%height)-height/2);
390 glColor3f(1.0,1.0,1.0);
391 for (int i=0; i<n; i++)
392 glVertex2f((float)(rand()%width)-width/2,(float)(rand()%height)-height/2);
393 glEnd();
394 glPopMatrix();
396 glEndList();
397 return star_list;
400 int gl_init()
402 //glClearColor(0,0,0,0);
404 glMatrixMode(GL_PROJECTION);
405 glLoadIdentity();
407 double w = v_info()->width, h = v_info()->height;
408 double aspect = w/h;
410 gluPerspective(45,aspect,1,1001);
412 glEnable(GL_DEPTH_TEST);
413 glClearDepth(1);
415 glEnable(GL_CULL_FACE);
416 glFrontFace(GL_CCW);
418 glShadeModel(GL_SMOOTH);
420 //glEnable(GL_TEXTURE_2D);
421 glEnable(GL_BLEND);
422 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
424 return 0;