不带窗口使用SDL输入 [英] Use SDL Input without window

查看:59
本文介绍了不带窗口使用SDL输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想在没有创建窗口的情况下使用SDL输入,(我想在后台运行我的程序)



例如我最初想要关闭我的程序。但是如果它没有窗口,它就无法读取键盘



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;
}

推荐答案

你可以考虑妥协,如果SDL需要一个窗口但是让它不可见,就有一个窗口。每个Windows窗口都有一个样式设置,用于确定它是否可见,例如MFC中的WS_VISIBLE。 SDL将具有相同的功能。

但是,如果您需要获取键盘输入,那么您的窗口不应该是不可见的或在后台运行,因为它正在与用户交互。这两个要求似乎是矛盾的。
You could consider a compromise, have a Window if SDL needs one but make it invisible. Every Windows Window has a style setting which determines if it''s visible e.g. WS_VISIBLE in MFC. SDL will have the equivalent.
However if you need to be getting keyboard input then your Window should not be invisible or running in the background because it is interacting with the user. These two requirments would seem to be in contradiction.


这篇关于不带窗口使用SDL输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