Files
wagic/projects/mtg/src/TranslateKeys.cpp
wrenczes@gmail.com 76cba56a1c Resuming on my threading support work with the card caching mechanism. This change unfortunately touches quite a few files, but I needed to get it out of the way before things got out of hand: one significant hurdle is the assumed lifetime of a JQuad pointer. In a single threaded model, the life time of the pointer is clear: you fetch it into the cache, the cache makes room, you use the pointer immediately. In a multithreaded context however, it's unsafe, as the drawing thread can request a few JQuads, and the cache operating on a separate thread can potentially bounce a JQuad out of the cache before the draw routine is done using it, which ends up in an access violation when you attempt to draw using an invalidated quad pointer. To prevent this, the bulk of this change swaps out the use of naked JQuad* pointers in the code with a JQuadPtr, which is basically a typedef to a boost shared_ptr<JQuad>.
This btw points out another circular dependancy between the texture and the JQuad - a texture owns a bunch of JQuads, yet the renderer uses JQuads and always assumes that the texture is valid.  We're going to need to add more defensiveness to JGE to protect against this.

Other changes in this check-in:  WResourceManager doesn't derive from JResourceManager anymore.  It actually didn't require anything from the base, so I killed the dependency.  Also cleaned up the notion of a WTrackedQuad in the WCachedResource - it didn't need a separate class, just a better container.

I've build this & tested against PSP, win, linux, QT (linux).  I haven't tried against iOS and QT Win, or Maemo.  If these other platforms are broken, I apologize in advance! - I'm hoping it should be fairly simple to put them back into play.
2011-02-01 10:37:21 +00:00

210 lines
12 KiB
C++

