So I'm coding emoji's into a game that I play on occasion. I originally did it as a vector array with memset, strcpy, etc., but the guys that run the server didn't like the way I did it and requested I do a map instead. I've almost got it working, but it seems to be ignoring one value in the array. Example, <3 creates the love emoji, but it only looks for the 3 instead of looking for both < & 3. I was wondering if someone could give me some tips on fixing this(btw I'm not paid for any of the code I contribute to the server, it's strictly for fun).
typedef std::map<char, string> Emoji; // no struct/class, strictly this typedef
Emoji m_Emojis; // pointer declared in the drawcontext class.
//array that I'm using to handle the emoji drawing.
void MDrawContext::LoadEmojis()
{
m_Emojis['<3'] = "love.png";
m_Emojis[':)'] = "smile.png";
m_Emojis[';)'] = "wink.png";
m_Emojis[':d'] = "excited.png";
m_Emojis[':('] = "sad.png";
m_Emojis[':o'] = "surprised.png";
m_Emojis[':p'] = "tongue.png";
}
//declared in the function that handles text output
unsignedchar c = szCurrent[i], cc = szCurrent[i + 1];
for (auto itor = m_Emojis.begin(); itor != m_Emojis.end(); itor++)
{
if(c == (*itor).first && cc == (*itor).first)
{
bFound = true;
if (bFound)
{
c = ' ';
cc = ' ';
szCurrent[i] = ' ';
szCurrent[i + 1] = ' '; // all 4 of these replace the already existing char in the chatbox with a blank space, so as to prevent overlapping of text onto the emoji.
FLUSH;
SetBitmap(MBitmapManager::Get((*itor).second.c_str())); //uses the string from the array to draw the bitmap
int nSize = SetEmojiSize(); //adjusts emoji size based on user defined resolution.
int Mod = 5; //prevents emotjis from overlapping
Draw(r.x + nLastX - Mod, y, nSize, nSize);
}
break;
}
}
Yeah I ended up getting it working with 2 strings, was a bit complicated since c & cc are both unsigned chars, and changing those would require atleast 8 other functions to be changed. I'm having a new issue now though. I decided I wanted to take my emoji's to the next level and allow user customization. I can't quite figure out how to get my bitmapmanager to draw it though.
Nah it didn't complain about them, it ignored the first character in the key and just looked for the second. The latest thign I posted is post-key change to string. But, in the code above, when I type the map for the emoji (example: :p), it puts nothing instead of putting the bitmap in its place. :(
Edit: I take that back, I just changed it back temporarily and saw it give a warning about truncation?
6:11: warning: multi-character character constant [-Wmultichar] In function 'int main()':
6:9: warning: overflow in implicit constant conversion [-Woverflow]
The fact is that for C++ the :) is not a char. It is a string with two characters. You could store it in a std::string.
Your map uses char as a keytype. Only one character from each of your emojis is actually stored as key. That has plenty of consequences.
I do suggest: typedef std::map<string, string> Emoji;
You were right, i changed it to use 2 strings & it's working fine now. I'm still having an issue making it user customizable though. What I'm trying to do in the code above is somehow search PATH_EMOJIS, and then display only the png that it finds in the path.