2013-05-26 1 views
-1

공/찾기 충돌을 이동시키는 함수를 호출하면 작동하지 않습니다. 그러나 "b"를 "ball1"로 대체하는 함수의 내용을 입력하면 작동합니다. 나는 그것을 고치기 위해 많은, 많은, 많은 것들을 시도했지만 아무 소용이 없습니다. 코드가 너무 길어서 죄송합니다. 다만 스 니펫을 포함하면 수정 될 수 없습니다.구조체의 값을 변경하는 호출 함수의 문제점

코드 :

#include "SDL/SDL.h" 
#include "SDL/SDL_image.h" 
#include <math.h> 
#include <stdbool.h> 

#define SCREEN_WIDTH 500 
#define SCREEN_HEIGHT 500 
#define SCREEN_BPP 32 
#define GRAVITY 1 

SDL_Surface *image = NULL; 
SDL_Surface *screen = NULL; 
SDL_Event event; 

SDL_Surface *loadImage(char *filename) { 
    SDL_Surface *optimizedImage = NULL; 
    SDL_Surface *loadedImage = IMG_Load(filename); 
    if (loadedImage != NULL) { 
     optimizedImage = SDL_DisplayFormat(loadedImage); /*optimizes image*/ 
     SDL_FreeSurface(loadedImage); 
    } 

    return optimizedImage; 
} 

void applySurface(int x, int y, SDL_Surface *source, SDL_Surface *destination) { 
    SDL_Rect offset; 
    offset.x = x; 
    offset.y = y; 

    SDL_BlitSurface(source, NULL, destination, &offset); 
} 

bool initSDL(void) { 
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1) { 
     return NULL; 
    } 
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE); 

    if (screen == NULL) { 
     return NULL; 
    } 

    SDL_WM_SetCaption("Bounce", NULL); 
    return true; 
} 

/*Cleans all needed things*/ 
void cleanUp(SDL_Surface *image) { 
    SDL_FreeSurface(image); 
    SDL_Quit(); 
} 

/*The Ball structure for OOP*/ 
struct Ball { 
    int x; 
    int y; 
    int vy; 
    int vx; 
    SDL_Surface *image; 
}; 

/*initialize a Ball structure*/ 
struct Ball ball_initBall(int x, int y, int vy, int vx, char *filename) { 
    struct Ball b; 
    b.x = x; 
    b.y = y; 
    b.vy = vy; 
    b.vx = vx; 
    b.image = loadImage(filename); 

    return b; 
} 

void ball_move(struct Ball b) { 
    b.x += b.vx; 
    b.y += b.vy; 
    b.vy += GRAVITY; 
} 

void ball_wallCollide(struct Ball b) { 
    if (b.x <= 1 || b.x >= 500 - 48) { 
     b.vx *= -1; 
    } 

    if (b.y <= 1 || b.y >= 500 - 49) { 
     b.vy *= -0.95; 
    } 
} 

int main(int argc, char *argv[]) { 
    bool running = true; 
    initSDL(); 

    struct Ball ball1 = ball_initBall(SCREEN_HEIGHT/2, SCREEN_WIDTH/2, 1, 3, "resources/ball.jpg"); 

    while (running) { 
     applySurface(ball1.x, ball1.y, ball1.image, screen); 


     ball_move(ball1); 
     ball_wallCollide(ball1); 

     while (SDL_PollEvent(&event)) { 
      if (event.type == SDL_QUIT) { 
       running = false; 
      } 
     } 
     SDL_Flip(screen); 
     SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0, 0, 0)); 
     SDL_Delay(50); 
    } 
    cleanUp(ball1.image); 
    return 0; 
} 
+1

는 "작동하지 않는"정의 업데이트 할 Ball의 주소를 전달하는 호출합니다. 지금까지 어떤 디버깅을 시도 했습니까? –

+0

그래, 아마해야 겠어. 공이 움직이지 않는다. –

답변

0

ball_moveball_wallCollide은 값으로 자신의 Ball 인수를. 즉, 호출자의 인스턴스 사본을 얻습니다. 호출자의 인스턴스를 업데이트하려면 대신 해당 인스턴스에 대한 포인터를 전달해야합니다.

void ball_move(struct Ball* b) { 
    b->x += b->vx; 
    b->y += b->vy; 
    b->vy += GRAVITY; 
} 

변화는

ball_move(&ball1); 
+0

오, 고마워요! 나는 결코 그것을 이해할 수 없었을 것이다! –