2011-12-21 3 views
0

코드에서 어디에서나 확인한 결과 프로그램을로드 할 때마다 프로그램이 정지되는 이유를 찾을 수 없었습니다. 문제는 새로운 AI 헤더 인 "schoolboy.h"가 포함 된 후에 발생했습니다. 나는 이미지를 blit하려고 시도하는 것이 문제가 아니 었는지 확인했다. 그래서, 약간의 테스트 후에, 나는 "schoolboy.h"내의 "void schoolboy_action()"에 문제가 있다고 생각했습니다. 다음은 전체 프로젝트 코드입니다. 나는 한 시간 동안 그 부분을 보았고 해결책을 찾지 못했습니다. 나는 그것이 정말로 길다는 것을 알고있다, 그리고 내가 구멍을 채울 것을 계획하는 그것에 약간의 구멍이있다. 그러나 나와 나와 견뎌 라.프로그램이 정지되고 해결책을 찾을 수 없습니다. SDL 및 C++

하여 Main.cpp

#include "schoolboy.h" 
#include "include_file.h" 

int main(int argc,char* argv[]) 
{ 
    SDL_Init(SDL_INIT_EVERYTHING); 
    variables(); 

    while (quit == 1) 
    { 
     while (MARS.alive == 1) 
     { 
      SDL_WM_SetCaption("Mobile Anihilation Robot System", NULL); 
      applysurface(0,0,background,screen); 
      MARS_action(); 
      MARS_bullet_action(1); 
      gunshot_explosion_action(1); 
      if (create_schoolboy_variable == 1) 
      {create_schoolboy(); create_schoolboy_variable = 0;} 
      else if (create_schoolboy_variable < 1) 
      {create_schoolboy_variable = rand() % 1;}; 
      schoolboy_action(0,0,0); 
      applysurface(MARS.x-25, MARS.y-25, MARS_image, screen); 
      SDL_Flip(screen); 
      SDL_Delay(1); 
     }; 
    while (MARS.alive == 0) 
    { 
      SDL_WM_SetCaption("Mobil Anihilation Robot System", NULL); 
    }; 
    }; 
    SDL_FreeSurface(screen); 
    SDL_Quit(); 
    return 0; 
}; 

include_file.h

#ifndef INCLUDE_FILE_H_INCLUDED 
#define INCLUDE_FILE_H_INCLUDED 

#include <map> 
#include <cstdlib> 
#include <SDL/SDL.h> 
#include <SDL/SDL_image.h> 

/*structs*/ 

struct instance_struct 
{ 
    int gunshot_explosion; 
    int schoolboy_instance; 
}; 
struct MARS_struct 
{ 
int x; 
int y; 
int alive; 
int bullet; 
}; 
struct MARS_bullet_struct 
{ 
int x; 
int y; 
int direction; 
int exist; 
int id; 
bool operator<(const MARS_bullet_struct & n)const{return this->id<n.id;} 
}; 
struct schoolboy_struct 
{ 
    int x; 
    int y; 
    int direction; 
    int id; 
    int exist; 
    int walk_delay; 
    int shoot_delay; 
    int walked; 
    SDL_Rect clip[3]; 
    bool operator<(const schoolboy_struct&n)const{return this->id<n.id;} 
}; 
struct gunshot_explosion_struct 
{ 
    int x; 
    int y; 
    int id; 
    int life; 
    int exist; 
    bool operator<(const gunshot_explosion_struct&n)const{return this->id<n.id;} 
}; 

/*declaring structs*/ 
MARS_struct MARS; 
instance_struct instance_body; 
std::map<int, MARS_bullet_struct>MARS_bullet; 
std::map<int, schoolboy_struct>schoolboy; 
std::map<int, gunshot_explosion_struct>gunshot_explosion; 

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

/*declaring global variables*/ 
int quit; 
int create_schoolboy_variable; 
SDL_Event event; 
SDL_Rect clip[3]; 
SDL_Surface *screen = NULL; 
SDL_Surface *background = NULL; 
SDL_Surface *gunshot_explosion_image = NULL; 
SDL_Surface *MARS_image = NULL; 
SDL_Surface *MARS_bullet_image = NULL; 
SDL_Surface *schoolboy_image = NULL; 

