Commit dc498aec authored by bitplane's avatar bitplane

Added a font for the console device, fixed grammar in IGUIFont docs

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@2241 dfc29bdd-3216-0410-991c-e03cc46cb475
parent b15e9e9f
......@@ -39,7 +39,7 @@ class IGUIFont : public virtual IReferenceCounted
{
public:
//! Draws an text and clips it to the specified rectangle if wanted.
//! Draws some text and clips it to the specified rectangle if wanted.
/** \param text: Text to draw
\param position: Rectangle specifying position where to draw the text.
\param color: Color of the text
......@@ -51,7 +51,7 @@ public:
video::SColor color, bool hcenter=false, bool vcenter=false,
const core::rect<s32>* clip=0) = 0;
//! Calculates the dimension of a text.
//! Calculates the width and height of a given string of text.
/** \return Returns width and height of the area covered by the text if
it would be drawn. */
virtual core::dimension2d<u32> getDimension(const wchar_t* text) const = 0;
......
......@@ -7,6 +7,8 @@
#ifdef _IRR_USE_CONSOLE_DEVICE_
#include "os.h"
#include "IGUISkin.h"
#include "IGUIEnvironment.h"
// to close the device on terminate signal
irr::CIrrDeviceConsole *DeviceToClose;
......@@ -62,7 +64,7 @@ const u16 ASCIIArtCharsCount = 32;
//! constructor
CIrrDeviceConsole::CIrrDeviceConsole(const SIrrlichtCreationParameters& params)
: CIrrDeviceStub(params), IsDeviceRunning(true), IsWindowFocused(true)
: CIrrDeviceStub(params), IsDeviceRunning(true), IsWindowFocused(true), ConsoleFont(0)
{
DeviceToClose = this;
......@@ -133,12 +135,34 @@ CIrrDeviceConsole::CIrrDeviceConsole(const SIrrlichtCreationParameters& params)
break;
}
// set up output buffer
for (u32 y=0; y<CreationParams.WindowSize.Height; ++y)
{
core::stringc str;
str.reserve(CreationParams.WindowSize.Width);
for (u32 x=0; x<CreationParams.WindowSize.Width; ++x)
str += " ";
OutputBuffer.push_back(str);
}
#ifdef _IRR_WINDOWS_NT_CONSOLE_
CursorControl = new CCursorControl(CreationParams.WindowSize);
#endif
if (VideoDriver)
{
createGUIAndScene();
#ifdef _IRR_USE_CONSOLE_FONT_
ConsoleFont = new gui::CGUIConsoleFont(this);
gui::IGUISkin *skin = GUIEnvironment->getSkin();
if (skin)
{
for (u32 i=0; i < gui::EGDF_COUNT; ++i)
skin->setFont(ConsoleFont, gui::EGUI_DEFAULT_FONT(i));
}
#endif
}
}
//! destructor
......@@ -150,6 +174,11 @@ CIrrDeviceConsole::~CIrrDeviceConsole()
CursorControl->drop();
CursorControl = 0;
}
if (ConsoleFont)
{
ConsoleFont->drop();
ConsoleFont = 0;
}
#ifdef _IRR_VT100_CONSOLE_
// reset terminal
printf("%cc", 27);
......@@ -263,7 +292,7 @@ bool CIrrDeviceConsole::run()
// set input mode
SetConsoleMode(WindowsSTDIn, oldMode);
#else
// todo: process terminal keyboard input
// todo: keyboard input from terminal in raw mode
#endif
return IsDeviceRunning;
......@@ -334,27 +363,40 @@ bool CIrrDeviceConsole::isWindowMinimized() const
//! presents a surface in the client area
bool CIrrDeviceConsole::present(video::IImage* surface, void* windowId, core::rect<s32>* src)
{
if (surface)
{
OutputLine.reserve(surface->getDimension().Width + 1);
for (u32 y=0; y < surface->getDimension().Height; ++y)
{
setTextCursorPos(0,y);
for (u32 x=0; x< surface->getDimension().Width; ++x)
{
// get average pixel
u32 avg = surface->getPixel(x,y).getAverage() * (ASCIIArtCharsCount-1);
avg /= 255;
OutputLine += ASCIIArtChars[avg];
OutputBuffer[y] [x] = ASCIIArtChars[avg];
}
printf("%s", OutputLine.c_str());
OutputLine = "";
}
}
#ifdef _IRR_USE_CONSOLE_FONT_
for (u32 i=0; i< Text.size(); ++i)
{
s32 y = Text[i].Pos.Y;
return true;
if ( y < (s32)OutputBuffer.size() && y > 0)
for (u32 c=0; c < Text[i].Text.size() && c + Text[i].Pos.X < OutputBuffer[y].size(); ++c)
//if (Text[i].Text[c] != ' ')
OutputBuffer[y] [c+Text[i].Pos.X] = Text[i].Text[c];
}
Text.clear();
#endif
// draw output
for (u32 y=0; y<OutputBuffer.size(); ++y)
{
setTextCursorPos(0,y);
printf("%s", OutputBuffer[y].c_str());
}
return surface != 0;
}
//! notifies the device that it should close itself
......@@ -386,6 +428,15 @@ void CIrrDeviceConsole::setTextCursorPos(s16 x, s16 y)
#endif
}
void CIrrDeviceConsole::addPostPresentText(s16 X, s16 Y, const wchar_t *text)
{
SPostPresentText p;
p.Text = text;
p.Pos.X = X;
p.Pos.Y = Y;
Text.push_back(p);
}
extern "C" IRRLICHT_API IrrlichtDevice* IRRCALLCONV createDeviceEx(
const SIrrlichtCreationParameters& parameters)
{
......
......@@ -9,11 +9,13 @@
#include "IrrCompileConfig.h"
#ifdef _IRR_USE_CONSOLE_DEVICE_
#define _IRR_USE_CONSOLE_FONT_
#include "SIrrCreationParameters.h"
#include "CIrrDeviceStub.h"
#include "IImagePresenter.h"
//#undef _IRR_WINDOWS_API_
// for console font
#include "IGUIFont.h"
#ifdef _IRR_WINDOWS_API_
#define WIN32_LEAN_AND_MEAN
......@@ -34,6 +36,7 @@
namespace irr
{
class CIrrDeviceConsole : public CIrrDeviceStub, video::IImagePresenter
{
public:
......@@ -75,7 +78,9 @@ namespace irr
//! Sets if the window should be resizeable in windowed mode.
virtual void setResizeAble(bool resize=false);
//! Implementation of the win32 cursor control
void addPostPresentText(s16 X, s16 Y, const wchar_t *text);
//! Implementation of the win32 console mouse cursor
class CCursorControl : public gui::ICursorControl
{
public:
......@@ -194,15 +199,22 @@ namespace irr
private:
//! Set the position of the text caret
void setTextCursorPos(s16 x, s16 y);
void setMouseCursorPos(s32 x, s32 y);
core::position2di getMouseCursorPos();
// text to be added after drawing the screen
struct SPostPresentText
{
core::position2d<s16> Pos;
core::stringc Text;
};
bool IsDeviceRunning,
IsWindowFocused;
core::stringc OutputLine;
core::array<core::stringc> OutputBuffer;
gui::IGUIFont *ConsoleFont;
core::array<SPostPresentText> Text;
#ifdef _IRR_WINDOWS_NT_CONSOLE_
HANDLE WindowsSTDIn, WindowsSTDOut;
......@@ -210,6 +222,73 @@ namespace irr
#endif
};
#ifdef _IRR_USE_CONSOLE_FONT_
namespace gui
{
class CGUIConsoleFont : public IGUIFont
{
public:
CGUIConsoleFont(CIrrDeviceConsole* device) : Device(device) { }
//! Draws some text and clips it to the specified rectangle if wanted.
virtual void draw(const wchar_t* text, const core::rect<s32>& position,
video::SColor color, bool hcenter=false, bool vcenter=false,
const core::rect<s32>* clip=0)
{
core::rect<s32> Area = clip ? *clip : position;
core::position2d<s16> pos;
// centre vertically
pos.Y = vcenter ? (Area.UpperLeftCorner.Y + Area.LowerRightCorner.Y) / 2 : Area.UpperLeftCorner.Y;
// nothing to display?
if (pos.Y < Area.UpperLeftCorner.Y || pos.Y > Area.LowerRightCorner.Y)
return;
tempText = text;
// centre horizontally
pos.X = hcenter ? Area.getCenter().X - ( tempText.size() / 2) : Area.UpperLeftCorner.X;
// clip
//if (pos.X < Area.UpperLeftCorner.X)
//{
// nothing to display?
// if (pos.X
//}
// todo: clip, centre
Device->addPostPresentText(pos.X, pos.Y, text);
}
//! Calculates the dimension of some text.
virtual core::dimension2d<u32> getDimension(const wchar_t* text) const
{
return core::dimension2d<u32>(wcslen(text),1);
}
//! Calculates the index of the character in the text which is on a specific position.
virtual s32 getCharacterFromPos(const wchar_t* text, s32 pixel_x) const { return pixel_x; };
//! No kerning
virtual void setKerningWidth (s32 kerning) { }
virtual void setKerningHeight (s32 kerning) { }
virtual s32 getKerningWidth(const wchar_t* thisLetter=0, const wchar_t* previousLetter=0) const {return 0;}
virtual s32 getKerningHeight() const { return 0;}
virtual void setInvisibleCharacters( const wchar_t *s ) { }
// I guess this is an OS specific font
virtual EGUI_FONT_TYPE getType() const { return EGFT_OS; }
private:
CIrrDeviceConsole* Device;
core::stringw tempText;
};
} // end namespace gui
#endif // _IRR_USE_CONSOLE_FONT_
} // end namespace irr
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment