Cursor drawing

- fixed cursor drawing in CEngine
- changed event loop to generate more events
dev-ui
Piotr Dziwinski 2012-07-23 21:41:27 +02:00
parent 8797569d33
commit 9d59204531
11 changed files with 263 additions and 246 deletions

View File

@ -58,7 +58,6 @@ graphics/common/terrain.cpp
graphics/common/text.cpp
graphics/common/water.cpp
graphics/opengl/gldevice.cpp
graphics/opengl/glengine.cpp
# object/auto/auto.cpp
# object/auto/autobase.cpp
# object/auto/autoconvert.cpp

View File

@ -457,27 +457,24 @@ int CApplication::Run()
while (true)
{
// Use SDL_PeepEvents() if the app is active, so we can use idle time to
// render the scene. Else, use SDL_PollEvent() to avoid eating CPU time.
int count = 0;
// To be sure no old event remains
m_private->currentEvent.type = SDL_NOEVENT;
if (m_active)
SDL_PumpEvents();
bool haveEvent = true;
while (haveEvent)
{
haveEvent = false;
int count = 0;
// Use SDL_PeepEvents() if the app is active, so we can use idle time to
// render the scene. Else, use SDL_PollEvent() to avoid eating CPU time.
if (m_active)
{
SDL_PumpEvents();
count = SDL_PeepEvents(&m_private->currentEvent, 1, SDL_GETEVENT, SDL_ALLEVENTS);
}
else
{
count = SDL_PollEvent(&m_private->currentEvent);
}
// If received an event
if (count > 0)

View File

@ -337,11 +337,11 @@ public:
virtual Gfx::Color GetTextureFactor() = 0;
//! Renders primitive composed of vertices with single texture
virtual void DrawPrimitive(Gfx::PrimitiveType type, Gfx::Vertex *vertices, int vertexCount) = 0;
virtual void DrawPrimitive(Gfx::PrimitiveType type, const Gfx::Vertex *vertices , int vertexCount) = 0;
//! Renders primitive composed of vertices with color information and single texture
virtual void DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexCol *vertices, int vertexCount) = 0;
virtual void DrawPrimitive(Gfx::PrimitiveType type, const Gfx::VertexCol *vertices , int vertexCount) = 0;
//! Renders primitive composed of vertices with multitexturing (2 textures)
virtual void DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexTex2 *vertices, int vertexCount) = 0;
virtual void DrawPrimitive(Gfx::PrimitiveType type, const Gfx::VertexTex2 *vertices, int vertexCount) = 0;
//! Tests whether a sphere intersects the 6 clipping planes of projection volume
virtual int ComputeSphereVisibility(const Math::Vector &center, float radius) = 0;

View File

