1
// Copyright (C) 2009-2011 Gaz Davidson
2
// This file is part of the "Irrlicht Engine".
3
// For conditions of distribution and use, see copyright notice in irrlicht.h
5
#ifndef __C_IRR_DEVICE_CONSOLE_H_INCLUDED__
6
#define __C_IRR_DEVICE_CONSOLE_H_INCLUDED__
8
#include "IrrCompileConfig.h"
9
#ifdef _IRR_COMPILE_WITH_CONSOLE_DEVICE_
11
//#define _IRR_USE_CONSOLE_FONT_
13
#include "SIrrCreationParameters.h"
14
#include "CIrrDeviceStub.h"
15
#include "IImagePresenter.h"
19
#ifdef _IRR_WINDOWS_API_
20
#define WIN32_LEAN_AND_MEAN
21
#if !defined(_IRR_XBOX_PLATFORM_)
24
#if(_WIN32_WINNT >= 0x0500)
25
#define _IRR_WINDOWS_NT_CONSOLE_
31
// for now we assume all other terminal types are VT100
32
#ifndef _IRR_WINDOWS_NT_CONSOLE_
33
#define _IRR_VT100_CONSOLE_
39
class CIrrDeviceConsole : public CIrrDeviceStub, video::IImagePresenter
44
CIrrDeviceConsole(const SIrrlichtCreationParameters& params);
47
virtual ~CIrrDeviceConsole();
49
//! runs the device. Returns false if device wants to be deleted
52
//! Cause the device to temporarily pause execution and let other processes to run
53
// This should bring down processor usage without major performance loss for Irrlicht
56
//! Pause execution and let other processes to run for a specified amount of time.
57
virtual void sleep(u32 timeMs, bool pauseTimer);
59
//! sets the caption of the window
60
virtual void setWindowCaption(const wchar_t* text);
62
//! returns if window is active. if not, nothing need to be drawn
63
virtual bool isWindowActive() const;
65
//! returns if window has focus
66
virtual bool isWindowFocused() const;
68
//! returns if window is minimized
69
virtual bool isWindowMinimized() const;
71
//! presents a surface in the client area
72
virtual bool present(video::IImage* surface, void* windowId=0, core::rect<s32>* src=0);
74
//! notifies the device that it should close itself
75
virtual void closeDevice();
77
//! Sets if the window should be resizable in windowed mode.
78
virtual void setResizable(bool resize=false);
80
//! Minimizes the window.
81
virtual void minimizeWindow();
83
//! Maximizes the window.
84
virtual void maximizeWindow();
86
//! Restores the window size.
87
virtual void restoreWindow();
89
//! Get the device type
90
virtual E_DEVICE_TYPE getType() const
95
void addPostPresentText(s16 X, s16 Y, const wchar_t *text);
97
//! Implementation of the win32 console mouse cursor
98
class CCursorControl : public gui::ICursorControl
102
CCursorControl(const core::dimension2d<u32>& wsize)
103
: WindowSize(wsize), InvWindowSize(0.0f, 0.0f), IsVisible(true), UseReferenceRect(false)
105
if (WindowSize.Width!=0)
106
InvWindowSize.Width = 1.0f / WindowSize.Width;
108
if (WindowSize.Height!=0)
109
InvWindowSize.Height = 1.0f / WindowSize.Height;
112
//! Changes the visible state of the mouse cursor.
113
virtual void setVisible(bool visible)
115
if(visible != IsVisible)
118
setPosition(CursorPos.X, CursorPos.Y);
122
//! Returns if the cursor is currently visible.
123
virtual bool isVisible() const
128
//! Sets the new position of the cursor.
129
virtual void setPosition(const core::position2d<f32> &pos)
131
setPosition(pos.X, pos.Y);
134
//! Sets the new position of the cursor.
135
virtual void setPosition(f32 x, f32 y)
137
if (!UseReferenceRect)
138
setPosition((s32)(x*WindowSize.Width), (s32)(y*WindowSize.Height));
140
setPosition((s32)(x*ReferenceRect.getWidth()), (s32)(y*ReferenceRect.getHeight()));
143
//! Sets the new position of the cursor.
144
virtual void setPosition(const core::position2d<s32> &pos)
146
setPosition(pos.X, pos.Y);
149
//! Sets the new position of the cursor.
150
virtual void setPosition(s32 x, s32 y)
152
setInternalCursorPosition(core::position2di(x,y));
155
//! Returns the current position of the mouse cursor.
156
virtual const core::position2d<s32>& getPosition()
161
//! Returns the current position of the mouse cursor.
162
virtual core::position2d<f32> getRelativePosition()
164
if (!UseReferenceRect)
166
return core::position2d<f32>(CursorPos.X * InvWindowSize.Width,
167
CursorPos.Y * InvWindowSize.Height);
170
return core::position2d<f32>(CursorPos.X / (f32)ReferenceRect.getWidth(),
171
CursorPos.Y / (f32)ReferenceRect.getHeight());
174
//! Sets an absolute reference rect for calculating the cursor position.
175
virtual void setReferenceRect(core::rect<s32>* rect=0)
179
ReferenceRect = *rect;
180
UseReferenceRect = true;
182
// prevent division through zero and uneven sizes
184
if (!ReferenceRect.getHeight() || ReferenceRect.getHeight()%2)
185
ReferenceRect.LowerRightCorner.Y += 1;
187
if (!ReferenceRect.getWidth() || ReferenceRect.getWidth()%2)
188
ReferenceRect.LowerRightCorner.X += 1;
191
UseReferenceRect = false;
195
//! Updates the internal cursor position
196
void setInternalCursorPosition(const core::position2di &pos)
200
if (UseReferenceRect)
201
CursorPos -= ReferenceRect.UpperLeftCorner;
206
core::position2d<s32> CursorPos;
207
core::dimension2d<u32> WindowSize;
208
core::dimension2d<f32> InvWindowSize;
211
core::rect<s32> ReferenceRect;
216
//! Set the position of the text caret
217
void setTextCursorPos(s16 x, s16 y);
219
// text to be added after drawing the screen
220
struct SPostPresentText
222
core::position2d<s16> Pos;
226
bool IsWindowFocused;
228
core::array<core::stringc> OutputBuffer;
229
gui::IGUIFont *ConsoleFont;
230
core::array<SPostPresentText> Text;
234
#ifdef _IRR_WINDOWS_NT_CONSOLE_
235
HANDLE WindowsSTDIn, WindowsSTDOut;
236
u32 MouseButtonStates;
240
#ifdef _IRR_USE_CONSOLE_FONT_
244
class CGUIConsoleFont : public IGUIFont
248
CGUIConsoleFont(CIrrDeviceConsole* device) : Device(device) { }
250
//! Draws some text and clips it to the specified rectangle if wanted.
251
virtual void draw(const wchar_t* text, const core::rect<s32>& position,
252
video::SColor color, bool hcenter=false, bool vcenter=false,
253
const core::rect<s32>* clip=0)
255
core::rect<s32> Area = clip ? *clip : position;
257
if (Area.UpperLeftCorner.X < 0)
258
Area.UpperLeftCorner.X = 0;
260
if (Area.UpperLeftCorner.Y < 0)
261
Area.UpperLeftCorner.Y = 0;
263
core::position2d<s16> pos;
266
pos.Y = vcenter ? (position.UpperLeftCorner.Y + position.LowerRightCorner.Y) / 2 : position.UpperLeftCorner.Y;
268
// nothing to display?
269
if (pos.Y < Area.UpperLeftCorner.Y || pos.Y > Area.LowerRightCorner.Y)
274
// centre horizontally
275
pos.X = hcenter ? position.getCenter().X - ( tempText.size() / 2) : position.UpperLeftCorner.X;
282
if (pos.X + (s32)tempText.size() > Area.LowerRightCorner.X)
283
xrclip = Area.LowerRightCorner.X - pos.X;
286
if (pos.X < Area.UpperLeftCorner.X)
287
xlclip = Area.UpperLeftCorner.X - pos.X;
290
if ((s32)tempText.size() - xlclip - xrclip < 0)
293
// null terminate the string
295
tempText[xrclip] = L'\0';
297
Device->addPostPresentText(pos.X + xlclip, pos.Y, &(tempText.c_str()[xlclip]));
300
//! Calculates the dimension of some text.
301
virtual core::dimension2d<u32> getDimension(const wchar_t* text) const
303
return core::dimension2d<u32>(wcslen(text),1);
306
//! Calculates the index of the character in the text which is on a specific position.
307
virtual s32 getCharacterFromPos(const wchar_t* text, s32 pixel_x) const { return pixel_x; };
310
virtual void setKerningWidth (s32 kerning) { }
311
virtual void setKerningHeight (s32 kerning) { }
312
virtual s32 getKerningWidth(const wchar_t* thisLetter=0, const wchar_t* previousLetter=0) const {return 0;}
313
virtual s32 getKerningHeight() const { return 0;}
314
virtual void setInvisibleCharacters( const wchar_t *s ) { }
315
// I guess this is an OS specific font
316
virtual EGUI_FONT_TYPE getType() const { return EGFT_OS; }
318
CIrrDeviceConsole* Device;
319
core::stringw tempText;
322
} // end namespace gui
324
#endif // _IRR_USE_CONSOLE_FONT_
326
} // end namespace irr
328
#endif // _IRR_COMPILE_WITH_CONSOLE_DEVICE_
329
#endif // __C_IRR_DEVICE_CONSOLE_H_INCLUDED__