Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在linux上运行几秒钟后SDL没有响应_C++_Linux_Sdl_Sdl 2 - Fatal编程技术网

C++ 在linux上运行几秒钟后SDL没有响应

C++ 在linux上运行几秒钟后SDL没有响应,c++,linux,sdl,sdl-2,C++,Linux,Sdl,Sdl 2,我在遵循SDL游戏开发手册,我甚至不能让第一个文件正常工作。当应用程序启动时,它呈现窗口,几秒钟后,Linux说游戏没有响应,并要求我强制退出。如果单击“等待”,则每隔几秒钟重复一次。我注意到的一件事是SDL_PollEvent永远不会返回真值。我不知道为什么事情不起作用。这是我的密码 Main.cpp #ifdef __cplusplus #include <cstdlib> #else #include <stdlib.h> #endif #include "Gam

我在遵循SDL游戏开发手册,我甚至不能让第一个文件正常工作。当应用程序启动时,它呈现窗口,几秒钟后,Linux说游戏没有响应,并要求我强制退出。如果单击“等待”,则每隔几秒钟重复一次。我注意到的一件事是SDL_PollEvent永远不会返回真值。我不知道为什么事情不起作用。这是我的密码

Main.cpp

#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif

#include "Game.h"

// our Game object
Game* g_game = 0;
int main(int argc, char* argv[]) {
    g_game = new Game();
    g_game->init("Chapter 1", 100, 100, 640, 480, 0);
    while(g_game->running()) {
        g_game->handleEvents();
        //g_game->update();
        g_game->render();
    }
    g_game->clean();
    return 0;
}
\ifdef\uuucplusplus
#包括
#否则
#包括
#恩迪夫
#包括“Game.h”
//我们的游戏目标
Game*g_Game=0;
int main(int argc,char*argv[]){
g_game=新游戏();
g_game->init(“第1章”,100、100、640、480、0);
while(游戏->跑步()){
g_game->handleEvents();
//游戏->更新();
游戏->渲染();
}
g_游戏->清洁();
返回0;
}
游戏

#ifndef __Game__
#define __Game__
#if !WINDOWS
#include <SDL2/SDL.h>
#else
#include <SDL.h>
#endif

class Game {
public:
    Game() {}
    ~Game() {}
// simply set the running variable to true
    bool init(const char* title, int xpos, int ypos, int width, int
        height, bool fullscreen);
    void render();
    void update();
    void handleEvents();
    void clean();
// a function to access the private running variable
    bool running() {
        return m_bRunning;
    }
private:

    SDL_Window* m_pWindow;
    SDL_Renderer* m_pRenderer;

    bool m_bRunning;
};

#endif /* defined(__Game__) */
\ifndef\uu游戏__
#定义游戏__
#如果!窗户
#包括
#否则
#包括
#恩迪夫
班级游戏{
公众:
游戏(){}
~Game(){}
//只需将运行变量设置为true
bool init(const char*title,int xpos,int ypos,int width,int
高度,bool全屏);
void render();
无效更新();
void handleEvents();
无效清除();
//访问专用运行变量的函数
bool running(){
返回m_bRunning;
}
私人:
SDL_Window*m_pWindow;
SDL_渲染器*m_pRenderer;
布尔·穆布朗宁;
};
#endif/*已定义(\uuuu游戏\uuuuu)*/
Game.cpp

#include "Game.h"
#include <iostream>

bool Game::init(const char* title, int xpos, int ypos, int width,
                int height, bool fullscreen) {
// attempt to initialize SDL
    if(SDL_Init(SDL_INIT_EVERYTHING) == 0) {
        std::cout << "SDL init success\n";
// init the window
        int flags = 0;
        if(fullscreen) {
            flags = SDL_WINDOW_FULLSCREEN;
        }
        m_pWindow = SDL_CreateWindow(title, xpos, ypos,
                                     width, height, flags);
        if(m_pWindow != 0) { // window init success
            std::cout << "window creation success\n";
            m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
            if(m_pRenderer != 0) { // renderer init success
                std::cout << "renderer creation success\n";
                SDL_SetRenderDrawColor(m_pRenderer,
                                       255,0,255,255);
            } else {
                std::cout << "renderer init fail\n";
                return false; // renderer init fail
            }
        } else {
            std::cout << "window init fail\n";
            return false; // window init fail
        }
    } else {
        std::cout << "SDL init fail\n";
        return false; // SDL init fail
    }
    std::cout << "init success\n";
    m_bRunning = true; // everything inited successfully, start the main loop
    return true;
}

