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"
37 int space_run(void *);
39 /* see mods/list.h for what this is for... */
40 struct mod space_mod
= {
43 "A simple demo with planets and satellites flying about"
46 static GLuint
make_stars(int,int);
49 void show_message(const char *message
)
51 static int duration
= 2000;
53 static const char *m
= 0;
55 time
= SDL_GetTicks();
59 int now
= SDL_GetTicks();
62 float alpha
= 1 - (float)(now
-time
)/(float)duration
;
63 glColor4f(1,1,1,alpha
);
64 //text_draw(-1,0.75,-2,SIMPLE,m);
66 if (now
-time
> duration
) m
= 0;
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");
85 if (!sat_tex
.isReady() || !moon_tex
.isReady() || !earth_tex
.isReady()) {
92 float x
= 0, y
= 0, _x
, _y
;
95 double lpos
[2] = {0,0};
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
;
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);
137 if (c
>= 0.2 || c
<= 0.0) {
145 glClear(GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
149 keysym_t
*key
= i_keystate();
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
;
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
);
177 glTranslatef(0,0,-zoom
*1000);
179 rpos
= v_unproject(pos
[0],pos
[1],1);
186 cam
.setTarget(-x
,y
,-10);
187 cam
.setPosition(-x
,y
,0);
190 //glScalef(zoom,zoom,zoom);
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
;
203 show_message("Object tracking activated!");
205 show_message("Object tracking deactivated!");
206 operating_camera
= true;
207 if (following
) // go back to origin
214 else if (last_object
) // go back to object
217 cam
.follow(last_object
);
223 else if (!key
[K_SPACE
]) {
224 operating_camera
= false;
227 if (i_mbutton(M_MIDDLE
) || key
[K_SPACE
]) {
228 int *rpos
= i_mrelpos();
232 else if (i_mbutton(M_LEFT
)) {
233 if (following
&& dropping
== 0) {
239 else if (dropping
== 0) {
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);
253 else if (dropping
== 2) {
255 glDisable(GL_TEXTURE_2D
);
258 glVertex2f(lpos
[0],lpos
[1]);
260 glVertex2f(rpos
[0],rpos
[1]);
262 current_object
->setPosition(rpos
[0],rpos
[1],0);
265 else if (dropping
== 2) {
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
);
276 cam
.follow(current_object
);
284 last_object
= current_object
;
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();
307 glCallList(star_list
);
309 static float deg
= 0;
315 //glScalef(radius,radius,1);
317 glRotatef(deg,1,-1,1);
318 glutWireSphere(radius,64,64);
319 //glutSolidSphere(radius,64,64);
323 glEnable(GL_TEXTURE_2D
);
326 static float rotation
= 0;
330 glRotatef(rotation
,0,1,0);
336 if (current_object
) {
337 current_object
->draw();
340 for (size_t i
=0; i
<objects
.size(); i
++) {
347 glDisable(GL_TEXTURE_2D
);
348 glDisable(GL_DEPTH_TEST
);
351 glTranslatef(rpos
[0],rpos
[1],0);
361 show_message(0); // this will update the text if it was set
362 glEnable(GL_DEPTH_TEST
);
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
);
381 glTranslatef(0,0,-1000);
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);
402 //glClearColor(0,0,0,0);
404 glMatrixMode(GL_PROJECTION
);
407 double w
= v_info()->width
, h
= v_info()->height
;
410 gluPerspective(45,aspect
,1,1001);
412 glEnable(GL_DEPTH_TEST
);
415 glEnable(GL_CULL_FACE
);
418 glShadeModel(GL_SMOOTH
);
420 //glEnable(GL_TEXTURE_2D);
422 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);