(360) xdk360_video_console no longer class-based

This commit is contained in:
TwinAphex51224 2012-02-16 18:19:22 +01:00
parent 8ec0cb4f6c
commit 356d565ce6
4 changed files with 116 additions and 150 deletions

View File

@ -72,7 +72,6 @@ static bool g_quitting;
static bool g_first_msg; static bool g_first_msg;
unsigned g_frame_count; unsigned g_frame_count;
void *g_d3d; void *g_d3d;
Console g_screen_console;
static void xdk360_gfx_free(void * data) static void xdk360_gfx_free(void * data)
{ {
@ -284,12 +283,12 @@ static bool xdk360_gfx_frame(void *data, const void *frame,
{ {
if(IS_TIMER_EXPIRED() || g_first_msg) if(IS_TIMER_EXPIRED() || g_first_msg)
{ {
g_screen_console.Format(msg); xdk360_console_format(msg);
g_first_msg = 0; g_first_msg = 0;
SET_TIMER_EXPIRATION(30); SET_TIMER_EXPIRATION(30);
} }
g_screen_console.Render(); xdk360_console_draw();
} }
if(!vid->block_swap) if(!vid->block_swap)
@ -364,7 +363,7 @@ void xdk360_video_init(void)
g_first_msg = true; g_first_msg = true;
HRESULT hr = g_screen_console.Create("game:\\media\\Arial_12.xpr", HRESULT hr = xdk360_console_init("game:\\media\\Arial_12.xpr",
0xff000000, 0xffffffff ); 0xff000000, 0xffffffff );
if(FAILED(hr)) if(FAILED(hr))
{ {
@ -376,6 +375,7 @@ void xdk360_video_deinit(void)
{ {
void *data = g_d3d; void *data = g_d3d;
g_d3d = NULL; g_d3d = NULL;
xdk360_console_deinit();
xdk360_gfx_free(data); xdk360_gfx_free(data);
} }

View File

@ -58,7 +58,6 @@ void xdk360_video_init(void);
void xdk360_video_deinit(void); void xdk360_video_deinit(void);
void xdk360_video_set_vsync(bool vsync); void xdk360_video_set_vsync(bool vsync);
extern Console g_screen_console;
extern unsigned g_frame_count; extern unsigned g_frame_count;
extern void *g_d3d; extern void *g_d3d;

View File

@ -23,34 +23,56 @@
#include "xdk360_video_debugfonts.h" #include "xdk360_video_debugfonts.h"
#include "../general.h" #include "../general.h"
Console::Console() static video_console_t video_console;
static XdkFont m_Font;
void xdk360_console_draw(void)
{ {
first_message = true; xdk360_video_t *vid = (xdk360_video_t*)g_d3d;
m_Buffer = NULL; D3DDevice *m_pd3dDevice = vid->xdk360_render_device;
m_Lines = NULL;
m_nScrollOffset = 0; // The top line
unsigned int nTextLine = ( video_console.m_nCurLine -
video_console.m_cScreenHeight + video_console.m_cScreenHeightVirtual -
video_console.m_nScrollOffset + 1 )
% video_console.m_cScreenHeightVirtual;
m_Font.Begin();
for( unsigned int nScreenLine = 0; nScreenLine < video_console.m_cScreenHeight; nScreenLine++ )
{
m_Font.DrawText( (float)( video_console.m_cxSafeAreaOffset ),
(float)( video_console.m_cySafeAreaOffset +
video_console.m_fLineHeight * nScreenLine ),
video_console.m_colTextColor,
video_console.m_Lines[nTextLine] );
nTextLine = ( nTextLine + 1 ) % video_console.m_cScreenHeightVirtual;
} }
Console::~Console() m_Font.End();
{
Destroy();
} }
HRESULT Console::Create( LPCSTR strFontFileName, unsigned long colBackColor, HRESULT xdk360_console_init( LPCSTR strFontFileName, unsigned long colBackColor,
unsigned long colTextColor) unsigned long colTextColor)
{ {
xdk360_video_t *vid = (xdk360_video_t*)g_d3d; xdk360_video_t *vid = (xdk360_video_t*)g_d3d;
D3DDevice *m_pd3dDevice = vid->xdk360_render_device; D3DDevice *m_pd3dDevice = vid->xdk360_render_device;
video_console.first_message = true;
video_console.m_Buffer = NULL;
video_console.m_Lines = NULL;
video_console.m_nScrollOffset = 0;
// Calculate the safe area // Calculate the safe area
unsigned int uiSafeAreaPct = vid->video_mode.fIsHiDef ? SAFE_AREA_PCT_HDTV unsigned int uiSafeAreaPct = vid->video_mode.fIsHiDef ? SAFE_AREA_PCT_HDTV
: SAFE_AREA_PCT_4x3; : SAFE_AREA_PCT_4x3;
m_cxSafeArea = ( vid->d3dpp.BackBufferWidth * uiSafeAreaPct ) / 100; video_console.m_cxSafeArea = ( vid->d3dpp.BackBufferWidth * uiSafeAreaPct ) / 100;
m_cySafeArea = ( vid->d3dpp.BackBufferHeight * uiSafeAreaPct ) / 100; video_console.m_cySafeArea = ( vid->d3dpp.BackBufferHeight * uiSafeAreaPct ) / 100;
m_cxSafeAreaOffset = ( vid->d3dpp.BackBufferWidth - m_cxSafeArea ) / 2; video_console.m_cxSafeAreaOffset = ( vid->d3dpp.BackBufferWidth - video_console.m_cxSafeArea ) / 2;
m_cySafeAreaOffset = ( vid->d3dpp.BackBufferHeight - m_cySafeArea ) / 2; video_console.m_cySafeAreaOffset = ( vid->d3dpp.BackBufferHeight - video_console.m_cySafeArea ) / 2;
// Create the font // Create the font
HRESULT hr = m_Font.Create( strFontFileName ); HRESULT hr = m_Font.Create( strFontFileName );
@ -61,115 +83,84 @@ HRESULT Console::Create( LPCSTR strFontFileName, unsigned long colBackColor,
} }
// Save the colors // Save the colors
m_colBackColor = colBackColor; video_console.m_colBackColor = colBackColor;
m_colTextColor = colTextColor; video_console.m_colTextColor = colTextColor;
// Calculate the number of lines on the screen // Calculate the number of lines on the screen
float fCharWidth, fCharHeight; float fCharWidth, fCharHeight;
m_Font.GetTextExtent( L"i", &fCharWidth, &fCharHeight, FALSE ); m_Font.GetTextExtent( L"i", &fCharWidth, &fCharHeight, FALSE );
m_cScreenHeight = (unsigned int)( m_cySafeArea / fCharHeight ); video_console.m_cScreenHeight = (unsigned int)( video_console.m_cySafeArea / fCharHeight );
m_cScreenWidth = (unsigned int)( m_cxSafeArea / fCharWidth ); video_console.m_cScreenWidth = (unsigned int)( video_console.m_cxSafeArea / fCharWidth );
m_cScreenHeightVirtual = m_cScreenHeight; video_console.m_cScreenHeightVirtual = video_console.m_cScreenHeight;
m_fLineHeight = fCharHeight; video_console.m_fLineHeight = fCharHeight;
// Allocate memory to hold the lines // Allocate memory to hold the lines
m_Buffer = new wchar_t[ m_cScreenHeightVirtual * ( m_cScreenWidth + 1 ) ]; video_console.m_Buffer = new wchar_t[ video_console.m_cScreenHeightVirtual * ( video_console.m_cScreenWidth + 1 ) ];
m_Lines = new wchar_t *[ m_cScreenHeightVirtual ]; video_console.m_Lines = new wchar_t *[ video_console.m_cScreenHeightVirtual ];
// Set the line pointers as indexes into the buffer // Set the line pointers as indexes into the buffer
for( unsigned int i = 0; i < m_cScreenHeightVirtual; i++ ) for( unsigned int i = 0; i < video_console.m_cScreenHeightVirtual; i++ )
m_Lines[ i ] = m_Buffer + ( m_cScreenWidth + 1 ) * i; video_console.m_Lines[ i ] = video_console.m_Buffer + ( video_console.m_cScreenWidth + 1 ) * i;
m_nCurLine = 0; video_console.m_nCurLine = 0;
m_cCurLineLength = 0; video_console.m_cCurLineLength = 0;
memset( m_Buffer, 0, m_cScreenHeightVirtual * ( m_cScreenWidth + 1 ) * sizeof( wchar_t ) ); memset( video_console.m_Buffer, 0, video_console.m_cScreenHeightVirtual * ( video_console.m_cScreenWidth + 1 ) * sizeof( wchar_t ) );
Render(); xdk360_console_draw();
return hr; return hr;
} }
//-------------------------------------------------------------------------------------- void xdk360_console_deinit()
// Name: Destroy()
// Desc: Tear everything down
//--------------------------------------------------------------------------------------
void Console::Destroy()
{ {
// Delete the memory we've allocated // Delete the memory we've allocated
if( m_Lines ) if(video_console.m_Lines)
{ {
delete[] m_Lines; delete[] video_console.m_Lines;
m_Lines = NULL; video_console.m_Lines = NULL;
} }
if( m_Buffer ) if(video_console.m_Buffer)
{ {
delete[] m_Buffer; delete[] video_console.m_Buffer;
m_Buffer = NULL; video_console.m_Buffer = NULL;
} }
// Destroy the font // Destroy the font
m_Font.Destroy(); m_Font.Destroy();
} }
void xdk360_console_add( wchar_t wch )
//--------------------------------------------------------------------------------------
// Name: Render()
// Desc: Render the console to the screen
//--------------------------------------------------------------------------------------
void Console::Render (void)
{
xdk360_video_t *vid = (xdk360_video_t*)g_d3d;
D3DDevice *m_pd3dDevice = vid->xdk360_render_device;
// The top line
unsigned int nTextLine = ( m_nCurLine - m_cScreenHeight + m_cScreenHeightVirtual - m_nScrollOffset + 1 )
% m_cScreenHeightVirtual;
m_Font.Begin();
for( unsigned int nScreenLine = 0; nScreenLine < m_cScreenHeight; nScreenLine++ )
{
m_Font.DrawText( (float)( m_cxSafeAreaOffset ),
(float)( m_cySafeAreaOffset + m_fLineHeight * nScreenLine ),
m_colTextColor, m_Lines[nTextLine] );
nTextLine = ( nTextLine + 1 ) % m_cScreenHeightVirtual;
}
m_Font.End();
}
//--------------------------------------------------------------------------------------
// Name: Add( WCHAR )
// Desc: Add a wide character to the current line
//--------------------------------------------------------------------------------------
void Console::Add( wchar_t wch )
{ {
// If this is a newline, just increment lines and move on // If this is a newline, just increment lines and move on
if( wch == L'\n' ) if( wch == L'\n' )
{ {
m_nCurLine = ( m_nCurLine + 1 ) % m_cScreenHeightVirtual; video_console.m_nCurLine = ( video_console.m_nCurLine + 1 )
m_cCurLineLength = 0; % video_console.m_cScreenHeightVirtual;
memset( m_Lines[m_nCurLine], 0, ( m_cScreenWidth + 1 ) * sizeof( wchar_t ) ); video_console.m_cCurLineLength = 0;
memset(video_console.m_Lines[video_console.m_nCurLine], 0,
( video_console.m_cScreenWidth + 1 ) * sizeof( wchar_t ) );
return; return;
} }
int bIncrementLine = FALSE; // Whether to wrap to the next line int bIncrementLine = FALSE; // Whether to wrap to the next line
if( m_cCurLineLength == m_cScreenWidth ) if( video_console.m_cCurLineLength == video_console.m_cScreenWidth )
bIncrementLine = TRUE; bIncrementLine = TRUE;
else else
{ {
// Try to append the character to the line // Try to append the character to the line
m_Lines[ m_nCurLine ][ m_cCurLineLength ] = wch; video_console.m_Lines[ video_console.m_nCurLine ]
[ video_console.m_cCurLineLength ] = wch;
if( m_Font.GetTextWidth( m_Lines[ m_nCurLine ] ) > m_cxSafeArea ) if( m_Font.GetTextWidth( video_console.m_Lines
[ video_console.m_nCurLine ] ) > video_console.m_cxSafeArea )
{ {
// The line is too long, we need to wrap the character to the next line // The line is too long, we need to wrap the character to the next line
m_Lines[ m_nCurLine][ m_cCurLineLength ] = L'\0'; video_console.m_Lines[video_console.m_nCurLine]
[ video_console.m_cCurLineLength ] = L'\0';
bIncrementLine = TRUE; bIncrementLine = TRUE;
} }
} }
@ -177,31 +168,31 @@ void Console::Add( wchar_t wch )
// If we need to skip to the next line, do so // If we need to skip to the next line, do so
if( bIncrementLine ) if( bIncrementLine )
{ {
m_nCurLine = ( m_nCurLine + 1 ) % m_cScreenHeightVirtual; video_console.m_nCurLine = ( video_console.m_nCurLine + 1 )
m_cCurLineLength = 0; % video_console.m_cScreenHeightVirtual;
memset( m_Lines[m_nCurLine], 0, ( m_cScreenWidth + 1 ) * sizeof( wchar_t ) ); video_console.m_cCurLineLength = 0;
m_Lines[ m_nCurLine ][0] = wch; memset( video_console.m_Lines[video_console.m_nCurLine],
0, ( video_console.m_cScreenWidth + 1 ) * sizeof( wchar_t ) );
video_console.m_Lines[video_console.m_nCurLine ][0] = wch;
} }
m_cCurLineLength++; video_console.m_cCurLineLength++;
} }
void xdk360_console_format(_In_z_ _Printf_format_string_ LPCSTR strFormat, ... )
//--------------------------------------------------------------------------------------
// Name: Format()
// Desc: Output a variable argument list using a format string
//--------------------------------------------------------------------------------------
void Console::Format(_In_z_ _Printf_format_string_ LPCSTR strFormat, ... )
{ {
m_nCurLine = 0; video_console.m_nCurLine = 0;
m_cCurLineLength = 0; video_console.m_cCurLineLength = 0;
memset( m_Buffer, 0, m_cScreenHeightVirtual * ( m_cScreenWidth + 1 ) * sizeof( wchar_t ) ); memset( video_console.m_Buffer, 0,
video_console.m_cScreenHeightVirtual *
( video_console.m_cScreenWidth + 1 ) * sizeof( wchar_t ) );
va_list pArgList; va_list pArgList;
va_start( pArgList, strFormat ); va_start( pArgList, strFormat );
// Count the required length of the string // Count the required length of the string
unsigned long dwStrLen = _vscprintf( strFormat, pArgList ) + 1; // +1 = null terminator unsigned long dwStrLen = _vscprintf( strFormat, pArgList ) + 1;
// +1 = null terminator
char * strMessage = ( char * )_malloca( dwStrLen ); char * strMessage = ( char * )_malloca( dwStrLen );
vsprintf_s( strMessage, dwStrLen, strFormat, pArgList ); vsprintf_s( strMessage, dwStrLen, strFormat, pArgList );
@ -216,7 +207,7 @@ void Console::Format(_In_z_ _Printf_format_string_ LPCSTR strFormat, ... )
1, // Convert one byte 1, // Convert one byte
&wch, // Target wide character buffer &wch, // Target wide character buffer
1 ); // One wide character 1 ); // One wide character
Add( wch ); xdk360_console_add( wch );
} }
_freea( strMessage ); _freea( strMessage );
@ -224,11 +215,12 @@ void Console::Format(_In_z_ _Printf_format_string_ LPCSTR strFormat, ... )
va_end( pArgList ); va_end( pArgList );
} }
void Console::FormatW(_In_z_ _Printf_format_string_ LPCWSTR wstrFormat, ... ) void xdk360_console_format_w(_In_z_ _Printf_format_string_ LPCWSTR wstrFormat, ... )
{ {
m_nCurLine = 0; video_console.m_nCurLine = 0;
m_cCurLineLength = 0; video_console.m_cCurLineLength = 0;
memset( m_Buffer, 0, m_cScreenHeightVirtual * ( m_cScreenWidth + 1 ) * sizeof( wchar_t ) ); memset( video_console.m_Buffer, 0, video_console.m_cScreenHeightVirtual
* ( video_console.m_cScreenWidth + 1 ) * sizeof( wchar_t ) );
va_list pArgList; va_list pArgList;
va_start( pArgList, wstrFormat ); va_start( pArgList, wstrFormat );
@ -241,7 +233,7 @@ void Console::FormatW(_In_z_ _Printf_format_string_ LPCWSTR wstrFormat, ... )
// Output the string to the console // Output the string to the console
unsigned long uStringLength = wcslen( strMessage ); unsigned long uStringLength = wcslen( strMessage );
for( unsigned long i = 0; i < uStringLength; i++ ) for( unsigned long i = 0; i < uStringLength; i++ )
Add( strMessage[i] ); xdk360_console_add( strMessage[i] );
_freea( strMessage ); _freea( strMessage );

View File

@ -32,55 +32,30 @@
#define SAFE_AREA_PCT_4x3 85 #define SAFE_AREA_PCT_4x3 85
#define SAFE_AREA_PCT_HDTV 90 #define SAFE_AREA_PCT_HDTV 90
//-------------------------------------------------------------------------------------- typedef struct
// Name: class Console
// Desc: Class to implement the console.
//--------------------------------------------------------------------------------------
class Console
{ {
public: float m_fLineHeight; // height of a single line in pixels
Console(); unsigned int m_nScrollOffset; // offset to display text (in lines)
~Console(); unsigned int first_message;
// Initialization
HRESULT Create( LPCSTR strFontFileName, D3DCOLOR colBackColor, D3DCOLOR colTextColor);
void Destroy();
// Console output
void Format(_In_z_ _Printf_format_string_ LPCSTR strFormat, ... );
void FormatW(_In_z_ _Printf_format_string_ LPCWSTR wstrFormat, ... );
// method for rendering the console
void Render();
// Font for rendering text
XdkFont m_Font;
private:
int first_message;
// Safe area dimensions
unsigned int m_cxSafeArea; unsigned int m_cxSafeArea;
unsigned int m_cySafeArea; unsigned int m_cySafeArea;
unsigned int m_cxSafeAreaOffset; unsigned int m_cxSafeAreaOffset;
unsigned int m_cySafeAreaOffset; unsigned int m_cySafeAreaOffset;
unsigned int m_nCurLine; // index of current line being written to
// Colors unsigned int m_cCurLineLength; // length of the current line
unsigned long m_colBackColor; unsigned long m_colBackColor;
unsigned long m_colTextColor; unsigned long m_colTextColor;
// Text Buffers
unsigned int m_cScreenHeight; // height in lines of screen area unsigned int m_cScreenHeight; // height in lines of screen area
unsigned int m_cScreenHeightVirtual; // height in lines of text storage buffer unsigned int m_cScreenHeightVirtual; // height in lines of text storage buffer
unsigned int m_cScreenWidth; // width in characters unsigned int m_cScreenWidth; // width in characters
float m_fLineHeight; // height of a single line in pixels
wchar_t * m_Buffer; // buffer big enough to hold a full screen wchar_t * m_Buffer; // buffer big enough to hold a full screen
wchar_t ** m_Lines; // pointers to individual lines wchar_t ** m_Lines; // pointers to individual lines
unsigned int m_nCurLine; // index of current line being written to } video_console_t;
unsigned int m_cCurLineLength; // length of the current line
int m_nScrollOffset; // offset to display text (in lines)
// Add a character to the current line HRESULT xdk360_console_init ( LPCSTR strFontFileName, D3DCOLOR colBackColor, D3DCOLOR colTextColor);
void Add( wchar_t wch ); void xdk360_console_deinit (void);
}; void xdk360_console_format (_In_z_ _Printf_format_string_ LPCSTR strFormat, ... );
void xdk360_console_format_w (_In_z_ _Printf_format_string_ LPCWSTR wstrFormat, ... );
void xdk360_console_draw (void);
#endif #endif