Alibek Omarov
6 years ago
7 changed files with 199 additions and 473 deletions
@ -1,118 +0,0 @@
@@ -1,118 +0,0 @@
|
||||
/*
|
||||
vgui_main.h - vgui main header |
||||
Copyright (C) 2011 Uncle Mike |
||||
|
||||
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 3 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 General Public License for more details. |
||||
*/ |
||||
|
||||
#ifndef VGUI_MAIN_H |
||||
#define VGUI_MAIN_H |
||||
|
||||
#include<VGUI.h> |
||||
#include<VGUI_App.h> |
||||
#include<VGUI_Font.h> |
||||
#include<VGUI_Panel.h> |
||||
#include<VGUI_Cursor.h> |
||||
#include<VGUI_SurfaceBase.h> |
||||
#include<VGUI_InputSignal.h> |
||||
#include<VGUI_MouseCode.h> |
||||
#include<VGUI_KeyCode.h> |
||||
|
||||
using namespace vgui; |
||||
|
||||
struct PaintStack |
||||
{ |
||||
Panel *m_pPanel; |
||||
int iTranslateX; |
||||
int iTranslateY; |
||||
int iScissorLeft; |
||||
int iScissorRight; |
||||
int iScissorTop; |
||||
int iScissorBottom; |
||||
}; |
||||
|
||||
class CEngineSurface : public SurfaceBase |
||||
{ |
||||
private: |
||||
void InitVertex( vpoint_t &vertex, int x, int y, float u, float v ); |
||||
public: |
||||
CEngineSurface( Panel *embeddedPanel ); |
||||
~CEngineSurface(); |
||||
public: |
||||
// not used in engine instance
|
||||
virtual bool setFullscreenMode( int wide, int tall, int bpp ) { return false; } |
||||
virtual void setWindowedMode( void ) { } |
||||
virtual void setTitle( const char *title ) { } |
||||
virtual void createPopup( Panel* embeddedPanel ) { } |
||||
virtual bool isWithin( int x, int y ) { return true; } |
||||
void SetupPaintState( const PaintStack *paintState ); |
||||
#ifdef NEW_VGUI_DLL |
||||
virtual void GetMousePos( int &x, int &y ) { } |
||||
#endif |
||||
virtual bool hasFocus( void ) { return true; } |
||||
protected: |
||||
virtual int createNewTextureID( void ); |
||||
virtual void drawSetColor( int r, int g, int b, int a ); |
||||
virtual void drawSetTextColor( int r, int g, int b, int a ); |
||||
virtual void drawFilledRect( int x0, int y0, int x1, int y1 ); |
||||
virtual void drawOutlinedRect( int x0,int y0,int x1,int y1 ); |
||||
virtual void drawSetTextFont( Font *font ); |
||||
virtual void drawSetTextPos( int x, int y ); |
||||
virtual void drawPrintText( const char* text, int textLen ); |
||||
virtual void drawSetTextureRGBA( int id, const char* rgba, int wide, int tall ); |
||||
virtual void drawSetTexture( int id ); |
||||
virtual void drawTexturedRect( int x0, int y0, int x1, int y1 ); |
||||
virtual void drawPrintChar( int x, int y, int wide, int tall, float s0, float t0, float s1, float t1, int color[4] ); |
||||
virtual void addCharToBuffer( const vpoint_t *ul, const vpoint_t *lr, int color[4] ); |
||||
virtual void setCursor( Cursor* cursor ); |
||||
virtual void pushMakeCurrent( Panel* panel, bool useInsets ); |
||||
virtual void popMakeCurrent( Panel* panel ); |
||||
// not used in engine instance
|
||||
virtual bool createPlat( void ) { return false; } |
||||
virtual bool recreateContext( void ) { return false; } |
||||
virtual void enableMouseCapture( bool state ) { } |
||||
virtual void invalidate( Panel *panel ) { } |
||||
virtual void setAsTopMost( bool state ) { } |
||||
virtual void applyChanges( void ) { } |
||||
virtual void swapBuffers( void ) { } |
||||
virtual void flushBuffer( void ); |
||||
protected: |
||||
int _drawTextPos[2]; |
||||
int _drawColor[4]; |
||||
int _drawTextColor[4]; |
||||
int _translateX, _translateY; |
||||
int _currentTexture; |
||||
Panel *currentPanel; |
||||
}; |
||||
|
||||
// initialize VGUI::App as external (part of engine)
|
||||
class CEngineApp : public App |
||||
{ |
||||
public: |
||||
virtual void main( int argc, char* argv[] ) { } |
||||
virtual void setCursorPos( int x, int y ); // we need to recompute abs position to window
|
||||
virtual void getCursorPos( int &x,int &y ); |
||||
protected: |
||||
virtual void platTick(void) { } |
||||
}; |
||||
|
||||
extern Panel *rootPanel; |
||||
extern CEngineSurface *engSurface; |
||||
extern CEngineApp *engApp; |
||||
|
||||
//
|
||||
// vgui_input.cpp
|
||||
//
|
||||
void VGUI_InitCursors( void ); |
||||
void VGUI_CursorSelect( Cursor *cursor ); |
||||
void VGUI_ActivateCurrentCursor( void ); |
||||
|
||||
#endif//VGUI_MAIN_H
|
@ -1,185 +0,0 @@
@@ -1,185 +0,0 @@
|
||||
/*
|
||||
vgui_font.cpp - fonts management |
||||
Copyright (C) 2011 Uncle Mike |
||||
|
||||
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 3 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 General Public License for more details. |
||||
|
||||
In addition, as a special exception, the author gives permission |
||||
to link the code of this program with VGUI library developed by |
||||
Valve, L.L.C ("Valve"). You must obey the GNU General Public License |
||||
in all respects for all of the code used other than VGUI library. |
||||
If you modify this file, you may extend this exception to your |
||||
version of the file, but you are not obligated to do so. If |
||||
you do not wish to do so, delete this exception statement |
||||
from your version. |
||||
|
||||
*/ |
||||
|
||||
#include "vgui_main.h" |
||||
|
||||
int FontCache::s_pFontPageSize[FONT_PAGE_SIZE_COUNT] = { 16, 32, 64, 128 }; |
||||
|
||||
FontCache::FontCache() : m_CharCache( 0, 256, CacheEntryLessFunc ) |
||||
{ |
||||
CacheEntry_t listHead = { 0, 0 }; |
||||
|
||||
m_LRUListHeadIndex = m_CharCache.Insert( listHead ); |
||||
m_CharCache[m_LRUListHeadIndex].nextEntry = m_LRUListHeadIndex; |
||||
m_CharCache[m_LRUListHeadIndex].prevEntry = m_LRUListHeadIndex; |
||||
for( int i = 0; i < FONT_PAGE_SIZE_COUNT; i++ ) |
||||
{ |
||||
m_pCurrPage[i] = -1; |
||||
} |
||||
} |
||||
|
||||
bool FontCache::CacheEntryLessFunc( CacheEntry_t const &lhs, CacheEntry_t const &rhs ) |
||||
{ |
||||
if( lhs.font < rhs.font ) |
||||
return true; |
||||
else if( lhs.font > rhs.font ) |
||||
return false; |
||||
|
||||
return ( lhs.ch < rhs.ch ); |
||||
} |
||||
|
||||
bool FontCache::GetTextureForChar( Font *font, char ch, int *textureID, float **texCoords ) |
||||
{ |
||||
static CacheEntry_t cacheitem; |
||||
|
||||
cacheitem.font = font; |
||||
cacheitem.ch = ch; |
||||
|
||||
Assert( texCoords != NULL ); |
||||
|
||||
*texCoords = cacheitem.texCoords; |
||||
|
||||
HCacheEntry cacheHandle = m_CharCache.Find( cacheitem ); |
||||
|
||||
if( cacheHandle != 65535 && m_CharCache.IsValidIndex( cacheHandle )) |
||||
{ |
||||
// we have an entry already, return that
|
||||
int page = m_CharCache[cacheHandle].page; |
||||
*textureID = m_PageList[page].textureID; |
||||
//else return false;
|
||||
*texCoords = m_CharCache[cacheHandle].texCoords; |
||||
return true; |
||||
} |
||||
|
||||
// get the char details
|
||||
int fontTall = font->getTall(); |
||||
int a, b, c; |
||||
font->getCharABCwide( (byte)ch, a, b, c ); |
||||
int fontWide = b; |
||||
|
||||
// get a texture to render into
|
||||
int page, drawX, drawY, twide, ttall; |
||||
if( !AllocatePageForChar( fontWide, fontTall, page, drawX, drawY, twide, ttall )) |
||||
return false; |
||||
|
||||
// create a buffer and render the character into it
|
||||
int nByteCount = s_pFontPageSize[FONT_PAGE_SIZE_COUNT-1] * s_pFontPageSize[FONT_PAGE_SIZE_COUNT-1] * 4; |
||||
byte * rgba = (byte *)g_api->EngineMalloc(nByteCount);//(byte *)Z_Malloc( nByteCount );
|
||||
font->getCharRGBA( (byte)ch, 0, 0, fontWide, fontTall, rgba ); |
||||
|
||||
// upload the new sub texture
|
||||
g_api->BindTexture( m_PageList[page].textureID ); |
||||
g_api->UploadTextureBlock( m_PageList[page].textureID, drawX, drawY, rgba, fontWide, fontTall ); |
||||
|
||||
// set the cache info
|
||||
cacheitem.page = page; |
||||
|
||||
cacheitem.texCoords[0] = (float)((double)drawX / ((double)twide)); |
||||
cacheitem.texCoords[1] = (float)((double)drawY / ((double)ttall)); |
||||
cacheitem.texCoords[2] = (float)((double)(drawX + fontWide) / (double)twide); |
||||
cacheitem.texCoords[3] = (float)((double)(drawY + fontTall) / (double)ttall); |
||||
|
||||
m_CharCache.Insert( cacheitem ); |
||||
|
||||
// return the data
|
||||
*textureID = m_PageList[page].textureID; |
||||
// memcpy( texCoords, cacheitem.texCoords, sizeof( float ) * 4 );
|
||||
return true; |
||||
} |
||||
|
||||
int FontCache::ComputePageType( int charTall ) const |
||||
{ |
||||
for( int i = 0; i < FONT_PAGE_SIZE_COUNT; i++ ) |
||||
{ |
||||
if( charTall < s_pFontPageSize[i] ) |
||||
return i; |
||||
} |
||||
return -1; |
||||
} |
||||
|
||||
bool FontCache::AllocatePageForChar( int charWide, int charTall, int &pageIndex, int &drawX, int &drawY, int &twide, int &ttall ) |
||||
{ |
||||
// see if there is room in the last page for this character
|
||||
int nPageType = ComputePageType( charTall ); |
||||
|
||||
if( nPageType < 0 ) |
||||
return false; |
||||
|
||||
pageIndex = m_pCurrPage[nPageType]; |
||||
|
||||
int nNextX = 0; |
||||
bool bNeedsNewPage = true; |
||||
|
||||
if( pageIndex > -1 ) |
||||
{ |
||||
Page_t &page = m_PageList[pageIndex]; |
||||
|
||||
nNextX = page.nextX + charWide; |
||||
|
||||
// make sure we have room on the current line of the texture page
|
||||
if( nNextX > page.wide ) |
||||
{ |
||||
// move down a line
|
||||
page.nextX = 0; |
||||
nNextX = charWide; |
||||
page.nextY += page.fontHeight + 1; |
||||
} |
||||
|
||||
bNeedsNewPage = (( page.nextY + page.fontHeight + 1 ) > page.tall ); |
||||
} |
||||
|
||||
if( bNeedsNewPage ) |
||||
{ |
||||
// allocate a new page
|
||||
pageIndex = m_PageList.AddToTail(); |
||||
Page_t &newPage = m_PageList[pageIndex]; |
||||
m_pCurrPage[nPageType] = pageIndex; |
||||
|
||||
newPage.textureID = g_api->GenerateTexture(); |
||||
|
||||
newPage.fontHeight = s_pFontPageSize[nPageType]; |
||||
newPage.wide = 256; |
||||
newPage.tall = 256; |
||||
newPage.nextX = 0; |
||||
newPage.nextY = 0; |
||||
|
||||
nNextX = charWide; |
||||
|
||||
// create empty texture
|
||||
g_api->CreateTexture( newPage.textureID, 256, 256 ); |
||||
} |
||||
|
||||
// output the position
|
||||
Page_t &page = m_PageList[pageIndex]; |
||||
drawX = page.nextX; |
||||
drawY = page.nextY; |
||||
twide = page.wide; |
||||
ttall = page.tall; |
||||
|
||||
// update the next position to draw in
|
||||
page.nextX = nNextX + 1; |
||||
|
||||
return true; |
||||
} |
Loading…
Reference in new issue