/*giving variables values*/ 
void variables() 
{ 
    quit = 1; 
    MARS.alive = 1; 
    MARS.x = 256; 
    MARS.y = 256; 
    create_schoolboy_variable = 0; 

    clip[0].x = 0; 
    clip[0].y = 0; 
    clip[0].w = 50; 
    clip[0].h = 50; 
    clip[1].x = 50; 
    clip[1].y = 50; 
    clip[1].w = 100; 
    clip[1].h = 100; 
    clip[2].x = 100; 
    clip[2].y = 100; 
    clip[2].w = 150; 
    clip[2].h = 150; 
    clip[3].x = 150; 
    clip[3].y = 150; 
    clip[3].w = 200; 
    clip[3].h = 200; 

    screen = SDL_SetVideoMode(512,512,32,SDL_SWSURFACE); 
    background = IMG_Load("images/background.png"); 
    gunshot_explosion_image = IMG_Load("images/gunshot_explosion.png"); 
    MARS_image = IMG_Load("images/MARS.png"); 
    MARS_bullet_image = IMG_Load("images/MARS_bullet.png"); 
    schoolboy_image = IMG_Load("images/schoolboy.png"); 
}; 

void gunshot_explosion_action(int instance) 
{ 
    while (instance <= instance_body.gunshot_explosion) 
    { 
     if (gunshot_explosion[instance].exist == 0) 
     { 
      gunshot_explosion[instance].life = gunshot_explosion[instance].life + 1; 
      if (gunshot_explosion[instance].life > 7) 
      {gunshot_explosion[instance].exist = 1;}; 
      applysurface(gunshot_explosion[instance].x-6,gunshot_explosion[instance].y-6,gunshot_explosion_image,screen); 
     }; 
     instance = instance + 1; 
    }; 
}; 

#endif // INCLUDE_FILE_H_INCLUDED 

MARS.h

#ifndef MARS_H_INCLUDED 
#define MARS_H_INCLUDED 

#include "include_file.h" 

/*character functions*/ 
void MARS_action() 
{ 
    if (SDL_PollEvent(&event)) 
    { 
     if (event.type == SDL_QUIT) 
     { 
      MARS.alive = 2; 
      quit = 0; 
     }; 

     if (event.type == SDL_KEYDOWN) 
     { 
      switch(event.key.keysym.sym) 
      { 
       case SDLK_a: 
        if(MARS.x > 0){MARS.x = MARS.x - 16;}; break; 
       case SDLK_d: 
        if(MARS.x < 512){MARS.x = MARS.x + 16;}; break; 
       case SDLK_w: 
        if(MARS.y > 0){MARS.y = MARS.y - 16;}; break; 
       case SDLK_s: 
        if(MARS.y < 512){MARS.y = MARS.y + 16;}; break; 
       case SDLK_LEFT: 
        MARS.bullet = MARS.bullet + 1; 
        MARS_bullet[MARS.bullet].direction = 2; 
        MARS_bullet[MARS.bullet].x = MARS.x-25; 
        MARS_bullet[MARS.bullet].y = MARS.y; 
        instance_body.gunshot_explosion = instance_body.gunshot_explosion+1; 
        gunshot_explosion[instance_body.gunshot_explosion].x = MARS.x-25; 
        gunshot_explosion[instance_body.gunshot_explosion].y = MARS.y; 
        break; 
       case SDLK_RIGHT: 
        MARS.bullet = MARS.bullet + 1; 
        MARS_bullet[MARS.bullet].direction = 0; 
        MARS_bullet[MARS.bullet].x = MARS.x+25; 
        MARS_bullet[MARS.bullet].y = MARS.y; 
        instance_body.gunshot_explosion = instance_body.gunshot_explosion+1; 
        gunshot_explosion[instance_body.gunshot_explosion].x = MARS.x+25; 
        gunshot_explosion[instance_body.gunshot_explosion].y = MARS.y; 
        break; 
       case SDLK_UP: 
        MARS.bullet = MARS.bullet + 1; 
        MARS_bullet[MARS.bullet].direction = 1; 
        MARS_bullet[MARS.bullet].x = MARS.x; 
        MARS_bullet[MARS.bullet].y = MARS.y-25; 
        instance_body.gunshot_explosion = instance_body.gunshot_explosion+1; 
        gunshot_explosion[instance_body.gunshot_explosion].x = MARS.x; 
        gunshot_explosion[instance_body.gunshot_explosion].y = MARS.y-25; 
        break; 
       case SDLK_DOWN: 
        MARS.bullet = MARS.bullet + 1; 
        MARS_bullet[MARS.bullet].direction = 3; 
        MARS_bullet[MARS.bullet].x = MARS.x; 
        MARS_bullet[MARS.bullet].y = MARS.y+25; 
        instance_body.gunshot_explosion = instance_body.gunshot_explosion+1; 
        gunshot_explosion[instance_body.gunshot_explosion].x = MARS.x; 
        gunshot_explosion[instance_body.gunshot_explosion].y = MARS.y+25; 
        break; 
       case SDLK_ESCAPE: quit = 0; MARS.alive = 2; break; 
      }; 
     }; 
    }; 
}; 

