Issue with map array

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).

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
unsigned char 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;
      }
  }
Last edited on
'<3' is supposed to be a string, not a char
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.

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#define PATH_EMOJIS "CUSTOM/Emojis/"
for (auto itor = m_Emojis.begin(); itor != m_Emojis.end(); itor++)
{
	if (!ZGetConfiguration()->GetEtc()->bEmoji) continue;

	if (c && cc && (*itor).first.length() == 2 && c == (*itor).first.c_str()[0] && cc == (*itor).first.c_str()[1] ||
		c && cc && (*itor).first.length() == 2 && c == tolower((*itor).first.c_str()[0]) && cc == tolower((*itor).first.c_str()[1]))
	{
		bFound = true;
		if (bFound)
		{
			c = ' ';
			cc = ' ';
			szCurrent[i] = ' ';
			szCurrent[i + 1] = ' ';
			FLUSH;
			char szPath[256] = PATH_EMOJIS;
			strcpy(szPath, (*itor).second.c_str());
			SetBitmap(MBitmapManager::Get(szPath));
			int nSize = SetEmojiSize();
			int Mod = 5;
			Draw(r.x + nLastX - Mod, y, nSize, nSize);
		}
		break;
	}
}


I know my strcpy is correct, I can't figure out why it's not drawing though.
Last edited on
Doesn't your compiler complain about those 'ab'?

Single quotes surround a char.

Double quotes surround a C-string literal. "ab"

The key in your map is a char. One character.
'a' is a char. 'b' is a char.
"ab" is const char *, and 'ab' ... I have no idea.
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?
Last edited on
Some things are easy to test:
1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{
    char foo;
    foo = ':)';
    std::cout << foo << '\n';
    return 0;
}






)

And the compiler isn't exactly happy:
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.

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
					for (auto itor = m_Emojis.begin(); itor != m_Emojis.end(); itor++)
					{
						if (!ZGetConfiguration()->GetEtc()->bEmoji) continue;

						if (c && cc && (*itor).first.length() == 2 && c == (*itor).first.c_str()[0] && cc == (*itor).first.c_str()[1]||
							c && cc && (*itor).first.length() == 2 && c == tolower((*itor).first.c_str()[0]) && cc == tolower((*itor).first.c_str()[1]))
						{
							bFound = true;
							if (bFound)
							{
								c = ' ';
								cc = ' ';
								szCurrent[i] = ' ';
								szCurrent[i + 1] = ' ';
								FLUSH;
								char szEmoji[256] = PATH_EMOJIS;
								strcpy(szEmoji, (*itor).second.c_str());
								strcat(szEmoji, (*itor).second.c_str());
								SetBitmap(MBitmapManager::Get(szEmoji));
								int nSize = SetEmojiSize();
								int Mod = 5;
								Draw(r.x + nLastX - Mod, y, nSize, nSize);
							}
							break;
						}
					}
Topic archived. No new replies allowed.