简体   繁体   English

SDL - 获取原生屏幕分辨率

[英]SDL - Get native Screen Resolution

My code: 我的代码:

  1. window.cpp window.cpp

     Window::Window(int w, int h, const char *title, const char *icon) { height = h; width = w; if(SDL_Init( SDL_INIT_EVERYTHING ) == 0) { SDL_WM_SetCaption(title, NULL); SDL_WM_SetIcon(SDL_LoadBMP(icon),NULL); screen = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF); if(screen == NULL) { running = false; return; } fullscreen = false; } else running = false; return; } Window::Window() { const SDL_VideoInfo* info = SDL_GetVideoInfo(); screenWidth = info->current_w; screenHeight = info->current_h; Window(640, 480, "Flatgu game", "rsc/img/icon.bmp"); } 
  2. window.h 在window.h

     class Window { public: Window(); ~Window(); int getWidth() {return width;} int getHeight() {return height;} bool isFullscreen() {return fullscreen;} void toggleFullscreen(); private: Window(int w, int h, const char *title, const char *icon); bool fullscreen, running; int height, width, screenWidth, screenHeight; SDL_Surface *screen; }; 

It compiles fine, but then, after compiling, I'm getting this ugly error: 它编译得很好,但是在编译之后,我得到了这个丑陋的错误: 一些解决问题

What's the reason of my problem? 我的问题是什么原因? Why do I get so weird numbers? 为什么我会得到如此奇怪的数字?

My aim is to store original screen resolution for further use (like toggling to fullscreen ), and I have to do this before calling SDL_SetVideoMode() . 我的目标是存储原始屏幕分辨率以供进一步使用 (如切换到全屏 ),我必须在调用SDL_SetVideoMode()之前执行此操作。 That's why it is in the constructor. 这就是它在构造函数中的原因。

You have a problem with calling SDL Video Functions before actually initializing SDL. 在实际初始化SDL之前调用SDL视频功能时遇到问题。

SDL_Init( SDL_INIT_EVERYTHING )

has to be called before 一直被称为

SDL_GetVideoInfo(); 

In your case you call SDL_GetVideoInfo(); 在您的情况下,您调用SDL_GetVideoInfo(); first 第一

const SDL_VideoInfo* info = SDL_GetVideoInfo();   //<-- calls SDL_GetVideoInfo();   
screenWidth = info->current_w;
screenHeight = info->current_h;
Window(640, 480, "Flatgu game", "rsc/img/icon.bmp");    //<-- initializes SDL

So the solution is simple; 所以解决方案很简单; make the call SDL_Init( SDL_INIT_EVERYTHING ) immediately at the start of your program, then you can call SDL_GetVideoInfo(); 在程序开始时立即调用SDL_Init( SDL_INIT_EVERYTHING ) ,然后可以调用SDL_GetVideoInfo(); as much as you like. 尽你所能。 You will have to restructure your class Window slightly. 您将不得不稍微重构您的类窗口。

To get the best video mode call SDL_GetVideoInfo before setting up the video (before calling SDL_SetVideoMode ). 为了获得最佳的视频模式调用SDL_GetVideoInfo设置视频(之前调用之前SDL_SetVideoMode )。

But you still have to initialize the video subsystem before calling it ( SDL_Init(SDL_INIT_VIDEO) ). 但是在调用它之前你仍然必须初始化视频子系统( SDL_Init(SDL_INIT_VIDEO) )。

I know this is old, but there's a big mistake in the code. 我知道这已经过时了,但代码中存在一个很大的错误。

Window(640, 480, "Flatgu game", "rsc/img/icon.bmp");

creates a nameless instance of a Window, so the instance that calls it will still have uninitialized variables. 创建一个无名的Window实例,因此调用它的实例仍然会有未初始化的变量。 It looks like you were trying to use delegating constructors, but in that case the call to the other constructor must be in the member initializer list. 看起来您正在尝试使用委托构造函数,但在这种情况下,对其他构造函数的调用必须位于成员初始化列表中。 See this page . 看到这个页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM