git repos / blockattack-game

commit 9b99cf7a

sago007 · 2015-12-04 20:22
9b99cf7a4b0f91511544c9d040b9ab96b550d970 patch · browse files
parent dfdb7d6655b56730ed3bb9b64a69d587b18b931f

Now uses the Unicode value from SDL to find the char that was types. Still limted to ASCII.

Changed files

M source/code/ReadKeyboard.cpp before
M source/code/ReadKeyboard.h before
M source/code/main.cpp before
A source/code/scopeHelpers.hpp
diff --git a/source/code/ReadKeyboard.cpp b/source/code/ReadKeyboard.cpp index a31cc4a..71b1837 100644 --- a/source/code/ReadKeyboard.cpp +++ b/source/code/ReadKeyboard.cpp
@@ -88,6 +88,20 @@ void ReadKeyboard::removeChar() {
}
}
+bool ReadKeyboard::ReadKey(const SDL_keysym& key) {
+ if (
+ (key.unicode >= 'a' && key.unicode <= 'z') ||
+ (key.unicode >= '0' && key.unicode <= '9') ||
+ (key.unicode >= 'A' && key.unicode <= 'Z')) {
+ ReadKeyboard::putchar(key.unicode);
+ return true;
+ }
+ if (key.unicode == '.' || key.unicode == ',') {
+ ReadKeyboard::putchar('.');
+ }
+ return ReadKey(key.sym);
+}
+
bool ReadKeyboard::ReadKey(SDLKey keyPressed) {
if (keyPressed == SDLK_SPACE) {
ReadKeyboard::putchar(' ');
@@ -125,240 +139,6 @@ bool ReadKeyboard::ReadKey(SDLKey keyPressed) {
position++;
return true;
}
- char charToPut;
- if (keyPressed)
- switch (keyPressed) {
- case SDLK_a:
- charToPut = 'a';
- break;
- case SDLK_b:
- charToPut = 'b';
- break;
- case SDLK_c:
- charToPut = 'c';
- break;
- case SDLK_d:
- charToPut = 'd';
- break;
- case SDLK_e:
- charToPut = 'e';
- break;
- case SDLK_f:
- charToPut = 'f';
- break;
- case SDLK_g:
- charToPut = 'g';
- break;
- case SDLK_h:
- charToPut = 'h';
- break;
- case SDLK_i:
- charToPut = 'i';
- break;
- case SDLK_j:
- charToPut = 'j';
- break;
- case SDLK_k:
- charToPut = 'k';
- break;
- case SDLK_l:
- charToPut = 'l';
- break;
- case SDLK_m:
- charToPut = 'm';
- break;
- case SDLK_n:
- charToPut = 'n';
- break;
- case SDLK_o:
- charToPut = 'o';
- break;
- case SDLK_p:
- charToPut = 'p';
- break;
- case SDLK_q:
- charToPut = 'q';
- break;
- case SDLK_r:
- charToPut = 'r';
- break;
- case SDLK_s:
- charToPut = 's';
- break;
- case SDLK_t:
- charToPut = 't';
- break;
- case SDLK_u:
- charToPut = 'u';
- break;
- case SDLK_v:
- charToPut = 'v';
- break;
- case SDLK_w:
- charToPut = 'w';
- break;
- case SDLK_x:
- charToPut = 'x';
- break;
- case SDLK_y:
- charToPut = 'y';
- break;
- case SDLK_z:
- charToPut = 'z';
- break;
- case SDLK_0:
- charToPut = '0';
- break;
- case SDLK_1:
- charToPut = '1';
- break;
- case SDLK_2:
- charToPut = '2';
- break;
- case SDLK_3:
- charToPut = '3';
- break;
- case SDLK_4:
- charToPut = '4';
- break;
- case SDLK_5:
- charToPut = '5';
- break;
- case SDLK_6:
- charToPut = '6';
- break;
- case SDLK_7:
- charToPut = '7';
- break;
- case SDLK_8:
- charToPut = '8';
- break;
- case SDLK_9:
- charToPut = '9';
- break;
- case SDLK_KP0:
- charToPut = '0';
- break;
- case SDLK_KP1:
- charToPut = '1';
- break;
- case SDLK_KP2:
- charToPut = '2';
- break;
- case SDLK_KP3:
- charToPut = '3';
- break;
- case SDLK_KP4:
- charToPut = '4';
- break;
- case SDLK_KP5:
- charToPut = '5';
- break;
- case SDLK_KP6:
- charToPut = '6';
- break;
- case SDLK_KP7:
- charToPut = '7';
- break;
- case SDLK_KP8:
- charToPut = '8';
- break;
- case SDLK_KP9:
- charToPut = '9';
- break;
- case SDLK_KP_PERIOD:
- case SDLK_PERIOD:
- charToPut='.';
- break;
- default:
- return false;
- }
- if ((keys[SDLK_LSHIFT]) || (keys[SDLK_RSHIFT])) {
- switch (charToPut) {
- case 'a':
- charToPut = 'A';
- break;
- case 'b':
- charToPut = 'B';
- break;
- case 'c':
- charToPut = 'C';
- break;
- case 'd':
- charToPut = 'D';
- break;
- case 'e':
- charToPut = 'E';
- break;
- case 'f':
- charToPut = 'F';
- break;
- case 'g':
- charToPut = 'G';
- break;
- case 'h':
- charToPut = 'H';
- break;
- case 'i':
- charToPut = 'I';
- break;
- case 'j':
- charToPut = 'J';
- break;
- case 'k':
- charToPut = 'K';
- break;
- case 'l':
- charToPut = 'L';
- break;
- case 'm':
- charToPut = 'M';
- break;
- case 'n':
- charToPut = 'N';
- break;
- case 'o':
- charToPut = 'O';
- break;
- case 'p':
- charToPut = 'P';
- break;
- case 'q':
- charToPut = 'Q';
- break;
- case 'r':
- charToPut = 'R';
- break;
- case 's':
- charToPut = 'S';
- break;
- case 't':
- charToPut = 'T';
- break;
- case 'u':
- charToPut = 'U';
- break;
- case 'v':
- charToPut = 'V';
- break;
- case 'w':
- charToPut = 'W';
- break;
- case 'x':
- charToPut = 'X';
- break;
- case 'y':
- charToPut = 'Y';
- break;
- case 'z':
- charToPut = 'Z';
- break;
-
- default:
- charToPut = charToPut;
- }
- }
- ReadKeyboard::putchar(charToPut);
return true;
}
diff --git a/source/code/ReadKeyboard.h b/source/code/ReadKeyboard.h index b4be478..4e39b53 100644 --- a/source/code/ReadKeyboard.h +++ b/source/code/ReadKeyboard.h
@@ -40,6 +40,7 @@ public:
~ReadKeyboard(void);
ReadKeyboard(const char*);
Uint8 CharsBeforeCursor(); //Where should the cursor be placed?
+ bool ReadKey(const SDL_keysym&);
bool ReadKey(SDLKey); //true if key accepted
const char* GetString(void);
};
diff --git a/source/code/main.cpp b/source/code/main.cpp index d457dc4..c7046e1 100644 --- a/source/code/main.cpp +++ b/source/code/main.cpp
@@ -23,6 +23,7 @@ http://blockattack.sf.net
#include "common.h"
#include "global.hpp"
+#include "scopeHelpers.hpp"
#include <string.h>
@@ -561,7 +562,6 @@ static int InitImages() {
CONVERTA(bSaveToFile);
CONVERTA(bTestPuzzle);
#endif
-
SDL_Color nf_button_color, nf_standard_blue_color, nf_standard_small_color;
memset(&nf_button_color,0,sizeof(SDL_Color));
nf_button_color.b = 255;
@@ -1467,14 +1467,13 @@ void MakeBackground(int xsize,int ysize,BlockGame& theGame, BlockGame& theGame2)
bool OpenDialogbox(int x, int y, char* name) {
bool done = false; //We are done!
bool accept = false; //New name is accepted! (not Cancelled)
- bool repeating = false; //The key is being held (BACKSPACE)
- const int repeatDelay = 200; //Repeating
unsigned long time = 0;
ReadKeyboard rk = ReadKeyboard(name);
Uint8* keys;
string strHolder;
MakeBackground(xsize,ysize);
DrawIMG(background,screen,0,0);
+ SDLUnicodeScope unicodeScope;
while (!done && !Config::getInstance()->isShuttingDown()) {
DrawIMG(dialogBox,screen,x,y);
NFont_Write(screen, x+40,y+76,rk.GetString());
@@ -1504,39 +1503,25 @@ bool OpenDialogbox(int x, int y, char* name) {
accept = false;
}
else if (!(event.key.keysym.sym == SDLK_BACKSPACE)) {
- if ((rk.ReadKey(event.key.keysym.sym))&&(SoundEnabled)&&(!NoSound)) {
+ if ((rk.ReadKey(event.key.keysym))&&(SoundEnabled)&&(!NoSound)) {
Mix_PlayChannel(1,typingChunk,0);
}
}
- else if ((event.key.keysym.sym == SDLK_BACKSPACE)&&(!repeating)) {
- if ((rk.ReadKey(event.key.keysym.sym))&&(SoundEnabled)&&(!NoSound)) {
+ else if (event.key.keysym.sym == SDLK_BACKSPACE) {
+ if ((rk.ReadKey(event.key.keysym))&&(SoundEnabled)&&(!NoSound)) {
Mix_PlayChannel(1,typingChunk,0);
}
- repeating = true;
- time=SDL_GetTicks();
}
}
} //while(event)
- if (SDL_GetTicks()>(time+repeatDelay)) {
- time = SDL_GetTicks();
- keys = SDL_GetKeyState(nullptr);
- if ( (keys[SDLK_BACKSPACE])&&(repeating) ) {
- if ((rk.ReadKey(SDLK_BACKSPACE))&&(SoundEnabled)&&(!NoSound)) {
- Mix_PlayChannel(1,typingChunk,0);
- }
- }
- else {
- repeating = false;
- }
- }
-
SDL_Flip(screen); //Update screen
} //while(!done)
strcpy(name,rk.GetString());
bScreenLocked = false;
showDialog = false;
+ unicodeScope.Release();
return accept;
}
diff --git a/source/code/scopeHelpers.hpp b/source/code/scopeHelpers.hpp new file mode 100644 index 0000000..83e18e8 --- /dev/null +++ b/source/code/scopeHelpers.hpp
@@ -0,0 +1,45 @@
+/*
+===========================================================================
+blockattack - Block Attack - Rise of the Blocks
+Copyright (C) 2005-2012 Poul Sander
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see http://www.gnu.org/licenses/
+
+Source information and contacts persons can be found at
+http://www.blockattack.net
+===========================================================================
+*/
+
+#ifndef SCOPEHELPERS_HPP
+#define SCOPEHELPERS_HPP
+
+class SDLUnicodeScope {
+private:
+ int oldStatus;
+public:
+ SDLUnicodeScope() {
+ oldStatus = SDL_EnableUNICODE(1);
+ }
+
+ void Release() {
+ SDL_EnableUNICODE(oldStatus);
+ }
+
+ ~SDLUnicodeScope() {
+ Release();
+ }
+};
+
+#endif /* SCOPEHELPERS_HPP */
+