void Game::render() {
    SDL_RenderClear(m_pRenderer); // clear the renderer to the draw color
    SDL_RenderPresent(m_pRenderer); // draw to the screen
}

void Game::clean() {
    std::cout << "cleaning game\n";
    SDL_DestroyWindow(m_pWindow);
    SDL_DestroyRenderer(m_pRenderer);
    SDL_Quit();
}

void Game::handleEvents() {
    SDL_Event event;
    while(SDL_PollEvent(&event)) {
        std::cout << "Checking Events";
        switch(event.type) {
        case SDL_QUIT:
            std::cout << "Quiting";
            m_bRunning = false;
            break;
        default:
            break;
        }
    }
}
#包括“Game.h”
#包括
bool Game::init(const char*title,int xpos,int ypos,int width,
整数高度,布尔全屏){
//尝试初始化SDL
if(SDL_Init(SDL_Init_EVERYTHING)==0){

std::cout我已经在Linux和SDL2.0.4下成功编译并运行了您的最小代码。我使用了一个简单的图像“cb.bmp”

请尝试以下操作:

  • 像这样编译:g++-Wall-ggdb$(sdl2 config--cflags)-o program main.cpp-lSDL2

  • gdb./program,然后键入“run”

  • 当它崩溃时,键入“bt”(用于回溯)

  • 在这里复制输出

  • 我的猜测是:“bmp”文件格式不受支持,或者渲染驱动程序不受支持。请尝试使用:

    SDL_(win,-1,0)


    handleEvent
    中,您可以在默认情况下添加一些调试信息吗?@Thomas我将其更改为:std::cout是否有人有任何想法,因为代码看起来很好?代码太多了。请添加一个最小的示例。
    #ifdef __cplusplus
    #include <cstdlib>
    #else
    #include <stdlib.h>
    #endif
    #include <iostream>
    #include <SDL2/SDL.h>
    
    int main(int argc, char** argv) {
        if(SDL_Init(SDL_INIT_EVERYTHING) != 0) {
            std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
            return 1;
        }
        SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
        if(win == NULL) {
            std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
            SDL_Quit();
            return 1;
        }
        SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
        if(ren == NULL) {
            SDL_DestroyWindow(win);
            std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
            SDL_Quit();
            return 1;
        }
        std::string imagePath = "cb.bmp";
        SDL_Surface *bmp = SDL_LoadBMP(imagePath.c_str());
        if(bmp == NULL) {
            SDL_DestroyRenderer(ren);
            SDL_DestroyWindow(win);
            std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl;
            SDL_Quit();
            return 1;
        }
        SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);
        if(tex == NULL) {
            SDL_DestroyRenderer(ren);
            SDL_DestroyWindow(win);
            std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl;
            SDL_Quit();
            return 1;
        }
        SDL_FreeSurface(bmp);
        SDL_Event e;
        bool quit = false;
        while(!quit) {
            while(SDL_PollEvent(&e)) {
                //If user closes the window
                if(e.type == SDL_QUIT) {
                    quit = true;
                }
                //If user presses any key
                if(e.type == SDL_KEYDOWN) {
                    quit = true;
                }
                //If user clicks the mouse
                if(e.type == SDL_MOUSEBUTTONDOWN) {
                    quit = true;
                }
            }
            SDL_RenderClear(ren);
            SDL_RenderPresent(ren);
        }
        SDL_DestroyTexture(tex);
        SDL_DestroyRenderer(ren);
        SDL_DestroyWindow(win);
        SDL_Quit();
        return 0;
    }