c++ - 程序在 SDL 中立即关闭

标签 c++ makefile sdl

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

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

这是我的所有代码,不用担心试图找到与手头的错误无关的逻辑错误。我只是想找出程序在启动时立即关闭的原因。

盖伊.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

盖伊.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;
}

主要.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;
    }
}

我的生成文件:

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,在您的情况下,程序启动时会发生这种情况。如果用户必须提供窗口输入,没有要处理的事件是规则而不是异常(exception)。

你有两个选择:

  1. 切换到SDL_WaitEvent .它与 SDL_PollEvent 非常相似,但不同之处在于它是一个阻塞调用。它的优点是可以简单地获取您对应用程序的期望行为。
  2. 继续使用 SDL_PollEvent,并添加一种机制以在没有事件要处理时延迟执行。通常人们使用 SDL_Delay延迟为 0 或 10。优点是您可以控制应用程序在没有事件时执行的操作。

流程循环场景示例。

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

关于c++ - 程序在 SDL 中立即关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21079143/

相关文章:

boost - 了解 bjam 的目标以及如何指定新目标?

c++ - 为什么编译后没有.o?

c++ - SDL_surface 到 OpenGL 纹理

Android with SDL 2.0,设置图片为背景

c++ - llvm::DenseMap 和 std::map 之间的差异/相似之处

c++ - 完善转发模板功能

makefile - 如何简化 Makefile 中的重复规则 (GNU Make)

c++ - 如何在 C++、SDL 中更改图像大小

c++ - 减少 STL vector 的容量

c++ - 链接不返回对象的运算符