For my informatics studies I have to write a Space Invaders Clone using Windows API.
One of our tasks is to write and display a Highscore list which works very well.
If you have lost all credits you should be able to write your name into the highscore list (if you have enough points).
Windows API has no easy possibility to get a text input so I wrote an own version that works not as I want it to.
I use pName to write the name and save it into an array called “Name”.
This array is used to save the Highscore and the name together into the Highscore.txt.
The Saving progress is working fine so far.
But my problem now is:
I’m able to write letters but they’re sorted automatically.
So if i input: asdf
it is sorted: adfs
I can’t see any code that does something like that so where’s my fault?
Thank you guys!
Greets Michael
bool HighScore::EnterName(HDC hdc)
{
#define VK_A 0x41
#define VK_B 0x42
#define VK_C 0x43
#define VK_D 0x44
#define VK_E 0x45
#define VK_F 0x46
#define VK_G 0x47
#define VK_H 0x48
#define VK_I 0x49
#define VK_J 0x4A
#define VK_K 0x4B
#define VK_L 0x4C
#define VK_M 0x4D
#define VK_N 0x4E
#define VK_O 0x4F
#define VK_P 0x50
#define VK_Q 0x51
#define VK_R 0x52
#define VK_S 0x53
#define VK_T 0x54
#define VK_U 0x55
#define VK_V 0x56
#define VK_W 0x57
#define VK_X 0x58
#define VK_Y 0x59
#define VK_Z 0x5A
string pName;
if(GetKeyState(VK_A)) pName.append("A"); if(GetKeyState(VK_B)) pName.append("B");
if(GetKeyState(VK_C)) pName.append("C"); if(GetKeyState(VK_D)) pName.append("D");
if(GetKeyState(VK_E)) pName.append("E"); if(GetKeyState(VK_F)) pName.append("F");
if(GetKeyState(VK_G)) pName.append("G"); if(GetKeyState(VK_H)) pName.append("H");
if(GetKeyState(VK_I)) pName.append("I"); if(GetKeyState(VK_J)) pName.append("J");
if(GetKeyState(VK_K)) pName.append("K"); if(GetKeyState(VK_L)) pName.append("L");
if(GetKeyState(VK_M)) pName.append("M"); if(GetKeyState(VK_N)) pName.append("N");
if(GetKeyState(VK_O)) pName.append("O"); if(GetKeyState(VK_P)) pName.append("P");
if(GetKeyState(VK_Q)) pName.append("Q"); if(GetKeyState(VK_R)) pName.append("R");
if(GetKeyState(VK_S)) pName.append("S"); if(GetKeyState(VK_T)) pName.append("T");
if(GetKeyState(VK_U)) pName.append("U"); if(GetKeyState(VK_V)) pName.append("V");
if(GetKeyState(VK_W)) pName.append("W"); if(GetKeyState(VK_X)) pName.append("X");
if(GetKeyState(VK_Y)) pName.append("Y"); if(GetKeyState(VK_Z)) pName.append("Z");
TextOut(hdc, 20, 200, "TRAGE DEINEN NAMEN EIN",22);
if(GetAsyncKeyState(VK_BACK)) pName.erase(pName.begin() + pName.length()-1);
sprintf(Name,"%s", pName.c_str());
TextOut(hdc, 50,250,Name, strlen(Name));
return true;
}
I found a soultion that was pretty easy.
I made a big fault that I couldn’t see after hours of programming:
I initiate my string pName again ang again from the beginning. So I moved it to my constructor and now it all works fine.
To get a better sensibility, you have to change
(GetKeyState(VK_LETTER))to
Now it works much better!