void MARS_bullet_action(int instance) 
{ 
    while (instance <= MARS.bullet) 
    { 
     if (MARS_bullet[instance].exist == 0) 
     { 
      if (MARS_bullet[instance].direction == 0) 
      {MARS_bullet[instance].x = MARS_bullet[instance].x + 5;}; 
      if (MARS_bullet[instance].direction == 1) 
      {MARS_bullet[instance].y = MARS_bullet[instance].y - 5;}; 
      if (MARS_bullet[instance].direction == 2) 
      {MARS_bullet[instance].x = MARS_bullet[instance].x - 5;}; 
      if (MARS_bullet[instance].direction == 3) 
      {MARS_bullet[instance].y = MARS_bullet[instance].y + 5;}; 
      if (MARS_bullet[instance].x < 0 or MARS_bullet[instance].x > 512 or MARS_bullet[instance].y < 0 or MARS_bullet[instance].y > 512) 
      {MARS_bullet[instance].exist = 1;}; 
    applysurface(MARS_bullet[instance].x-5, MARS_bullet[instance].y-5, MARS_bullet_image, screen); 
     }; 
     instance = instance + 1; 
    }; 
}; 

#endif // MARS_H_INCLUDED 

schoolboy.h

#ifndef SCHOOLBOY_H_INCLUDED 
#define SCHOOLBOY_H_INCLUDED 

#include "include_file.h" 

void create_schoolboy(int positionx = 0, int positiony = 0) 
{ 
    instance_body.schoolboy_instance = instance_body.schoolboy_instance + 1; 
    positionx = rand() % 1; 
    positiony = rand() % 1; 
    if (positionx == 0 and positiony == 0) 
    { 
     schoolboy[instance_body.schoolboy_instance].x = 0; 
     schoolboy[instance_body.schoolboy_instance].y = 0; 
    }; 
    if (positionx == 1 and positiony == 0) 
    { 
     schoolboy[instance_body.schoolboy_instance].x = 512; 
     schoolboy[instance_body.schoolboy_instance].y = 0; 
    }; 
    if (positionx == 0 and positiony == 1) 
    { 
     schoolboy[instance_body.schoolboy_instance].x = 0; 
     schoolboy[instance_body.schoolboy_instance].y = 512; 
    }; 
    if (positionx == 1 and positiony == 1) 
    { 
     schoolboy[instance_body.schoolboy_instance].x = 512; 
     schoolboy[instance_body.schoolboy_instance].y = 512; 
    }; 
}; 

