SDL_Surface 指针在两个类之间传递

发布于 2024-10-14 22:21:39 字数 999 浏览 6 评论 0原文

如果我在一个类中声明一个 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;
}

编译运行,但是黑屏,谁能帮忙?

If I declare a SDL_Surface pointer in a class, can i share it with another class to draw on it in somehow?

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

compiles and run, but the screen is black, can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

任谁 2024-10-21 22:21:40

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

Screen is black probably because you're using double buffering and never flip the buffer (call SDL_Flip(a.getSurf()) after b.draw).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文