/* 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 */ #include "client/client.h" #include "client/camera.h" #include "client/chat.h" #include "client/console.h" #include "client/video.h" #include "client/draw.h" #include "render/render.h" #include "core/core.h" #include "math/mathlib.h" #include "sys/sys.h" #include #include #include #include namespace client { namespace view { float fps = 0; void init() { camera::init(); } void shutdown() { camera::shutdown(); } void reset() { using namespace render; // set clear color gl::clearcolor(0.0f, 0.0f, 0.0f, 1.0f); // shading model: Gouraud (smooth). gl::shademodel(GL_SMOOTH); // culling gl::cullface(GL_BACK); gl::frontface(GL_CCW); // alpha-blending gl::blendfunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); gl::disable(GL_BLEND); // depth gl::depthmask(GL_TRUE); gl::disable(GL_DEPTH_TEST); gl::disable(GL_CULL_FACE); } void draw_loader() { using namespace render; gl::enable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, render::textures[0]); // bitmaps/loader.tga gl::color(1.0f, 1.0f, 1.0f, 1.0f); gl::begin(gl::Quads); glTexCoord2f(0.0f, 0.0f); gl::vertex(0,0, 0); glTexCoord2f(1.0f, 0.0f); gl::vertex(video::width,0,0); glTexCoord2f(1.0f, 1.0f); gl::vertex(video::width,video::height,0); glTexCoord2f(0.0f, 1.0f); gl::vertex(0,video::height,0); gl::end(); gl::disable(GL_TEXTURE_2D); } void draw_status() { using namespace render; if (console::visible()) return; glBindTexture(GL_TEXTURE_2D, render::textures[1]); // bitmaps/conchars.tga gl::enable(GL_TEXTURE_2D); // print the status in the upper left corner gl::color(1.0f, 1.0f, 1.0f, 1.0f); std::stringstream status; int hours = (int) sys::time() / 3600; int minutes = (int)(sys::time() - 3600*hours) / 60; int seconds = (int)(sys::time() - 3600*hours - 60 *minutes); status << " clock " << std::setfill('0') << std::setw(2) << hours << ":" << std::setfill('0') << std::setw(2) << minutes << ":" << std::setfill('0') << std::setw(2) << seconds; minutes = (int) floorf(core::application()->time() / 60.0f); seconds = (int) floorf(core::application()->time() - (float) minutes* 60.0f); status << " time " << std::setfill('0') << std::setw(2) << minutes << ":" << std::setfill('0') << std::setw(2) << seconds; status << " fps " << std::setw(4) << fps; draw_text(CHARWIDTH, 4, status); // print the version number in the upper right corner gl::color(0.0f, 1.0f, 0.0f, 1.0f); std::string version("ver. "); version.append(core::version()); draw_text(video::width-(version.size()+1)*CHARWIDTH, 4, version); // draw notifications gl::color(1.0f, 1.0f, 1.0f, 1.0f); size_t n = console::notify_pos % MAXNOTIFYLINES; float now = sys::time(); int h = 4 + CHARHEIGHT; for (size_t l = 0; l < MAXNOTIFYLINES; l++) { if (console::notify_text[n].size() > 2 && console::notify_time[n] + 5 > now) { if (console::notify_text[n][0] == '?') gl::color(0.7f,0.7f,0.7f, 1.0f); else if (console::notify_text[n][0] == '*') gl::color(1.0f,1.0f,0.0f, 1.0f); else if (console::notify_text[n][0] == '!') gl::color(1.0f,0.0f,0.0f, 1.0f); else gl::color(1.0f,1.0f,1.0f, 1.0f); draw_text(CHARWIDTH, h, console::notify_text[n].substr(2)); h += CHARHEIGHT; } n = (n+1) % MAXNOTIFYLINES; } gl::disable(GL_TEXTURE_2D); } void frame(float seconds) { using namespace render; // calculate frames per second if (seconds > 0.0f) fps = floorf(1.0f / seconds); else fps = 9999; // flush console messages console::flush(); // Clear the color and depth buffers. gl::clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (core::application()->connected()) { // draw the game world // Change to the projection matrix and set our viewing volume. gl::matrixmode(GL_PROJECTION); gl::loadidentity(); const float frustumsize = 0.5f; gl::frustum(-frustumsize*video::aspect, frustumsize*video::aspect, -frustumsize, frustumsize, 1.0f, 1024.0f); gl::matrixmode(GL_MODELVIEW); // map world to screen coordinates gl::loadidentity(); gl::rotate(90.0f, 0, 1.0, 0); camera::draw(seconds); // draw the current camera transformation gl::enable(GL_DEPTH_TEST); // enable depth buffer writing gl::enable(GL_CULL_FACE); // enable culling gl::enable(GL_BLEND); // enable alpha blending draw_world(seconds); // draw the world gl::disable(GL_CULL_FACE); // disable culling gl::disable(GL_DEPTH_TEST); // disable depth buffer writing } // switch to ortographic projection to draw the GUI gl::matrixmode(GL_PROJECTION); gl::loadidentity(); glOrtho(0, video::width, video::height, 0, -1000.0f, 1000.0f); gl::matrixmode(GL_MODELVIEW); gl::loadidentity(); if (!core::application()->connected()) { // draw the loader bitmap draw_loader(); gl::enable(GL_BLEND); // enable alpha blending } // draw the console console::draw(); chat::draw(); // draw the status line draw_status(); gl::disable(GL_BLEND); } } //namespace view } // namespace client