aboutsummaryrefslogtreecommitdiff
path: root/sons_of_sol/private_db/ApplicationInit.cpp
blob: 1794fd553bf95f0203bc9649a5ff8ccfafbdc518 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "sons_of_sol/SonsOfSolApplication.hpp"

#include "glox/GloxLightSource.hpp"
#include "glox/GloxRotation.hpp"
#include "glox/GloxScopedRotation.hpp"

#include <iostream>

using namespace slox;
using namespace glox;
using namespace std;

bool SonsOfSolApplication::initialize( int argc, char** argv ) {
	(void) argc;
	(void) argv;
	/* Use our texture factory to load textures */
	m_object_loader.setTextureFactory( &m_texture_factory );
    m_is_paused = false ;

	setupControls();

    SDL_Init( SDL_INIT_VIDEO );
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	const SDL_VideoInfo* info = SDL_GetVideoInfo();   //<-- calls SDL_GetVideoInfo();   
	m_screen_width = info->current_w;
	m_screen_height = info->current_h;
    m_screen = SDL_SetVideoMode( 600, 600, 0, SDL_OPENGL|SDL_RESIZABLE|SDL_DOUBLEBUF );

    m_perspective.setPosition( GloxPoint<>( 0, 0, 10 ) );
	m_perspective.setZFar ( FAR_DISTANCE_END );
	m_perspective.setZNear( FAR_DISTANCE_START );
	m_ship.setPerspective( m_perspective );
	m_ship.setPosition( GloxPointf( 0, 10, -3 ) );


    if( argv[1] == NULL || strcmp(argv[1],"nograb") ) {
	    SDL_ShowCursor( 0 );
	    SDL_WM_GrabInput(SDL_GRAB_ON);
    }

	if( glewInit() != GLEW_OK ) {
		cerr << "Unable to initialize GLEW" << endl ;
		exit( 122 ) ;
	}

	/* Add some listeners */
    this->addQuitListener( this );
	this->addKeyListener( this );

	this->addResizeListener( this );

	m_event_multiplexer.addControlMotionListener( & m_ship );
        
    if( ! m_screen ) {
        /* If the screen could not be initialized, print
         * a useful video message */
        this->setError( "Cannot set SDL video mode\n" );
        return false;
    }

    SDL_WM_SetCaption( "Sons Of Sol", "" );
    reshape( m_screen->w, m_screen->h );

	/* Load the textures into memory */
	if( ! loadTextures() )	return false;
    
    GloxTextureRepeat texrep( &m_sky_tex, 1.8, 1.8 );
    m_sky = new GloxTexturedCube( FAR_DISTANCE_END - 100, GloxColor( 255, 255, 255 ),
        texrep, texrep, texrep, texrep, texrep, texrep );

    texrep = GloxTextureRepeat( &m_crate_tex, 1, 1 );
    m_crate = new GloxTexturedCube( 5.0f, GloxColor( 255, 255, 255 ),
        texrep, texrep, texrep, texrep, texrep, texrep );
	
	m_object_loader.addToPath( "objects" );
	m_object_loader.addToPath( "objects/material" );
	m_earth = new GloxTexturedSphere( FAR_DISTANCE_END / 4.0, 5.0f, m_earth_tex );
	m_moon = new GloxTexturedSphere( FAR_DISTANCE_END / 24.0, 5.0f, m_moon_tex );
    m_probe = m_object_loader.loadObjectFromFile( "probe.obj" );

    m_script_ttl = 130 ;
    if( ! m_probe ) {
        cerr << "Unable to load probe: " << m_object_loader.getReason() << endl;
        exit( 123 );
    }

    m_frigate_industrial = m_object_loader.loadObjectFromFile( "frigate_industrial.obj" );
    if( ! m_frigate_industrial ) {
        cerr << "Unable to load frigate_industrial\n" << m_object_loader.getReason() << endl;
        exit( 123 );
    }

    m_other_ship = m_object_loader.loadObjectFromFile( "ship.obj" );
    SloxModelObject* ship_med = m_object_loader.loadObjectFromFile( "ship_med.obj" ) ;
    SloxModelObject* ship_low = m_object_loader.loadObjectFromFile( "ship_low.obj" ) ;
    if( ! (m_other_ship && ship_med && ship_low) ) {
        cerr << "Unable to load other ship\n" << m_object_loader.getReason() << endl;
        exit( 123 );
    }

    m_test_cube = m_object_loader.loadObjectFromFile( "cube.obj" );
    if( ! m_test_cube ) {
        cerr << "Unable to load test cube\n" << m_object_loader.getReason() << endl;
        exit( 123 );
    }

    m_deathstar = m_object_loader.loadObjectFromFile( "deathstar.obj" ) ;
    if( ! m_deathstar ) {
        cerr << "Unable to load deathstar\n" << m_object_loader.getReason() << endl;
        exit( 123 );
    }

    m_acceleration_cannon = m_object_loader.loadObjectFromFile( "acceleration_cannon.obj" ) ;
    if( ! m_acceleration_cannon ) {
        cerr << "Unable to load acceleration cannon\n" << m_object_loader.getReason() << endl;
        exit( 123 );
    }

    AIShip::setModel( m_other_ship, ship_med, ship_low ) ;

    float xstart = 0.5*(- FAR_DISTANCE_END / 5.0f);
    float xend   = 0.5*(- xstart);
    float ystart = 0.3*(FAR_DISTANCE_END / 3.0f - FAR_DISTANCE_END / 2);
    float yend   = 0.3*(ystart + 2*(FAR_DISTANCE_END / 5.0f));
	/* Add the points for the sun */
	m_star.add( GloxPointNormalTexture(
		GloxPoint<>( xstart, ystart, -(FAR_DISTANCE_END) / 3 ), GloxNormal<>(0,0,0), GloxPoint<>( 0,0 ) ) );
	m_star.add( GloxPointNormalTexture(
		GloxPoint<>( xend, ystart, -(FAR_DISTANCE_END) / 3 ), GloxNormal<>(0,0,0), GloxPoint<>( 1,0 ) ) );
	m_star.add( GloxPointNormalTexture(
		GloxPoint<>( xend, yend, -(FAR_DISTANCE_END) / 3 ), GloxNormal<>(0,0,0), GloxPoint<>( 1,1 ) ) );
	m_star.add( GloxPointNormalTexture(
		GloxPoint<>( xstart, yend, -(FAR_DISTANCE_END) / 3 ), GloxNormal<>(0,0,0), GloxPoint<>( 0,1 ) ) );

	/* Move the light souce to the sun */
	GloxLightSource* source = m_light_manager.getLightSource( 0 );
	source->setPosition( GloxPointf( 0, 200, -695 ) ); 

    m_quit = false;

    /* Starting ships */
    for( int i = 0; i < 5; ++ i ) {
        AIShip::Updater* updater = new EllipseUpdater( GloxPointf(0,-10,0), GloxPointf(0,10,10), GloxPointf(10,i*5,0), 0.01 );
        m_updaters.push_back( updater );
        AIShip tmp( updater );
        tmp.setTimeOffset( 1000 * i ) ;
        m_aiships.push_back( tmp ) ;
    }

    /* Frigate ships */
    for ( int j = 0; j < 360; j += 72 ) {
        for( int i = 0; i < 10; ++ i ) {
            float pos = 10000 + 100 * i;
            GloxPointf center( pos, pos, pos );
            float cos = GloxCos(j) ;
            float sin = GloxSin(j) ;
            AIShip::Updater* updater =
                new EllipseUpdater( center, GloxPointf(60*cos,-100*sin,2) , GloxPointf(50*cos,10*sin,50) * 2, 0.002 );
            m_updaters.push_back( updater );
            AIShip tmp( updater );
            tmp.setTimeOffset( 100000 * i ) ;
            m_aiships.push_back( tmp ) ;
        }
    }

    /* Deathstar ships */
    for ( int j = 0; j < 360; j += 72 ) {
        for( int i = 0; i < 10; ++ i ) {
            float pos = 100 * i;
            GloxPointf center( 5000 + pos, -1000+pos, -20000+pos );
            float cos = GloxCos(j) ;
            float sin = GloxSin(j) ;
            AIShip::Updater* updater =
                new EllipseUpdater( center, GloxPointf(60*cos,-70*sin,2) , GloxPointf(50*cos,10*sin,40) * 2, 0.002 );
            m_updaters.push_back( updater );
            AIShip tmp( updater );
            tmp.setTimeOffset( 10000 * i ) ;
            m_aiships.push_back( tmp ) ;
        }
    }

	m_earth_prog = new GloxProgram() ;
	if( m_earth_prog->attachShaderFromFile( "shaders/earth.vp", GL_VERTEX_SHADER ) ||
		m_earth_prog->attachShaderFromFile( "shaders/earth.fp", GL_FRAGMENT_SHADER ) ||
		m_earth_prog->link() ) {
		cerr << "Warning Unable to load shaders: " <<
			GloxShader::getMessage() << endl;
	}
	m_earth_shader_light_pos = m_earth_prog->getUniformLocation( "lightPos" );
	m_earth_shader_camera_pos = m_earth_prog->getUniformLocation( "cameraPos" );

	cout << "Earth light  " << m_earth_shader_light_pos  << endl;
	cout << "Earth camera " << m_earth_shader_camera_pos << endl;

	m_moon_prog = new GloxProgram() ;
	if( m_moon_prog->attachShaderFromFile( "shaders/moon.vp", GL_VERTEX_SHADER ) ||
		m_moon_prog->attachShaderFromFile( "shaders/moon.fp", GL_FRAGMENT_SHADER ) ||
		m_moon_prog->link() ) {
		cerr << "Warning Unable to load shaders: " <<
			GloxShader::getMessage() << endl;
	}
	m_moon_shader_light_pos = m_moon_prog->getUniformLocation( "lightPos" );
	m_moon_shader_camera_pos = m_moon_prog->getUniformLocation( "cameraPos" );


	// start the starting light
	GloxLightSource* start = m_light_manager.getLightSource( 0 );
	start->setLightModelAttribute( GL_LIGHT_MODEL_AMBIENT, GloxColor(50,50,50) );
	start->setAmbient ( GloxColor(30,30,30)  );
	start->setDiffuse ( GloxColor(255,0,0)  );
	start->setPosition( GloxPoint<>( 0,0,0 ) );

	start->setEnabled( false );

	glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.5);

    return true;
}