void schoolboy_action(int instance, int bullet, int first_direction) 
{ 
    while (instance <= instance_body.schoolboy_instance) 
    { 
     first_direction = rand() % 1; 
     if (schoolboy[instance].exist == 0) 
     { 
      while (bullet <= MARS.bullet) 
      { 
       if (schoolboy[instance].x-12 >= MARS_bullet[bullet].x and MARS_bullet[bullet].x <= schoolboy[instance].x+12 and schoolboy[instance].y-12 >= MARS_bullet[bullet].y and MARS_bullet[bullet].y <= schoolboy[instance].y+12) 
       { 
        schoolboy[instance].exist = 1; 
       }; 
       bullet = bullet + 1; 
      }; 
      if (schoolboy[instance].walk_delay == 0) 
      { 
       if (first_direction == 0) 
       { 
        schoolboy[instance].walked = 0; 
        if (MARS.x > schoolboy[instance].x) 
        { 
         schoolboy[instance].x = schoolboy[instance].x + 16; 
         applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[0]); 
         schoolboy[instance].walked = 1; 
        } 
        else if (MARS.x < schoolboy[instance].x) 
        { 
         schoolboy[instance].x = schoolboy[instance].x - 16; 
         applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[2]); 
         schoolboy[instance].walked = 1; 
        }; 
        if (schoolboy[instance].walked = 0) 
        { 
         if (MARS.y > schoolboy[instance].y) 
         { 
          schoolboy[instance].y = schoolboy[instance].y+16; 
          applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[3]); 
          schoolboy[instance].walked = 1; 
         } 
         else if (MARS.y < schoolboy[instance].y) 
         { 
          schoolboy[instance].y = schoolboy[instance].y+16; 
          applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[1]); 
          schoolboy[instance].walked = 1; 
         }; 
        }; 

       }; 
       if (first_direction == 1) 
       { 
        schoolboy[instance].walked = 0; 
        if (MARS.y > schoolboy[instance].y) 
        { 
         schoolboy[instance].y = schoolboy[instance].y+16; 
         applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[3]); 
         schoolboy[instance].walked = 1; 
        } 
        else if (MARS.y < schoolboy[instance].y) 
        { 
         schoolboy[instance].y = schoolboy[instance].y+16; 
         applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[1]); 
         schoolboy[instance].walked = 1; 
        }; 
        if (schoolboy[instance].walked = 0) 
        { 
         if (MARS.x > schoolboy[instance].x) 
         { 
          schoolboy[instance].x = schoolboy[instance].x + 16; 
          applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[0]); 
          schoolboy[instance].walked = 1; 
         } 
         else if (MARS.x < schoolboy[instance].x) 
         { 
          schoolboy[instance].x = schoolboy[instance].x - 16; 
          applysurface(schoolboy[instance].x-25, schoolboy[instance].y-25, schoolboy_image, screen, &clip[2]); 
          schoolboy[instance].walked = 1; 
         }; 
        }; 
       }; 
       schoolboy[instance].walk_delay = schoolboy[instance].walk_delay + 1; 
      } 
      else {schoolboy[instance].walk_delay = schoolboy[instance].walk_delay + 1;}; 
      if (schoolboy[instance].walk_delay == 10){schoolboy[instance].walk_delay = 0;}; 
     }; 
    }; 
    instance = instance + 1; 
}; 

#endif // SCHOOLBOY_H_INCLUDED 

모든 파일이 서로 관련되어 있습니다. 특히 schoolboy.h가 그 이유입니다. 대답을 찾으면 그 이유를 설명 할 수 있습니까? 어떤 도움을 주셔서 감사합니다!

+1

산술 연산자'++'와'+ ='를 살펴보십시오. 'x = x + 5'는'x + = 5'보다 읽기에 더 많은 노력이 필요하며'x = x + 1'과'++ x'에서도 마찬가지입니다. – moshbear

+0

오케이. 팁 고마워. 그것은 그것을 더 확신하게 만들 것입니다. – Ripspace

답변

1

schoolboy.h 끝에있는 instance = instance + 1 행은 while 루프 외부에있는 것처럼 보입니다. 결국 while 루프가 끝나기 위해 증가합니다.

+0

감사! 나는 그것을 이해하지 못 하겠어! 그러나 왜 그 오류로 인해 프로그램이 무한 루프로 진행되는 대신 멈추는가? – Ripspace

+0

확실하지 않습니다. 프로그램이 실제로 멈췄을 때 실제로 무엇을보고 있습니까? 오류가 있습니까? – twsaef

+0

SDL_Flip (화면)을 읽을 수 있기 전에 멈 춥니 다. 따라서 색상 값이없는 빈 화면입니다. 냉동 관련 경고는 없었습니다. – Ripspace

관련 문제