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;
}
编译运行,但是黑屏,谁能帮忙?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
屏幕为黑色可能是因为您使用双缓冲并且从不翻转缓冲区(在
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())
afterb.draw
).