从数组访问SDL_Rect成员时的垃圾值?

时间:2018-08-30 19:14:57

标签: c++ arrays object sdl

我一直在关注LazyFoo的SDL tutorials(并添加了自己的组织和编码风格)。当我到达他的animation tutorial时,我决定制作一个单独的类来存储与动画算法有关的变量和方法,而不要使用全局变量。他使用SDL_Rects数组定义Sprite表单上不同Sprite的边界,因此我使用SDL_Rect指针将数组存储在自定义类中。当我编译所有内容时,我没有看到动画,而当我编译原始源代码时,却看到了动画。当我开始调试东西时,我发现渲染精灵时,这些rect实际上充满了垃圾,即使在初始化它们时也没问题。我已经尝试过多次简化问题,但是我在更简单的环境中重现该错误的每种方法实际上都能按预期工作!因此,考虑到这一点,我为大量代码表示歉意,因为我似乎无法减少问题。

texture.h

#ifndef TEXTURE_H
#define TEXTURE_H

#include <SDL2/SDL.h>
#include <string>

class Animation {
public:
    Animation(SDL_Renderer* renderer);
    ~Animation();

    void load(std::string path, int frames, SDL_Rect* clips),
         free(),
         render(int x, int y),
         next_frame();
private:
    SDL_Renderer* _renderer=NULL;
    SDL_Rect* _clips=NULL;
    SDL_Texture* _texture=NULL;
    int _frame=0, _frames=0, _width=0, _height=0;
};

#endif

texture.cpp

#include <stdio.h>
#include <SDL2/SDL_image.h>
#include "texture.h"
#include "error.h"

Animation::Animation(SDL_Renderer* renderer) {
    _renderer = renderer;
}

Animation::~Animation() {
    free();
    _renderer = NULL;
}

void Animation::load(std::string path, int frames, SDL_Rect* clips) {
    free();
    SDL_Texture* texture = NULL;

    SDL_Surface* surface = IMG_Load(path.c_str());
    if (!surface)
        throw ErrorIMG("Could not load image "+path);
    SDL_SetColorKey(surface, SDL_TRUE,
                    SDL_MapRGB(surface->format, 0, 0xFF, 0xFF));

    texture = SDL_CreateTextureFromSurface(_renderer, surface);
    if (!texture)
        throw ErrorSDL("Could not create texture from image "+path);

    _width = surface->w;
    _height = surface->h;
    SDL_FreeSurface(surface);

    _frames = frames;
    _clips = clips;
    printf("clips[%d]: w: %d h: %d\n", 0, _clips[0].w, _clips[0].h);
}

void Animation::free() {
    if (_texture) {
        SDL_DestroyTexture(_texture);
        _texture = NULL;
        _clips = NULL;
        _frames = 0;
        _frame = 0;
        _width = 0;
        _height = 0;
    }
}

void Animation::render(int x, int y) {
    SDL_Rect crect = _clips[_frame/4];
    printf("in render (clips[%d]): w: %d, h: %d\n", _frame/4, crect.w, crect.h);
    SDL_Rect render_space = {x, y, crect.w, crect.h};
    SDL_RenderCopy(_renderer, _texture, &_clips[_frame], &render_space);
}

void Animation::next_frame() {
    SDL_Rect crect = _clips[_frame/4];
    printf("in next frame (clips[%d]): w: %d, h: %d\n", _frame/4, crect.w, crect.h);
    ++_frame;
    if (_frame/4 >= _frames)
        _frame = 0;
}

game.h

#ifndef GAME_H
#define GAME_H

#include "texture.h"

class Game {
public:
    Game();
    ~Game();
    void main();
private:
    void load_media();

    SDL_Window* _window=NULL;
    SDL_Renderer* _renderer=NULL;
    Animation* _anim=NULL;

    const int SCREEN_WIDTH=640, SCREEN_HEIGHT=480;
};

#endif

game.cpp

#include <SDL2/SDL_image.h>
#include "game.h"
#include "error.h"

void Game::main() {
    load_media();

    bool has_quit = false;
    SDL_Event event;

    while (!has_quit) {
        while (SDL_PollEvent(&event))
            if (event.type == SDL_QUIT)
                has_quit = true;

        SDL_SetRenderDrawColor(_renderer, 0xff, 0xff, 0xff, 0xff);
        SDL_RenderClear(_renderer);

        _anim->render(100, 100);
        _anim->next_frame();
        SDL_RenderPresent(_renderer);
    }
}

Game::Game() {
    if (SDL_Init(SDL_INIT_VIDEO))
        throw ErrorSDL("SDL could not initialize");

    _window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED,
                               SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH,
                               SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    if (!_window)
        throw ErrorSDL("Window could not be created");

    Uint32 render_flags = SDL_RENDERER_ACCELERATED;
    render_flags |= SDL_RENDERER_PRESENTVSYNC;
    _renderer = SDL_CreateRenderer(_window, -1, render_flags);
    if (!_renderer)
        throw ErrorSDL("Renderer could not be created");
    SDL_SetRenderDrawColor(_renderer, 0xff, 0xff, 0xff, 0xff);

    if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG))
        throw ErrorIMG("SDL_image could not initialize");
}

Game::~Game() {
    delete _anim;

    SDL_DestroyRenderer(_renderer);
    SDL_DestroyWindow(_window);
    _renderer = NULL;
    _window = NULL;

    IMG_Quit();
    SDL_Quit();
}

void Game::load_media() {
    const int nclips = 4;
    SDL_Rect clips[nclips];

    for (int i=0; i < nclips; i++) {
        clips[i].x = i*64;
        clips[i].y = 0;
        clips[i].w = 64;
        clips[i].h = 164;
    }

    _anim = new Animation(_renderer);
    _anim->load("sheet.png", nclips, &clips[0]);
}

2 个答案:

答案 0 :(得分:2)

您正在存储指向临时目录的指针。您传递给SDL_Rect* clips的{​​{1}}指针将分配给函数返回后使用的成员Animation::load。为了使此功能正常运行,指向{em> 的数据必须一直存在,只要_clips类正在使用它。问题出现在这里:

Animation

在这段代码中,void Game::load_media() { const int nclips = 4; SDL_Rect clips[nclips]; ... _anim->load("sheet.png", nclips, &clips[0]); } 是一个 local 变量。这意味着它在clips的末尾被销毁,该位置的内存内容将变成垃圾。

有多种方法可以解决此问题。一种简单的方法是使用load_media()而不是std::vector<SDL_Rect>SDL_Rect*可以安全地复制并为您管理其内部。您的新代码如下所示:

std::vector

也不要忘记class Animation { ... std::vector<SDL_Rect> _clips; ... } void Animation::load(std::string path, int frames, std::vector<SDL_Rect> clips) { ... _clips = clips; ... } void Game::load_media() { const int nclips = 4; std::vector<SDL_Rect> clips; clips.resize(nclips); ... _anim->load("sheet.png", nclips, clips); } #include <vector>的文档为here。请注意,std::vector有一个std::vector方法,可以在出现的任何地方替换size()

答案 1 :(得分:1)

超出范围的堆栈分配的Game::load_media()::clips数组会消失。在Animation::load()中进行复制,而不仅仅是存储指针。