繁体   English   中英

程序立即在SDL中关闭

[英]Program immediately closes in SDL

我试图在SDL 1.2中构建我的第一个程序只是为了熟悉SDL。 我很头疼,想了解为什么我的程序立即关闭。

这与我的编译方式有关吗?

这是我的全部代码,不必担心尝试查找与手头的错误无关的逻辑错误。 我只是想找出为什么程序在启动时立即关闭。

Guy.h:

#ifndef GUY_H
#define GUY_H

class Guy{
    public:
        Guy(int, int);
        void move();
        void adjustVelocity(int, int);
        int getX();
        int getY();
        int getXVel();
        int getYVel();
    private:
        int xPos, yPos, xVel, yVel;
};
#endif

Guy.cpp:

#include "Guy.h"

Guy::Guy(int x, int y){
    xPos = x;
    yPos = y;
}
void Guy::move(){
    xPos += xVel;
    yPos += yVel;
}
void Guy::adjustVelocity(int x, int y){
    xVel += x;
    yVel += y;
}
int Guy::getX(){
    return xPos;
}
int Guy::getY(){
        return yPos;
}
int Guy::getXVel(){
    return xVel;
}
int Guy::getYVel(){
    return yVel;
}

main.cpp中:

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "Guy.h"
#include <string>
#include <iostream>

bool init();
bool loadAllFiles();
void paintScreen();
void update();
bool handle();
void cleanUp();

void addSurface(int, int, SDL_Surface*, SDL_Surface*, SDL_Rect* clip = NULL);
SDL_Surface* loadFile(std::string);
SDL_Surface* Screen;
SDL_Surface* BackgroundIMG;
SDL_Surface* GuyIMG;
Guy* Hunter;


int main(int argc, char* args[]){
    std::cout << "Why won't it even cout this?? :(" << endl;
    if(!init()){
        return 1;
    }
    if(!loadAllFiles()){
        return 2;
    }
    Hunter = new Guy(0,0);
    paintScreen();
    while(handle()){
        update();
    }

    cleanUp();
    return 0;

    SDL_Init(SDL_INIT_EVERYTHING);
    Screen = SDL_SetDisplayMode(640, 480, 32, SDL_SWSURFACE);
    SDL_Flip(Screen);
    SDL_Delay(1000);
}

bool init(){
    if(SDL_Init(SDL_INIT_EVERYTHING)==-1){
        return false;
    }
    Screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
    if(Screen==NULL){
        return false;
    }
    SDL_WM_SetCaption("First SDL Test On Own", NULL);
    return true;
}

bool loadAllFiles(){
    BackgroundIMG = loadFile("Background.png");
    GuyIMG = loadFile("Guy.png");
    if(BackgroundIMG==NULL || GuyIMG==NULL){
        return false;
    }
    else{
        return true;
    }
}

void paintScreen(){
    addSurface(0, 0, BackgroundIMG, Screen);
    addSurface(Hunter->getX(), Hunter->getY(), GuyIMG, Screen);
    SDL_Flip(Screen);
}

void update(){
    Hunter->move();
    addSurface(Hunter->getX(), Hunter->getY(), GuyIMG, Screen);
    SDL_Flip(Screen);
}

bool handle(){ 
    SDL_Event event;
    while(SDL_PollEvent(&event)){
        if(event.type==SDL_QUIT){
            return false;
        }
        else if(event.type==SDL_KEYDOWN){
            switch(event.key.keysym.sym){
                case SDLK_UP:
                    Hunter->adjustVelocity(0, -1);
                    break;
                case SDLK_DOWN:
                    Hunter->adjustVelocity(0, 1);
                    break;
                case SDLK_RIGHT:
                    Hunter->adjustVelocity(1, 0);
                    break;
                case SDLK_LEFT:
                    Hunter->adjustVelocity(-1, 0);
                    break;
            }
        }
    }
    return true;
}

void cleanUp(){
    SDL_FreeSurface(GuyIMG);
    SDL_FreeSurface(BackgroundIMG);
    SDL_Quit();
    delete Hunter;
}



void addSurface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip){
    SDL_Rect offset;
    offset.x = x;
    offset.y = y;
    SDL_BlitSurface(source, clip, destination, &offset);
}

SDL_Surface* loadFile(std::string s){
    SDL_Surface* surface = IMG_Load(s.c_str());
    if(surface==NULL){
        return NULL;
    }
    else{
        SDL_Surface* opsurface = SDL_DisplayFormat(surface);
        SDL_FreeSurface(surface);
        return opsurface;
    }
}

我的makefile:

all: main.o
        g++ -o run main.o Guy.o -lSDL -lSDL_image
main.o: Guy.o
        g++ -c main.cpp
Guy.o:
        g++ -c Guy.cpp

每当我使用此makefile对其进行编译时,都不会出错。

由于您正在使用SDL_PollEvent(&event)因此程序立即关闭。 如果没有要处理的事件(在您的情况下是在程序启动时发生),则此函数将返回false。 如果用户必须提供窗口输入, 则没有事件要处理的事实是规则,而不是例外。

您有2种选择:

  1. 切换到SDL_WaitEvent 它确实类似于SDL_PollEvent,但不同之处在于它是一个阻塞调用,它的优点是可以很容易地从应用程序中获得所需的行为。
  2. SDL_PollEvent ,并添加一种机制来在没有事件要处理时推迟执行。 通常,人们使用延迟为0或10的SDL_Delay 。优点是您可以控制无事件时应用程序的操作。

过程循环方案的示例。

SDL_Event event;
bool stop = false;
while(!stop){
    bool got_event = SDL_PollEvent(&event) > 0;
    if(got_event){
       stop = processEvent(event);
    }
    else {
        if(backGroundWorkTodo()){
           doBackGroundWork();
        }
        else{
          //may defer execution
          SDL_Delay(0);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM