C++ Visual Studio Express 2013 SDL_Init_SubSystem(SDL_INIT_EVENTS)失败

问题描述:

我想制作一个基本的游戏引擎,并且我希望控制器由SDL运行,如果可以的话。我的最终目标是将所有SDL特定代码抽象为一个Controller类,但我只是试图让它现在起作用。以下是我有:C++ Visual Studio Express 2013 SDL_Init_SubSystem(SDL_INIT_EVENTS)失败

//User defined Includes 
#include <Atom.h> 
#include <Timer.h> 
#include <Renderer.h> 
#include <OGLRenderingWindow.h> 

//temp includes for now 
#include <Box.h> 
#include <Point.hpp> 
#include <Color.hpp> 

int WINAPI WinMain(HINSTANCE hInstance, 
       HINSTANCE hPrevInstance, 
       LPSTR  cmdLine, 
       S32  cmdShow) 
{ 
//Window settings, will be abstrated out to a config file later 
const S32 windowHeight   = 1072; 
const S32 windowWidth   = 768; 
const S32 windowBPP    = 32; 
const bool windowFullScreen  = false; 

//Start up OGL Window 
OGLRenderingWindow programWindow(hInstance); 

if(!programWindow.Init(windowHeight, windowWidth, windowBPP, windowFullScreen)) { 
    MessageBox(NULL, "Unable to create the OpenGL Window", "An Error occured", MB_ICONERROR | MB_OK); 
    programWindow.ShutDown(); 
    return 1; 
} 

//Start Timer 
Timer* timer = Timer::Instance(); 

Renderer* renderer = Renderer::Instance(); 

if(SDL_Init(0)) { 
    MessageBox(NULL, "Unable to initialize SDL", "An Error occured", MB_ICONERROR | MB_OK); 
    programWindow.ShutDown(); 
    return 1; 
} 

if(!SDL_InitSubSystem(SDL_INIT_EVENTS)) { 
    MessageBox(NULL, "Unable to initialize SDL_EVENTS", "An Error occured", MB_ICONERROR | MB_OK); 
    programWindow.ShutDown(); 
    return 1; 
} 

Point<> pos(0.0f, 0.0f, 0.0f); 
Color<> col(1.0f, 0.0f, 0.0f); 

Box redbox(25.0f, 25.0f, pos, col); 

pos = new Point<>(80.0f, 80.0f, 0.0f); 
col = new Color<>(0.0f, 0.0f, 1.0f); 

Box greenbox(25.0f, 25.0f, pos, col); 

pos = new Point<>(-80.0f, 80.0f, 0.0f); 
col = new Color<>(0.0f, 1.0f, 0.0f); 

Box bluebox(25.0f, 25.0f, pos, col); 

//Main Loop Beings 
while(programWindow.isRunning()) { 
    //Proecess windows events 
    programWindow.ProcessEvents(); 

    //Update the Timer 
    timer->Update(); 

    //Test cell render 
    redbox.v_Update(); 
    redbox.v_Render(); 
    greenbox.v_Render(); 
    bluebox.v_Render(); 
    //WorldManager Update 

    //WorldManager Render 

    //Force Render at the end of the frame 
    renderer->Render(); 

    programWindow.Swap(); 
} 
programWindow.ShutDown(); 

//redbox.v_ShutDown(); 
//greenbox.v_ShutDown(); 
//bluebox.v_ShutDown(); 
delete timer; 
delete renderer; 


return 0; 

}

这里是一个正被调用框中的更新(redbox.v_Update();)

void Box::v_Update(void) { 

//_keyCode = _controller->UpdateInput(); 

SDL_Event event; 

while (SDL_PollEvent(&event)) { 
    switch (event.type){ 
     case SDL_KEYDOWN: { 
      switch (event.key.keysym.sym) { 
       case SDLK_UP: 
        _velosity = Vector<F32>(0.0f, 1.0f, 0.0f); 
        break; 
       case SDLK_DOWN: 
        _velosity = Vector<F32>(0.0f, -1.0f, 0.0f); 
        break; 
       case SDLK_RIGHT: 
       case 3: 
        _velosity = Vector<F32>(1.0f, 0.0f, 0.0f); 
        break; 
       case SDLK_LEFT: 
        _velosity = Vector<F32>(-1.0f, 0.0f, 0.0f); 
        break; 
       default: 
        break; 
      } 
     } 
    } 
} 

F32 delta = _timer->DeltaTime() * _speed; 
_position += Point<>((_velosity.x * delta), (_velosity.y * delta), (_velosity.z * delta)); 
_cell.SetPosition(_position); 
} 

我加入了一个错误消息框如果SDL无法初始化事件子系统,并且截至目前,它是。我在链接器中链接到SDL2.lib,SLD_Init(0);不会出错。我也尝试了它作为SDL_Init(SDL_INIT_EVENTS),它失败了。

Atom.h有一些我喜欢使用的typedef。这具体为:

#ifndef ATOM_H 
#define ATOM_H 

//Includes. These will be the files that everything will need to have access to 
//User defined Includes 
#include <KillerMath.h> 
//Sytem and library includes 
#include <windows.h> 

//Signed Typedefs 
typedef signed __int8 S8; 
typedef signed __int16 S16; 
typedef signed __int32 S32; 
typedef signed __int64 S64; 

//Unsigned Typedefs 
typedef unsigned __int8 U8; 
typedef unsigned __int16 U16; 
typedef unsigned __int32 U32; 
typedef unsigned __int64 U64; 

//Floating types 
typedef float F32; 
typedef double F64; 

#endif 

所以,这里是我的基本问题:

  1. 这是处理与SDL控制器的正确方法?重点是使控制器类成为可以在运行时根据组件映射从框中添加和删除的组件。我想这样做,以便我可以控制用户控制哪个对象。

  2. 所有的图形都是由opengl处理的,它们工作正常。如果我对关键事件的值进行硬编码,并强制更新速度矢量,它将移动框而没有问题,所以我知道系统的一部分正在工作。似乎这只是SDL问题,而且似乎没有Init。你能发现我对Init SDL所做的任何错误吗?

+0

您是否检查过SDL_GetError? – 2015-03-25 14:08:19

+0

正如我在大多数/所有平台上所了解的那样,SDL需要一个本地窗口句柄来将其自身插入到事件循环中以提醒事件。通常的做法是让SDL通过'SDL_CreateWindow()'创建该窗口。也许用'OGLRenderingWindow'的'HWND'试试'SDL_Init(SDL_INIT_EVERYTHING)',然后['SDL_CreateWindowFrom()'](https://wiki.libsdl.org/SDL_CreateWindowFrom)? – genpfault 2015-03-25 14:39:14

@Rollen您说的没错,问题在于SDL没有窗口句柄。我尝试将我生成的hwnd传递到SDL中,以使控制器/键盘输入正常工作,但是我一直无法做到正确(很可能是由于我没有完全理解SDL)。将来,我可能会让一个项目只使用SDL,这样我可以更好地处理它的工作方式。就目前而言,DirectInput就像一个冠军,我在很短的时间内完成并运行。感谢您的意见。