2011-12-21 17 views
0

私のコードのどこかでチェックしたところ、ロードするたびにプログラムがフリーズする理由が見つかりませんでした。この問題は、新しいAIヘッダー「schoolboy.h」を追加した後に起こりました。私は画像をblitしようとするのが問題ではないことを確認した。それは問題ではなかった。だから、いくつかのテストの後、私は問題が "schoolboy.h"内の "void schoolboy_action()"に嘘をついていると考えました。ここにプロジェクトのコード全体があります。私はそのセクションを見て一時間を費やし、解決策を見つけることができませんでした。私は本当に長いことを知っている、と私は記入する予定のいくつかの穴があるが、私と一緒に耐えてください。プログラムがフリーズし、解決策が見つかりません。 SDLとC++

#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 

include_file.h

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

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

あなたのinstance = instance + schoolboy.hの最後の1行は、最終的に終了するためにインクリメントされているwhileループの外側にあるようです。

+0

ありがとう!私はそれをキャッチしていない!しかし、なぜそのエラーはプログラムを無限ループに陥るのではなくフリーズさせるのでしょうか? – Ripspace

+0

私は確信していませんが、プログラムが実際にフリーズしているのを見ていますか?間違いはありますか? – twsaef

+0

SDL_Flip(画面)を読む前にフリーズします。だから、それは色の値がないだけの空白の画面です。凍結に関するコンパイラの警告はありませんでした。 – Ripspace

関連する問題