Hi
I want use SDL input without create window, ( I want to run my program in the background )
for example at first I want close my program. but if it doesn't have the window, it can't read the keyboard
#include <Windows.h>
#include <stdlib.h>
#include "Input.h"
#ifndef __SDL_H_I__
#define __SDL_H_I__
#include "SDL.h"
#endif
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
SDL_Init(SDL_INIT_EVERYTHING);
Input::Init();
while(1)
{
Input::Update();
if(Input::GetKey(SDLKey::SDLK_u))
{
SDL_Quit();
return 0;
}
}
return 0;
}
#ifndef __INPUT_H__
#define __INPUT_H__
#ifndef __SDL_H_I__
#define __SDL_H_I__
#include "SDL.h"
#endif
class Input
{
public:
static void Init();
static void Update();
static bool GetKey(SDLKey key);
static bool GetKeyDown(SDLKey key);
static bool GetKeyUp(SDLKey key);
static bool GetButton(int btn);
static bool GetButtonDown(int btn);
static bool GetButtonUp(int btn);
private:
static bool _curKey[322];
static bool _preKey[322];
static bool _curBtn[3];
static bool _preBtn[3];
static SDL_Event _event;
};
#endif
#include "Input.h"
bool Input::_curKey[322];
bool Input::_preKey[322];
bool Input::_curBtn[3];
bool Input::_preBtn[3];
SDL_Event Input::_event;
void Input::Init()
{
memset(_curKey,0,sizeof(bool)*322);
memset(_preKey,0,sizeof(bool)*322);
memset(_curBtn,0,sizeof(bool)*322);
memset(_preBtn,0,sizeof(bool)*322);
}
void Input::Update()
{
memcpy(_preKey,_curKey,sizeof(bool)*322);
memcpy(_preBtn,_curBtn,sizeof(bool)*3);
while(SDL_PollEvent(&_event))
{
switch(_event.type)
{
case SDL_KEYDOWN:
{
_curKey[_event.key.keysym.sym] = true;
break;
}
case SDL_KEYUP:
{
_curKey[_event.key.keysym.sym] = false;
break;
}
case SDL_MOUSEBUTTONDOWN:
{
_curBtn[_event.button.button]=true;
break;
}
case SDL_MOUSEBUTTONUP:
{
_curBtn[_event.button.button]=false;
break;
}
}
}
}
bool Input::GetKey(SDLKey key)
{
return _curKey[key];
}
bool Input::GetKeyDown(SDLKey key)
{
return _curKey[key] && !_preKey[key];
}
bool Input::GetKeyUp(SDLKey key)
{
return !_curKey[key] && _preKey[key];
}
bool Input::GetButton( int btn )
{
return _curBtn[btn];
}
bool Input::GetButtonDown( int btn )
{
return _curBtn[btn]==true && _curBtn[btn]==false;
}
bool Input::GetButtonUp( int btn )
{
return _curBtn[btn]==false && _curBtn[btn]==true;
}