@ -23,6 +23,7 @@
#include "common/iman.h"
#include "common/image.h"
#include "common/key.h"
#include "common/logger.h"
#include "graphics/common/device.h"
#include "math/geometry.h"
@ -144,6 +145,24 @@ Gfx::CEngine::CEngine(CInstanceManager *iMan, CApplication *app)
m_updateGeometry = false;
m_mice[Gfx::ENG_MOUSE_NORM] = Gfx::EngineMouse( 0, 1, 32, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point( 1.0f, 1.0f));
m_mice[Gfx::ENG_MOUSE_WAIT] = Gfx::EngineMouse( 2, 3, 33, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point( 8.0f, 12.0f));
m_mice[Gfx::ENG_MOUSE_HAND] = Gfx::EngineMouse( 4, 5, 34, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point( 7.0f, 2.0f));
m_mice[Gfx::ENG_MOUSE_NO] = Gfx::EngineMouse( 6, 7, 35, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point(10.0f, 10.0f));
m_mice[Gfx::ENG_MOUSE_EDIT] = Gfx::EngineMouse( 8, 9, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 6.0f, 10.0f));
m_mice[Gfx::ENG_MOUSE_CROSS] = Gfx::EngineMouse(10, 11, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(10.0f, 10.0f));
m_mice[Gfx::ENG_MOUSE_MOVEV] = Gfx::EngineMouse(12, 13, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 5.0f, 11.0f));
m_mice[Gfx::ENG_MOUSE_MOVEH] = Gfx::EngineMouse(14, 15, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(11.0f, 5.0f));
m_mice[Gfx::ENG_MOUSE_MOVED] = Gfx::EngineMouse(16, 17, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 9.0f));
m_mice[Gfx::ENG_MOUSE_MOVEI] = Gfx::EngineMouse(18, 19, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 9.0f));
m_mice[Gfx::ENG_MOUSE_MOVE] = Gfx::EngineMouse(20, 21, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(11.0f, 11.0f));
m_mice[Gfx::ENG_MOUSE_TARGET] = Gfx::EngineMouse(22, 23, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(15.0f, 15.0f));
m_mice[Gfx::ENG_MOUSE_SCROLLL] = Gfx::EngineMouse(24, 25, 43, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 2.0f, 9.0f));
m_mice[Gfx::ENG_MOUSE_SCROLLR] = Gfx::EngineMouse(26, 27, 44, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(17.0f, 9.0f));
m_mice[Gfx::ENG_MOUSE_SCROLLU] = Gfx::EngineMouse(28, 29, 45, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 2.0f));
m_mice[Gfx::ENG_MOUSE_SCROLLD] = Gfx::EngineMouse(30, 31, 46, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 17.0f));
m_mouseSize = Math::Point(0.04f, 0.04f * (800.0f / 600.0f));
m_mousePos = Math::Point(0.5f, 0.5f);
m_mouseType = Gfx::ENG_MOUSE_NORM;
m_mouseVisible = false;
@ -251,30 +270,50 @@ bool Gfx::CEngine::AfterDeviceSetInit()
return true;
}
bool Gfx::CEngine::ProcessEvent(const Event &event)
Gfx::Texture Gfx::CEngine::CreateTexture(const std::string &texName, const Gfx::TextureCreateParams &params)
{
if (event.type == EVENT_MOUSE_MOVE)
CImage img;
if (! img.Load(m_app->GetDataFilePath(m_texPath, texName)))
{
m_mousePos = event.mouseMove.pos;
}
else if (event.type == EVENT_KEY_DOWN)
{
// !! Debug, to be removed later !!
if (event.key.key == KEY(F1))
{
m_mouseVisible = !m_mouseVisible;
m_app->SetSystemMouseVisible(! m_app->GetSystemMouseVisibile());
}
else if (event.key.key == KEY(F2))
{
int index = (int)m_mouseType;
m_mouseType = (Gfx::EngineMouseType)( (index + 1) % Gfx::ENG_MOUSE_COUNT );
}
std::stringstream str;
str << "Couldn't load texture '" << texName << "': " << img.GetError();
m_error = str.str();
return Gfx::Texture(); // invalid texture
}
// By default, pass on all events
return true;
Gfx::Texture result = m_device->CreateTexture(&img, params);
if (! result.valid)
{
std::stringstream str;
str << "Couldn't load texture '" << texName << "': " << m_device->GetError();
m_error = str.str();
return result;
}
m_texNameMap[texName] = result;
m_revTexNameMap[result] = texName;
return result;
}
Gfx::Texture Gfx::CEngine::CreateTexture(const std::string &texName)
{
return CreateTexture(texName, m_defaultTexParams);
}
void Gfx::CEngine::DestroyTexture(const std::string &texName)
{
std::map<std::string, Gfx::Texture>::iterator it = m_texNameMap.find(texName);
if (it == m_texNameMap.end())
return;
std::map<Gfx::Texture, std::string>::iterator revIt = m_revTexNameMap.find((*it).second);
m_device->DestroyTexture((*it).second);
m_revTexNameMap.erase(revIt);
m_texNameMap.erase(it);
}
void Gfx::CEngine::SetTexture(const std::string &name, int stage)
@ -307,22 +346,25 @@ void Gfx::CEngine::SetState(int state, Gfx::Color color)
state |= Gfx::ENG_RSTATE_TTEXTURE_BLACK;
}
// TODO other modes & thorough testing
if (state & Gfx::ENG_RSTATE_TTEXTURE_BLACK) // The transparent black texture?
{
m_device->SetRenderState(Gfx::RENDER_STATE_FOG, false);
m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, false);
m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, true);
m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false);
m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true);
m_device->SetBlendFunc(Gfx::BLEND_ONE, Gfx::BLEND_INV_SRC_COLOR);
m_device->SetTextureEnabled(0, true);
m_device->SetTextureFactor(color);
Gfx::TextureParams params;
params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE;
params.colorArg1 = Gfx::TEX_MIX_ARG_TEXTURE;
params.colorArg2 = Gfx::TEX_MIX_ARG_FACTOR;
// TODO: params.alphaOperation = Gfx::TEX_MIX_OPER_DISABLED;
params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE;
m_device->SetTextureParams(0, params);
}
else if (state & Gfx::ENG_RSTATE_TTEXTURE_WHITE) // The transparent white texture?
@ -331,88 +373,93 @@ void Gfx::CEngine::SetState(int state, Gfx::Color color)
m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, false);
m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, true);
m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false);
m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true);
m_device->SetBlendFunc(Gfx::BLEND_DST_COLOR, Gfx::BLEND_ZERO);
m_device->SetTextureEnabled(0, true);
m_device->SetTextureFactor(color.Inverse());
Gfx::TextureParams params;
params.colorOperation = Gfx::TEX_MIX_OPER_ADD;
params.colorArg1 = Gfx::TEX_MIX_ARG_TEXTURE;
params.colorArg2 = Gfx::TEX_MIX_ARG_FACTOR;
// TODO: params.alphaOperation = Gfx::TEX_MIX_OPER_DISABLED;
params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE;
m_device->SetTextureParams(0, params);
}
// TODO other modes
else if (state & Gfx::ENG_RSTATE_TCOLOR_BLACK) // The transparent black color?
{
/*
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, false);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, false);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, true);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, false);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND, m_blackSrcBlend[1]);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_DESTBLEND, m_blackDestBlend[1]);
m_device->SetRenderState(Gfx::RENDER_STATE_FOG, false);
m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, false);
m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, true);
m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false);
m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_TEXTUREFACTOR, color);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/
m_device->SetBlendFunc(Gfx::BLEND_ONE, Gfx::BLEND_INV_SRC_COLOR);
m_device->SetTextureFactor(color);
m_device->SetTextureEnabled(0, true);
m_device->SetTextureParams(0, Gfx::TextureParams());
}
else if (state & Gfx::ENG_RSTATE_TCOLOR_WHITE) // The transparent white color?
{
/*m_pD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, false);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, false);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, true);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, false);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND, m_whiteSrcBlend[1]);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_DESTBLEND, m_whiteDestBlend[1]);
m_device->SetRenderState(Gfx::RENDER_STATE_FOG, false);
m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, false);
m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, true);
m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false);
m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_TEXTUREFACTOR, ~color);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/
m_device->SetBlendFunc(Gfx::BLEND_DST_COLOR, Gfx::BLEND_ZERO);
m_device->SetTextureFactor(color.Inverse());
m_device->SetTextureEnabled(0, true);
m_device->SetTextureParams(0, Gfx::TextureParams());
}
else if (state & Gfx::ENG_RSTATE_TDIFFUSE) // diffuse color as transparent?
{
/*m_pD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, false);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, false);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, true);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, false);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND, m_diffuseSrcBlend[1]);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_DESTBLEND, m_diffuseDestBlend[1]);
/*m_device->SetRenderState(D3DRENDERSTATE_FOGENABLE, false);
m_device->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, false);
m_device->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, true);
m_device->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, false);
m_device->SetRenderState(D3DRENDERSTATE_SRCBLEND, m_diffuseSrcBlend[1]);
m_device->SetRenderState(D3DRENDERSTATE_DESTBLEND, m_diffuseDestBlend[1]);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/
m_device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
m_device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
m_device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/
}
else if (state & Gfx::ENG_RSTATE_ALPHA) // image with alpha channel?
{
/*m_pD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, true);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, true);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, false);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, true);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHAFUNC, D3DCMP_GREATER);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHAREF, (DWORD)(128));
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND, m_alphaSrcBlend[1]);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_DESTBLEND, m_alphaSrcBlend[1]);
/*m_device->SetRenderState(D3DRENDERSTATE_FOGENABLE, true);
m_device->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, true);
m_device->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, false);
m_device->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, true);
m_device->SetRenderState(D3DRENDERSTATE_ALPHAFUNC, D3DCMP_GREATER);
m_device->SetRenderState(D3DRENDERSTATE_ALPHAREF, (DWORD)(128));
m_device->SetRenderState(D3DRENDERSTATE_SRCBLEND, m_alphaSrcBlend[1]);
m_device->SetRenderState(D3DRENDERSTATE_DESTBLEND, m_alphaSrcBlend[1]);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_TEXTUREFACTOR, color);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);*/
m_device->SetRenderState(D3DRENDERSTATE_TEXTUREFACTOR, color);
m_device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
m_device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
m_device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
m_device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
m_device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);*/
}
else // normal ?
{
/*m_pD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, true);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, true);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, false);
m_pD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, false);
m_device->SetRenderState(Gfx::RENDER_STATE_FOG, true);
m_device->SetRenderState(Gfx::RENDER_STATE_DEPTH_WRITE, true);
m_device->SetRenderState(Gfx::RENDER_STATE_BLENDING, false);
m_device->SetRenderState(Gfx::RENDER_STATE_ALPHA_TEST, false);
m_device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/
m_device->SetTextureEnabled(0, true);
m_device->SetTextureParams(0, Gfx::TextureParams());
/*m_device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
m_device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
m_device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
m_device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/
}
if (state & Gfx::ENG_RSTATE_FOG)
@ -426,40 +473,39 @@ void Gfx::CEngine::SetState(int state, Gfx::Color color)
if ( (state & ENG_RSTATE_DUAL_BLACK) && second )
{
/*m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
m_pD3DDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
m_pD3DDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);*/
/*m_device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
m_device->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
m_device->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
m_device->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
m_device->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);*/
}
else if ( (state & ENG_RSTATE_DUAL_WHITE) && second )
{
/*m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADD);
m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
m_pD3DDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
m_pD3DDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);*/
/*m_device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADD);
m_device->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
m_device->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
m_device->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
m_device->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);*/
}
else
{
/*m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
m_pD3DDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);*/
m_device->SetTextureEnabled(1, false);
}
if (state & Gfx::ENG_RSTATE_WRAP)
{
/*m_pD3DDevice->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_WRAP);
m_pD3DDevice->SetTextureStageState(1, D3DTSS_ADDRESS, D3DTADDRESS_WRAP);*/
/*m_device->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_WRAP);
m_device->SetTextureStageState(1, D3DTSS_ADDRESS, D3DTADDRESS_WRAP);*/
}
else if (state & Gfx::ENG_RSTATE_CLAMP)
{
/*m_pD3DDevice->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP);
m_pD3DDevice->SetTextureStageState(1, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP);*/
/*m_device->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP);
m_device->SetTextureStageState(1, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP);*/
}
else
{
/*m_pD3DDevice->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP);
m_pD3DDevice->SetTextureStageState(1, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP);*/
/*m_device->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP);
m_device->SetTextureStageState(1, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP);*/
}
if (state & Gfx::ENG_RSTATE_2FACE)
@ -478,10 +524,39 @@ void Gfx::CEngine::SetState(int state, Gfx::Color color)
m_device->SetGlobalAmbient(m_ambientColor[m_rankView]);
}
bool Gfx::CEngine::ProcessEvent(const Event &event)
{
if (event.type == EVENT_MOUSE_MOVE)
{
m_mousePos = event.mouseMove.pos;
}
else if (event.type == EVENT_KEY_DOWN)
{
// !! Debug, to be removed later !!
if (event.key.key == KEY(F1))
{
m_mouseVisible = !m_mouseVisible;
m_app->SetSystemMouseVisible(! m_app->GetSystemMouseVisibile());
}
else if (event.key.key == KEY(F2))
{
int index = static_cast<int>(m_mouseType);
m_mouseType = static_cast<Gfx::EngineMouseType>( (index + 1) % Gfx::ENG_MOUSE_COUNT );
}
}
// By default, pass on all events
return true;
}
bool Gfx::CEngine::Render()
{
m_statisticTriangle = 0;
m_lastState = -1;
SetState(Gfx::ENG_RSTATE_NORMAL);
m_device->BeginScene();
SetUp3DView();
@ -535,61 +610,12 @@ bool Gfx::CEngine::DrawInterface()
void Gfx::CEngine::DrawMouse()
{
struct EngineMouse
{
Gfx::EngineMouseType type;
int icon1;
int icon2;
int iconShadow;
Gfx::EngineRenderState mode1;
Gfx::EngineRenderState mode2;
Math::Point hotPoint;
EngineMouse(Gfx::EngineMouseType = Gfx::ENG_MOUSE_NORM,
int icon1 = -1, int icon2 = -1, int iconShadow = -1,
Gfx::EngineRenderState mode1 = Gfx::ENG_RSTATE_NORMAL,
Gfx::EngineRenderState mode2 = ENG_RSTATE_NORMAL,
Math::Point hotPoint = Math::Point())
{
this->type = type;
this->icon1 = icon1;
this->icon2 = icon2;
this->iconShadow = iconShadow;
this->mode1 = mode1;
this->mode2 = mode2;
this->hotPoint = hotPoint;
}
};
static const EngineMouse MICE[Gfx::ENG_MOUSE_COUNT] =
{
EngineMouse(Gfx::ENG_MOUSE_NORM, 0, 1, 32, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point( 1.0f, 1.0f)),
EngineMouse(Gfx::ENG_MOUSE_WAIT, 2, 3, 33, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point( 8.0f, 12.0f)),
EngineMouse(Gfx::ENG_MOUSE_HAND, 4, 5, 34, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point( 7.0f, 2.0f)),
EngineMouse(Gfx::ENG_MOUSE_NO, 6, 7, 35, Gfx::ENG_RSTATE_TCOLOR_WHITE, Gfx::ENG_RSTATE_TCOLOR_BLACK, Math::Point(10.0f, 10.0f)),
EngineMouse(Gfx::ENG_MOUSE_EDIT, 8, 9, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 6.0f, 10.0f)),
EngineMouse(Gfx::ENG_MOUSE_CROSS, 10, 11, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(10.0f, 10.0f)),
EngineMouse(Gfx::ENG_MOUSE_MOVEV, 12, 13, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 5.0f, 11.0f)),
EngineMouse(Gfx::ENG_MOUSE_MOVEH, 14, 15, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(11.0f, 5.0f)),
EngineMouse(Gfx::ENG_MOUSE_MOVED, 16, 17, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 9.0f)),
EngineMouse(Gfx::ENG_MOUSE_MOVEI, 18, 19, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 9.0f)),
EngineMouse(Gfx::ENG_MOUSE_MOVE, 20, 21, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(11.0f, 11.0f)),
EngineMouse(Gfx::ENG_MOUSE_TARGET, 22, 23, -1, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(15.0f, 15.0f)),
EngineMouse(Gfx::ENG_MOUSE_SCROLLL, 24, 25, 43, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 2.0f, 9.0f)),
EngineMouse(Gfx::ENG_MOUSE_SCROLLR, 26, 27, 44, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point(17.0f, 9.0f)),
EngineMouse(Gfx::ENG_MOUSE_SCROLLU, 28, 29, 45, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 2.0f)),
EngineMouse(Gfx::ENG_MOUSE_SCROLLD, 30, 31, 46, Gfx::ENG_RSTATE_TCOLOR_BLACK, Gfx::ENG_RSTATE_TCOLOR_WHITE, Math::Point( 9.0f, 17.0f))
};
static const Math::Point MOUSE_SIZE(0.05f, 0.05f);
if (! m_mouseVisible)
return;
if (m_app->GetSystemMouseVisibile())
return;
Gfx::Material material;
material.diffuse = Gfx::Color(1.0f, 1.0f, 1.0f);
material.ambient = Gfx::Color(0.5f, 0.5f, 0.5f);
@ -597,32 +623,24 @@ void Gfx::CEngine::DrawMouse()
SetMaterial(material);
SetTexture("mouse.png");
for (int i = 0; i < Gfx::ENG_MOUSE_COUNT; ++i)
{
if (m_mouseType != MICE[i].type)
continue;
int index = static_cast<int>(m_mouseType);
Math::Point pos;
pos.x = m_mousePos.x - (MICE[i].hotPoint.x * MOUSE_SIZE.x) / 32.0f;
pos.y = m_mousePos.y - ((32.0f - MICE[i].hotPoint.y) * MOUSE_SIZE.y) / 32.0f;
Math::Point pos = m_mousePos;
pos.x = m_mousePos.x - (m_mice[index].hotPoint.x * m_mouseSize.x) / 32.0f;
pos.y = m_mousePos.y - ((32.0f - m_mice[index].hotPoint.y) * m_mouseSize.y) / 32.0f;
Math::Point shadowPos;
shadowPos.x = pos.x+(4.0f/640.0f);
shadowPos.y = pos.y-(3.0f/480.0f);
Math::Point shadowPos;
shadowPos.x = pos.x + (4.0f/800.0f);
shadowPos.y = pos.y - (3.0f/600.0f);
// FIXME: doesn't work yet
SetState(Gfx::ENG_RSTATE_TCOLOR_WHITE);
DrawMouseSprite(shadowPos, m_mouseSize, m_mice[index].iconShadow);
SetState(Gfx::ENG_RSTATE_TCOLOR_WHITE);
DrawMouseSprite(shadowPos, MOUSE_SIZE, MICE[i].iconShadow);
SetState(m_mice[index].mode1);
DrawMouseSprite(pos, m_mouseSize, m_mice[index].icon1);
SetState(MICE[i].mode1);
DrawMouseSprite(pos, MOUSE_SIZE, MICE[i].icon1);
SetState(MICE[i].mode2);
DrawMouseSprite(pos, MOUSE_SIZE, MICE[i].icon2);
break;
}
SetState(m_mice[index].mode2);
DrawMouseSprite(pos, m_mouseSize, m_mice[index].icon2);
}
void Gfx::CEngine::DrawMouseSprite(Math::Point pos, Math::Point size, int icon)
@ -635,8 +653,8 @@ void Gfx::CEngine::DrawMouseSprite(Math::Point pos, Math::Point size, int icon)
float u1 = (32.0f / 256.0f) * (icon % 8);
float v1 = (32.0f / 256.0f) * (icon / 8);
float u2 = (32.0f / 256.0f) + u1;
float v2 = (32.0f / 256.0f) + v1;
float u2 = u1 + (32.0f / 256.0f);
float v2 = v1 + (32.0f / 256.0f);
float dp = 0.5f / 256.0f;
u1 += dp;
@ -649,8 +667,8 @@ void Gfx::CEngine::DrawMouseSprite(Math::Point pos, Math::Point size, int icon)
Gfx::Vertex vertex[4] =
{
Gfx::Vertex(Math::Vector(p1.x, p1.y, 0.0f), normal, Math::Point(u1, v2)),
Gfx::Vertex(Math::Vector(p1.x, p2.y, 0.0f), normal, Math::Point(u1, v1)),
Gfx::Vertex(Math::Vector(p2.x, p1.y, 0.0f), normal, Math::Point(u2, v2)),
Gfx::Vertex(Math::Vector(p1.x, p2.y, 0.0f), normal, Math::Point(u1, v1)),
Gfx::Vertex(Math::Vector(p2.x, p2.y, 0.0f), normal, Math::Point(u2, v1))
};
@ -658,52 +676,6 @@ void Gfx::CEngine::DrawMouseSprite(Math::Point pos, Math::Point size, int icon)
AddStatisticTriangle(2);
}
Gfx::Texture Gfx::CEngine::CreateTexture(const std::string &texName, const Gfx::TextureCreateParams &params)
{
CImage img;
if (! img.Load(m_app->GetDataFilePath(m_texPath, texName)))
{
std::stringstream str;
str << "Couldn't load texture '" << texName << "': " << img.GetError();
m_error = str.str();
return Gfx::Texture(); // invalid texture
}
Gfx::Texture result = m_device->CreateTexture(&img, params);
if (! result.valid)
{
std::stringstream str;
str << "Couldn't load texture '" << texName << "': " << m_device->GetError();
m_error = str.str();
return result;
}
m_texNameMap[texName] = result;
m_revTexNameMap[result] = texName;
return result;
}
Gfx::Texture Gfx::CEngine::CreateTexture(const std::string &texName)
{
return CreateTexture(texName, m_defaultTexParams);
}
void Gfx::CEngine::DestroyTexture(const std::string &texName)
{
std::map<std::string, Gfx::Texture>::iterator it = m_texNameMap.find(texName);
if (it == m_texNameMap.end())
return;
std::map<Gfx::Texture, std::string>::iterator revIt = m_revTexNameMap.find((*it).second);
m_device->DestroyTexture((*it).second);
m_revTexNameMap.erase(revIt);
m_texNameMap.erase(it);
}
void Gfx::CEngine::SetMouseVisible(bool visible)
{
m_mouseVisible = visible;

View File

@ -360,6 +360,7 @@ enum EngineTextureMapping
ENG_TEX_MAPPING_1Z = 6
};
/**
\enum EngineRenderState
\brief Render state of graphics engine
@ -408,6 +409,7 @@ enum EngineRenderState
ENG_RSTATE_TCOLOR_WHITE = (1<<17)
};
/**
\enum EngineMouseType
\brief Type of mouse cursor displayed in-game */
@ -452,6 +454,39 @@ enum EngineMouseType
ENG_MOUSE_COUNT
};
/**
\struct EngineMouse
\brief Information about mouse cursor */
struct EngineMouse
{
//! Index of texture element for 1st image
int icon1;
//! Index of texture element for 2nd image
int icon2;
//! Shadow texture part
int iconShadow;
//! Mode to render 1st image in
Gfx::EngineRenderState mode1;
//! Mode to render 2nd image in
Gfx::EngineRenderState mode2;
//! Hot point
Math::Point hotPoint;
EngineMouse(int icon1 = -1, int icon2 = -1, int iconShadow = -1,
Gfx::EngineRenderState mode1 = Gfx::ENG_RSTATE_NORMAL,
Gfx::EngineRenderState mode2 = Gfx::ENG_RSTATE_NORMAL,
Math::Point hotPoint = Math::Point())
{
this->icon1 = icon1;
this->icon2 = icon2;
this->iconShadow = iconShadow;
this->mode1 = mode1;
this->mode2 = mode2;
this->hotPoint = hotPoint;
}
};
/**
\class CEngine
\brief The graphics engine
@ -934,6 +969,8 @@ protected:
std::map<std::string, Gfx::Texture> m_texNameMap;
std::map<Gfx::Texture, std::string> m_revTexNameMap;
Gfx::EngineMouse m_mice[Gfx::ENG_MOUSE_COUNT];
Math::Point m_mouseSize;
Gfx::EngineMouseType m_mouseType;
Math::Point m_mousePos;
bool m_mouseVisible;

View File

@ -4,4 +4,4 @@ set(CMAKE_BUILD_TYPE debug)
set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0")
include_directories(. ../../..)
add_executable(modelfile_test modelfile_test.cpp ../modelfile.cpp ../../../common/stringutils.cpp ../../../common/iman.cpp)
add_executable(modelfile_test modelfile_test.cpp ../modelfile.cpp ../../../common/logger.cpp ../../../common/stringutils.cpp ../../../common/iman.cpp)

View File

@ -658,7 +658,7 @@ Gfx::Color Gfx::CGLDevice::GetTextureFactor()
return Gfx::Color(color[0], color[1], color[2], color[3]);
}
void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, Vertex *vertices, int vertexCount)
void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, const Vertex *vertices, int vertexCount)
{
if (type == Gfx::PRIMITIVE_LINES)
glBegin(GL_LINES);
@ -671,15 +671,15 @@ void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, Vertex *vertices, in
for (int i = 0; i < vertexCount; ++i)
{
glNormal3fv((GLfloat*)vertices[i].normal.Array());
glMultiTexCoord2fvARB(GL_TEXTURE0_ARB, (GLfloat*)vertices[i].texCoord.Array());
glVertex3fv((GLfloat*)vertices[i].coord.Array());
glNormal3fv(const_cast<GLfloat*>(vertices[i].normal.Array()));
glMultiTexCoord2fvARB(GL_TEXTURE0_ARB, const_cast<GLfloat*>(vertices[i].texCoord.Array()));
glVertex3fv(const_cast<GLfloat*>(vertices[i].coord.Array()));
}
glEnd();
}
void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexCol *vertices, int vertexCount)
void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, const Gfx::VertexCol *vertices, int vertexCount)
{
if (type == Gfx::PRIMITIVE_LINES)
glBegin(GL_LINES);
@ -690,16 +690,16 @@ void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexCol *vert
for (int i = 0; i < vertexCount; ++i)
{
glColor4fv((GLfloat*)vertices[i].color.Array());
glSecondaryColor3fv((GLfloat*)vertices[i].specular.Array());
glMultiTexCoord2fvARB(GL_TEXTURE0_ARB, (GLfloat*)vertices[i].texCoord.Array());
glVertex3fv((GLfloat*)vertices[i].coord.Array());
glColor4fv(const_cast<GLfloat*>(vertices[i].color.Array()));
glSecondaryColor3fv(const_cast<GLfloat*>(vertices[i].specular.Array()));
glMultiTexCoord2fvARB(GL_TEXTURE0_ARB, const_cast<GLfloat*>(vertices[i].texCoord.Array()));
glVertex3fv(const_cast<GLfloat*>(vertices[i].coord.Array()));
}
glEnd();
}
void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, VertexTex2 *vertices, int vertexCount)
void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, const VertexTex2 *vertices, int vertexCount)
{
if (type == Gfx::PRIMITIVE_LINES)
glBegin(GL_LINES);
@ -708,12 +708,14 @@ void Gfx::CGLDevice::DrawPrimitive(Gfx::PrimitiveType type, VertexTex2 *vertices
else if (type == Gfx::PRIMITIVE_TRIANGLE_STRIP)
glBegin(GL_TRIANGLE_STRIP);
glColor3f(1.0f, 1.0f, 1.0f);
for (int i = 0; i < vertexCount; ++i)
{
glNormal3fv((GLfloat*) vertices[i].normal.Array());
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, vertices[i].texCoord.x, vertices[i].texCoord.y);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, vertices[i].texCoord2.x, vertices[i].texCoord2.y);
glVertex3fv((GLfloat*) vertices[i].coord.Array());
glNormal3fv(const_cast<GLfloat*>(vertices[i].normal.Array()));
glMultiTexCoord2fvARB(GL_TEXTURE0_ARB, const_cast<GLfloat*>(vertices[i].texCoord.Array()));
glMultiTexCoord2fvARB(GL_TEXTURE1_ARB, const_cast<GLfloat*>(vertices[i].texCoord.Array()));
glVertex3fv(const_cast<GLfloat*>(vertices[i].coord.Array()));
}
glEnd();

View File

@ -113,9 +113,9 @@ public:
virtual void SetTextureFactor(const Gfx::Color &color);
virtual Gfx::Color GetTextureFactor();
virtual void DrawPrimitive(Gfx::PrimitiveType type, Vertex *vertices, int vertexCount);
virtual void DrawPrimitive(Gfx::PrimitiveType type, Gfx::VertexCol *vertices, int vertexCount);
virtual void DrawPrimitive(Gfx::PrimitiveType type, VertexTex2 *vertices, int vertexCount);
virtual void DrawPrimitive(Gfx::PrimitiveType type, const Gfx::Vertex *vertices, int vertexCount);
virtual void DrawPrimitive(Gfx::PrimitiveType type, const Gfx::VertexCol *vertices, int vertexCount);
virtual void DrawPrimitive(Gfx::PrimitiveType type, const Gfx::VertexTex2 *vertices, int vertexCount);
virtual int ComputeSphereVisibility(const Math::Vector &center, float radius);

View File

@ -38,7 +38,7 @@ void Init(Gfx::CGLDevice *device)
device->SetRenderState(Gfx::RENDER_STATE_DEPTH_TEST, true);
device->SetShadeModel(Gfx::SHADE_SMOOTH);
}
#include <GL/gl.h>
void Render(Gfx::CGLDevice *device)
{
device->BeginScene();
@ -71,8 +71,6 @@ void Render(Gfx::CGLDevice *device)
Gfx::VertexCol line[2] = { Gfx::VertexCol() };
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
for (int x = -40; x <= 40; ++x)
{
line[0].color = Gfx::Color(0.7f + x / 120.0f, 0.0f, 0.0f);

View File

@ -74,6 +74,12 @@ struct Point
return (float*)this;
}
//! Returns the struct cast to <tt>const float*</tt> array; use with care!
inline const float* Array() const
{
return (const float*)this;
}
//! Returns the distance from (0,0) to the point (x,y)
inline float Length()
{

View File

@ -79,6 +79,12 @@ struct Vector
return (float*)this;
}
//! Returns the struct cast to <tt>const float*</tt> array; use with care!
inline const float* Array() const
{
return (const float*)this;
}
//! Returns the vector length
inline float Length() const
{