From d2e93235b9ccd37bf8c8fb7c4376ab1911c83639 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 11 May 2008 15:16:25 +0000 Subject: console font --- src/client/chat.cc | 10 ++++----- src/client/console.cc | 53 +++++++++++++++++++++++--------------------- src/client/view.cc | 60 ++++++++++++++++++++++++++++++-------------------- src/render/text.cc | 43 +++++++++++++++++++++--------------- src/render/text.h | 33 ++++++++++++++++++++------- src/render/textures.cc | 31 ++++++++++++++++++++------ src/render/textures.h | 8 ++++--- 7 files changed, 148 insertions(+), 90 deletions(-) diff --git a/src/client/chat.cc b/src/client/chat.cc index 4d2f759..0d1def6 100644 --- a/src/client/chat.cc +++ b/src/client/chat.cc @@ -74,24 +74,24 @@ void draw() if (console::visible() || !visible()) return; - size_t width = (size_t) (video::width / CHARWIDTH) - 7; + size_t width = (size_t) (video::width / Text::fontwidth()) - 7; size_t draw_pos = 0; while (input_pos - draw_pos > width) draw_pos += 2; // draw the console input gl::color(1.0f, 1.0f, 1.0f, 1.0f); - draw_text(CHARWIDTH , 4 + CHARHEIGHT * (MAXNOTIFYLINES+1), "say"); + Text::draw(4 , 5 + Text::fontheight() * (MAXNOTIFYLINES+1), "say"); gl::color(0.0f, 1.0f, .0f, 1.0f); - draw_text(CHARWIDTH*4 , 4 + CHARHEIGHT * (MAXNOTIFYLINES+1), ":"); + Text::draw(4+Text::fontwidth()*3 , 5 + Text::fontheight() * (MAXNOTIFYLINES+1), ":"); gl::color(1.0f, 1.0f, 1.0f, 1.0f); - draw_text(CHARWIDTH*6, 4 + CHARHEIGHT * (MAXNOTIFYLINES+1), (*history_pos).substr(draw_pos, width)); + Text::draw(4+Text::fontwidth()*5, 5 + Text::fontheight() * (MAXNOTIFYLINES+1), (*history_pos).substr(draw_pos, width)); // draw cursor if ((core::application()->time() - ::floorf(core::application()->time())) < 0.5f) { std::string cursor("_"); - draw_text(CHARWIDTH*(input_pos - draw_pos+6), 4 + CHARHEIGHT * (MAXNOTIFYLINES+1) , cursor); + Text::draw(4+Text::fontwidth()*(input_pos - draw_pos+5), 5 + Text::fontheight() * (MAXNOTIFYLINES+1) , cursor); } } diff --git a/src/client/console.cc b/src/client/console.cc index 3331792..fae402c 100644 --- a/src/client/console.cc +++ b/src/client/console.cc @@ -7,6 +7,7 @@ #include "filesystem/filesystem.h" #include "core/core.h" #include "render/render.h" +#include "render/textures.h" #include "client/console.h" #include "client/video.h" #include "client/keyboard.h" @@ -124,13 +125,13 @@ void draw() return; float con_height = 0.70f; - + // draw version below the bottom of the console gl::color(0.0f, 1.0f, 0.0f, 0.5f); std::string version(core::name()); version += ' '; version.append(core::version()); - draw_text(video::width-CHARWIDTH*(version.size()+1), video::height*con_height-CHARHEIGHT-4, version); + Text::draw(video::width-Text::fontwidth()*(version.size()+1), video::height*con_height-Text::fontheight()-4, version); gl::disable(GL_TEXTURE_2D); // draw the transparent console background @@ -148,15 +149,15 @@ void draw() console_scroll = text.size(); gl::enable(GL_TEXTURE_2D); + + //gl::color(0.7f,0.7f,0.7f, 1.0f); + gl::color(1.0f,1.0f,1.0f, 1.0f); - std::deque::reverse_iterator rit = text.rbegin(); - size_t width = (size_t) (video::width / CHARWIDTH) -2; - float bottom = video::height*con_height-2*CHARHEIGHT-8; - float y = bottom+console_scroll*CHARHEIGHT; - while (y > 0 && rit < text.rend()) { - if (y <= bottom) { - std::string line(*rit); - + size_t height = (size_t) (video::height * con_height / Text::fontheight()) -1; + size_t width = (size_t) (video::width / Text::fontwidth()) -2; + size_t bottom = text.size() - console_scroll; + size_t current_line = 0; +/* if (line[0] == '?') gl::color(0.7f,0.7f,0.7f, 1.0f); else if (line[0] == '*') @@ -165,41 +166,43 @@ void draw() gl::color(1.0f,0.0f,0.0f, 1.0f); else gl::color(1.0f,1.0f,1.0f, 1.0f); - line.erase(0,2); - - std::deque lines; +*/ + std::deque lines; + for (std::deque::iterator it = text.begin(); it != text.end() && current_line < bottom; it++) { + if (current_line >= bottom - height) { + std::string line(*it); + line.erase(0,2); + while (line.size() > width) { lines.push_back(line.substr(0, width)); line.erase(0, width); } if (line.size()) lines.push_back(line); - - - std::deque::reverse_iterator lrit; - for (lrit = lines.rbegin(); (lrit != lines.rend()) && (y > 0); ++lrit) { - draw_text(CHARWIDTH, y, (*lrit)); - y -= CHARHEIGHT; - } - } else { - y -= CHARHEIGHT; } - ++rit; + current_line++; } + float y = video::height*con_height-2*Text::fontheight()-4; + for (std::deque::reverse_iterator rit = lines.rbegin(); (y >= 4) && (rit != lines.rend()); ++rit) { + Text::draw(4, y, (*rit)); + y -= Text::fontheight(); + } + + // draw the console input size_t draw_pos = 0; while (input_pos - draw_pos > width) draw_pos += 2; gl::color(0.0f, 1.0f, 0.0f, 1.0f); - draw_text(CHARWIDTH, video::height*con_height - CHARHEIGHT - 4, (*history_pos).substr(draw_pos, width)); + Text::draw(Text::fontwidth(), video::height*con_height - Text::fontheight() - 4, (*history_pos).substr(draw_pos, width)); // draw cursor if ((core::application()->time() - ::floorf(core::application()->time())) < 0.5f) { std::string cursor("_"); - draw_text(CHARWIDTH*(input_pos-draw_pos+1), video::height*con_height - CHARHEIGHT - 4 , cursor); + Text::draw(Text::fontwidth()*(input_pos-draw_pos+1), video::height*con_height - Text::fontheight() - 4 , cursor); } } diff --git a/src/client/view.cc b/src/client/view.cc index 3576b90..7d11df3 100644 --- a/src/client/view.cc +++ b/src/client/view.cc @@ -142,7 +142,7 @@ void draw_status() int seconds = (int) floorf( core::game()->clientframetime() - (float) minutes* 60.0f); status << "time " << std::setfill('0') << std::setw(2) << minutes << ":" << std::setfill('0') << std::setw(2) << seconds; - draw_text(CHARWIDTH, 4, status); + Text::draw(4, 4, status); } // print stats if desired @@ -155,14 +155,37 @@ void draw_status() } stats << "tx "<< std::setw(5) << (core::Stats::network_bytes_sent >> 10) << "\n"; stats << "rx "<< std::setw(5) << (core::Stats::network_bytes_received >> 10) << "\n"; - draw_text(video::width-CHARWIDTH*12, video::height - CHARHEIGHT*10, stats); + Text::draw(video::width-Text::fontwidth()*12, video::height - Text::fontheight()*8, stats); } + // draw a basic HUD + if (core::localcontrol()) { + gl::color(1.0f,1.0f,1.0f, 1.0f); + + status.str(""); + status << "thrust " << std::setfill(' ') << std::setw(5) << std::fixed + << std::setprecision(2) << core::localcontrol()->thrust() << " "; + + status << "speed " << std::setfill(' ') << std::setw(5) << std::fixed + << std::setprecision(2) << core::localcontrol()->speed(); + + Text::draw(4, video::height - Text::fontheight() -4, status); + } + +} + +void draw_notify() +{ + using namespace render; + + if (console::visible()) + return; + // draw notifications gl::color(1.0f, 1.0f, 1.0f, 1.0f); - size_t width = (size_t) (video::width / CHARWIDTH) -2; + size_t width = (size_t) ((video::width - 8) / Text::fontwidth()); size_t n = console::notify_pos % MAXNOTIFYLINES; - int h = 4 + CHARHEIGHT; + float h = 4 + 2*Text::fontheight(); for (size_t l = 0; l < MAXNOTIFYLINES; l++) { if (console::notify_text[n].size() > 2 && console::notify_time[n] + 4 > core::application()->time()) { std::string line(console::notify_text[n]); @@ -180,32 +203,17 @@ void draw_status() std::deque lines; while (line.size() > width) { - draw_text(CHARWIDTH, h, line.substr(0, width)); + Text::draw(4, h, line.substr(0, width)); line.erase(0, width); - h += CHARHEIGHT; + h += Text::fontheight(); } if (line.size()) { - draw_text(CHARWIDTH, h, line); - h += CHARHEIGHT; + Text::draw(4, h, line); + h += Text::fontheight(); } } n = (n+1) % MAXNOTIFYLINES; } - - // draw a basic HUD - if (core::localcontrol()) { - gl::color(1.0f,1.0f,1.0f, 1.0f); - - status.str(""); - status << "thrust " << std::setfill(' ') << std::setw(5) << std::fixed - << std::setprecision(2) << core::localcontrol()->thrust() << " "; - - status << "speed " << std::setfill(' ') << std::setw(5) << std::fixed - << std::setprecision(2) << core::localcontrol()->speed(); - - draw_text(CHARWIDTH, video::height - CHARHEIGHT -4, status); - } - } void draw_cursor() @@ -301,10 +309,14 @@ void frame(float seconds) } // draw text elements - render::Textures::bind("bitmaps/conchars"); + // FIXME width and height should be derived from texture size + Text::setfont("bitmaps/fonts/console", 12, 18); console::draw(); + draw_notify(); chat::draw(); + + Text::setfont("bitmaps/fonts/gui", 16, 24); draw_status(); // draw the mouse cursor diff --git a/src/render/text.cc b/src/render/text.cc index 768ebf8..668f4ec 100644 --- a/src/render/text.cc +++ b/src/render/text.cc @@ -4,12 +4,23 @@ the terms of the GNU General Public License version 2 */ -#include "render/render.h" +#include "render/text.h" +#include "render/textures.h" #include "sys/sys.h" namespace render { -void draw_text(float x, float y, const char ascii) +float Text::text_fontwidth = 16.0f; +float Text::text_fontheight = 24.0f; + +void Text::setfont(const char *texture, float width, float height) +{ + Textures::bind(texture, false); + text_fontwidth = width; + text_fontheight = height; + +} +void Text::draw(float x, float y, const char ascii) { if (ascii != ' ') { int row = (int) ascii >> 4; @@ -21,49 +32,45 @@ void draw_text(float x, float y, const char ascii) gl::begin(gl::Quads); glTexCoord2f(fcol, frow); - gl::vertex(x,y,0); + gl::vertex(x,y,1); glTexCoord2f(fcol + 0.0625f, frow); - gl::vertex(x+CHARWIDTH,y,0); + gl::vertex(x+text_fontwidth,y,1); glTexCoord2f(fcol +0.0625f, frow + 0.0625f); - gl::vertex(x+CHARWIDTH,y+CHARHEIGHT,0); + gl::vertex(x+text_fontwidth,y+text_fontheight,1); glTexCoord2f(fcol, frow+0.0625f); - gl::vertex(x,y+CHARHEIGHT,0); + gl::vertex(x,y+text_fontheight,1); gl::end(); } } -void draw_text(float x, float y, const char *text) +void Text::draw(float x, float y, const char *text) { const char *c = text; while (*c) { - draw_text(x, y, *c); + draw(x, y, *c); c++; - x += CHARWIDTH; + x += text_fontwidth; } } -void draw_text(float x, float y, std::stringstream & textstream) +void Text::draw(float x, float y, std::stringstream & textstream) { char line[MAXCMDSIZE]; while (textstream.getline(line, MAXCMDSIZE-1)) { - draw_text(x, y, line); - y += CHARHEIGHT; + draw(x, y, line); + y += text_fontheight; } textstream.clear(); } -void draw_text(float x, float y, const std::string & text) +void Text::draw(float x, float y, const std::string & text) { - for (size_t i =0; i < text.size(); i++) { - draw_text(x, y, text[i]); - x += CHARWIDTH; - } + draw(x, y, text.c_str()); } } - diff --git a/src/render/text.h b/src/render/text.h index ac7d104..47d88fd 100644 --- a/src/render/text.h +++ b/src/render/text.h @@ -10,22 +10,39 @@ #include #include -#define CHARWIDTH 16 -#define CHARHEIGHT 24 - namespace render { - /// draw a character - void draw_text(float x, float y, const char ascii); +class Text { +public: /// draw a text string - void draw_text(float x, float y, const std::string & text); + static void draw(float x, float y, const std::string & text); + /// draw a text string - void draw_text(float x, float y, const char *text); + + static void draw(float x, float y, const char *text); /// draw a text stream /** If the stream contains multiple lines, each new line will be * drawn at the same x value. The stream is cleared after reading */ - void draw_text(float x, float y, std::stringstream & textstream); + static void draw(float x, float y, std::stringstream & textstream); + + /// draw a character + static void draw(float x, float y, const char ascii); + + /// set the font + static void setfont(const char *texture, float width, float height); + + /// current font width + static inline float fontwidth() { return text_fontwidth; } + + /// current font height + static inline float fontheight() { return text_fontheight; } + +private: + static float text_fontwidth; + static float text_fontheight; +}; + } #endif //__INCLUDED_RENDER_TEXT_H__ diff --git a/src/render/textures.cc b/src/render/textures.cc index eef5f1d..8dac3a3 100644 --- a/src/render/textures.cc +++ b/src/render/textures.cc @@ -28,8 +28,14 @@ void Textures::init() load("textures/common/notex"); // console characters - if (load("bitmaps/conchars") == 0) { - con_error << "Essential file bitmaps/conchars missing" << std::endl; + if (!load("bitmaps/fonts/console", false)) { + con_error << "Essential file bitmaps/fonts/console missing" << std::endl; + core::application()->shutdown(); + } + + // console characters + if (!load("bitmaps/fonts/gui", false)) { + con_error << "Essential file bitmaps/fonts/gui missing" << std::endl; core::application()->shutdown(); } @@ -59,7 +65,7 @@ void Textures::clear() index = 0; } -size_t Textures::load(std::string name) +size_t Textures::load(std::string name, bool filter) { // check if it is already loaded iterator it = registry.find(name); @@ -97,8 +103,7 @@ size_t Textures::load(std::string name) gluBuild2DMipmaps(GL_TEXTURE_2D, image->channels(), image->width(), image->height(), texture_type, GL_UNSIGNED_BYTE, image->data()); - glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); - glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR); + set_filter(filter); // add to the registry registry[name] = id; @@ -119,7 +124,7 @@ size_t Textures::find(std::string name) return id; } -size_t Textures::bind(std::string name) +size_t Textures::bind(std::string name, bool filter) { size_t id = 0; iterator it = registry.find(name); @@ -129,17 +134,29 @@ size_t Textures::bind(std::string name) id = load(name); glBindTexture(GL_TEXTURE_2D, textures[id]); + set_filter(filter); return id; } -size_t Textures::bind(size_t texture) +size_t Textures::bind(size_t texture, bool filter) { size_t id = texture; if (texture >= index) id = 0; glBindTexture(GL_TEXTURE_2D, textures[id]); + set_filter(filter); return id; } +void Textures::set_filter(bool filter) +{ + if (filter) { + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR); + } else { + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); + } + } } diff --git a/src/render/textures.h b/src/render/textures.h index f8e35b3..12873ef 100644 --- a/src/render/textures.h +++ b/src/render/textures.h @@ -29,15 +29,15 @@ public: /// Load a texture /** Returns 0 on failure, and the texture index on success */ - static size_t load(std::string name); + static size_t load(std::string name, bool filter = true); /// bind a texture for OpenGL usage /** Returns 0 on failure, and the texture index on success */ - static size_t bind(std::string name); + static size_t bind(std::string name, bool filter = true); /// bind a texture for OpenGL usage - static size_t bind(size_t texture); + static size_t bind(size_t texture, bool filter = true); /// find the texture index for a given name static size_t find(std::string name); @@ -45,6 +45,8 @@ public: private: static void clear(); + static void set_filter(bool filter); + typedef std::map::iterator iterator; static std::map registry; -- cgit v1.2.3