Программа зависает, не может найти решение. SDL и C ++ - PullRequest
0 голосов
/ 21 декабря 2011

Я везде проверял свой код и не мог найти причину, по которой моя программа зависает при каждой загрузке.Проблема начала возникать после того, как я включил новый заголовок AI "schoolboy.h".Я проверил, чтобы убедиться, что попытка перетянуть изображение не было проблемой, и это не так.Итак, после некоторого тестирования я понял, что проблема заключается в "void schoolboy_action ()" внутри "schoolboy.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;
};

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 Ответ

1 голос
/ 21 декабря 2011

Ваша строка instance = instance + 1 в конце schoolboy.h выглядит так, как будто она находится вне цикла while, который основывается на увеличении ее до конечного завершения.

...