c - 调用更改结构体值的函数时出现问题

标签 c function struct sdl

如果我调用移动球/查找碰撞的函数,它不起作用。但是,如果我输入函数的内容,用“ball1”替换“b”,它就可以工作。为了解决这个问题,我尝试了很多很多很多方法,但都无济于事。抱歉,代码太长了,但我担心如果我只包含一个片段,将无法修复它。

代码:

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

最佳答案

ball_moveball_wallCollide 按值获取其 Ball 参数。这意味着他们获得了调用者实例的副本。如果您想更新调用者的实例,则需要传递一个指向该实例的指针。

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

并更改调用以传递要更新的Ball地址

ball_move(&ball1);

关于c - 调用更改结构体值的函数时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16761001/

相关文章:

C,从文本文件读取并在 If Else 语句中的 bool 表达式中使用扫描字符

C++函数问题

c - 频繁使用fopen时出错

c++ - 检查 vector 中的所有结构是否相等

c++ - 为什么结构的 sizeof 不等于每个成员的 sizeof 之和?

c - 在 Sublime Text 3 中构建和运行 C 时的警告消息

c - ap_log_error 没有登录我的 apache 模块

c - Hop_Size在aubio中的含义

c++ - 使用递归 C++ 最大化函数

c++ - 命名结构与未命名结构的类型别名