/* * $Id: console.cc,v 1.4 2002/09/19 00:18:43 aeneas Exp $ * Copyright (c) 2002, Dominik Schnitzer * * JFK - JFK Fucking Killerz, a massive multiplayer 2d shoot'em-up game * http://relax.ath.cx/jfk/ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #include "console.h" #include "font.h" #include "clock.h" #include "debug.h" using namespace JFK::client; console::console(output* outp, int maxlines) : fnt_g(outp, "console.png"), fnt_r(outp, "consoler.png"), fnt_y(outp, "consoley.png"), out(outp), plys(0), fpscount(0), fpstime(0), fpsstr(""), lines(maxlines), input_mode(CONSOLE_INPUTDONE), input_text("") { /* Load the console font */ highscore = out->load_image("font", "highscore.png", 0); cl.start(); } console::~console() { out->free_image(highscore); } void console::add_log(string text) { log.push_back(text); log_time.push_back(0); /* Remove messages ignoring the time, if there would be more messages * displayed than allowed (set in constructor */ if (log_time.size() > lines) log_time.pop_front(); /* Keep a log message limit too */ if (log.size() > 400) log.pop_front(); } void console::set_input(string text, console_t where) { input_mode = where; input_text = text; } void console::set_players(vector* players) { plys = *players; } void console::draw() { unsigned long timediff = timer.diff(); unsigned int i_time = 0; /* The i_log variable is the index counter for the log messages (which * are stored longer and have a longer buffer) */ unsigned int i_log = log.size() - log_time.size(); /* Check all log_time fields if the mssage should be shown, or deleted */ while (i_time < log_time.size()) { /* If the logtime of a log item has exceeded, remove it. ATTN: We * always remove the front item, cause it's the oldest item! It should * NEVER HAPPEN, that the front item is NOT the oldest log item... */ log_time[i_time] += timediff; if (log_time[i_time] > DISPLAY_TIME) log_time.pop_front(); else { /* If a log item isn't timed out yet, draw it and increase the * index for the logitems displayed */ fnt_g.draw_text(5, 5 + i_time * fnt_g.get_height(), log[i_log]); i_time++; } i_log++; } /* Draw the Highscore */ sort(plys.begin(), plys.end(), compare_score()); int y = 0; for (size_t i = 0; i < plys.size(); i++) { stringstream sts; person* ply = dynamic_cast (plys[i]); string name(ply->name); name.resize(8); sts << (i + 1) << ". " << name << ":"; out->draw_image(highscore, 530, y); if (i == 0) fnt_r.draw_text(540, y, sts.str()); else fnt_g.draw_text(540, y, sts.str()); sts.flush(); sts.seekp(0); sts << ply->score << "/" << ply->deaths; fnt_g.draw_text(800 - fnt_g.get_textwidth(sts.str()), y, sts.str()); y += fnt_g.get_height() + 1; } /* Draw the FPS */ fpscount++; fpstime += cl.diff(); if ((fpstime >= FPS_TIME) && (fpscount != 0)) { double fps = (fpscount * 1000000) / fpstime; stringstream sts; sts << fps << " fps"; fpsstr = sts.str(); LOG((fpsstr.c_str())); fpscount = 0; fpstime = 0; } fnt_r.draw_text(800 - fnt_g.get_textwidth(fpsstr), y, fpsstr); /* Draw the input area of messages plus text, if available */ if (input_mode != CONSOLE_INPUTDONE) fnt_g.draw_text(5, 485, input_text); }