Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
25 KB
Referenced Files
None
Subscribers
None
diff --git a/src/GUIOverlay.cpp b/src/GUIOverlay.cpp
new file mode 100644
index 0000000..6868f97
--- /dev/null
+++ b/src/GUIOverlay.cpp
@@ -0,0 +1,100 @@
+/****************************************************************************
+** Copyright (C) 2011 Luka Horvat <redreaper132 at gmail.com>
+** Copyright (C) 2011 Edward Lii <edward_iii at myway.com>
+** Copyright (C) 2011 O. Bahri Gordebak <gordebak at gmail.com>
+**
+**
+** This file may be used under the terms of the GNU General Public
+** License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of
+** this file.
+**
+** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program. If not, see <http://www.gnu.org/licenses/>.
+**
+****************************************************************************/
+#include "Functions.h"
+#include "GameState.h"
+#include "Globals.h"
+#include "Objects.h"
+#include "GUIOverlay.h"
+
+using namespace std;
+
+GUIOverlay::GUIOverlay(GUIObject* root,bool dim):root(root),dim(dim){
+ //First keep the pointer to the current GUIObjectRoot and currentState.
+ parentState=currentState;
+ tempGUIObjectRoot=GUIObjectRoot;
+
+ //Now set the GUIObject root to the new root.
+ currentState=this;
+ GUIObjectRoot=root;
+
+ //Dim the background.
+ if(dim){
+ SDL_FillRect(tempSurface,NULL,0);
+ SDL_SetAlpha(tempSurface,SDL_SRCALPHA,155);
+ SDL_BlitSurface(tempSurface,NULL,screen,NULL);
+ }
+}
+
+GUIOverlay::~GUIOverlay(){
+ //We need to place everything back.
+ currentState=parentState;
+ parentState=NULL;
+
+ //Delete the GUI if present.
+ if(GUIObjectRoot)
+ delete GUIObjectRoot;
+
+ //Now put back the parent gui.
+ GUIObjectRoot=tempGUIObjectRoot;
+ tempGUIObjectRoot=NULL;
+}
+
+
+void GUIOverlay::handleEvents(){
+ //Check if we need to quit, if so we enter the exit state.
+ if(event.type==SDL_QUIT){
+ setNextState(STATE_EXIT);
+ }
+}
+
+//Nothing to do here
+void GUIOverlay::logic(){
+ //Check if the GUIObjectRoot (of the overlay) is deleted.
+ if(!GUIObjectRoot)
+ delete this;
+}
+void GUIOverlay::render(){}
+
+
+void GUIOverlay::resize(){
+ //We recenter the GUI.
+ GUIObjectRoot->left=(SCREEN_WIDTH-GUIObjectRoot->width)/2;
+ GUIObjectRoot->top=(SCREEN_HEIGHT-GUIObjectRoot->height)/2;
+
+ //Now let the parent state resize.
+ GUIObjectRoot=tempGUIObjectRoot;
+ parentState->resize();
+ //NOTE: After the resize it's likely that the GUIObjectRoot is new so we need to update our tempGUIObjectRoot pointer.
+ tempGUIObjectRoot=GUIObjectRoot;
+
+ //Now render the parentState.
+ parentState->render();
+ if(GUIObjectRoot)
+ GUIObjectRoot->render();
+
+ //And set the GUIObjectRoot back to the overlay gui.
+ GUIObjectRoot=root;
+
+ //Dim the background.
+ if(dim){
+ SDL_FillRect(tempSurface,NULL,0);
+ SDL_SetAlpha(tempSurface,SDL_SRCALPHA,155);
+ SDL_BlitSurface(tempSurface,NULL,screen,NULL);
+ }
+}
diff --git a/src/GUIOverlay.h b/src/GUIOverlay.h
new file mode 100644
index 0000000..d7c48ea
--- /dev/null
+++ b/src/GUIOverlay.h
@@ -0,0 +1,57 @@
+
+/****************************************************************************
+** Copyright (C) 2011 Luka Horvat <redreaper132 at gmail.com>
+** Copyright (C) 2011 Edward Lii <edward_iii at myway.com>
+** Copyright (C) 2011 O. Bahri Gordebak <gordebak at gmail.com>
+**
+**
+** This file may be used under the terms of the GNU General Public
+** License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of
+** this file.
+**
+** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program. If not, see <http://www.gnu.org/licenses/>.
+**
+****************************************************************************/
+#ifndef GUI_OVERLAY_H
+#define GUI_OVERLAY_H
+
+#include <SDL/SDL.h>
+#include "GameState.h"
+#include "GUIObject.h"
+
+
+//The GUIOverlay state, this is a special state since it doens't appear in the states list.
+//It is used to properly handle GUIs that overlay the current state, dialogs for example.
+class GUIOverlay : public GameState{
+private:
+ //A pointer to the current state to put back when needed.
+ GameState* parentState;
+
+ //Pointer to the GUI root of the overlay.
+ GUIObject* root;
+ //Pointer to the previous GUIObjectRoot.
+ GUIObject* tempGUIObjectRoot;
+
+ //Boolean if the screen should be dimmed.
+ bool dim;
+public:
+ //Constructor.
+ //root: Pointer to the new GUIObjectRoot.
+ //dim: Boolean if the parent state should be dimmed.
+ GUIOverlay(GUIObject* root,bool dim=true);
+ //Destructor.
+ ~GUIOverlay();
+
+ //Inherited from GameState.
+ void handleEvents();
+ void logic();
+ void render();
+ void resize();
+};
+
+#endif
diff --git a/src/Globals.h b/src/Globals.h
index cee5e4b..fccab8b 100644
--- a/src/Globals.h
+++ b/src/Globals.h
@@ -1,181 +1,181 @@
/****************************************************************************
** Copyright (C) 2011 Luka Horvat <redreaper132 at gmail.com>
** Copyright (C) 2011 Edward Lii <edward_iii at myway.com>
** Copyright (C) 2011 O. Bahri Gordebak <gordebak at gmail.com>
**
**
** This file may be used under the terms of the GNU General Public
** License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/
#ifndef GLOBALS_H
#define GLOBALS_H
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL_ttf.h>
#include <string>
#include "libs/tinygettext/tinygettext.hpp"
#ifdef WIN32
//#define DATA_PATH
#else
#include "config.h"
#endif
#define TITLE_FONT_RAISE 19
#define GUI_FONT_RAISE 5
//Global constants
//The width of the screen.
extern int SCREEN_WIDTH;
//The height of the screen.
extern int SCREEN_HEIGHT;
//The depth of the screen.
const int SCREEN_BPP=32;
//Strin containing the
const std::string version="V0.4 Development version";
//The height of the current level.
extern int LEVEL_HEIGHT;
//The width of the current level.
extern int LEVEL_WIDTH;
//The target frames per seconds.
const int g_FPS=40;
//The language that in which the game should be translated.
extern std::string language;
//The DictionaryManager that is used to translate the game itself.
extern tinygettext::DictionaryManager* dictionaryManager;
//The screen surface, it's used to draw on before it's drawn to the real screen.
extern SDL_Surface* screen;
//SDL_Surface with the same dimensions as screen which can be used for all kinds of (temp) drawing.
extern SDL_Surface* tempSurface;
//The background image for the menu (scaled if needed).
extern SDL_Surface* menuBackground;
//Font that is used for titles.
//Knewave large.
extern TTF_Font* fontTitle;
//Font that is used for captions of buttons and other GUI elements.
//Knewave small.
extern TTF_Font* fontGUI;
//Font that is used for long captions of buttons and other GUI elements.
//Knewave smaller.
extern TTF_Font* fontGUISmall;
//Font that is used for (long) text.
//Blokletter-Viltstift small.
extern TTF_Font* fontText;
//Event, used for event handling.
extern SDL_Event event;
//GUI
class GUIObject;
extern GUIObject *GUIObjectRoot;
//The state id of the current state.
extern int stateID;
//Integer containing what the next state will be.
extern int nextState;
//String containing the name of the current level.
extern std::string levelName;
//SDL rectangle used to store the camera.
//x is the x location of the camera.
//y is the y location of the camera.
//w is the width of the camera. (equal to SCREEN_WIDTH)
//h is the height of the camera. (equal to SCREEN_HEIGHT)
extern SDL_Rect camera;
//Enumeration containing the ids of the game states.
enum GameStates
{
//State null is a special state used to indicate no state.
//This is used when no next state is defined.
STATE_NULL,
//This state is before the actual leveleditor used to make levelpacks.
STATE_LEVEL_EDIT_SELECT,
//This state is for the level editor.
STATE_LEVEL_EDITOR,
//This state is for the main menu.
STATE_MENU,
//This state is for the actual game.
STATE_GAME,
//Special state used when exiting meandmyshadow.
STATE_EXIT,
//This state is for the help screen.
STATE_LEVEL_SELECT,
//This state is for the options screen.
STATE_OPTIONS,
//This state is for the addon screen.
STATE_ADDONS
};
//Enumeration containing the ids of the different block types.
enum GameTileType{
//The normal solid block.
TYPE_BLOCK=0,
//Block representing the start location of the player.
TYPE_START_PLAYER,
//Block representing the start location of the shadow.
TYPE_START_SHADOW,
//The exit of the level.
TYPE_EXIT,
//The shadow block which is only solid for the shadow.
TYPE_SHADOW_BLOCK,
//Block that can kill both the player and the shadow.
TYPE_SPIKES,
//Special point where the player can save.
TYPE_CHECKPOINT,
//Block that will switch the location of the player and the shadow when invoked.
TYPE_SWAP,
//Block that will crumble to dust when stepped on it for the third time.
TYPE_FRAGILE,
//Normal block that moves along a path.
TYPE_MOVING_BLOCK,
//Shadow block that moves along a path.
TYPE_MOVING_SHADOW_BLOCK,
//A spike block that moves along a path.
TYPE_MOVING_SPIKES,
//Special block which, once entered, moves the player/shadow to a different portal.
TYPE_PORTAL,
//A block with a button which can activate or stop moving blocks, converyor belts
TYPE_BUTTON,
//A switch which can activate or stop moving blocks, converyor belts
TYPE_SWITCH,
//Solid block which works like
TYPE_CONVEYOR_BELT,
TYPE_SHADOW_CONVEYOR_BELT,
//Block that contains a message that can be read.
TYPE_NOTIFICATION_BLOCK,
- //A collectable that is able to open locked doors
+ //A collectable that is able to open locked doors
TYPE_COLLECTABLE,
//The (max) number of tiles.
TYPE_MAX
};
#endif
diff --git a/src/InputManager.cpp b/src/InputManager.cpp
index 17e82e5..f822f98 100644
--- a/src/InputManager.cpp
+++ b/src/InputManager.cpp
@@ -1,443 +1,437 @@
/****************************************************************************
** Copyright (C) 2012 me and my shadow developers
**
**
** This file may be used under the terms of the GNU General Public
** License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/
#include "InputManager.h"
#include "Globals.h"
#include "Settings.h"
#include "Functions.h"
#include "GUIObject.h"
#include "GUIListBox.h"
+#include "GUIOverlay.h"
#include <stdlib.h>
#include <stdio.h>
#include <string>
using namespace std;
InputManager inputMgr;
//the order must be the same as InputManagerKeys
static const char* keySettingNames[INPUTMGR_MAX]={
"key_up","key_down","key_left","key_right","key_jump","key_action","key_space",
"key_escape","key_restart","key_tab","key_save","key_load","key_swap",
"key_teleport","key_suicide","key_shift","key_next","key_previous","key_select"
};
//the order must be the same as InputManagerKeys
static const char* keySettingDescription[INPUTMGR_MAX]={
__("Up (in menu)"),__("Down (in menu)"),__("Left"),__("Right"),__("Jump"),__("Action"),__("Space (Record)"),
__("Escape"),__("Restart"),__("Tab (View shadow/Level prop.)"),__("Save game (in editor)"),__("Load game"),__("Swap (in editor)"),
__("Teleport (in editor)"),__("Suicide (in editor)"),__("Shift (in editor)"),__("Next block type (in Editor)"),
__("Previous block type (in editor)"), __("Select (in menu)")
};
class InputDialogHandler:public GUIEventCallback{
private:
//the list box which contains keys.
GUIListBox* listBox;
//the parent object.
InputManager* parent;
//check if it's alternative key
bool isAlternativeKey;
//update specified key config item
void updateConfigItem(int index){
//get the description
std::string s=_(keySettingDescription[index]);
s+=": ";
//get key code name
int keyCode=parent->getKeyCode((InputManagerKeys)index,isAlternativeKey);
s+=_(InputManager::getKeyCodeName(keyCode));
//show it
listBox->item[index]=s;
}
public:
//Constructor.
InputDialogHandler(GUIListBox* listBox,InputManager* parent):listBox(listBox),parent(parent),isAlternativeKey(false){
//load the avaliable keys to the list box.
for(int i=0;i<INPUTMGR_MAX;i++){
//get the description
std::string s=_(keySettingDescription[i]);
s+=": ";
//get key code name
int keyCode=parent->getKeyCode((InputManagerKeys)i,false);
s+=_(InputManager::getKeyCodeName(keyCode));
//add item
listBox->item.push_back(s);
}
}
//when a key is pressed call this to set the key to currently-selected item
void onKeyDown(int keyCode){
//check if an item is selected.
int index=listBox->value;
if(index<0 || index>=INPUTMGR_MAX) return;
//set it.
parent->setKeyCode((InputManagerKeys)index,keyCode,isAlternativeKey);
updateConfigItem(index);
}
void GUIEventCallback_OnEvent(std::string name,GUIObject* obj,int eventType){
//Make sure it's a click event.
if(eventType==GUIEventClick){
if(name=="cmdOK"){
//config is done, exiting
delete GUIObjectRoot;
GUIObjectRoot=NULL;
}else if(name=="cmdUnset"){
onKeyDown(0);
}else if(name=="lstType"){
isAlternativeKey=(obj->value==1);
for(int i=0;i<INPUTMGR_MAX;i++){
updateConfigItem(i);
}
}
/*else if(name=="cmdEscape"){
//set a key to Escape
//simply call onKeyDown().
onKeyDown(SDLK_ESCAPE);
}*/
}
}
};
int InputManager::getKeyCode(InputManagerKeys key,bool isAlternativeKey){
if(isAlternativeKey) return alternativeKeys[key];
else return keys[key];
}
void InputManager::setKeyCode(InputManagerKeys key,int keyCode,bool isAlternativeKey){
if(isAlternativeKey) alternativeKeys[key]=keyCode;
else keys[key]=keyCode;
}
void InputManager::loadConfig(){
int i;
for(i=0;i<INPUTMGR_MAX;i++){
string s=keySettingNames[i];
keys[i]=atoi(getSettings()->getValue(s).c_str());
s+="2";
alternativeKeys[i]=atoi(getSettings()->getValue(s).c_str());
}
}
void InputManager::saveConfig(){
int i;
char c[32];
for(i=0;i<INPUTMGR_MAX;i++){
string s=keySettingNames[i];
sprintf(c,"%d",keys[i]);
getSettings()->setValue(s,c);
s+="2";
sprintf(c,"%d",alternativeKeys[i]);
getSettings()->setValue(s,c);
}
}
-void InputManager::showConfig(){
- //Save the old GUI.
- GUIObject* tmp=GUIObjectRoot;
+//Event handler.
+static InputDialogHandler* handler;
+void InputManager::showConfig(){
//Create the new GUI.
- GUIObjectRoot=new GUIObject((SCREEN_WIDTH-600)/2,(SCREEN_HEIGHT-420)/2,600,400,GUIObjectFrame,_("Config Keys"));
+ GUIObject* GUIObjectRoot=new GUIObject((SCREEN_WIDTH-600)/2,(SCREEN_HEIGHT-420)/2,600,400,GUIObjectFrame,_("Config Keys"));
GUIObject* obj;
obj=new GUIObject(0,44,GUIObjectRoot->width,36,GUIObjectLabel,_("Select an item and press a key to config it."),0,true,true,GUIGravityCenter);
GUIObjectRoot->childControls.push_back(obj);
//The list box.
GUIListBox *listBox=new GUIListBox(20,126,560,220);
-
- //Event handler.
- InputDialogHandler handler(listBox,this);
+ //Create the event handler.
+ handler=new InputDialogHandler(listBox,this);
GUIObjectRoot->childControls.push_back(listBox);
//another box to select key type
GUISingleLineListBox *listBox0=new GUISingleLineListBox(120,80,360,36);
listBox0->name="lstType";
listBox0->item.push_back(_("Primary key"));
listBox0->item.push_back(_("Alternative key"));
listBox0->value=0;
- listBox0->eventCallback=&handler;
+ listBox0->eventCallback=handler;
GUIObjectRoot->childControls.push_back(listBox0);
//two buttons
obj=new GUIObject(32,360,-1,36,GUIObjectButton,_("Unset the key"),0,true,true,GUIGravityLeft);
obj->name="cmdUnset";
- obj->eventCallback=&handler;
- GUIObjectRoot->childControls.push_back(obj);
- /*
- obj=new GUIObject(20,360,360,36,GUIObjectButton,"Set to ESCAPE key");
- obj->name="cmdEscape";
- obj->eventCallback=&handler;
+ obj->eventCallback=handler;
GUIObjectRoot->childControls.push_back(obj);
- */
+
obj=new GUIObject(GUIObjectRoot->width-32,360,-1,36,GUIObjectButton,_("OK"),0,true,true,GUIGravityRight);
obj->name="cmdOK";
- obj->eventCallback=&handler;
+ obj->eventCallback=handler;
GUIObjectRoot->childControls.push_back(obj);
- //Now we keep rendering and updating the GUI.
- SDL_FillRect(tempSurface,NULL,0);
- SDL_SetAlpha(tempSurface,SDL_SRCALPHA,155);
- SDL_BlitSurface(tempSurface,NULL,screen,NULL);
- while(GUIObjectRoot){
- while(SDL_PollEvent(&event)){
- //check if some key is down.
- if(event.type==SDL_KEYDOWN){
- handler.onKeyDown(event.key.keysym.sym);
- }
- //Joystick
- else if(event.type==SDL_JOYAXISMOTION){
- if(event.jaxis.value>3200){
- handler.onKeyDown(0x00010001 | (int(event.jaxis.axis)<<8));
- }else if(event.jaxis.value<-3200){
- handler.onKeyDown(0x000100FF | (int(event.jaxis.axis)<<8));
- }
- }
- else if(event.type==SDL_JOYBUTTONDOWN){
- handler.onKeyDown(0x00020000 | (int(event.jbutton.button)<<8));
- }
- else if(event.type==SDL_JOYHATMOTION){
- if(event.jhat.value & SDL_HAT_LEFT){
- handler.onKeyDown(0x00030000 | (int(event.jhat.hat)<<8) | SDL_HAT_LEFT);
- }else if(event.jhat.value & SDL_HAT_RIGHT){
- handler.onKeyDown(0x00030000 | (int(event.jhat.hat)<<8) | SDL_HAT_RIGHT);
- }else if(event.jhat.value & SDL_HAT_UP){
- handler.onKeyDown(0x00030000 | (int(event.jhat.hat)<<8) | SDL_HAT_UP);
- }else if(event.jhat.value & SDL_HAT_DOWN){
- handler.onKeyDown(0x00030000 | (int(event.jhat.hat)<<8) | SDL_HAT_DOWN);
- }
- }
- //now process GUI events.
- GUIObjectHandleEvents(true);
- }
- if(GUIObjectRoot)
- GUIObjectRoot->render();
- flipScreen();
- SDL_Delay(30);
- }
-
- //Restore the old GUI.
- GUIObjectRoot=tmp;
+ //Create a GUIOverlayState
+ GUIOverlay* overlay=new GUIOverlay(GUIObjectRoot,true);
+
+// //Now we keep rendering and updating the GUI.
+// SDL_FillRect(tempSurface,NULL,0);
+// SDL_SetAlpha(tempSurface,SDL_SRCALPHA,155);
+// SDL_BlitSurface(tempSurface,NULL,screen,NULL);
+// while(GUIObjectRoot){
+// while(SDL_PollEvent(&event)){
+// //check if some key is down.
+// if(event.type==SDL_KEYDOWN){
+// handler.onKeyDown(event.key.keysym.sym);
+// }
+// //Joystick
+// else if(event.type==SDL_JOYAXISMOTION){
+// if(event.jaxis.value>3200){
+// handler.onKeyDown(0x00010001 | (int(event.jaxis.axis)<<8));
+// }else if(event.jaxis.value<-3200){
+// handler.onKeyDown(0x000100FF | (int(event.jaxis.axis)<<8));
+// }
+// }
+// else if(event.type==SDL_JOYBUTTONDOWN){
+// handler.onKeyDown(0x00020000 | (int(event.jbutton.button)<<8));
+// }
+// else if(event.type==SDL_JOYHATMOTION){
+// if(event.jhat.value & SDL_HAT_LEFT){
+// handler.onKeyDown(0x00030000 | (int(event.jhat.hat)<<8) | SDL_HAT_LEFT);
+// }else if(event.jhat.value & SDL_HAT_RIGHT){
+// handler.onKeyDown(0x00030000 | (int(event.jhat.hat)<<8) | SDL_HAT_RIGHT);
+// }else if(event.jhat.value & SDL_HAT_UP){
+// handler.onKeyDown(0x00030000 | (int(event.jhat.hat)<<8) | SDL_HAT_UP);
+// }else if(event.jhat.value & SDL_HAT_DOWN){
+// handler.onKeyDown(0x00030000 | (int(event.jhat.hat)<<8) | SDL_HAT_DOWN);
+// }
+// }
+// //now process GUI events.
+// GUIObjectHandleEvents(true);
+// }
+// if(GUIObjectRoot)
+// GUIObjectRoot->render();
+// flipScreen();
+// SDL_Delay(30);
+// }
}
-
//get key name from key code
std::string InputManager::getKeyCodeName(int keyCode){
char c[64];
if(keyCode>0 && keyCode <0x1000){
//keyboard
char* s=SDL_GetKeyName((SDLKey)keyCode);
if(s!=NULL){
return s;
}else{
sprintf(c,"(Key %d)",keyCode);
return c;
}
}else if(keyCode>0x1000){
//Joystick. first set it to invalid value
sprintf(c,"(Joystick 0x%08X)",keyCode);
//check type
switch((keyCode & 0x00FF0000)>>16){
case 1:
//axis
switch(keyCode & 0xFF){
case 1:
sprintf(c,"Joystick axis %d +",(keyCode & 0x0000FF00)>>8);
break;
case 0xFF:
sprintf(c,"Joystick axis %d -",(keyCode & 0x0000FF00)>>8);
break;
}
break;
case 2:
//button
sprintf(c,"Joystick button %d",(keyCode & 0x0000FF00)>>8);
break;
case 3:
//hat
switch(keyCode & 0xFF){
case SDL_HAT_LEFT:
sprintf(c,"Joystick hat %d left",(keyCode & 0x0000FF00)>>8);
break;
case SDL_HAT_RIGHT:
sprintf(c,"Joystick hat %d right",(keyCode & 0x0000FF00)>>8);
break;
case SDL_HAT_UP:
sprintf(c,"Joystick hat %d up",(keyCode & 0x0000FF00)>>8);
break;
case SDL_HAT_DOWN:
sprintf(c,"Joystick hat %d down",(keyCode & 0x0000FF00)>>8);
break;
}
break;
}
return c;
}else{
//unknown??
return _("(Not set)");
}
}
InputManager::InputManager(){
//clear the array.
for(int i=0;i<INPUTMGR_MAX;i++){
keys[i]=alternativeKeys[i]=keyFlags[i]=0;
}
}
InputManager::~InputManager(){
closeAllJoysticks();
}
int InputManager::getKeyState(int keyCode,int oldState,bool hasEvent){
int state=0;
if(keyCode>0 && keyCode<0x1000){
//keyboard
if(hasEvent){
if(event.type==SDL_KEYDOWN && event.key.keysym.sym==keyCode){
state|=0x2;
}
if(event.type==SDL_KEYUP && event.key.keysym.sym==keyCode){
state|=0x4;
}
}
if(keyCode<SDLK_LAST && SDL_GetKeyState(NULL)[keyCode]){
state|=0x1;
}
}else if(keyCode>0x1000){
//Joystick
int index=(keyCode & 0x0000FF00)>>8;
int value=keyCode & 0xFF;
int i,v;
switch((keyCode & 0x00FF0000)>>16){
case 1:
//axis
if(hasEvent){
if(event.type==SDL_JOYAXISMOTION && event.jaxis.axis==index){
if((value==1 && event.jaxis.value>3200) || (value==0xFF && event.jaxis.value<-3200)){
if((oldState & 0x1)==0) state|=0x2;
}else{
if(oldState & 0x1) state|=0x4;
}
}
}
for(i=0;i<(int)joysticks.size();i++){
v=SDL_JoystickGetAxis(joysticks[i],index);
if((value==1 && v>3200) || (value==0xFF && v<-3200)){
state|=0x1;
break;
}
}
break;
case 2:
//button
if(hasEvent){
if(event.type==SDL_JOYBUTTONDOWN && event.jbutton.button==index){
state|=0x2;
}
if(event.type==SDL_JOYBUTTONUP && event.jbutton.button==index){
state|=0x4;
}
}
for(i=0;i<(int)joysticks.size();i++){
v=SDL_JoystickGetButton(joysticks[i],index);
if(v){
state|=0x1;
break;
}
}
break;
case 3:
//hat
if(hasEvent){
if(event.type==SDL_JOYHATMOTION && event.jhat.hat==index){
if(event.jhat.value & value){
if((oldState & 0x1)==0) state|=0x2;
}else{
if(oldState & 0x1) state|=0x4;
}
}
}
for(i=0;i<(int)joysticks.size();i++){
v=SDL_JoystickGetHat(joysticks[i],index);
if(v & value){
state|=0x1;
break;
}
}
break;
}
}
return state;
}
//update the key state, according to current SDL event, etc.
void InputManager::updateState(bool hasEvent){
for(int i=0;i<INPUTMGR_MAX;i++){
keyFlags[i]=getKeyState(keys[i],keyFlags[i],hasEvent)|getKeyState(alternativeKeys[i],keyFlags[i],hasEvent);
}
}
//check if there is KeyDown event.
bool InputManager::isKeyDownEvent(InputManagerKeys key){
return keyFlags[key]&0x2;
}
//check if there is KeyUp event.
bool InputManager::isKeyUpEvent(InputManagerKeys key){
return keyFlags[key]&0x4;
}
//check if specified key is down.
bool InputManager::isKeyDown(InputManagerKeys key){
return keyFlags[key]&0x1;
}
//open all joysticks.
void InputManager::openAllJoysitcks(){
int i,m;
//First close previous joysticks.
closeAllJoysticks();
//open all joysticks.
m=SDL_NumJoysticks();
for(i=0;i<m;i++){
SDL_Joystick *j=SDL_JoystickOpen(i);
if(j==NULL){
printf("ERROR: Couldn't open Joystick %d\n",i);
}else{
joysticks.push_back(j);
}
}
}
//close all joysticks.
void InputManager::closeAllJoysticks(){
for(int i=0;i<(int)joysticks.size();i++){
SDL_JoystickClose(joysticks[i]);
}
joysticks.clear();
}

File Metadata

Mime Type
text/x-diff
Expires
Tue, Jun 16, 1:06 AM (2 w, 21 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
72689
Default Alt Text
(25 KB)

Event Timeline