我正在尝试在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编译它时,我都没有错误。
答案 0 :(得分:1)
您的程序会立即关闭,因为您使用的是SDL_PollEvent(&event)
。如果没有要处理的事件,此函数将返回false,在您的情况下,程序启动时会发生这种情况。如果用户必须提供窗口输入, 没有要处理的事件是规则,而不是例外。
您有两种选择:
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);
}
}
}