c++ - Mac OS OpenGL - CPU 使用问题

标签 c++ macos opengl sdl cpu


自 Mavericks 更新以来(我现在在 10.10),调试窗口显示此消息:

The function ‘CGContextErase’ is obsolete and will be removed in an upcoming update. Unfortunately, this application, or a library it uses, is using this obsolete function, and is thereby contributing to an overall degradation of system performance.

我正在创建一个 OpenGL (SDL) 应用程序,它是用 C++ 编写的,现在由于我的应用程序(它使用 MacBook 的 Intel i5 处理器的 100% 功率),我的 CPU 内存出现问题。 所以,也许是因为所有这些 CGContextErase 函数。 我该如何解决? 好吧,也许我的代码中有错误:

//I'm using the SDL2
#include <SDL2/SDL.h>

class sWindow {
public:
    SDL_Window *win;
    SDL_Surface *winSur;
    SDL_Event e;

    void createWindow(char*,int,int,int,int,Uint32);
    void update();
    void render();
    void close();

    SDL_Rect WIN_RECT;
    char WIN_TITLE = NULL;
    int WIN_ID = -1;
};

SDL_Rect newRect(int x, int y, int w, int h) {
    SDL_Rect returnRect;

    returnRect.x = x;
    returnRect.y = y;
    returnRect.w = w;
    returnRect.h = h;

    return returnRect;
}

//The window, where the content(surface) will be rendered.
sWindow win1;

//Window's construct
void sWindow::createWindow(char* title, int x, int y, int w, int h, Uint32 flags) {
    win = SDL_CreateWindow(title, x, y, w, h, flags);
    winSur = SDL_GetWindowSurface(win);

    WIN_RECT = newRect(x, y, w, h);
    WIN_ID = SDL_GetWindowID(win);
}

//The logic and render actions...
void sWindow::update() {

}

//Window's destructor
void sWindow::close() {
    SDL_DestroyWindow(win);
    SDL_FreeSurface(winSur);
}

//Main loop control
bool quit = false;

//Initilize the OpenGL and other libs(SDL2)
bool inited() {
    bool result = true;

    if (SDL_INIT_VIDEO <= 0) {
        result = false;
        printf("SDL_INIT_VIDEO Failed");
    }

    return result;
}

//Main loop...
int main(int argc, char* argv[]) {
    if (inited()) {
        win1.createWindow((char*)"SpaceCode", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 380, 280, SDL_WINDOW_SHOWN);
        while (!quit) {

            while (SDL_PollEvent(&win1.e) != 0) {
                if (win1.e.type == SDL_QUIT) {
                    quit = true;
                }
            }
        }
    }

    win1.close();
    SDL_Quit();
    return 0;
}

最佳答案

这段代码总是会导致高 CPU 使用率,因为没有适当的帧速率上限,因此它只会尝试尽可能快地处理它。

查看下面的各种帖子:

还有更多在 Google 上的搜索。

关于c++ - Mac OS OpenGL - CPU 使用问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26798438/

相关文章:

C++ 防止程序手动执行

c++ - 带有 std::vector 的 VBO

macos - NCurses 和 OS X 10.6 发生了什么变化?

objective-c - 仅适用于我的应用程序的私有(private) cookie?为什么它会影响浏览器的 cookie?

c++ - OpenGL glDeleteTextures 导致内存泄漏?

c++ - IsIconic() 总是返回 false 而 OpenIcon() 从不打开窗口

c++ - 重新定义类成员数据类型

java - 带有 openCV lib 的可执行 jar(Eclipse、Mac)

opengl - 在OpenGL中将纹理映射到VBO的问题

ruby - 如何在 Windows 上安装 ruby​​-opengl?