From b4973888aeaea2dde6058bc06c3f6631349e7f3c Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 3 Feb 2008 01:43:03 +0000 Subject: command buffer handling engine function parsing buffered client console --- src/client/application.cc | 17 ++++---- src/client/application.h | 3 +- src/client/camera.cc | 9 ++--- src/client/console.cc | 77 +++++++++++++++++++++++++++++++++++-- src/client/console.h | 23 +++++++++++ src/client/input.cc | 19 +++++---- src/client/video.cc | 28 +++++--------- src/client/view.cc | 98 ++++++++++++++++++++++++++++------------------- src/client/view.h | 3 ++ 9 files changed, 193 insertions(+), 84 deletions(-) (limited to 'src/client') diff --git a/src/client/application.cc b/src/client/application.cc index 03dda2f..d3e7f4e 100644 --- a/src/client/application.cc +++ b/src/client/application.cc @@ -20,7 +20,7 @@ namespace client { void Application::quit(int status) { SDL_Quit(); - exit(status); + core::ApplicationInterface::quit(status); } void Application::init() @@ -28,7 +28,7 @@ void Application::init() // initialize core core::ApplicationInterface::init(); - con_debug << "Initializing client..." << std::endl; + con_debug << "Initializing client..." << std::endl; // Initialize the video subsystem video.init(); @@ -56,7 +56,7 @@ void Application::run() float elapsed = (float) ( current - chrono) / 1000.0f; chrono = current; - frame(elapsed); + core::ApplicationInterface::frame(elapsed); // update the video chronometers and draw video.draw(elapsed); @@ -70,15 +70,18 @@ void Application::run() void Application::shutdown() { con_debug << "Shutting down client..." << std::endl; - - input.shutdown(); + console.flush(); + input.shutdown(); + console.flush(); + video.shutdown(); + console.flush(); core::ApplicationInterface::shutdown(); + console.flush(); - quit(0); - + quit(0); } } diff --git a/src/client/application.h b/src/client/application.h index 571e5af..40da1da 100644 --- a/src/client/application.h +++ b/src/client/application.h @@ -23,9 +23,8 @@ public: /// shutdown the client Application virtual void shutdown(); -protected: /// quit the client Application - void quit(int result); + virtual void quit(int status); }; } diff --git a/src/client/camera.cc b/src/client/camera.cc index 5355a26..a7c3349 100644 --- a/src/client/camera.cc +++ b/src/client/camera.cc @@ -4,8 +4,7 @@ the terms and conditions of the GNU General Public License version 2 */ -#include "client/camera.h" -#include "game/game.h" +#include "client/client.h" #include "math/mathlib.h" using math::degrees360f; @@ -40,7 +39,7 @@ Camera::~Camera() void Camera::draw(float elapsed) { if (mode == Track) { - yaw_target = game::ship.yaw(); + yaw_target = game.ship.yaw(); } // adjust yaw @@ -107,7 +106,7 @@ void Camera::nextmode() { 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; @@ -116,7 +115,7 @@ void Camera::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/console.cc b/src/client/console.cc index 6a3dfaa..50093f9 100644 --- a/src/client/console.cc +++ b/src/client/console.cc @@ -4,24 +4,93 @@ the terms and conditions of the GNU General Public License version 2 */ +#include "client/client.h" #include "client/console.h" +#include "core/core.h" +#include "render/render.h" + +#include namespace client { +Console::Console() { + visible = false; +} + std::ostream & Console::messagestream() { - return (std::cout << ". "); + return (buffer << ". "); } std::ostream & Console::warningstream() { - return (std::cout << "! "); + return (buffer << "* "); +} + +std::ostream & Console::errorstream() +{ + return (buffer << "! "); } std::ostream & Console::debugstream() { - return (std::cout << "? "); + return (buffer << "? "); } -} // namespace client +void Console::draw() +{ + using namespace render; + + flush(); + + float height; + if (core::game()) { + if (!core::game()->ready()) + height = 0.6f; + else if (visible) + height = 0.6f; + else + return; + } else + height = 1.0f; + + // console background rectangle + gl::enable(GL_BLEND); + gl::begin(gl::Quads); + gl::color(1, 1, 1, .1); + gl::vertex(-0.5f*video.ratio , 0.5, -1.0f); + gl::vertex(0.5f*video.ratio ,0.5, -1.0f); + gl::vertex(0.5f*video.ratio , 0.5-height, -1.0f); + gl::vertex(-0.5f*video.ratio , 0.5-height, -1.0f); + gl::end(); + gl::disable(GL_BLEND); +} + +void Console::flush() +{ + char line[MAXCMDSIZE]; + while(this->buffer.getline(line, MAXCMDSIZE-1)) { + + while (text.size() >= 32765 - MAXCMDSIZE) { + size_t i = 0; + while (i+1 < text.size() && text[i] != '\n') + i++; + text.erase(0, i+1); + } + + text.append(line); + text.append("\n"); + + std::cout << line << std::endl; + } + + buffer.clear(); +} + +void Console::toggle() +{ + visible = !visible; +} + +} // namespace client diff --git a/src/client/console.h b/src/client/console.h index 1a36d00..ee3b85e 100644 --- a/src/client/console.h +++ b/src/client/console.h @@ -8,21 +8,44 @@ #define __INCLUDED_CLIENT_CONSOLE_H__ #include "sys/consoleinterface.h" +#include namespace client { /// client console implementation class Console : public sys::ConsoleInterface { public: + Console(); + /// stream to send normal messages too virtual std::ostream & messagestream(); /// stream to send warning messages too virtual std::ostream & warningstream(); + /// stream to send error messages too + virtual std::ostream & errorstream(); + /// stream to send debug messages too virtual std::ostream & debugstream(); + + /// flush buffer + void flush(); + + /// draw the console + void draw(); + + /// toggle the console on or off + void toggle(); + + bool visible; + +protected: + /// console text buffer + std::stringstream buffer; + /// console text data + std::string text; }; } diff --git a/src/client/input.cc b/src/client/input.cc index 3e37a19..d30a97e 100644 --- a/src/client/input.cc +++ b/src/client/input.cc @@ -5,8 +5,7 @@ */ //project headers -#include "client.h" -#include "game/game.h" +#include "client/client.h" namespace client { @@ -33,6 +32,10 @@ http://docs.mandragor.org/files/Common_libs_documentation/SDL/SDL_Documentation_ void Input::handle_keyreleased(SDL_keysym* keysym) { switch( keysym->sym ) { + case '`': + case '~': + console.toggle(); + break; case SDLK_SPACE: camera.nextmode(); break; @@ -46,7 +49,7 @@ void Input::handle_keypressed(SDL_keysym* keysym) { switch( keysym->sym ) { case SDLK_ESCAPE: - application.shutdown(); + client::application.shutdown(); break; case SDLK_LEFT: camera.rotate_left(); @@ -61,16 +64,16 @@ void Input::handle_keypressed(SDL_keysym* keysym) camera.rotate_down(); break; case SDLK_KP_PLUS: - game::ship.set_thrust(game::ship.thrust() + 0.08f); + game.ship.set_thrust(game.ship.thrust() + 0.08f); break; case SDLK_KP_MINUS: - game::ship.set_thrust(game::ship.thrust() - 0.1f); + game.ship.set_thrust(game.ship.thrust() - 0.1f); break; case SDLK_KP4: - game::ship.set_yaw(game::ship.yaw() + 10); + game.ship.set_yaw(game.ship.yaw() + 10); break; case SDLK_KP6: - game::ship.set_yaw(game::ship.yaw() - 10); + game.ship.set_yaw(game.ship.yaw() - 10); break; default: break; @@ -91,7 +94,7 @@ void Input::process() handle_keyreleased( &event.key.keysym ); break; case SDL_QUIT: - application.shutdown(); + client::application.shutdown(); break; } diff --git a/src/client/video.cc b/src/client/video.cc index 5772961..4018834 100644 --- a/src/client/video.cc +++ b/src/client/video.cc @@ -30,20 +30,7 @@ void Video::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 ); - - // Depth buffer writing - gl::depthmask(GL_TRUE); - gl::enable(GL_DEPTH_TEST); - - // Alpha blending - gl::blendfunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Set the clear color gl::clearcolor( 0, 0, 0, 0 ); @@ -64,18 +51,18 @@ void Video::init() int flags = 0; if( SDL_Init(SDL_INIT_VIDEO) < 0 ) { - con_warn << "SDL_Init() failed: " << SDL_GetError() << std::endl; + std::cerr << "SDL_Init() failed: " << SDL_GetError() << std::endl; return; } const SDL_VideoInfo* sdl_videoinfo = SDL_GetVideoInfo(); if( !sdl_videoinfo) { - con_warn << "SDL_GetVideoInfo() failed: " << SDL_GetError() << std::endl; + std::cerr << "SDL_GetVideoInfo() failed: " << SDL_GetError() << std::endl; return; } - width = 800; - height = 600; + width = 1024; + height = 768; bpp = sdl_videoinfo->vfmt->BitsPerPixel; SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 ); @@ -88,7 +75,7 @@ void Video::init() flags = SDL_OPENGL | SDL_FULLSCREEN; if(!SDL_SetVideoMode(width, height, bpp, flags )) { - con_warn << "SDL_SetVideoMode() failed: " << SDL_GetError() << std::endl; + std::cerr << "SDL_SetVideoMode() failed: " << SDL_GetError() << std::endl; return; } @@ -103,7 +90,10 @@ void Video::init() void Video::draw(float elapsed) { - view.draw(elapsed); + if (core::game() && core::game()->ready()) + view.draw(elapsed); + + SDL_GL_SwapBuffers(); } void Video::shutdown() diff --git a/src/client/view.cc b/src/client/view.cc index 8754549..8881ac2 100644 --- a/src/client/view.cc +++ b/src/client/view.cc @@ -22,14 +22,14 @@ namespace client ShipDrawer *shipdrawer = 0; StarDrawer *stardrawer = 0; -game::Ship *target =0; // the view's target +game::Ship *target = 0; // the view's target void View::init() { // draw scene if (!shipdrawer) { - stardrawer = new StarDrawer(&game::star); - shipdrawer = new ShipDrawer(&game::ship); - target = &game::ship; + stardrawer = new StarDrawer(&game.star); + shipdrawer = new ShipDrawer(&game.ship); + target = &game.ship; } } @@ -43,28 +43,47 @@ void View::shutdown() } void View::reset() { - // Change to the projection matrix and set our viewing volume. - gl::matrixmode( GL_PROJECTION ); - gl::loadidentity(); + // Our shading model--Gouraud (smooth). + gl::shademodel(GL_SMOOTH); + + // Culling + gl::cullface( GL_BACK ); + gl::frontface(GL_CCW ); +} + + +void View::draw_world(float elapsed) +{ + + gl::enable( GL_CULL_FACE ); + + // Depth buffer writing + gl::depthmask(GL_TRUE); + gl::enable(GL_DEPTH_TEST); + + // 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(); + + gl::disable( GL_CULL_FACE ); - //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.0f, 1024.0f); - /* - 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 View::draw_background(float elapsed) { using namespace gl; -// // // enable Alpha blending + // enable Alpha blending + gl::blendfunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); gl::enable(GL_BLEND); // galactic axis @@ -109,23 +128,7 @@ void View::draw_background(float elapsed) end(); gl::disable(GL_BLEND); -} - -void View::draw_world(float elapsed) -{ - // 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(); - + gl::disable(GL_DEPTH_TEST); } void View::draw(float elapsed) @@ -133,10 +136,20 @@ void View::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 ); + // 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.0f, 1024.0f); + + + gl::matrixmode( GL_MODELVIEW ); + + gl::loadidentity(); + gl::rotate(90.0f, 0, 1.0, 0); + // Camera transformation camera.draw(elapsed); @@ -146,7 +159,14 @@ void View::draw(float elapsed) // draw the semi-static background draw_background(elapsed); - SDL_GL_SwapBuffers(); + // draw the console + //gl::matrixmode( GL_PROJECTION ); + //gl::loadidentity(); + + gl::matrixmode( GL_MODELVIEW ); + gl::loadidentity(); + + console.draw(); } } // namespace view diff --git a/src/client/view.h b/src/client/view.h index fa62d98..e0a613f 100644 --- a/src/client/view.h +++ b/src/client/view.h @@ -24,6 +24,9 @@ public: /// reset the projection matrix void reset(); + float width; + float height; + protected: /// draw the world void draw_world(float elapsed); -- cgit v1.2.3