SDL_Surface指针在两个类之间传递

时间:2011-01-31 20:40:51

标签: c++ pointers parameters sdl surface

如果我在一个类中声明一个SDL_Surface指针,我可以与另一个类共享它以某种方式在它上面绘制吗?

class foo{
    private:
     SDL_Surface* mainScreen;
    public:
     foo() {
      mainScreen = SDL_SetVideoMode(400,300,32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_OPENGL);
     }

     ~foo() {
      SDL_FreeSurface(mainScreen);
     }

     SDL_Surface* getSurf() {
      return mainScreen;
     }

};

class fee{
    private:
     SDL_Surface* screen_passed;
    public:
     void draw(SDL_Surface* screen) {
      screen_passed = screen;

      SDL_Surface* img;
      SDL_Surface* app;
      app = IMG_Load("image.png");
      img = SDL_DisplayFormatAlpha(app);
      SDL_FreeSurface(app);
      SDL_Rect destR;

      destR.x=0;
      destR.y=0;

      SDL_BlitSurface(img, NULL, screen, &destR);
     }
};

int main(int argc, char* argv[]) {
     SDL_Init(SDL_INIT_VIDEO);
     foo a;
     fee b;
     b.draw(a.getSurf());
     SDL_Flip(a.getSurf());
     sleep(5);
     return 0;
}

编译并运行,但屏幕是黑色的,任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:4)

屏幕是黑色的可能是因为你正在使用双缓冲并且从不翻转缓冲区(在SDL_Flip(a.getSurf())之后调用b.draw)。