diff options
| author | Josh Rahm <rahm@google.com> | 2025-12-23 12:59:23 -0700 |
|---|---|---|
| committer | Josh Rahm <rahm@google.com> | 2025-12-23 12:59:23 -0700 |
| commit | 46efbf05fcade8199035f956e20a63ca91fc88fd (patch) | |
| tree | 9e3f3e297ea66e442e1a11b5ad16ad8ed6fc7fb7 /sons_of_sol/private_db | |
| parent | 306bb687414f54177eb37ab7cd744e491c397c31 (diff) | |
| download | SonsOfSol-46efbf05fcade8199035f956e20a63ca91fc88fd.tar.gz SonsOfSol-46efbf05fcade8199035f956e20a63ca91fc88fd.tar.bz2 SonsOfSol-46efbf05fcade8199035f956e20a63ca91fc88fd.zip | |
Run clang-format and add .clang-format
Diffstat (limited to 'sons_of_sol/private_db')
| -rw-r--r-- | sons_of_sol/private_db/AIShip.cpp | 196 | ||||
| -rw-r--r-- | sons_of_sol/private_db/ApplicationDisplay.cpp | 246 | ||||
| -rw-r--r-- | sons_of_sol/private_db/ApplicationInit.cpp | 449 | ||||
| -rw-r--r-- | sons_of_sol/private_db/ControlMultiplexer.cpp | 93 | ||||
| -rw-r--r-- | sons_of_sol/private_db/PlayerShip.cpp | 235 | ||||
| -rw-r--r-- | sons_of_sol/private_db/Projectile.cpp | 98 | ||||
| -rw-r--r-- | sons_of_sol/private_db/SonsOfSolApplication.cpp | 210 |
7 files changed, 761 insertions, 766 deletions
diff --git a/sons_of_sol/private_db/AIShip.cpp b/sons_of_sol/private_db/AIShip.cpp index 5940e42..b487d3c 100644 --- a/sons_of_sol/private_db/AIShip.cpp +++ b/sons_of_sol/private_db/AIShip.cpp @@ -3,124 +3,120 @@ #include <cmath> #include <iostream> +#include "glox/GloxColor.hpp" #include "glox/GloxScopedRotation.hpp" #include "glox/GloxScopedTranslation.hpp" -#include "glox/GloxColor.hpp" using namespace slox; using namespace glox; using namespace std; - -SloxModelObject* AIShip::s_high_model ; -SloxModelObject* AIShip::s_med_model ; -SloxModelObject* AIShip::s_low_model ; -GloxCube* AIShip::s_really_far_away_model ; - -void AIShip::setModel( slox::SloxModelObject* high, slox::SloxModelObject* med, slox::SloxModelObject* low ) { - s_high_model = high; - s_med_model = med; - s_low_model = low; - s_really_far_away_model = new glox::GloxCube( 4, 0.5, 8, GloxColor(245,245,220) ); +SloxModelObject* AIShip::s_high_model; +SloxModelObject* AIShip::s_med_model; +SloxModelObject* AIShip::s_low_model; +GloxCube* AIShip::s_really_far_away_model; + +void AIShip::setModel(slox::SloxModelObject* high, slox::SloxModelObject* med, + slox::SloxModelObject* low) { + s_high_model = high; + s_med_model = med; + s_low_model = low; + s_really_far_away_model = + new glox::GloxCube(4, 0.5, 8, GloxColor(245, 245, 220)); } -void AIShip::setUpdateFunction( Updater* updater ){ - update_func = updater; +void AIShip::setUpdateFunction(Updater* updater) { + update_func = updater; - /* Update 3 times so we have a full set + /* Update 3 times so we have a full set * of points to start with */ - update(); - update(); - update(); + update(); + update(); + update(); } void AIShip::calculate_roll_to() { - GloxPoint<> total_change = m_future_position - m_last_position ; - GloxPoint<> current_change = m_position - m_last_position ; - - float scalar_projection = ( current_change.dot( total_change ) / total_change.dot(total_change) ) ; - total_change *= scalar_projection ; - GloxPoint<> to_vector = current_change - total_change ; - to_vector.normalize(); - - float ang = to_vector.dot( GloxPointf(0,1,0) ) ; - float tmp = GloxToDegrees( acos( ang ) ); - - if( tmp == tmp ) { - /* Cut out NANs */ - m_roll_to = tmp; - } -// cout << "m_roll_to " << m_roll_to << endl; + GloxPoint<> total_change = m_future_position - m_last_position; + GloxPoint<> current_change = m_position - m_last_position; + + float scalar_projection = + (current_change.dot(total_change) / total_change.dot(total_change)); + total_change *= scalar_projection; + GloxPoint<> to_vector = current_change - total_change; + to_vector.normalize(); + + float ang = to_vector.dot(GloxPointf(0, 1, 0)); + float tmp = GloxToDegrees(acos(ang)); + + if (tmp == tmp) { + /* Cut out NANs */ + m_roll_to = tmp; + } + // cout << "m_roll_to " << m_roll_to << endl; } -void AIShip::setTimeOffset( uint32_t off ) { - m_time_offset = off ; +void AIShip::setTimeOffset(uint32_t off) { + m_time_offset = off; } -void AIShip::update( ) { - m_last_position = m_position; - m_position = m_future_position ; - m_future_position = update_func->update( SDL_GetTicks() + m_time_offset ) ; +void AIShip::update() { + m_last_position = m_position; + m_position = m_future_position; + m_future_position = update_func->update(SDL_GetTicks() + m_time_offset); } -void AIShip::draw( float dist ) { - GloxScopedTranslation __gstr1( m_position ); - /* The the change in the vector, this is where +void AIShip::draw(float dist) { + GloxScopedTranslation __gstr1(m_position); + /* The the change in the vector, this is where * the front of the ship goes */ - GloxPoint<> dpos = m_position - m_last_position ; - - /* Project the point onto the x,y plane */ - GloxPoint<> first( dpos.getX(), dpos.getY(), 0 ); - /* A point that represents the x axis */ - GloxPoint<> xaxis( 1, 0, 0 ); - - /* Rotation around the z axis */ - float dot = first.dot( xaxis ) ; - float rot = GloxToDegrees(acos( dot )) ; - - if( first.getY() < 0 ) { - rot = - rot; - } - rot = rot + 180 ; - // if( rot != rot ) { - // cout << "Non existant Z rotation!" << endl ; - // } - GloxScopedRotation __gscr1( rot, 0, 0, 1 ); - /* x axis now follows *first* vector */ - /* Rotate around y axis to get x to follow *dpos* vector */ - dot = first.dot( dpos ); - - rot = acos( dot ); - rot = GloxToDegrees( rot ); - - if( dpos.getZ() < 0 ) { - rot = - rot; - } - - // if( rot != rot ) { - // cout << "Non existant Y rotation!" << endl ; - // } - GloxScopedRotation __glcr2( rot, 0, 1, 0 ); - - calculate_roll_to(); - if( m_roll != m_roll ) { - m_roll = 0.0f; - } - m_roll = ( m_roll_to + (5 - 1) * m_roll) / 5.0f; - - float roll = m_roll; - // if( dpos.getX() < 0 ) { - // roll += 180 ; - // } - - GloxScopedRotation __glco3( roll, 1, 0, 0 ); - GloxScale( 2, - if( dist < 20 ) - s_high_model->draw(); - else if ( dist < 50 ) - s_med_model->draw(); - else if ( dist < 1000 ) - s_low_model->draw(); - else - s_really_far_away_model->draw(); - ); + GloxPoint<> dpos = m_position - m_last_position; + + /* Project the point onto the x,y plane */ + GloxPoint<> first(dpos.getX(), dpos.getY(), 0); + /* A point that represents the x axis */ + GloxPoint<> xaxis(1, 0, 0); + + /* Rotation around the z axis */ + float dot = first.dot(xaxis); + float rot = GloxToDegrees(acos(dot)); + + if (first.getY() < 0) { + rot = -rot; + } + rot = rot + 180; + // if( rot != rot ) { + // cout << "Non existant Z rotation!" << endl ; + // } + GloxScopedRotation __gscr1(rot, 0, 0, 1); + /* x axis now follows *first* vector */ + /* Rotate around y axis to get x to follow *dpos* vector */ + dot = first.dot(dpos); + + rot = acos(dot); + rot = GloxToDegrees(rot); + + if (dpos.getZ() < 0) { + rot = -rot; + } + + // if( rot != rot ) { + // cout << "Non existant Y rotation!" << endl ; + // } + GloxScopedRotation __glcr2(rot, 0, 1, 0); + + calculate_roll_to(); + if (m_roll != m_roll) { + m_roll = 0.0f; + } + m_roll = (m_roll_to + (5 - 1) * m_roll) / 5.0f; + + float roll = m_roll; + // if( dpos.getX() < 0 ) { + // roll += 180 ; + // } + + GloxScopedRotation __glco3(roll, 1, 0, 0); + GloxScale(2, if (dist < 20) s_high_model->draw(); + else if (dist < 50) s_med_model->draw(); + else if (dist < 1000) s_low_model->draw(); + else s_really_far_away_model->draw();); } diff --git a/sons_of_sol/private_db/ApplicationDisplay.cpp b/sons_of_sol/private_db/ApplicationDisplay.cpp index c9e773e..bf94b33 100644 --- a/sons_of_sol/private_db/ApplicationDisplay.cpp +++ b/sons_of_sol/private_db/ApplicationDisplay.cpp @@ -11,146 +11,128 @@ using namespace slox; using namespace std; float randJitter() { - return ((rand() & 0xFF) - 128.0) / (256.0 * 10) ; + return ((rand() & 0xFF) - 128.0) / (256.0 * 10); } void SonsOfSolApplication::display() { - if ( m_script_ttl >= 0 ) { - GloxLightSource* golight = m_light_manager.getLightSource( 0 ); - golight->setPosition( m_ship.getPosition() ); - - if( m_script_ttl > 90 ) { - m_ship.setPosition( m_ship.getPosition() - GloxPointf( randJitter(),0.25,randJitter() ) ); - } else if( m_script_ttl > 80 ) { - float coef = 11 - (m_script_ttl - 80); - m_ship.setPosition( GloxPointf( randJitter()/coef,randJitter()/coef,randJitter()/coef-3 ) ); - } else if( m_script_ttl == 80 ) { - m_ship.setPosition( GloxPointf( 0,0,-3 ) ); - } - - golight->setEnabled( true ); - if ( m_script_ttl == 70 ) { - m_ship.setDForwardTo( 30 ); - golight->setLightModelAttribute( GL_LIGHT_MODEL_AMBIENT, GloxColor(50,50,50) ); - golight->setAmbient ( GloxColor(50,50,50) ); - golight->setDiffuse ( GloxColor(0,255,0) ); - } - if ( m_script_ttl == 0 ) { - this->addKeyListener( & m_event_multiplexer ); - this->addMouseMotionListener( & m_event_multiplexer ); - m_ship.setDForwardTo( 20 ) ; - enableLighting(); - } - // cout << m_script_ttl << endl ; - m_script_ttl -= 1 ; + if (m_script_ttl >= 0) { + GloxLightSource* golight = m_light_manager.getLightSource(0); + golight->setPosition(m_ship.getPosition()); + + if (m_script_ttl > 90) { + m_ship.setPosition(m_ship.getPosition() - + GloxPointf(randJitter(), 0.25, randJitter())); + } else if (m_script_ttl > 80) { + float coef = 11 - (m_script_ttl - 80); + m_ship.setPosition(GloxPointf(randJitter() / coef, randJitter() / coef, + randJitter() / coef - 3)); + } else if (m_script_ttl == 80) { + m_ship.setPosition(GloxPointf(0, 0, -3)); } - /* Translate to the this perspective */ - m_ship.drawHUD(); + golight->setEnabled(true); + if (m_script_ttl == 70) { + m_ship.setDForwardTo(30); + golight->setLightModelAttribute(GL_LIGHT_MODEL_AMBIENT, + GloxColor(50, 50, 50)); + golight->setAmbient(GloxColor(50, 50, 50)); + golight->setDiffuse(GloxColor(0, 255, 0)); + } + if (m_script_ttl == 0) { + this->addKeyListener(&m_event_multiplexer); + this->addMouseMotionListener(&m_event_multiplexer); + m_ship.setDForwardTo(20); + enableLighting(); + } + // cout << m_script_ttl << endl ; + m_script_ttl -= 1; + } - m_ship.getPerspective().setZNear( FAR_DISTANCE_START ); - m_ship.getPerspective().setZFar( FAR_DISTANCE_END ); - m_ship.getPerspective().render(); + /* Translate to the this perspective */ + m_ship.drawHUD(); - /* We don't want lighting for the skybox */ - GloxDisableFor( GL_LIGHTING, - /* Draw some stuff relative to the + m_ship.getPerspective().setZNear(FAR_DISTANCE_START); + m_ship.getPerspective().setZFar(FAR_DISTANCE_END); + m_ship.getPerspective().render(); + + /* We don't want lighting for the skybox */ + GloxDisableFor( + GL_LIGHTING, + /* Draw some stuff relative to the * ship so the ship cannot break out * of the skybox */ - GloxWithTranslation( m_ship.getPosition(), - /* Draw the background sky */ - m_sky->draw(); - /* Draw the star texture */ - GloxEnableFor( GL_TEXTURE_2D, - m_star_tex.bind(); - GloxWith( GL_QUADS, m_star.plot() ); - ); - ) - - - - /* Draw the earth with its + GloxWithTranslation(m_ship.getPosition(), + /* Draw the background sky */ + m_sky->draw(); + /* Draw the star texture */ + GloxEnableFor(GL_TEXTURE_2D, m_star_tex.bind(); + GloxWith(GL_QUADS, m_star.plot()););) + + /* Draw the earth with its * shaders */ - m_earth_prog->render() ; - m_earth_prog->setUniformVector3( m_earth_shader_light_pos, - m_light_manager.getLightSource(0)->getPosition() ) ; - m_earth_prog->setUniformFloat( m_earth_shader_time, SDL_GetTicks() / 300000.0 ) ; - - //GloxPoint<> pos( GloxCos( ticks ) * FAR_DISTANCE_END/4.0, 0, GloxSin( ticks ) * FAR_DISTANCE_END/4.0 ); - GloxPoint<> pos( -250000, 0, 0 ); - m_earth_prog->setUniformVector3( m_earth_shader_camera_pos, - pos ); - // cout << "Pos : " << pos.toString() << " " << (pos.getZ() / pos.getX()) << endl; - // cout << "Ship Position: " << m_ship.getPosition().toString() << (m_ship.getPosition().getZ() / m_ship.getPosition().getX()) << endl ; - GloxWithTranslation( m_ship.getPosition() + GloxPoint<>( 700,-FAR_DISTANCE_END / 2,0 ), - GloxRotation( -90, 0, 0, 1 ).render(); - GloxEnableFor( GL_BLEND, - m_earth->draw(); - ) - ) - - /* Draw the moon with its shaders */ - m_moon_prog->render() ; - m_moon_prog->setUniformVector3( m_moon_shader_light_pos, - m_light_manager.getLightSource( 0 )->getPosition() ) ; - - m_moon_prog->setUniformVector3( m_moon_shader_camera_pos, - m_ship.getPosition() ) ; - - GloxWithTranslation( m_ship.getPosition() + GloxPoint<>( 700,FAR_DISTANCE_END / 3.0f, 0 ), - GloxRotation( -90, 0, 0, 1 ).render(); - m_moon->draw() - ) - - GloxProgram::unloadPrograms() ; - ); - - - - m_light_manager.getLightSource( 1 )->setEnabled( true ); - m_light_manager.render(); - - GloxScale( 6, - m_acceleration_cannon->draw() ) ; - - for( vector<AIShip>::iterator itr = m_aiships.begin() ; itr != m_aiships.end() ; ++ itr ) { - // cout << itr->getPosition().toString() << endl ; - itr->draw( (itr->getPosition() - m_ship.getPosition()).getMagnitude() ); - } - - glPushMatrix(); - - // m_crate->draw(); - GloxWithTranslation( GloxPoint<>( 0,10,0 ), - m_probe->draw() ); - GloxWithTranslation( GloxPoint<>( 0,-10,0 ), - m_test_cube->draw() ); - - GloxWithTranslation( GloxPoint<>( 10000,10000,10000 ), - GloxScale( 1000, - glPushMatrix(); - glRotatef( 90, 1.2, 1.3, 1.4 ); - m_frigate_industrial->draw(); - glPopMatrix(); - ); - ); - - GloxWithTranslation( GloxPoint<>( 5000,-1000,-20000 ), - GloxScale( 100, - glPushMatrix(); - glRotatef( 90, 0.3, 1, 0 ); - m_deathstar->draw(); - glPopMatrix(); - ); - ); - - GloxDisableFor( GL_LIGHTING, - GloxEnableFor(GL_BLEND, - glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - m_ship.drawProjectiles(); - ) - ) - - - glPopMatrix(); + m_earth_prog->render(); + m_earth_prog->setUniformVector3( + m_earth_shader_light_pos, + m_light_manager.getLightSource(0)->getPosition()); + m_earth_prog->setUniformFloat(m_earth_shader_time, + SDL_GetTicks() / 300000.0); + + //GloxPoint<> pos( GloxCos( ticks ) * FAR_DISTANCE_END/4.0, 0, GloxSin( ticks ) * FAR_DISTANCE_END/4.0 ); + GloxPoint<> pos(-250000, 0, 0); + m_earth_prog->setUniformVector3(m_earth_shader_camera_pos, pos); + // cout << "Pos : " << pos.toString() << " " << (pos.getZ() / pos.getX()) << endl; + // cout << "Ship Position: " << m_ship.getPosition().toString() << (m_ship.getPosition().getZ() / m_ship.getPosition().getX()) << endl ; + GloxWithTranslation( + m_ship.getPosition() + GloxPoint<>(700, -FAR_DISTANCE_END / 2, 0), + GloxRotation(-90, 0, 0, 1).render(); + GloxEnableFor(GL_BLEND, m_earth->draw();)) + + /* Draw the moon with its shaders */ + m_moon_prog->render(); + m_moon_prog->setUniformVector3( + m_moon_shader_light_pos, + m_light_manager.getLightSource(0)->getPosition()); + + m_moon_prog->setUniformVector3(m_moon_shader_camera_pos, + m_ship.getPosition()); + + GloxWithTranslation( + m_ship.getPosition() + GloxPoint<>(700, FAR_DISTANCE_END / 3.0f, 0), + GloxRotation(-90, 0, 0, 1).render(); + m_moon->draw()) + + GloxProgram::unloadPrograms();); + + m_light_manager.getLightSource(1)->setEnabled(true); + m_light_manager.render(); + + GloxScale(6, m_acceleration_cannon->draw()); + + for (vector<AIShip>::iterator itr = m_aiships.begin(); itr != m_aiships.end(); + ++itr) { + // cout << itr->getPosition().toString() << endl ; + itr->draw((itr->getPosition() - m_ship.getPosition()).getMagnitude()); + } + + glPushMatrix(); + + // m_crate->draw(); + GloxWithTranslation(GloxPoint<>(0, 10, 0), m_probe->draw()); + GloxWithTranslation(GloxPoint<>(0, -10, 0), m_test_cube->draw()); + + GloxWithTranslation( + GloxPoint<>(10000, 10000, 10000), + GloxScale(1000, glPushMatrix(); glRotatef(90, 1.2, 1.3, 1.4); + m_frigate_industrial->draw(); glPopMatrix(););); + + GloxWithTranslation(GloxPoint<>(5000, -1000, -20000), + GloxScale(100, glPushMatrix(); glRotatef(90, 0.3, 1, 0); + m_deathstar->draw(); glPopMatrix(););); + + GloxDisableFor( + GL_LIGHTING, + GloxEnableFor(GL_BLEND, glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + m_ship.drawProjectiles();)) + + glPopMatrix(); } diff --git a/sons_of_sol/private_db/ApplicationInit.cpp b/sons_of_sol/private_db/ApplicationInit.cpp index 7729d12..dccc99a 100644 --- a/sons_of_sol/private_db/ApplicationInit.cpp +++ b/sons_of_sol/private_db/ApplicationInit.cpp @@ -10,221 +10,242 @@ 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 +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 ); + 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); } - - 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 ); + } + + /* 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_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() ; - - - printf( "Using GLSL version %s\n", glGetString( GL_SHADING_LANGUAGE_VERSION ) ); - - const char* earth_frag_shader = "shaders/earth.fp"; - float version = atof( (const char*) glGetString( GL_SHADING_LANGUAGE_VERSION ) ); - if( version < 1.29 ) { - fprintf( stderr, "Using version %02f, unfortunately, I must use the fallback shader\n", version ); - earth_frag_shader = "shaders/earth_fallback.fp"; - } - - if( m_earth_prog->attachShaderFromFile( "shaders/earth.vp", GL_VERTEX_SHADER ) || - m_earth_prog->attachShaderFromFile( earth_frag_shader, 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" ); - m_earth_shader_time = m_earth_prog->getUniformLocation( "time" ) ; - - // 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; + } + + m_earth_prog = new GloxProgram(); + + printf("Using GLSL version %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION)); + + const char* earth_frag_shader = "shaders/earth.fp"; + float version = atof((const char*)glGetString(GL_SHADING_LANGUAGE_VERSION)); + if (version < 1.29) { + fprintf( + stderr, + "Using version %02f, unfortunately, I must use the fallback shader\n", + version); + earth_frag_shader = "shaders/earth_fallback.fp"; + } + + if (m_earth_prog->attachShaderFromFile("shaders/earth.vp", + GL_VERTEX_SHADER) || + m_earth_prog->attachShaderFromFile(earth_frag_shader, + 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"); + m_earth_shader_time = m_earth_prog->getUniformLocation("time"); + + // 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; } diff --git a/sons_of_sol/private_db/ControlMultiplexer.cpp b/sons_of_sol/private_db/ControlMultiplexer.cpp index 97ae2f4..aee9672 100644 --- a/sons_of_sol/private_db/ControlMultiplexer.cpp +++ b/sons_of_sol/private_db/ControlMultiplexer.cpp @@ -1,58 +1,61 @@ #include "sons_of_sol/ControlMultiplexer.hpp" -ControlMultiplexer::ControlMultiplexer() { - -} +ControlMultiplexer::ControlMultiplexer() {} -void ControlMultiplexer::onKeyUp( const SDL_KeyboardEvent& evt ) { - std::map< SDLKey, M_MapVal >::iterator itr; - itr = m_keysym_map.find( evt.keysym.sym ); +void ControlMultiplexer::onKeyUp(const SDL_KeyboardEvent& evt) { + std::map<SDLKey, M_MapVal>::iterator itr; + itr = m_keysym_map.find(evt.keysym.sym); - if( itr != m_keysym_map.end() ) { - ControlMotionEvent event( (*itr).second.type, 0, ControlMotionEvent::BUTTON ); - fireControlEvent( event ); - } + if (itr != m_keysym_map.end()) { + ControlMotionEvent event((*itr).second.type, 0, ControlMotionEvent::BUTTON); + fireControlEvent(event); + } } -void ControlMultiplexer::onKeyDown( const SDL_KeyboardEvent& evt ) { - std::map< SDLKey, M_MapVal >::iterator itr; - itr = m_keysym_map.find( evt.keysym.sym ); +void ControlMultiplexer::onKeyDown(const SDL_KeyboardEvent& evt) { + std::map<SDLKey, M_MapVal>::iterator itr; + itr = m_keysym_map.find(evt.keysym.sym); - if( itr != m_keysym_map.end() ) { - ControlMotionEvent event( (*itr).second.type, 1 * (*itr).second.multiplier, ControlMotionEvent::BUTTON ); - fireControlEvent( event ); - } + if (itr != m_keysym_map.end()) { + ControlMotionEvent event((*itr).second.type, 1 * (*itr).second.multiplier, + ControlMotionEvent::BUTTON); + fireControlEvent(event); + } } -void ControlMultiplexer::onMouseMoved( const SDL_MouseMotionEvent& evt ) { - std::map< uint16_t, M_MapVal >::iterator itr; - /* x direction on mousepad */ - itr = m_joy_axis_map.find( 0xFF00 ); - const SDL_VideoInfo* info = SDL_GetVideoInfo(); //<-- calls SDL_GetVideoInfo(); - int screen_width = info->current_w; - int screen_height = info->current_h; - - float x_norm = (evt.x - screen_width / 2) / ((float) screen_width / 2.0); - if( itr != m_joy_axis_map.end() ) { - ControlMotionEvent event( itr->second.type, x_norm * (*itr).second.multiplier, - ControlMotionEvent::MOUSE ); - fireControlEvent( event ); - } - - /* y direction */ - float y_norm = (evt.y - screen_height / 2) / ((float) screen_height / 2.0); - itr = m_joy_axis_map.find( 0xFF01 ); - if( itr != m_joy_axis_map.end() ) { - ControlMotionEvent event( itr->second.type, y_norm * (*itr).second.multiplier, - ControlMotionEvent::MOUSE ); - fireControlEvent( event ); - } +void ControlMultiplexer::onMouseMoved(const SDL_MouseMotionEvent& evt) { + std::map<uint16_t, M_MapVal>::iterator itr; + /* x direction on mousepad */ + itr = m_joy_axis_map.find(0xFF00); + const SDL_VideoInfo* info = + SDL_GetVideoInfo(); //<-- calls SDL_GetVideoInfo(); + int screen_width = info->current_w; + int screen_height = info->current_h; + + float x_norm = (evt.x - screen_width / 2) / ((float)screen_width / 2.0); + if (itr != m_joy_axis_map.end()) { + ControlMotionEvent event(itr->second.type, + x_norm * (*itr).second.multiplier, + ControlMotionEvent::MOUSE); + fireControlEvent(event); + } + + /* y direction */ + float y_norm = (evt.y - screen_height / 2) / ((float)screen_height / 2.0); + itr = m_joy_axis_map.find(0xFF01); + if (itr != m_joy_axis_map.end()) { + ControlMotionEvent event(itr->second.type, + y_norm * (*itr).second.multiplier, + ControlMotionEvent::MOUSE); + fireControlEvent(event); + } } -void ControlMultiplexer::fireControlEvent( const ControlMotionEvent& evt ) { - std::vector< ControlMotionListener* >::iterator itr; +void ControlMultiplexer::fireControlEvent(const ControlMotionEvent& evt) { + std::vector<ControlMotionListener*>::iterator itr; - for( itr = m_motion_listeners.begin(); itr != m_motion_listeners.end() ; ++ itr ) { - (*itr)->onControlMotion( evt ); - } + for (itr = m_motion_listeners.begin(); itr != m_motion_listeners.end(); + ++itr) { + (*itr)->onControlMotion(evt); + } } diff --git a/sons_of_sol/private_db/PlayerShip.cpp b/sons_of_sol/private_db/PlayerShip.cpp index 14eff74..6e2fb23 100644 --- a/sons_of_sol/private_db/PlayerShip.cpp +++ b/sons_of_sol/private_db/PlayerShip.cpp @@ -1,161 +1,160 @@ #include "sons_of_sol/PlayerShip.hpp" -#include "glox/GloxCommon.hpp" #include "glox/GloxColor.hpp" +#include "glox/GloxCommon.hpp" using namespace glox; using namespace std; - -float randomJitter( float jitter ) { - return (( rand() % 2000 - 1000 ) / 1000.0) * jitter ; +float randomJitter(float jitter) { + return ((rand() % 2000 - 1000) / 1000.0) * jitter; } -void PlayerShip::pitch( float angle ) { - /* Update the forward vector */ - m_forward *= (float)GloxCos( angle ); - m_forward += ( m_up * (float)GloxSin( angle ) ); - m_forward.normalize(); +void PlayerShip::pitch(float angle) { + /* Update the forward vector */ + m_forward *= (float)GloxCos(angle); + m_forward += (m_up * (float)GloxSin(angle)); + m_forward.normalize(); - m_up = m_right.cross( m_forward ); + m_up = m_right.cross(m_forward); } -void PlayerShip::roll( float angle ) { - m_right *= (float)GloxCos( angle ); - m_right += m_up * GloxSin( angle ); - m_right.normalize(); +void PlayerShip::roll(float angle) { + m_right *= (float)GloxCos(angle); + m_right += m_up * GloxSin(angle); + m_right.normalize(); - m_up = m_right.cross( m_forward ); + m_up = m_right.cross(m_forward); } -void PlayerShip::yaw( float angle ) { - m_right *= (float)GloxCos( angle ); - m_right += m_forward * GloxSin( angle ); - m_right.normalize(); +void PlayerShip::yaw(float angle) { + m_right *= (float)GloxCos(angle); + m_right += m_forward * GloxSin(angle); + m_right.normalize(); - m_forward = m_up.cross( m_right ); + m_forward = m_up.cross(m_right); } -void PlayerShip::renderPerspective( ) { +void PlayerShip::renderPerspective() { - GloxPoint<> noise( randomJitter( m_jitter ), randomJitter( m_jitter ), randomJitter( m_jitter ) ) ; - if( m_is_firing ) { - noise += GloxPointf( randomJitter(0.01),randomJitter(0.01),randomJitter(0.01) ); - } - m_perspective.setLookAtPoint( m_position + m_forward.toPoint() + noise ); - m_perspective.setPosition( m_position + (m_forward.toPoint() * 0.2f) ); - m_perspective.setUpVector( m_up.toPoint() ); - - GloxColor( 255,255,255 ).render(); - GloxWith( GL_LINES, - GloxPoint<>( 0,0,0 ).plot(); - m_position.plot() ); -} - -void PlayerShip::drawHUD() { + GloxPoint<> noise(randomJitter(m_jitter), randomJitter(m_jitter), + randomJitter(m_jitter)); + if (m_is_firing) { + noise += + GloxPointf(randomJitter(0.01), randomJitter(0.01), randomJitter(0.01)); + } + m_perspective.setLookAtPoint(m_position + m_forward.toPoint() + noise); + m_perspective.setPosition(m_position + (m_forward.toPoint() * 0.2f)); + m_perspective.setUpVector(m_up.toPoint()); + GloxColor(255, 255, 255).render(); + GloxWith(GL_LINES, GloxPoint<>(0, 0, 0).plot(); m_position.plot()); } -static inline float update_val( float to, float x, int inertia ) { - return ( to + (inertia - 1) * x ) / inertia; +void PlayerShip::drawHUD() {} + +static inline float update_val(float to, float x, int inertia) { + return (to + (inertia - 1) * x) / inertia; } -void PlayerShip::onControlMotion( const ControlMotionEvent& evt ) { - //printf( "Event Mag: %f\n", evt.getMagnitude() ); +void PlayerShip::onControlMotion(const ControlMotionEvent& evt) { + //printf( "Event Mag: %f\n", evt.getMagnitude() ); - if( evt.getType() == ControlMotionEvent::PITCH ) { - m_dpitch = update_val( evt.getMagnitude(), m_dpitch, 10 ); - } + if (evt.getType() == ControlMotionEvent::PITCH) { + m_dpitch = update_val(evt.getMagnitude(), m_dpitch, 10); + } - else if( evt.getType() == ControlMotionEvent::THROTTLE ) { - m_dforward_to = evt.getMagnitude(); - } + else if (evt.getType() == ControlMotionEvent::THROTTLE) { + m_dforward_to = evt.getMagnitude(); + } - else if( evt.getType() == ControlMotionEvent::YAW ) { - m_dyaw = update_val( evt.getMagnitude(), m_dyaw, 10 ); - } + else if (evt.getType() == ControlMotionEvent::YAW) { + m_dyaw = update_val(evt.getMagnitude(), m_dyaw, 10); + } - else if( evt.getType() == ControlMotionEvent::ROLL ) { - m_droll_to = evt.getMagnitude(); - } + else if (evt.getType() == ControlMotionEvent::ROLL) { + m_droll_to = evt.getMagnitude(); + } - else if( evt.getType() == ControlMotionEvent::STRAFE_SIDE ) { - m_dright_to = evt.getMagnitude(); - } + else if (evt.getType() == ControlMotionEvent::STRAFE_SIDE) { + m_dright_to = evt.getMagnitude(); + } - else if( evt.getType() == ControlMotionEvent::STRAFE_UP ) { - m_dup_to = evt.getMagnitude(); - } + else if (evt.getType() == ControlMotionEvent::STRAFE_UP) { + m_dup_to = evt.getMagnitude(); + } - else if( evt.getType() == ControlMotionEvent::FIRE_PRIMARY ) { - m_is_firing = evt.getMagnitude() != 0 ; - } + else if (evt.getType() == ControlMotionEvent::FIRE_PRIMARY) { + m_is_firing = evt.getMagnitude() != 0; + } } void PlayerShip::update() { - static const float projectile_speed = 150; - // printf( "dpitch: %f, droll: %f, dyaw: %f, dforward: %f, dright: %f, dup: %f\n", - // m_dpitch, m_droll, m_dyaw, m_dforward, m_dright, m_dup ); - - pitch( m_dpitch ); - roll( m_droll ); - yaw( m_dyaw ); - - int i = 0; - for( vector<Projectile>::iterator itr = m_living_projectiles.begin(); - itr != m_living_projectiles.end() ; ++ itr ) { - if( ! (*itr).isDead() ) { - (*itr).update(); - if( (*itr).isDead() ) { - m_open_positions.push( i ); - } - } - ++ i; + static const float projectile_speed = 150; + // printf( "dpitch: %f, droll: %f, dyaw: %f, dforward: %f, dright: %f, dup: %f\n", + // m_dpitch, m_droll, m_dyaw, m_dforward, m_dright, m_dup ); + + pitch(m_dpitch); + roll(m_droll); + yaw(m_dyaw); + + int i = 0; + for (vector<Projectile>::iterator itr = m_living_projectiles.begin(); + itr != m_living_projectiles.end(); ++itr) { + if (!(*itr).isDead()) { + (*itr).update(); + if ((*itr).isDead()) { + m_open_positions.push(i); + } } - - m_position += (m_forward * m_dforward).toPoint(); - m_position += (m_right * m_dright).toPoint(); - m_position += (m_up * m_dup).toPoint(); - - m_droll = update_val( m_droll_to, m_droll, 20 ); - m_dup = update_val( m_dup_to, m_dup, 20 ); - m_dright = update_val( m_dright_to, m_dright, 20 ); - - float last_dforward = m_dforward ; - m_dforward = update_val( m_dforward_to, m_dforward, 200 ); - - float accel = m_dforward - last_dforward ; - if( accel > 0 ) { - m_jitter = accel / 40.0 ; + ++i; + } + + m_position += (m_forward * m_dforward).toPoint(); + m_position += (m_right * m_dright).toPoint(); + m_position += (m_up * m_dup).toPoint(); + + m_droll = update_val(m_droll_to, m_droll, 20); + m_dup = update_val(m_dup_to, m_dup, 20); + m_dright = update_val(m_dright_to, m_dright, 20); + + float last_dforward = m_dforward; + m_dforward = update_val(m_dforward_to, m_dforward, 200); + + float accel = m_dforward - last_dforward; + if (accel > 0) { + m_jitter = accel / 40.0; + } else { + m_jitter = 0.0; + } + + // This is only the case if we are using a mouse + m_dpitch = update_val(0, m_dpitch, 100); + m_dyaw = update_val(0, m_dyaw, 100); + // m_droll = (m_droll_to * ( 1 - exp( - m_droll_count ) ) + m_droll * ( 1 - exp( - m_droll_count ) ) )/ 2.0; + if (m_is_firing) { + GloxPointf spread(randomJitter(0.03), randomJitter(0.03), + randomJitter(0.03)); + Projectile proj((m_position - m_up.toPoint() - m_right.toPoint()), + (m_forward.toPoint() + spread) * projectile_speed); + + if (m_open_positions.empty()) { + m_living_projectiles.push_back(proj); } else { - m_jitter = 0.0 ; - } - - // This is only the case if we are using a mouse - m_dpitch = update_val( 0, m_dpitch, 100 ); - m_dyaw = update_val( 0, m_dyaw, 100 ); - // m_droll = (m_droll_to * ( 1 - exp( - m_droll_count ) ) + m_droll * ( 1 - exp( - m_droll_count ) ) )/ 2.0; - if( m_is_firing ) { - GloxPointf spread( randomJitter(0.03), randomJitter(0.03), randomJitter(0.03) ); - Projectile proj( (m_position - m_up.toPoint() - m_right.toPoint()), (m_forward.toPoint() + spread) * projectile_speed ) ; - - if( m_open_positions.empty() ) { - m_living_projectiles.push_back( proj ); - } else { - int idx = m_open_positions.front(); - m_open_positions.pop(); - m_living_projectiles[idx] = proj; - } + int idx = m_open_positions.front(); + m_open_positions.pop(); + m_living_projectiles[idx] = proj; } + } - renderPerspective(); + renderPerspective(); } void PlayerShip::drawProjectiles() { - for( vector<Projectile>::iterator itr = m_living_projectiles.begin(); - itr != m_living_projectiles.end() ; ++ itr ) { - if( ! (*itr).isDead() ) { - itr->draw(); - } + for (vector<Projectile>::iterator itr = m_living_projectiles.begin(); + itr != m_living_projectiles.end(); ++itr) { + if (!(*itr).isDead()) { + itr->draw(); } + } } diff --git a/sons_of_sol/private_db/Projectile.cpp b/sons_of_sol/private_db/Projectile.cpp index 7819a0c..339c1de 100644 --- a/sons_of_sol/private_db/Projectile.cpp +++ b/sons_of_sol/private_db/Projectile.cpp @@ -2,72 +2,70 @@ #include "glox/GloxColor.hpp" #include "glox/GloxScopedRotation.hpp" -using namespace glox ; -using namespace std ; +using namespace glox; +using namespace std; GloxCube* Projectile::m_model; -void Projectile::loadModel( ) { - if( ! m_model ) { - m_model = new GloxCube( 10, 0.1, 0.1, GloxColor( 255, 255, 0 ) ); - } +void Projectile::loadModel() { + if (!m_model) { + m_model = new GloxCube(10, 0.1, 0.1, GloxColor(255, 255, 0)); + } } inline float jitter() { - return (2 * rand() & 0xFF) / 256.0 ; + return (2 * rand() & 0xFF) / 256.0; } void Projectile::draw() { - GloxPoint<> next = m_position + (m_dpos) * 0.2 + GloxPointf(jitter(),jitter(),jitter()); - GloxScopedAttributes __glsa( GL_CURRENT_BIT ); + GloxPoint<> next = + m_position + (m_dpos) * 0.2 + GloxPointf(jitter(), jitter(), jitter()); + GloxScopedAttributes __glsa(GL_CURRENT_BIT); - GloxColor( - (uint8_t)(255.0f / TTL * m_ttl ), - (uint8_t)(250.0f / pow(TTL,2) * pow(m_ttl,2)), - (uint8_t)(200.0f / pow(TTL,4) * pow(m_ttl,4)), - (uint8_t)(100.0) - ).render(); + GloxColor((uint8_t)(255.0f / TTL * m_ttl), + (uint8_t)(250.0f / pow(TTL, 2) * pow(m_ttl, 2)), + (uint8_t)(200.0f / pow(TTL, 4) * pow(m_ttl, 4)), (uint8_t)(100.0)) + .render(); - glLineWidth( m_ttl / 10.0f ); - GloxWith( GL_LINES, - m_position.plot(); - GloxColor( - (uint8_t)(255.0f / TTL * (m_ttl-1) ), - (uint8_t)(200.0f / pow(TTL,2) * pow(m_ttl-1,2)), - (uint8_t)(100.0f / pow(TTL,4) * pow(m_ttl-1,4))).render(); - next.plot(); - ); + glLineWidth(m_ttl / 10.0f); + GloxWith(GL_LINES, m_position.plot(); + GloxColor((uint8_t)(255.0f / TTL * (m_ttl - 1)), + (uint8_t)(200.0f / pow(TTL, 2) * pow(m_ttl - 1, 2)), + (uint8_t)(100.0f / pow(TTL, 4) * pow(m_ttl - 1, 4))) + .render(); + next.plot();); } -Projectile::Projectile( const glox::GloxPoint<>& position, const glox::GloxPoint<>& vec ) { - m_position = position; - m_dpos = vec ; - m_ttl = TTL; +Projectile::Projectile(const glox::GloxPoint<>& position, + const glox::GloxPoint<>& vec) { + m_position = position; + m_dpos = vec; + m_ttl = TTL; - loadModel(); + loadModel(); - /* Project the point onto the x,y plane */ - GloxPoint<> first( m_dpos.getX(), m_dpos.getY(), 0 ); - /* A point that represents the x axis */ - GloxPoint<> xaxis( 1, 0, 0 ); + /* Project the point onto the x,y plane */ + GloxPoint<> first(m_dpos.getX(), m_dpos.getY(), 0); + /* A point that represents the x axis */ + GloxPoint<> xaxis(1, 0, 0); - /* Rotation around the z axis */ - float dot = first.dot( xaxis ) ; - float rot = GloxToDegrees(acos( dot )) ; + /* Rotation around the z axis */ + float dot = first.dot(xaxis); + float rot = GloxToDegrees(acos(dot)); - if( first.getY() < 0 ) { - rot = - rot; - } - rot = rot + 180 ; - m_roty = rot; - /* x axis now follows *first* vector */ - /* Rotate around y axis to get x to follow *dpos* vector */ - dot = first.dot( m_dpos ); - rot = acos( dot ); - rot = GloxToDegrees( rot ); + if (first.getY() < 0) { + rot = -rot; + } + rot = rot + 180; + m_roty = rot; + /* x axis now follows *first* vector */ + /* Rotate around y axis to get x to follow *dpos* vector */ + dot = first.dot(m_dpos); + rot = acos(dot); + rot = GloxToDegrees(rot); - if( m_dpos.getZ() < 0 ) { - rot = - rot; - } - m_rotz = rot; + if (m_dpos.getZ() < 0) { + rot = -rot; + } + m_rotz = rot; } diff --git a/sons_of_sol/private_db/SonsOfSolApplication.cpp b/sons_of_sol/private_db/SonsOfSolApplication.cpp index 59f7e62..bd93015 100644 --- a/sons_of_sol/private_db/SonsOfSolApplication.cpp +++ b/sons_of_sol/private_db/SonsOfSolApplication.cpp @@ -11,136 +11,132 @@ using namespace glox; using namespace std; void SonsOfSolApplication::setupControls() { - m_event_multiplexer.setKeyMapping( SDLK_w, ControlMotionEvent::THROTTLE, 30.0f ); - m_event_multiplexer.setKeyMapping( SDLK_s, ControlMotionEvent::THROTTLE, -20.0f ); - m_event_multiplexer.setKeyMapping( SDLK_d, ControlMotionEvent::STRAFE_SIDE ); - m_event_multiplexer.setKeyMapping( SDLK_a, ControlMotionEvent::STRAFE_SIDE, -1.0f ); - m_event_multiplexer.setKeyMapping( SDLK_z, ControlMotionEvent::STRAFE_UP, -1.0f ); - m_event_multiplexer.setKeyMapping( SDLK_x, ControlMotionEvent::STRAFE_UP, 0.3f ); - - m_event_multiplexer.setKeyMapping( SDLK_q, ControlMotionEvent::ROLL, 5.0f ); - m_event_multiplexer.setKeyMapping( SDLK_e, ControlMotionEvent::ROLL, -5.0f ); - m_event_multiplexer.setKeyMapping( SDLK_k, ControlMotionEvent::PITCH ); - m_event_multiplexer.setKeyMapping( SDLK_j, ControlMotionEvent::PITCH, -1.0f ); - m_event_multiplexer.setKeyMapping( SDLK_l, ControlMotionEvent::YAW, -1.0f ); - m_event_multiplexer.setKeyMapping( SDLK_h, ControlMotionEvent::YAW ); - - m_event_multiplexer.setKeyMapping( SDLK_SPACE, ControlMotionEvent::FIRE_PRIMARY ); - m_event_multiplexer.setKeyMapping( SDLK_LSHIFT, ControlMotionEvent::FIRE_PRIMARY ); - - m_event_multiplexer.setJoyAxisMapping( 255, 0, ControlMotionEvent::YAW, -3.0f ); - m_event_multiplexer.setJoyAxisMapping( 255, 1, ControlMotionEvent::PITCH, -3.0f ); + m_event_multiplexer.setKeyMapping(SDLK_w, ControlMotionEvent::THROTTLE, + 30.0f); + m_event_multiplexer.setKeyMapping(SDLK_s, ControlMotionEvent::THROTTLE, + -20.0f); + m_event_multiplexer.setKeyMapping(SDLK_d, ControlMotionEvent::STRAFE_SIDE); + m_event_multiplexer.setKeyMapping(SDLK_a, ControlMotionEvent::STRAFE_SIDE, + -1.0f); + m_event_multiplexer.setKeyMapping(SDLK_z, ControlMotionEvent::STRAFE_UP, + -1.0f); + m_event_multiplexer.setKeyMapping(SDLK_x, ControlMotionEvent::STRAFE_UP, + 0.3f); + + m_event_multiplexer.setKeyMapping(SDLK_q, ControlMotionEvent::ROLL, 5.0f); + m_event_multiplexer.setKeyMapping(SDLK_e, ControlMotionEvent::ROLL, -5.0f); + m_event_multiplexer.setKeyMapping(SDLK_k, ControlMotionEvent::PITCH); + m_event_multiplexer.setKeyMapping(SDLK_j, ControlMotionEvent::PITCH, -1.0f); + m_event_multiplexer.setKeyMapping(SDLK_l, ControlMotionEvent::YAW, -1.0f); + m_event_multiplexer.setKeyMapping(SDLK_h, ControlMotionEvent::YAW); + + m_event_multiplexer.setKeyMapping(SDLK_SPACE, + ControlMotionEvent::FIRE_PRIMARY); + m_event_multiplexer.setKeyMapping(SDLK_LSHIFT, + ControlMotionEvent::FIRE_PRIMARY); + + m_event_multiplexer.setJoyAxisMapping(255, 0, ControlMotionEvent::YAW, -3.0f); + m_event_multiplexer.setJoyAxisMapping(255, 1, ControlMotionEvent::PITCH, + -3.0f); } void SonsOfSolApplication::setFullScreen() { - m_screen = SDL_SetVideoMode(m_screen_width,m_screen_height,0, - SDL_OPENGL|SDL_RESIZABLE|SDL_DOUBLEBUF|SDL_FULLSCREEN); - reshape( m_screen_width, m_screen_height ); + m_screen = SDL_SetVideoMode( + m_screen_width, m_screen_height, 0, + SDL_OPENGL | SDL_RESIZABLE | SDL_DOUBLEBUF | SDL_FULLSCREEN); + reshape(m_screen_width, m_screen_height); } void SonsOfSolApplication::pause() { - if( m_is_paused ) { - SDL_ShowCursor( 0 ) ; - SDL_WM_GrabInput(SDL_GRAB_ON); - } else { - SDL_ShowCursor( 1 ) ; - SDL_WM_GrabInput(SDL_GRAB_OFF); - } - m_is_paused = ! m_is_paused ; + if (m_is_paused) { + SDL_ShowCursor(0); + SDL_WM_GrabInput(SDL_GRAB_ON); + } else { + SDL_ShowCursor(1); + SDL_WM_GrabInput(SDL_GRAB_OFF); + } + m_is_paused = !m_is_paused; } - #define NTEX 6 bool SonsOfSolApplication::loadTextures() { - const char* files[NTEX] = { - "stars.jpg", - "leaf.jpg", - "crate.jpg", - "star.jpg", - "earth.jpg", - "moon.jpg" - }; - - GloxTexture* textures[NTEX] = { - &m_sky_tex, - &m_leaf_tex, - &m_crate_tex, - &m_star_tex, - &m_earth_tex, - &m_moon_tex - }; - - unsigned int tex = 0; - - m_texture_factory.addToPath( "objects/textures/" ); - m_texture_factory.addToPath( "objects/" ); - for ( int i = 0; i < NTEX; ++ i ) { - if( m_texture_factory.readImageFile( files[i], &tex ) ) { - /* Either there was a warning or an error, print out + const char* files[NTEX] = {"stars.jpg", "leaf.jpg", "crate.jpg", + "star.jpg", "earth.jpg", "moon.jpg"}; + + GloxTexture* textures[NTEX] = {&m_sky_tex, &m_leaf_tex, &m_crate_tex, + &m_star_tex, &m_earth_tex, &m_moon_tex}; + + unsigned int tex = 0; + + m_texture_factory.addToPath("objects/textures/"); + m_texture_factory.addToPath("objects/"); + for (int i = 0; i < NTEX; ++i) { + if (m_texture_factory.readImageFile(files[i], &tex)) { + /* Either there was a warning or an error, print out * either */ - fprintf( stderr, "Reading %s: %s\n", files[i], m_texture_factory.getMessage().c_str() ); - - if( tex == 0 ) { - /* If the texture hasn't changed, its an error, otherwise + fprintf(stderr, "Reading %s: %s\n", files[i], + m_texture_factory.getMessage().c_str()); + + if (tex == 0) { + /* If the texture hasn't changed, its an error, otherwise * it's a warning, so continue */ - this->setError( "Aborting: unable to load texture" ); - return false; - } - } - textures[i]->setId( tex ); - tex = 0; - } - - return true; + this->setError("Aborting: unable to load texture"); + return false; + } + } + textures[i]->setId(tex); + tex = 0; + } + + return true; } void SonsOfSolApplication::enableLighting() { - /* There is very little ambient light + /* There is very little ambient light * in space */ - // int ambient_coef = 0.5; - // int diffuse_coef = 100; - int specular_coef = 0; - - GloxLightSource* sun = m_light_manager.getLightSource( 0 ); - GloxLightSource* earth = m_light_manager.getLightSource( 1 ); - - GloxColor base(2,2,0); - sun->setLightModelAttribute( GL_LIGHT_MODEL_AMBIENT, GloxColor(100,100,90) ); - sun->setAmbient ( GloxColor( 25, 25, 25 ) ); - sun->setDiffuse ( GloxColor( 255, 235, 200 ) ); - sun->setSpecular( base * specular_coef ); - sun->setLightModelAttribute( GL_LIGHT_MODEL_LOCAL_VIEWER, 1 ); - sun->setEnabled( true ); - - earth->setLightModelAttribute( GL_LIGHT_MODEL_AMBIENT, GloxColor(0,0,0) ); - earth->setAmbient ( GloxColor(0,0,0) ); - earth->setDiffuse ( GloxColor(130,155,155) ); - earth->setSpecular( base * specular_coef ); - earth->setLightModelAttribute( GL_LIGHT_MODEL_LOCAL_VIEWER, 1 ); - earth->setPosition( GloxPoint<>( 700,-1000,0 ) ); + // int ambient_coef = 0.5; + // int diffuse_coef = 100; + int specular_coef = 0; + + GloxLightSource* sun = m_light_manager.getLightSource(0); + GloxLightSource* earth = m_light_manager.getLightSource(1); + + GloxColor base(2, 2, 0); + sun->setLightModelAttribute(GL_LIGHT_MODEL_AMBIENT, GloxColor(100, 100, 90)); + sun->setAmbient(GloxColor(25, 25, 25)); + sun->setDiffuse(GloxColor(255, 235, 200)); + sun->setSpecular(base * specular_coef); + sun->setLightModelAttribute(GL_LIGHT_MODEL_LOCAL_VIEWER, 1); + sun->setEnabled(true); + + earth->setLightModelAttribute(GL_LIGHT_MODEL_AMBIENT, GloxColor(0, 0, 0)); + earth->setAmbient(GloxColor(0, 0, 0)); + earth->setDiffuse(GloxColor(130, 155, 155)); + earth->setSpecular(base * specular_coef); + earth->setLightModelAttribute(GL_LIGHT_MODEL_LOCAL_VIEWER, 1); + earth->setPosition(GloxPoint<>(700, -1000, 0)); } -void SonsOfSolApplication::update( uint32_t ticks ) { - (void) ticks; - m_ship.update(); +void SonsOfSolApplication::update(uint32_t ticks) { + (void)ticks; + m_ship.update(); - for( vector<AIShip>::iterator itr = m_aiships.begin() ; - itr != m_aiships.end() ; ++ itr ) { - itr->update(); - } + for (vector<AIShip>::iterator itr = m_aiships.begin(); itr != m_aiships.end(); + ++itr) { + itr->update(); + } } void SonsOfSolApplication::renderFar() { - // m_ship.getPerspective().setZNear( FAR_DISTANCE_START ); - // m_ship.getPerspective().setZNear( FAR_DISTANCE_START ); + // m_ship.getPerspective().setZNear( FAR_DISTANCE_START ); + // m_ship.getPerspective().setZNear( FAR_DISTANCE_START ); } +void SonsOfSolApplication::reshape(int width, int height) { + m_viewport.setWidth(width); + m_viewport.setHeight(height); -void SonsOfSolApplication::reshape( int width, int height ) { - m_viewport.setWidth( width ); - m_viewport.setHeight( height ); - - m_viewport.render(); - m_ship.getPerspective().setAspectRatio( m_viewport.getAspectRatio() ); - m_ship.getPerspective().project(); + m_viewport.render(); + m_ship.getPerspective().setAspectRatio(m_viewport.getAspectRatio()); + m_ship.getPerspective().project(); } |