From 48aa068b036f565d6b94d4207242066ba655afe4 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 9 Feb 2008 17:48:16 +0000 Subject: entities, step 1 --- src/client/Makefile.am | 8 +-- src/client/camera.cc | 22 ++++---- src/client/camera.h | 15 ++++-- src/client/client.cc | 4 +- src/client/console.cc | 13 +++-- src/client/draw.cc | 128 +++++++++++++++++++++++++++++++++++++++++++++++ src/client/draw.h | 21 ++++++++ src/client/input.cc | 42 +++++++++------- src/client/shipdrawer.cc | 113 ----------------------------------------- src/client/shipdrawer.h | 32 ------------ src/client/stardrawer.cc | 30 ----------- src/client/stardrawer.h | 30 ----------- src/client/view.cc | 45 +++-------------- 13 files changed, 218 insertions(+), 285 deletions(-) create mode 100644 src/client/draw.cc create mode 100644 src/client/draw.h delete mode 100644 src/client/shipdrawer.cc delete mode 100644 src/client/shipdrawer.h delete mode 100644 src/client/stardrawer.cc delete mode 100644 src/client/stardrawer.h (limited to 'src/client') diff --git a/src/client/Makefile.am b/src/client/Makefile.am index 1550238..74dc743 100644 --- a/src/client/Makefile.am +++ b/src/client/Makefile.am @@ -1,13 +1,13 @@ METASOURCES = AUTO INCLUDES = -I$(top_srcdir)/src -libclient_la_SOURCES = camera.cc client.cc console.cc hud.cc input.cc \ - keyboard.cc shipdrawer.cc stardrawer.cc video.cc view.cc +libclient_la_SOURCES = camera.cc client.cc console.cc draw.cc hud.cc input.cc \ + keyboard.cc video.cc view.cc libclient_la_CFLAGS = $(LIBSDL_CFLAGS) $(GL_CFLAGS) libclient_la_LDFLAGS = -avoid-version -no-undefined $(GL_LIBS) $(LIBSDL_LIBS) noinst_LTLIBRARIES = libclient.la -noinst_HEADERS = camera.h client.h console.h input.h keyboard.h shipdrawer.h \ - stardrawer.h video.h view.h +noinst_HEADERS = camera.h client.h console.h draw.h input.h keyboard.h video.h \ + view.h libclient_la_LIBADD = $(top_builddir)/src/render/librender.la \ $(top_builddir)/src/core/libcore.la $(top_builddir)/src/filesystem/libfilesystem.la \ $(top_builddir)/src/game/libgame.la $(top_builddir)/src/math/libmath.la $(top_builddir)/src/sys/libsys.la diff --git a/src/client/camera.cc b/src/client/camera.cc index c1ceaa9..45cd65a 100644 --- a/src/client/camera.cc +++ b/src/client/camera.cc @@ -22,8 +22,9 @@ namespace camera // public variables -// camera target +// gameworld coordinates of the camera target math::Vector3f target; + // target yaw, angle in XZ plane, positive is looking left float yaw_target; // target pitch, angle in XZ plane, positive is looking left @@ -71,7 +72,7 @@ void draw(float elapsed) { // TODO camera needs to get this from selected core entity if (mode == Track) { - yaw_target = game.ship.yaw(); + yaw_target = game.ship->yaw(); } // adjust yaw @@ -87,37 +88,40 @@ void draw(float elapsed) gl::translate(1.0f+distance, -distance/2, 0.0f); gl::rotate(-pitch_current, 0, 0, 1.0f); gl::rotate(-yaw_current, 0, 1.0f, 0); + gl::translate(-1*target); break; case Track: gl::translate(1.0f+distance, -distance , 0.0f); gl::rotate(-pitch_current, 0, 0, 1.0f); gl::rotate(-yaw_current, 0, 1.0f, 0); + gl::translate(-1*target); break; case Overview: gl::translate(-distance/2, -distance, 0.0f); gl::rotate(-pitch_current, 0, 0, 1.0f); gl::rotate(-yaw_current, 0, 1.0f, 0); + gl::translate(-1*target); break; default: break; } } -void rotate_right() +void key_right() { if (mode == Free || mode == Overview) { yaw_target = degrees360f( yaw_target + offset_inc); } } -void rotate_left() +void key_left() { if (mode == Free || mode == Overview) { yaw_target = degrees360f( yaw_target - offset_inc); } } -void rotate_up() +void key_up() { if (mode == Free) { pitch_target = pitch_target - offset_inc; @@ -125,7 +129,7 @@ void rotate_up() } } -void rotate_down() +void key_down() { if (mode == Free) { pitch_target = pitch_target + offset_inc; @@ -133,12 +137,12 @@ void rotate_down() } } -void nextmode() { +void next_mode() { switch(mode) { case Overview: // switch camera to Track mode mode = Track; - yaw_target = game.ship.yaw(); + yaw_target = game.ship->yaw(); yaw_current = yaw_target; pitch_target = pitch_track; pitch_current = pitch_target; @@ -147,7 +151,7 @@ void nextmode() { case Track: // switch camera to Free mode mode = Free; - yaw_target = game.ship.yaw(); + yaw_target = game.ship->yaw(); yaw_current = yaw_target; pitch_target = pitch_track; pitch_current = pitch_target; diff --git a/src/client/camera.h b/src/client/camera.h index b1245c8..93b44d1 100644 --- a/src/client/camera.h +++ b/src/client/camera.h @@ -7,6 +7,8 @@ #ifndef __INCLUDED_CLIENT_CAMERA_H__ #define __INCLUDED_CLIENT_CAMERA_H__ +#include "math/mathlib.h" + namespace client { /// camera functions @@ -25,19 +27,22 @@ namespace camera void draw(float elapsed); /// rotate the camera left - void rotate_left(); + void key_left(); /// rotate the camera right - void rotate_right(); + void key_right(); /// rotate the camera up - void rotate_up(); + void key_up(); /// rotate the camera down - void rotate_down(); + void key_down(); /// switch to next camera mode - void nextmode(); + void next_mode(); + + /// gameworld coordinates of the camera target + extern math::Vector3f target; } // namespace camera diff --git a/src/client/client.cc b/src/client/client.cc index 8970dea..faf9ac3 100644 --- a/src/client/client.cc +++ b/src/client/client.cc @@ -46,7 +46,7 @@ Client app; //--- engine functions -------------------------------------------- -extern "C" void func_r_restart(std::stringstream &args) +void func_r_restart(std::stringstream &args) { video::shutdown(); console::flush(); @@ -83,7 +83,9 @@ void Client::quit(int status) void Client::init() { con_print << "Initializing client..." << std::endl; + // initialize core + core::localstate.name = "Client"; core::Application::init(); // initialize SDL, but do not initialize any subsystems diff --git a/src/client/console.cc b/src/client/console.cc index 01f90da..1c2ab1e 100644 --- a/src/client/console.cc +++ b/src/client/console.cc @@ -58,9 +58,13 @@ size_t console_scroll = 0; //--- engine functions -------------------------------------------- -extern "C" void func_con_toggle(std::stringstream &args) +void func_con_toggle(std::stringstream &args) { - console_visible = !console_visible; + int i; + if (args >> i) { + if (i) console_visible = true; else console_visible = false; + } else + console_visible = !console_visible; } //--- public ------------------------------------------------------ @@ -215,6 +219,7 @@ void keypressed(const SDL_keysym &keysym) } core::cmd << (*history_pos) << std::endl; + (*history.rbegin()) = (*history_pos); history.push_back(""); history_pos = history.rbegin(); @@ -242,7 +247,7 @@ void keypressed(const SDL_keysym &keysym) input_pos = 0; break; case SDLK_KP1: - case SDLK_END: + case SDLK_END: input_pos = (*history_pos).size(); break; case SDLK_KP4: @@ -256,7 +261,7 @@ void keypressed(const SDL_keysym &keysym) input_pos++; break; case SDLK_BACKSPACE: - if ((*history_pos).size() && input_pos) { + if ((*history.rbegin()).size() && input_pos) { (*history_pos).erase(input_pos-1, 1); input_pos--; } diff --git a/src/client/draw.cc b/src/client/draw.cc new file mode 100644 index 0000000..f907267 --- /dev/null +++ b/src/client/draw.cc @@ -0,0 +1,128 @@ +/* + draw.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 +*/ + +// projet headers +#include "render/render.h" +#include "render/sphere.h" +#include "render/box.h" +#include "client/client.h" +#include "client/draw.h" + +namespace client +{ + +render::Sphere sphere(math::Vector3f(0,0,0),1); + +void draw_star(game::Star *star, float elapsed) +{ + render::gl::color(star->color); + sphere.radius = star->radius; + sphere.draw(); +} + +math::Vector3f v0(1.0f, -1.0f, -1.0f); +math::Vector3f v1(1.0f, 1.0f, -1.0f); +math::Vector3f v2(1.0f, 1.0f, 1.0f); +math::Vector3f v3(1.0f, -1.0f, 1.0f); + +math::Vector3f v4(-1.0f, -1.0f, -1.0f); +math::Vector3f v5(-1.0f, 1.0f, -1.0f); +math::Vector3f v6(-1.0f, 1.0f, 1.0f); +math::Vector3f v7(-1.0f, -1.0f, 1.0f); +float angle = 0; + +void draw_ship(game::Ship *ship, float elapsed) +{ + using math::Vector3f; + using math::Color; + using namespace render; + + gl::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 ) { + gl::color(1.0f,0 ,0 ); + gl::begin(gl::Lines); + gl::vertex(-0.5f, 0, 0.185); + gl::vertex(-0.5f-0.25f*ship->thrust(), 0, 0.185); + + gl::vertex(-0.5f, 0, -0.185f); + gl::vertex(-0.5f-0.25f*ship->thrust(), 0, -0.185f); + gl::end(); + } + + // shield rotation + gl::rotate(angle, 0.0f, 1.0f, 0.0f ); + angle += 180.0f * elapsed; + if( angle > 360.0f ) { + angle -= 360.0f; + } + + // draw the shield + gl::color(Color(0.0f, 1.0f ,0.0f , 0.5f)); + + gl::begin(gl::LineStrip); + gl::vertex(v0); + gl::vertex(v1); + gl::vertex(v2); + gl::vertex(v3); + gl::vertex(v0); + gl::end(); + + gl::begin(gl::LineStrip); + gl::vertex(v4); + gl::vertex(v5); + gl::vertex(v6); + gl::vertex(v7); + gl::vertex(v4); + gl::end(); +} + +void draw_world(float elapsed) +{ + using namespace render; + + // draw the ship + gl::push(); + gl::translate(game.ship->location); + gl::scale(0.2f, 0.2f, 0.2f); + draw_ship(game.ship, elapsed); + gl::pop(); + + // draw the star + gl::push(); + gl::translate(game.star->location); + draw_star(game.star, elapsed); + gl::pop(); +} + +} diff --git a/src/client/draw.h b/src/client/draw.h new file mode 100644 index 0000000..d019216 --- /dev/null +++ b/src/client/draw.h @@ -0,0 +1,21 @@ +/* + draw.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 "game/game.h" + +namespace client +{ + +/// draw a star +void draw_star(game::Star *star, float elapsed); + +/// draw a ship +void draw_ship(game::Ship *ship, float elapsed); + +/// draw the world +void draw_world(float elapsed); + +} diff --git a/src/client/input.cc b/src/client/input.cc index ccfd998..d3b2900 100644 --- a/src/client/input.cc +++ b/src/client/input.cc @@ -30,40 +30,49 @@ void shutdown() con_print << "Shutting down input..." << std::endl; } -// handle pressed keys for the game world -void keypressed(const SDL_keysym &keysym) +// handle key release for the game world +void keyreleased(const SDL_keysym &keysym) { switch( keysym.sym) { case SDLK_SPACE: - camera::nextmode(); + camera::next_mode(); + break; + default: break; + } +} + +// handle key press events for the game world +void keypressed(const SDL_keysym &keysym) +{ + switch( keysym.sym) { case SDLK_LEFT: - camera::rotate_left(); + camera::key_left(); break; case SDLK_RIGHT: - camera::rotate_right(); + camera::key_right(); break; case SDLK_UP: - camera::rotate_up(); + camera::key_up(); break; case SDLK_DOWN: - camera::rotate_down(); + camera::key_down(); break; case SDLK_KP_PLUS: // TODO set core entity params - game.ship.set_thrust(game.ship.thrust() + 0.08f); + game.ship->set_thrust(game.ship->thrust() + 0.08f); break; case SDLK_KP_MINUS: // TODO set core entity params - game.ship.set_thrust(game.ship.thrust() - 0.1f); + game.ship->set_thrust(game.ship->thrust() - 0.1f); break; case SDLK_KP4: // TODO set core entity params - game.ship.set_yaw(game.ship.yaw() + 10); + game.ship->set_yaw(game.ship->yaw() + 10); break; case SDLK_KP6: // TODO set core entity params - game.ship.set_yaw(game.ship.yaw() - 10); + game.ship->set_yaw(game.ship->yaw() - 10); break; default: break; @@ -78,10 +87,11 @@ void frame(float seconds) while( SDL_PollEvent( &event ) ) { switch( event.type ) { + case SDL_KEYUP: + if (!console::visible() && core::connected()) + keyreleased(event.key.keysym ); + break; case SDL_KEYDOWN: - /*if (event.key.keysym.sym == SDLK_ESCAPE) { - client::application.shutdown(); - } else */ if (event.key.keysym.sym == '`' || event.key.keysym.sym == '~') { console::toggle(); } else if (console::visible()) { @@ -92,10 +102,6 @@ void frame(float seconds) keypressed(event.key.keysym ); } break; - - case SDL_KEYUP: - break; - case SDL_QUIT: core::application()->shutdown(); break; diff --git a/src/client/shipdrawer.cc b/src/client/shipdrawer.cc deleted file mode 100644 index 074dbd8..0000000 --- a/src/client/shipdrawer.cc +++ /dev/null @@ -1,113 +0,0 @@ -/* - 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 "client/shipdrawer.h" -#include "render/render.h" -#include "render/box.h" - -#include - -namespace client { - -using namespace render; - -using math::Vector3f; -using math::Color; - -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(game::Ship *s) -{ - angle = 0; - ship = s; -} - -ShipDrawer::~ShipDrawer() -{ -} - -void ShipDrawer::draw(float elapsed) -{ - gl::push(); - - gl::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 ) { - gl::color(1.0f,0 ,0 ); - gl::begin(gl::Lines); - gl::vertex(-0.5f, 0, 0.185); - gl::vertex(-0.5f-0.25f*ship->thrust(), 0, 0.185); - - gl::vertex(-0.5f, 0, -0.185f); - gl::vertex(-0.5f-0.25f*ship->thrust(), 0, -0.185f); - gl::end(); - } - - // shield rotation - gl::rotate(angle, 0.0f, 1.0f, 0.0f ); - angle += 180.0f * elapsed; - if( angle > 360.0f ) { - angle -= 360.0f; - } - - // draw the shield - gl::color(Color(0.0f, 1.0f ,0.0f , 0.5f)); - - gl::begin(gl::LineStrip); - gl::vertex(v0); - gl::vertex(v1); - gl::vertex(v2); - gl::vertex(v3); - gl::vertex(v0); - gl::end(); - - gl::begin(gl::LineStrip); - gl::vertex(v4); - gl::vertex(v5); - gl::vertex(v6); - gl::vertex(v7); - gl::vertex(v4); - gl::end(); - - gl::pop(); -} - -} diff --git a/src/client/shipdrawer.h b/src/client/shipdrawer.h deleted file mode 100644 index 3ad7208..0000000 --- a/src/client/shipdrawer.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - 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" - -namespace client { - -class ShipDrawer -{ -public: - ShipDrawer(game::Ship *s); - ~ShipDrawer(); - - /// update the model state - void draw(float elapsed); - -private: - game::Ship *ship; - float angle; -}; - -} // namespace client - -#endif // __INCLUDED_SHIPDRAWER_H__ - diff --git a/src/client/stardrawer.cc b/src/client/stardrawer.cc deleted file mode 100644 index 534c1e6..0000000 --- a/src/client/stardrawer.cc +++ /dev/null @@ -1,30 +0,0 @@ -/* 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 -*/ - -// projet headers -#include "client/stardrawer.h" -#include "render/render.h" - -namespace client { - -using namespace render; - -StarDrawer::StarDrawer(game::Star *s) { - star = s; - sphere.radius = s->radius; - -} -StarDrawer::~StarDrawer() { - star = 0; -} - -void StarDrawer::draw(float elapsed) -{ - gl::color(star->color); - sphere.draw(); -} - -} // namespace client - diff --git a/src/client/stardrawer.h b/src/client/stardrawer.h deleted file mode 100644 index dbfd91a..0000000 --- a/src/client/stardrawer.h +++ /dev/null @@ -1,30 +0,0 @@ -/* 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 "render/sphere.h" -#include "game/star.h" - -namespace client { - -/// Class to draw stars -class StarDrawer -{ -public: - StarDrawer(game::Star *s); - ~StarDrawer(); - - void draw(float elapsed); - -private: - game::Star *star; - render::Sphere sphere; -}; - -} // namespace client - -#endif // __INCLUDED_STARDRAWER_H__ diff --git a/src/client/view.cc b/src/client/view.cc index 07e7cff..4627d26 100644 --- a/src/client/view.cc +++ b/src/client/view.cc @@ -7,9 +7,8 @@ #include "client/client.h" #include "client/camera.h" #include "client/console.h" -#include "client/shipdrawer.h" -#include "client/stardrawer.h" #include "client/video.h" +#include "client/draw.h" #include "render/render.h" #include "game/game.h" #include "sys/sys.h" @@ -26,31 +25,16 @@ namespace client namespace view { -ShipDrawer *shipdrawer = 0; -StarDrawer *stardrawer = 0; -game::Ship *target = 0; // the view's target - float fps = 0; void init() { - if (!shipdrawer) { - stardrawer = new StarDrawer(&game.star); - shipdrawer = new ShipDrawer(&game.ship); - target = &game.ship; - } - camera::init(); } void shutdown() { camera::shutdown(); - - delete stardrawer; - stardrawer = 0; - delete shipdrawer; - shipdrawer = 0; } void reset() @@ -77,26 +61,6 @@ void reset() gl::disable(GL_CULL_FACE); } - -void draw_world(float elapsed) -{ - using namespace render; - - // draw the world - gl::push(); - - gl::translate(game.ship.location - target->location); - gl::scale(0.2f, 0.2f, 0.2f); - shipdrawer->draw(elapsed); - gl::pop(); - - gl::push(); - gl::translate(game.star.location - target->location); - stardrawer->draw(elapsed); - gl::pop(); - -} - void draw_background(float elapsed) { using namespace render::gl; @@ -115,12 +79,14 @@ void draw_background(float elapsed) vertex(0,-1, 0); end(); + translate(camera::target); + 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); + float dx = camera::target.x - floorf(camera::target.x); + float dz = camera::target.z - floorf(camera::target.z); color(0,0, 1.0f); begin(Lines); @@ -233,6 +199,7 @@ void frame(float seconds) gl::loadidentity(); gl::rotate(90.0f, 0, 1.0, 0); + camera::target = game.ship->location; camera::draw(seconds); // draw the current camera transformation gl::enable(GL_DEPTH_TEST); // enable depth buffer writing -- cgit v1.2.3