#include "PrecompiledHeader.h"
#include "Translate.h"
#include "WResourceManager.h"
#include "TranslateKeys.h"
using std::string;
using std::map;
static map<const LocalKeySym, KeyRep> fattable;
static map<const JButton, KeyRep> slimtable;
#if defined(LINUX) || defined (IOS)
const KeyRep& translateKey(LocalKeySym key)
{
{
map<const LocalKeySym, KeyRep>::iterator res;
if ((res = fattable.find(key)) != fattable.end())
return res->second;
}
char* str = NULL;
#if !defined(QT_CONFIG) && !defined(IOS)
str = XKeysymToString(key);
#endif // QT_CONFIG
if (!str)
{
str = NEW char[11];
sprintf(str, "%lu", key);
}
const KeyRep k = make_pair(str, static_cast<JQuad*>(NULL));
fattable[key] = k;
return fattable[key];
}
#else
#ifdef WIN32
const KeyRep& translateKey(LocalKeySym key)
{
{
map<const LocalKeySym, KeyRep>::iterator res;
if ((res = fattable.find(key)) != fattable.end())
return (res->second);
}
unsigned int sc = MapVirtualKey(key, 0);
switch (key)
{
case VK_LEFT: case VK_UP: case VK_RIGHT: case VK_DOWN: // arrow keys
case VK_PRIOR: case VK_NEXT: // page up and page down
case VK_END: case VK_HOME:
case VK_INSERT: case VK_DELETE:
case VK_DIVIDE: // numpad slash
case VK_NUMLOCK:
{
sc |= 0x100; // set extended bit
break;
}
}
char buf[256];
memset(buf, 0, 256);
string s;
// Convert to ANSI string
if (GetKeyNameTextA(sc << 16, buf, 256) > 0)
s = buf;
KeyRep k;
if (0 == s.length())
{
char*str = NEW char[11];
sprintf(str, "%d", key);
k = make_pair(str, static_cast<JQuad*>(NULL));
}
else k = make_pair(s, static_cast<JQuad*>(NULL));
fattable[key] = k;
return fattable[key];
}
#else // PSP
const KeyRep& translateKey(LocalKeySym key)
{
map<const LocalKeySym, KeyRep>::iterator res;
if ((res = fattable.find(key)) == fattable.end())
{
if (fattable.end() == fattable.find(PSP_CTRL_SELECT))
{
fattable[PSP_CTRL_SELECT] = make_pair(_("Select"), static_cast<JQuad*>(NULL));
fattable[PSP_CTRL_START] = make_pair(_("Start"), static_cast<JQuad*>(NULL));
fattable[PSP_CTRL_UP] = make_pair(_("Up"), static_cast<JQuad*>(NULL));
fattable[PSP_CTRL_RIGHT] = make_pair(_("Right"), static_cast<JQuad*>(NULL));
fattable[PSP_CTRL_DOWN] = make_pair(_("Down"), static_cast<JQuad*>(NULL));
fattable[PSP_CTRL_LEFT] = make_pair(_("Left"), static_cast<JQuad*>(NULL));
fattable[PSP_CTRL_LTRIGGER] = make_pair(_("Left trigger"), static_cast<JQuad*>(NULL));
fattable[PSP_CTRL_RTRIGGER] = make_pair(_("Right trigger"), static_cast<JQuad*>(NULL));
fattable[PSP_CTRL_TRIANGLE] = make_pair(_("Triangle"), static_cast<JQuad*>(NULL));
fattable[PSP_CTRL_CIRCLE] = make_pair(_("Circle"), static_cast<JQuad*>(NULL));
fattable[PSP_CTRL_CROSS] = make_pair(_("Cross"), static_cast<JQuad*>(NULL));
fattable[PSP_CTRL_SQUARE] = make_pair(_("Square"), static_cast<JQuad*>(NULL));
fattable[PSP_CTRL_HOLD] = make_pair(_("Hold"), static_cast<JQuad*>(NULL));
}
else
{
char* str = NEW char[11];
sprintf(str, "%d", (int) key);
fattable[key] = make_pair(str, static_cast<JQuad*> (static_cast<JQuad*> (NULL)));
}
res = fattable.find(key);
}
KeyRep& k = res->second;
switch (key)
{
case PSP_CTRL_SELECT : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)2*32, 32, 64, 32, "PSP_CTRL_SELECT", RETRIEVE_NORMAL).get(); break;
case PSP_CTRL_START : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)0*32, 32, 64, 32, "PSP_CTRL_START", RETRIEVE_NORMAL).get(); break;
case PSP_CTRL_UP : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)0*32, 0, 32, 32, "PSP_CTRL_UP", RETRIEVE_NORMAL).get(); break;
case PSP_CTRL_RIGHT : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)3*32, 0, 32, 32, "PSP_CTRL_RIGHT", RETRIEVE_NORMAL).get(); break;
case PSP_CTRL_DOWN : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)1*32, 0, 32, 32, "PSP_CTRL_DOWN", RETRIEVE_NORMAL).get(); break;
case PSP_CTRL_LEFT : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)2*32, 0, 32, 32, "PSP_CTRL_LEFT", RETRIEVE_NORMAL).get(); break;
case PSP_CTRL_LTRIGGER : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)6*32, 32, 64, 32, "PSP_CTRL_LTRIGGER", RETRIEVE_NORMAL).get(); break;
case PSP_CTRL_RTRIGGER : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)8*32, 32, 64, 32, "PSP_CTRL_RTRIGGER", RETRIEVE_NORMAL).get(); break;
case PSP_CTRL_TRIANGLE : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)5*32, 0, 32, 32, "PSP_CTRL_TRIANGLE", RETRIEVE_NORMAL).get(); break;
case PSP_CTRL_CIRCLE : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)4*32, 0, 32, 32, "PSP_CTRL_CIRCLE", RETRIEVE_NORMAL).get(); break;
case PSP_CTRL_CROSS : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)7*32, 0, 32, 32, "PSP_CTRL_CROSS", RETRIEVE_NORMAL).get(); break;
case PSP_CTRL_SQUARE : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)6*32, 0, 32, 32, "PSP_CTRL_SQUARE", RETRIEVE_NORMAL).get(); break;
case PSP_CTRL_HOLD : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)4*32, 0, 32, 32, "PSP_CTRL_HOLD", RETRIEVE_NORMAL).get(); break;
default: /* Unknown key : no icon */ ;
}
return k;
}
#endif
#endif
const KeyRep& translateKey(JButton key) {
/*
{
map<const JButton, KeyRep>::iterator res;
if ((res = slimtable.find(key)) != slimtable.end())
return res->second;
}
slimtable[JGE_BTN_NONE] = make_pair(_("None"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_QUIT] = make_pair(_("Quit"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_MENU] = make_pair(_("Menu"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_CTRL] = make_pair(_("Control"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_POWER] = make_pair(_("Power"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_SOUND] = make_pair(_("Sound"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_RIGHT] = make_pair(_("Right"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_LEFT] = make_pair(_("Left"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_UP] = make_pair(_("Up"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_DOWN] = make_pair(_("Down"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_OK] = make_pair(_("Ok"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_CANCEL] = make_pair(_("Cancel"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_PRI] = make_pair(_("Primary"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_SEC] = make_pair(_("Secondary"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_PREV] = make_pair(_("Next phase/Previous item"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_NEXT] = make_pair(_("Open hand/Next item"), static_cast<JQuad*>(NULL));
return slimtable[key];
*/
map<const JButton, KeyRep>::iterator res;
if ((res = slimtable.find(key)) == slimtable.end())
{
if (slimtable.end() == slimtable.find(JGE_BTN_CTRL))
{
slimtable[JGE_BTN_NONE] = make_pair(_("Delete this binding"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_CTRL] = make_pair(_("Select"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_MENU] = make_pair(_("Start"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_UP] = make_pair(_("Up"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_RIGHT] = make_pair(_("Right"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_DOWN] = make_pair(_("Down"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_LEFT] = make_pair(_("Left"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_PREV] = make_pair(_("Left trigger"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_NEXT] = make_pair(_("Right trigger"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_CANCEL] = make_pair(_("Triangle"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_OK] = make_pair(_("Circle"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_SEC] = make_pair(_("Cross"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_PRI] = make_pair(_("Square"), static_cast<JQuad*>(NULL));
slimtable[JGE_BTN_FULLSCREEN] = make_pair(_("Fullscreen"), static_cast<JQuad*>(NULL));
}
else
{
char* str = NEW char[11];
sprintf(str, "%d", key);
slimtable[key] = make_pair(str, static_cast<JQuad*> (static_cast<JQuad*> (NULL)));
}
res = slimtable.find(key);
}
KeyRep& k = res->second;
switch (key)
{
case JGE_BTN_CTRL : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)2*32, 32, 64, 32, "PSP_CTRL_SELECT", RETRIEVE_NORMAL).get(); break;
case JGE_BTN_MENU : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)0*32, 32, 64, 32, "PSP_CTRL_START", RETRIEVE_NORMAL).get(); break;
case JGE_BTN_UP : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)0*32, 0, 32, 32, "PSP_CTRL_UP", RETRIEVE_NORMAL).get(); break;
case JGE_BTN_RIGHT : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)3*32, 0, 32, 32, "PSP_CTRL_RIGHT", RETRIEVE_NORMAL).get(); break;
case JGE_BTN_DOWN : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)1*32, 0, 32, 32, "PSP_CTRL_DOWN", RETRIEVE_NORMAL).get(); break;
case JGE_BTN_LEFT : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)2*32, 0, 32, 32, "PSP_CTRL_LEFT", RETRIEVE_NORMAL).get(); break;
case JGE_BTN_PREV : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)6*32, 32, 64, 32, "PSP_CTRL_LTRIGGER", RETRIEVE_NORMAL).get(); break;
case JGE_BTN_NEXT : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)8*32, 32, 64, 32, "PSP_CTRL_RTRIGGER", RETRIEVE_NORMAL).get(); break;
case JGE_BTN_CANCEL : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)5*32, 0, 32, 32, "PSP_CTRL_TRIANGLE", RETRIEVE_NORMAL).get(); break;
case JGE_BTN_OK : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)4*32, 0, 32, 32, "PSP_CTRL_CIRCLE", RETRIEVE_NORMAL).get(); break;
case JGE_BTN_SEC : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)7*32, 0, 32, 32, "PSP_CTRL_CROSS", RETRIEVE_NORMAL).get(); break;
case JGE_BTN_PRI : k.second = WResourceManager::Instance()->RetrieveQuad("iconspsp.png", (float)6*32, 0, 32, 32, "PSP_CTRL_SQUARE", RETRIEVE_NORMAL).get(); break;
default: /* Unknown key : no icon */ ;
}
return k;
}