From 3866f2b33313d891347f454537843befd295b78f Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 20 Oct 2007 10:02:51 +0000 Subject: Initial import. --- src/client/Makefile.am | 12 ++++ src/client/camera.cc | 107 +++++++++++++++++++++++++++++++++ src/client/camera.h | 44 ++++++++++++++ src/client/hud.cc | 0 src/client/hud.h | 0 src/client/input.cc | 86 ++++++++++++++++++++++++++ src/client/input.h | 24 ++++++++ src/client/main.cc | 65 ++++++++++++++++++++ src/client/shipdrawer.cc | 108 +++++++++++++++++++++++++++++++++ src/client/shipdrawer.h | 26 ++++++++ src/client/stardrawer | 0 src/client/stardrawer.cc | 22 +++++++ src/client/stardrawer.h | 26 ++++++++ src/client/video.cc | 109 +++++++++++++++++++++++++++++++++ src/client/video.h | 30 ++++++++++ src/client/view.cc | 153 +++++++++++++++++++++++++++++++++++++++++++++++ src/client/view.h | 24 ++++++++ 17 files changed, 836 insertions(+) create mode 100644 src/client/Makefile.am create mode 100644 src/client/camera.cc create mode 100644 src/client/camera.h create mode 100644 src/client/hud.cc create mode 100644 src/client/hud.h create mode 100644 src/client/input.cc create mode 100644 src/client/input.h create mode 100644 src/client/main.cc create mode 100644 src/client/shipdrawer.cc create mode 100644 src/client/shipdrawer.h create mode 100644 src/client/stardrawer create mode 100644 src/client/stardrawer.cc create mode 100644 src/client/stardrawer.h create mode 100644 src/client/video.cc create mode 100644 src/client/video.h create mode 100644 src/client/view.cc create mode 100644 src/client/view.h (limited to 'src/client') diff --git a/src/client/Makefile.am b/src/client/Makefile.am new file mode 100644 index 0000000..f234f49 --- /dev/null +++ b/src/client/Makefile.am @@ -0,0 +1,12 @@ + +METASOURCES = AUTO +bin_PROGRAMS = osirion +osirion_LDADD = $(top_builddir)/src/common/libcommon.la \ + $(top_builddir)/src/game/libgame.la $(top_builddir)/src/gl/libosiriongl.la -lSDL +osirion_SOURCES = camera.cc camera.h input.cc input.h main.cc video.cc video.h \ + view.cc view.h shipdrawer.cc shipdrawer.h stardrawer.cc stardrawer.h + + + +INCLUDES = -I$(top_srcdir)/src +osirion_LDFLAGS = -L/usr/X11/lib64 diff --git a/src/client/camera.cc b/src/client/camera.cc new file mode 100644 index 0000000..ca60d18 --- /dev/null +++ b/src/client/camera.cc @@ -0,0 +1,107 @@ +/* camera.cc + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#include "common/functions.h" +#include "common/osirion.h" + +#include "game/game.h" +#include "gl/osiriongl.h" + +namespace camera +{ +enum Mode {Free, Track}; +Mode mode = Track; + +const float track_pitch = 15.0f; // default tracking pitch +const float offset_inc = 5.0f; // default offset increment + +float yaw = 90; // current yaw, angle in XZ plane +float yaw_offset = 0; // target offset, relative to target yaw + +float pitch = -90; // current pitch, angle in XY +float pitch_offset = track_pitch; // target offset, relative to target pitch + +float distance = 2.0f; // distance from the eye to the target + + +void draw(float elapsed) +{ + // adjust yaw target + float yaw_target = game::ship.yaw - yaw_offset; + float d = degreesf(yaw - yaw_target); + yaw = degreesf( yaw - d * elapsed); + + // adjust pitch target + float pitch_target = 0.0f - pitch_offset; + d= degreesf(pitch - pitch_target); + pitch = degreesf( pitch - d *elapsed); + + /* + gl::translate(distance, 0.0, 0.0); + gl::rotate(-pitch,0, 0, 1.0 ); + gl::rotate(-yaw, 0, 1.0f, 0); + */ + switch (mode) { + case Free: + gl::translate(1+distance * GAMESCALE, 0.0f, 0.0f); + gl::rotate(-pitch, 0, 0, 1.0f); + gl::rotate(-yaw, 0, 1.0f, 0); + break; + case Track: + gl::translate(1+distance* GAMESCALE, -0.5f* GAMESCALE, 0.0f); + gl::rotate(-pitch, 0, 0, 1.0f); + gl::rotate(-yaw, 0, 1.0f, 0); + break; + default: + break; + } +} + +void rotate_right() +{ + if (mode == Free ) { + yaw_offset = degreesf( yaw_offset - offset_inc); + } +} + +void rotate_left() +{ + if (mode == Free ) { + yaw_offset = degreesf( yaw_offset + offset_inc); + } +} + +void rotate_up() +{ + if (mode == Free ) { + pitch_offset = degreesf( pitch_offset + offset_inc); + } +} + +void rotate_down() +{ + if (mode == Free ) { + pitch_offset = degreesf( pitch_offset - offset_inc); + } +} + +void nextmode() { + switch(mode) { + case Free: + mode = Track; + yaw_offset = 0; + yaw = game::ship.yaw; + pitch_offset = track_pitch; + break; + case Track: + mode = Free; + yaw_offset = 0; + break; + default: + break; + } +} + +} //namespace camera diff --git a/src/client/camera.h b/src/client/camera.h new file mode 100644 index 0000000..63c7aa5 --- /dev/null +++ b/src/client/camera.h @@ -0,0 +1,44 @@ +/* camera.h + This file is part of the Osirion project +*/ + +#ifndef __INCLUDED_CAMERA_H__ +#define __INCLUDED_CAMERA_H__ + +#include "common/vector3f.h" + +/// camera functions +/** The functions in this namespace performs the transformations +for the camera eye location. The camera always looks at (0,0,0) +*/ +namespace camera +{ + /// draw the OpenGL camera transformation + void draw(float elapsed); + + /// rotate the camera left + void rotate_left(); + /// rotate the camera right + void rotate_right(); + /// rotate the camera up + void rotate_up(); + /// rotate the camera down + void rotate_down(); + + /// switch to next camera mode + void nextmode(); + + /// camera target + /** The location the camera is looking at */ + extern Vector3f target; + + /// horizontal viewing angle x/z plane + extern float horiz_angle; + /// vertical viewing angle z/y plane + extern float vert_angle; + /// distance from the camera to the target + /** The distance in game units from the eye of the camera to the target */ + extern float distance; +}; + +#endif // __INCLUDED_CAMERA_H__ diff --git a/src/client/hud.cc b/src/client/hud.cc new file mode 100644 index 0000000..e69de29 diff --git a/src/client/hud.h b/src/client/hud.h new file mode 100644 index 0000000..e69de29 diff --git a/src/client/input.cc b/src/client/input.cc new file mode 100644 index 0000000..8af7414 --- /dev/null +++ b/src/client/input.cc @@ -0,0 +1,86 @@ +/* input.cc + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +// SDL headers +#include + +//project headers +#include "common/functions.h" +#include "game/game.h" + +#include "view.h" +#include "camera.h" + +namespace input +{ + +void init() +{ + SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); +} + +void shutdown() +{ +} + +void handle_keydown(SDL_keysym* keysym) +{ + switch( keysym->sym ) { + case SDLK_ESCAPE: + game::shutdown(); + break; + case SDLK_SPACE: + camera::nextmode(); + break; + case SDLK_LEFT: + camera::rotate_left(); + break; + case SDLK_RIGHT: + camera::rotate_right(); + break; + case SDLK_UP: + camera::rotate_up(); + break; + case SDLK_DOWN: + camera::rotate_down(); + break; + case SDLK_KP_PLUS: + game::ship.thrust_increase(); + break; + case SDLK_KP_MINUS: + game::ship.thrust_decrease(); + break; + case SDLK_KP4: + game::ship.turn_left(); + break; + case SDLK_KP6: + game::ship.turn_right(); + break; + default: + break; + } + +} + +void process() +{ + SDL_Event event; + + while( SDL_PollEvent( &event ) ) { + switch( event.type ) { +// case SDL_MOUSEBUTTONUP: + case SDL_KEYDOWN: + handle_keydown( &event.key.keysym ); + break; + case SDL_QUIT: + game::shutdown(); + break; + } + + } + +} + +} // namespace input diff --git a/src/client/input.h b/src/client/input.h new file mode 100644 index 0000000..f1e94d9 --- /dev/null +++ b/src/client/input.h @@ -0,0 +1,24 @@ +/* input.h + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_INPUT_H__ +#define __INCLUDED_INPUT_H__ + + +namespace input +{ + /// initialize the input subsystem + void init(); + /// shutdown the input subsystem + void shutdown(); + + /// exit the application + void quit(int exit_code); + + /// process input events + void process(); +}; + +#endif // __INCLUDED_INPUT_H__ diff --git a/src/client/main.cc b/src/client/main.cc new file mode 100644 index 0000000..66e0fa3 --- /dev/null +++ b/src/client/main.cc @@ -0,0 +1,65 @@ +/* main.cc + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +// SDL headers +#include + +// C++ headers +#include + +// project headers +#include "common/osirion.h" +#include "game/game.h" + +#include "input.h" +#include "video.h" + +void quit(int exit_code) +{ + SDL_Quit(); + exit(exit_code); +} + +int main( int argc, char *argv[] ) +{ + std::cout << "The Osirion project " << OSIRION_VERSION << std::endl; + + // Initialize the video subsystem + video::init(); + if (!video::initialized) { + quit(1); + } + + // initialize input + input::init(); + + // initialize game + game::init(); + + Uint32 startup = SDL_GetTicks(); + while(game::initialized) { + Uint32 chrono = SDL_GetTicks(); + + // overflow protection ~49 days + if (chrono < startup) { + startup = chrono; + } + + // update the game chronometers + float elapsed = (float) ( chrono - startup) / 1000.0f; + game::update(elapsed); + + // update the video chronometers and draw + video::draw(elapsed); + startup = chrono; + + // process input + input::process(); + } + + video::shutdown(); + + quit(0); +} diff --git a/src/client/shipdrawer.cc b/src/client/shipdrawer.cc new file mode 100644 index 0000000..d889178 --- /dev/null +++ b/src/client/shipdrawer.cc @@ -0,0 +1,108 @@ +/* shipdrawer.cc + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#include + +// project headers +#include "gl/osiriongl.h" +#include "gl/box.h" + +#include "shipdrawer.h" + +Vector3f v0(1.0f, -1.0f, -1.0f); +Vector3f v1(1.0f, 1.0f, -1.0f); +Vector3f v2(1.0f, 1.0f, 1.0f); +Vector3f v3(1.0f, -1.0f, 1.0f); + +Vector3f v4(-1.0f, -1.0f, -1.0f); +Vector3f v5(-1.0f, 1.0f, -1.0f); +Vector3f v6(-1.0f, 1.0f, 1.0f); +Vector3f v7(-1.0f, -1.0f, 1.0f); + +ShipDrawer::ShipDrawer(Ship *s) +{ + angle = 0; + ship = s; +} + +ShipDrawer::~ShipDrawer() +{ +} + +void ShipDrawer::draw(float elapsed) +{ + using namespace gl; + + gl::push(); + + rotate(ship->yaw, 0.0f, 1.0f, 0.0f ); + + + Vector3f tl(0.25, 0.125, 0.125); + Vector3f br(-0.25, -0.125, -0.125); + + Box box(tl, br); + box.draw(); + + tl = Vector3f(0, 0.07, 0.25); + br = Vector3f(-0.5, -0.07, 0.125); + Box engine1(tl, br); + engine1.topcolor = Color(0.7, 0.7, 0.7); + engine1.bottomcolor = engine1.topcolor * 0.5; + engine1.draw(); + + tl = Vector3f(0, 0.07, -0.125); + br = Vector3f(-0.5, -0.07, -0.25); + Box engine2(tl, br); + engine2.topcolor = engine1.topcolor; + engine2.bottomcolor = engine1.bottomcolor; + engine2.draw(); + + tl = Vector3f(0.4, 0.07, 0.07); + br = Vector3f(0.25, -0.07, -0.07); + Box cockpit(tl, br); + cockpit.topcolor = engine1.topcolor; + cockpit.bottomcolor = engine1.bottomcolor; + cockpit.draw(); + + if(ship->thrust > 0 ) { + color(1.0f,0 ,0 ); + begin(Lines); + vertex(-0.5f, 0, 0.185); + vertex(-0.5f-0.25f*ship->thrust, 0, 0.185); + + vertex(-0.5f, 0, -0.185f); + vertex(-0.5f-0.25f*ship->thrust, 0, -0.185f); + end(); + } + + // shield rotation + rotate(angle, 0.0f, 1.0f, 0.0f ); + angle += 180.0f * elapsed; + if( angle > 360.0f ) { + angle -= 360.0f; + } + + // draw the shield + color(Color(0.0f, 1.0f ,0.0f , 0.5f)); + + begin(LineStrip); + vertex(v0); + vertex(v1); + vertex(v2); + vertex(v3); + vertex(v0); + end(); + + begin(LineStrip); + vertex(v4); + vertex(v5); + vertex(v6); + vertex(v7); + vertex(v4); + end(); + + gl::pop(); +} diff --git a/src/client/shipdrawer.h b/src/client/shipdrawer.h new file mode 100644 index 0000000..8860585 --- /dev/null +++ b/src/client/shipdrawer.h @@ -0,0 +1,26 @@ +/* shipdrawer.h + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_SHIPDRAWER_H__ +#define __INCLUDED_SHIPDRAWER_H__ + +// project headers +#include "game/ship.h" + +class ShipDrawer +{ +public: + ShipDrawer(Ship *s); + ~ShipDrawer(); + + /// update the model state + void draw(float elapsed); + +private: + Ship *ship; + float angle; +}; + +#endif // __INCLUDED_SHIPDRAWER_H__ diff --git a/src/client/stardrawer b/src/client/stardrawer new file mode 100644 index 0000000..e69de29 diff --git a/src/client/stardrawer.cc b/src/client/stardrawer.cc new file mode 100644 index 0000000..6bdf34c --- /dev/null +++ b/src/client/stardrawer.cc @@ -0,0 +1,22 @@ +/* stardrawer.cc + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#include "gl/osiriongl.h" +#include "stardrawer.h" + +StarDrawer::StarDrawer(Star *s) { + star = s; + sphere.radius = s->radius; + +} +StarDrawer::~StarDrawer() { + star = 0; +} + +void StarDrawer::draw(float elapsed) +{ + gl::color(star->color); + sphere.draw(); +} diff --git a/src/client/stardrawer.h b/src/client/stardrawer.h new file mode 100644 index 0000000..7656414 --- /dev/null +++ b/src/client/stardrawer.h @@ -0,0 +1,26 @@ +/* stardrawer.h + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_STARDRAWER_H__ +#define __INCLUDED_STARDRAWER_H__ + +#include "gl/sphere.h" +#include "game/star.h" + +/// Class to draw stars +class StarDrawer +{ +public: + StarDrawer(Star *s); + ~StarDrawer(); + + void draw(float elapsed); + +private: + Star *star; + gl::Sphere sphere; +}; + +#endif // __INCLUDED_STARDRAWER_H__ diff --git a/src/client/video.cc b/src/client/video.cc new file mode 100644 index 0000000..eb4297e --- /dev/null +++ b/src/client/video.cc @@ -0,0 +1,109 @@ +/* video.cc + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +// SDL headers +#include + +// C++ headers +#include + +// project headers +#include "gl/osiriongl.h" +#include "view.h" + + +namespace video +{ +int width = 0; +int height = 0; +bool initialized = false; +float ratio = 1; + +void reset() +{ + ratio = (float) width / (float) height; + + // Our shading model--Gouraud (smooth). + gl::shademodel(GL_SMOOTH); + + // Culling + gl::cullface( GL_BACK ); + gl::frontface(GL_CCW ); + gl::enable( GL_CULL_FACE ); + + gl::depthmask(GL_TRUE); // Depth buffer writing + gl::enable(GL_DEPTH_TEST); + + // Set the clear color + gl::clearcolor( 0, 0, 0, 0 ); + + // Setup our viewport. + gl::viewport(0, 0, width, height ); + + view::reset(); +} + +void init() +{ + if (initialized) { + return; + } + + int bpp = 0; + int flags = 0; + + if( SDL_Init(SDL_INIT_VIDEO) < 0 ) { + std::cerr << "SDL_Init() failed: " << SDL_GetError() << std::endl; + return; + } + + const SDL_VideoInfo* sdl_videoinfo = SDL_GetVideoInfo(); + if( !sdl_videoinfo) { + std::cerr << "SDL_GetVideoInfo() failed: " << SDL_GetError() << std::endl; + return; + } + + width = 800; + height = 600; + bpp = sdl_videoinfo->vfmt->BitsPerPixel; + + SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 ); + SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 ); + SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 ); + SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 ); + SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); + + flags = SDL_OPENGL | SDL_FULLSCREEN; + + if(!SDL_SetVideoMode(width, height, bpp, flags )) { + std::cerr << "SDL_SetVideoMode() failed: " << SDL_GetError() << std::endl; + return; + } + + gl::init(); + + initialized = true; + view::init(); + + reset(); + return; +} + +void draw(float elapsed) +{ + view::draw(elapsed); +} + +void shutdown() +{ + view::shutdown(); + gl::shutdown(); + + initialized = false; + width = 0; + height = 0; +} + +} // namespace video diff --git a/src/client/video.h b/src/client/video.h new file mode 100644 index 0000000..3b126f1 --- /dev/null +++ b/src/client/video.h @@ -0,0 +1,30 @@ +/* video.h + This file is part of the Osirion project +*/ + +#ifndef __INCLUDED_VIDEO_H__ +#define __INCLUDED_VIDEO_H__ + + +namespace video +{ + /// initialize the video subsystem + void init(); + /// shutdown the video subsystem + void shutdown(); + /// Update the screen state and redraw + void draw(float elapsed); + /// reset and clear the viewport + void reset(); + + /// Width of the SDL window in pixels + extern int width; + /// Height of the SDL window in pixels + extern int height; + /// True if the video subsystem is initialized + extern bool initialized; + /// width/height ratio + extern float ratio; +}; + +#endif // __INCLUDED_VIDEO_H__ diff --git a/src/client/view.cc b/src/client/view.cc new file mode 100644 index 0000000..3641261 --- /dev/null +++ b/src/client/view.cc @@ -0,0 +1,153 @@ +/* view.cc + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +// C++ headers +#include + +// SDL headers +#include + +// project headers +#include "common/functions.h" +#include "common/osirion.h" + +#include "game/game.h" +#include "gl/osiriongl.h" + +#include "video.h" +#include "camera.h" + +#include "shipdrawer.h" +#include "stardrawer.h" + +namespace view +{ + +ShipDrawer *shipdrawer = 0; +StarDrawer *stardrawer = 0; + +Ship *target =0; // the view's target + +void init() { + // draw scene + if (!shipdrawer) { + stardrawer = new StarDrawer(&game::star); + shipdrawer = new ShipDrawer(&game::ship); + target = &game::ship; + } + +} + +void shutdown() +{ + delete stardrawer; + stardrawer = 0; + delete shipdrawer; + shipdrawer = 0; +} + +void reset() { + // Change to the projection matrix and set our viewing volume. + gl::matrixmode( GL_PROJECTION ); + gl::loadidentity(); + + //glu::perspective( 64.0, video::ratio, 1.0, 1024.0 ); + const float frustumsize=0.5f; + gl::frustum( -frustumsize * video::ratio, frustumsize * video::ratio, -frustumsize, frustumsize, 1.0, 1024.0); + /* + map world coordinates to GL coordinates + + The world coordinates are identical to GL coordinates, + but the default viewing pitch (0 degrees) + is the positive X-axis + */ + gl::rotate(90.0f, 0, 1.0, 0); +} + +void draw_background() +{ + using namespace gl; + + begin(Lines); + color(0.9f, 0.5f, 0.0f); + vertex(-2,1,0); + color(1.0f, 1.0f, 0.0f); + vertex(2,1,0); + + vertex(0,1,-0.5); + vertex(0,1,0.5); + + vertex(0,2.0f,0); + vertex(0,-1, 0); + end(); + + int gridsize = 32; + float s = 1.0f / gridsize; + float y = -4.0f; + + float dx = target->location.x - floorf(target->location.x); + float dz = target->location.z - floorf(target->location.z); + + color(0,0, 1.0f); + begin(Lines); + for (int i=-gridsize; i <= gridsize; i++) { + color(0,0, 0); + vertex(i-dx, y, -gridsize-dz); + color(0,0, (gridsize-abs(i))*s); + vertex(i-dx, y, -dz); + vertex(i-dx, y, -dz); + color(0,0, 0); + vertex(i-dx, y, gridsize-dz); + + vertex(-gridsize-dx, y, i-dz); + color(0,0, (gridsize-abs(i))*s); + vertex(-dx, y, i-dz); + vertex(-dx, y, i-dz); + color(0,0, 0); + vertex(gridsize-dx, y, i-dz); + } + end(); +} + +void draw_world(float elapsed) +{ + // draw the world + gl::push(); + + //std::cerr << "ship at " << game::ship.location << " translate " << game::ship.location - target->location << std::endl; + gl::translate(game::ship.location - target->location); + gl::scale(GAMESCALE, GAMESCALE, GAMESCALE); + shipdrawer->draw(elapsed); + gl::pop(); + + //std::cerr << "star at " << game::star.location << " translate " << game::star.location - game::ship.location << std::endl; + gl::translate(game::star.location - target->location); + stardrawer->draw(elapsed); + +} + +void draw(float elapsed) +{ + // Clear the color and depth buffers. + gl::clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); + + // We don't want to modify the projection matrix. + gl::matrixmode( GL_MODELVIEW ); + gl::loadidentity(); + + // Camera transformation + camera::draw(elapsed); + + // draw the semi-static background + draw_background(); + + // draw the world + draw_world(elapsed); + + SDL_GL_SwapBuffers( ); +} + +} // namespace view + diff --git a/src/client/view.h b/src/client/view.h new file mode 100644 index 0000000..de73ab9 --- /dev/null +++ b/src/client/view.h @@ -0,0 +1,24 @@ +/* view.h + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_VIEW_H__ +#define __INCLUDED_VIEW_H__ + +#include "common/vector3f.h" + +/// Draws the view of the map +namespace view +{ + void init(); + void shutdown(); + + /// Update the chronometer and draw the game view + void draw(float elapsed); + + /// Reset the projection matrix + void reset(); +}; + +#endif // __INCLUDED_VIEW_H__ -- cgit v1.2.3