📄 dgl.cc
字号:
//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
#include "math/mPoint.h"
#include "dgl/gTexManager.h"
#include "dgl/dgl.h"
#include "core/color.h"
#include "math/mPoint.h"
#include "math/mRect.h"
#include "dgl/gFont.h"
#include "dgl/gNewFont.h"
#include "console/console.h"
#include "math/mMatrix.h"
#include "core/frameAllocator.h"
#include "platform/profiler.h"
#include "core/unicode.h"
namespace {
ColorI sg_bitmapModulation(255, 255, 255, 255);
ColorI sg_textAnchorColor(255, 255, 255, 255);
ColorI sg_stackColor(255, 255, 255, 255);
RectI sgCurrentClipRect;
} // namespace {}
//--------------------------------------------------------------------------
void dglSetBitmapModulation(const ColorF& in_rColor)
{
ColorF c = in_rColor;
c.clamp();
sg_bitmapModulation = c;
sg_textAnchorColor = sg_bitmapModulation;
}
void dglGetBitmapModulation(ColorF* color)
{
*color = sg_bitmapModulation;
}
void dglGetBitmapModulation(ColorI* color)
{
*color = sg_bitmapModulation;
}
void dglClearBitmapModulation()
{
sg_bitmapModulation.set(255, 255, 255, 255);
}
void dglSetTextAnchorColor(const ColorF& in_rColor)
{
ColorF c = in_rColor;
c.clamp();
sg_textAnchorColor = c;
}
#ifdef TGE_RPG
//--------------------------------------------------------------------------
void dglDrawBitmapTile(TextureObject* texture,
const RectI& dstRect,
const RectI& srcRect,
const U32 in_flip)
{
RectI drawRect,refRect;
S32 x,y,
xBase,yBase,
xMax,yMax;
drawRect.extent = srcRect.extent;
refRect.point = srcRect.point;
refRect.extent = srcRect.extent;
xBase = dstRect.point.x;
yBase = dstRect.point.y;
xMax = xBase + dstRect.extent.x - srcRect.extent.x;
yMax = yBase + dstRect.extent.y - srcRect.extent.y;
//Tile绘画中间可以填充部分
for(drawRect.point.y = yBase; drawRect.point.y < yMax; drawRect.point.y += srcRect.extent.y)
{
drawRect.extent.x = srcRect.extent.x;
for(drawRect.point.x = xBase; drawRect.point.x < xMax; drawRect.point.x += srcRect.extent.x)
dglDrawBitmapStretchSR(texture, drawRect, srcRect,in_flip);
//绘画x右边缘余下的空白
drawRect.extent.x = xBase + dstRect.extent.x - drawRect.point.x;
refRect.extent.x = drawRect.extent.x;
dglDrawBitmapStretchSR(texture, drawRect, refRect,in_flip);
}
////////////////////////////////////////
//绘画y底边缘余下的空白
drawRect.extent.y = yBase + dstRect.extent.y - drawRect.point.y;
drawRect.extent.x = srcRect.extent.x;
refRect.extent = drawRect.extent;
for(drawRect.point.x = xBase; drawRect.point.x < xMax; drawRect.point.x += srcRect.extent.x)
dglDrawBitmapStretchSR(texture, drawRect, refRect,in_flip);
//绘画右下角的空白
drawRect.extent.x = xBase + dstRect.extent.x - drawRect.point.x;
refRect.extent.x = drawRect.extent.x;
dglDrawBitmapStretchSR(texture, drawRect, refRect,in_flip);
}
void dglDrawBitmapRotated(TextureObject *texture,const RectI& dstRect,const RectI& srcRect,const U32 in_flip,F32 spinAngle)
{
AssertFatal(texture != NULL, "GSurface::drawBitmapStretchSR: NULL Handle");
if(!dstRect.isValidRect())
return;
AssertFatal(srcRect.isValidRect() == true,
"GSurface::drawBitmapRotated: routines assume normal rects");
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture->texGLName);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_LIGHTING);
F32 texLeft = F32(srcRect.point.x) / F32(texture->texWidth);
F32 texRight = F32(srcRect.point.x + srcRect.extent.x) / F32(texture->texWidth);
F32 texTop = F32(srcRect.point.y) / F32(texture->texHeight);
F32 texBottom = F32(srcRect.point.y + srcRect.extent.y) / F32(texture->texHeight);
F32 screenLeft = dstRect.point.x;
F32 screenRight = dstRect.point.x + dstRect.extent.x;
F32 screenTop = dstRect.point.y;
F32 screenBottom = dstRect.point.y + dstRect.extent.y;
if(in_flip & GFlip_X)
{
F32 temp = texLeft;
texLeft = texRight;
texRight = temp;
}
if(in_flip & GFlip_Y)
{
F32 temp = texTop;
texTop = texBottom;
texBottom = temp;
}
glColor4ub(sg_bitmapModulation.red,
sg_bitmapModulation.green,
sg_bitmapModulation.blue,
sg_bitmapModulation.alpha);
F32 X = 0;
// calculate the centroid of the rectangle (to use as rotation pivot)
if (screenLeft < screenRight)
{
X = screenLeft + ((screenRight - screenLeft)*0.5f);
}
else
{
X = screenRight + ((screenLeft - screenRight)*0.5f);
};
F32 Y = 0;
if (screenTop < screenBottom)
{
Y = screenTop + ((screenBottom - screenTop)*0.5f);
}
else
{
Y = screenBottom + ((screenTop - screenBottom)*0.5f);
};
// spin angle is in degree's so convert to radians..radians = degrees * pi /180
spinAngle = spinAngle * 3.14 / 180.0f;
Point2F screenPoint(X,Y);
F32 width = dstRect.extent.x;
width *= 0.5;
MatrixF rotMatrix( EulerF( 0.0, 0.0, spinAngle ) );
Point3F offset( screenPoint.x, screenPoint.y, 0.0 );
Point3F points[4];
points[0] = Point3F(-width, -width, 0.0);
points[1] = Point3F(-width, width, 0.0);
points[2] = Point3F( width, width, 0.0);
points[3] = Point3F( width, -width, 0.0);
for( int i=0; i<4; i++ )
{
rotMatrix.mulP( points[i] );
points[i] += offset;
}
glBegin(GL_TRIANGLE_FAN);
glTexCoord2f(texLeft,texTop);
glVertex2fv(points[0]);
glTexCoord2f(texLeft, texBottom);
glVertex2fv(points[1]);
glTexCoord2f(texRight, texBottom);
glVertex2fv(points[2]);
glTexCoord2f(texRight, texTop);
glVertex2fv(points[3]);
glEnd();
/*
glBegin(GL_TRIANGLE_FAN);
glTexCoord2f(0.0, 0.0);
glVertex2fv(points[0]);
glTexCoord2f(0.0, 1.0);
glVertex2fv(points[1]);
glTexCoord2f(1.0, 1.0);
glVertex2fv(points[2]);
glTexCoord2f(1.0, 0.0);
glVertex2fv(points[3]);
glEnd();
*/
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
}
#endif
//--------------------------------------------------------------------------
void dglDrawBitmapStretchSR(TextureObject* texture,
const RectI& dstRect,
const RectI& srcRect,
const U32 in_flip)
{
AssertFatal(texture != NULL, "GSurface::drawBitmapStretchSR: NULL Handle");
if(!dstRect.isValidRect())
return;
AssertFatal(srcRect.isValidRect() == true,
"GSurface::drawBitmapStretchSR: routines assume normal rects");
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture->texGLName);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
F32 texLeft = F32(srcRect.point.x) / F32(texture->texWidth);
F32 texRight = F32(srcRect.point.x + srcRect.extent.x) / F32(texture->texWidth);
F32 texTop = F32(srcRect.point.y) / F32(texture->texHeight);
F32 texBottom = F32(srcRect.point.y + srcRect.extent.y) / F32(texture->texHeight);
F32 screenLeft = dstRect.point.x;
F32 screenRight = dstRect.point.x + dstRect.extent.x;
F32 screenTop = dstRect.point.y;
F32 screenBottom = dstRect.point.y + dstRect.extent.y;
if(in_flip & GFlip_X)
{
F32 temp = texLeft;
texLeft = texRight;
texRight = temp;
}
if(in_flip & GFlip_Y)
{
F32 temp = texTop;
texTop = texBottom;
texBottom = temp;
}
glColor4ub(sg_bitmapModulation.red,
sg_bitmapModulation.green,
sg_bitmapModulation.blue,
sg_bitmapModulation.alpha);
glBegin(GL_TRIANGLE_FAN);
glTexCoord2f(texLeft, texBottom);
glVertex2f(screenLeft, screenBottom);
glTexCoord2f(texRight, texBottom);
glVertex2f(screenRight, screenBottom);
glTexCoord2f(texRight, texTop);
glVertex2f(screenRight, screenTop);
glTexCoord2f(texLeft, texTop);
glVertex2f(screenLeft, screenTop);
glEnd();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
}
void dglDrawBitmap(TextureObject* texture, const Point2I& in_rAt, const U32 in_flip)
{
AssertFatal(texture != NULL, "GSurface::drawBitmap: NULL Handle");
// All non-StretchSR bitmaps are transformed into StretchSR calls...
//
RectI subRegion(0, 0,
texture->bitmapWidth,
texture->bitmapHeight);
RectI stretch(in_rAt.x, in_rAt.y,
texture->bitmapWidth,
texture->bitmapHeight);
dglDrawBitmapStretchSR(texture,
stretch,
subRegion,
in_flip);
}
void dglDrawBitmapStretch(TextureObject* texture, const RectI& dstRect, const U32 in_flip)
{
AssertFatal(texture != NULL, "GSurface::drawBitmapStretch: NULL Handle");
AssertFatal(dstRect.isValidRect() == true,
"GSurface::drawBitmapStretch: routines assume normal rects");
RectI subRegion(0, 0,
texture->bitmapWidth,
texture->bitmapHeight);
dglDrawBitmapStretchSR(texture,
dstRect,
subRegion,
in_flip);
}
void dglDrawBitmapSR(TextureObject *texture, const Point2I& in_rAt, const RectI& srcRect, const U32 in_flip)
{
AssertFatal(texture != NULL, "GSurface::drawBitmapSR: NULL Handle");
AssertFatal(srcRect.isValidRect() == true,
"GSurface::drawBitmapSR: routines assume normal rects");
RectI stretch(in_rAt.x, in_rAt.y,
srcRect.len_x(),
srcRect.len_y());
dglDrawBitmapStretchSR(texture,
stretch,
srcRect,
in_flip);
}
U32 dglDrawText(GFont* font,
const Point2I& ptDraw,
const void* in_string,
const ColorI* colorTable,
const U32 maxColorIndex,
F32 rot)
{
// Just a note - dStrlen isn't strictly correct but it's guaranteed to be
// as long or longer than the real length. dglDrawTextN fails gracefully
// if you specify overlong, so this is ok.
return _dglDrawTextN(font, ptDraw, in_string, dStrlen((const UTF8 *) in_string), colorTable, maxColorIndex, rot);
}
struct TextVertex
{
Point2F p;
Point2F t;
ColorI c;
void set(F32 x, F32 y, F32 tx, F32 ty, ColorI color)
{
p.x = x;
p.y = y;
t.x = tx;
t.y = ty;
c = color;
}
};
/// Chinese
U32 dglDrawTextN(GFont* font,
const Point2I& ptDraw,
const UTF8* in_string,
U32 n,
const ColorI* colorTable,
const U32 maxColorIndex,
F32 rot)
{
return _dglDrawTextN(font, ptDraw, in_string, n*sizeof(UTF8), colorTable, maxColorIndex, rot,false);
}
/// Chinese
U32 dglDrawTextN(GFont* font,
const Point2I& ptDraw,
const UTF16* in_string,
U32 n,
const ColorI* colorTable,
const U32 maxColorIndex,
F32 rot)
{
return _dglDrawTextN(font, ptDraw, in_string, n*sizeof(UTF16), colorTable, maxColorIndex, rot,true);
}
/// Chinese
U32 _dglDrawTextN(GFont* font,
const Point2I& ptDraw,
const void* in_string,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -