跨平台硬件加速 2d C++应用程序?

发布于 2024-09-12 05:15:23 字数 1539 浏览 13 评论 0原文

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

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

发布评论

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

评论(6

划一舟意中人 2024-09-19 05:15:23

那是胡说八道

OpenGL IS 跨平台, 。不需要Qt之类的。只有少数部分必须进行调整:窗口 API 和输入 API,它们是唯一依赖于操作系统特定例程的函数。

你有几种可能性:

  • 自己动手。我不推荐这样做,因为您不会学到任何有趣的(几乎)
  • SDL。它有一个硬件加速标志,因此它与任何其他
  • SFML 一样好。这看起来不错,但恕我直言,还不够成熟。其他部分(网络等)是其他库的包装器,因此我并没有真正看到
  • GLUT 的优势。这个是邪恶的。
  • GLFW。这个很棒,真的。我已经使用它很多年了。从各个角度来看它都是完美的。唯一的限制是你只能有一个 openGL 窗口,但由于它是用于游戏的,所以应该没问题。

That's nonsense guys

OpenGL IS cross-platform. No need for Qt or such. Only a few part must be adapted : the windowing API and the input API, which are the only functions that depend on OS-specific routines.

You have several possibilities :

  • roll your own. I don't recommend this, since you won't learn anything interesting (hardly)
  • SDL. It has a flag to be HW accelerated, so it's as good as any other
  • SFML. This seems good but not mature enough IMHO. The other parts (networking, ...) are wrappers for other libraries so I don't really see the advantage
  • GLUT. This one is evil.
  • GLFW. This one is great, really. I've been using it for years now. It's perfect from every point of view. The only limitation is that you can only have one openGL window, but since it's for a game it should be ok.
梦初启 2024-09-19 05:15:23

跨平台硬件加速 2d C++ 应用程序?

SDL+OpenGL。也许还有 gleeglew 如果您使用着色器和/或扩展。
Glee比较好用,但是貌似不支持3.0以后的OpenGL版本。
此外,您可能需要 SDL_image 来加载图像。

我知道有 OpenGL,但我似乎找不到任何关于如何跨平台使用它的教程,它们都专注于一个平台。

    SDL_Init(SDL_INIT_VIDEO);

    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);//disable vsync

    if (SDL_SetVideoMode(scrWidth, scrHeight, 32, SDL_OPENGL) == 0){
        fprintf(stderr, "couldn't set mode %dx%d!\n", 640, 480);
        SDL_Quit();
        return -1;
    }
    glewInit();//if you use glew, shaders, extensions.

    while(gameIsRunning()){//game loop
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
        //OpenGL rendering here 
        glFlush();
        SDL_GL_SwapBuffers();       
    }
    SDL_Quit();

有关详细信息,请参阅 sdl 文档。

使用SDL也是一种可能性,但我担心如果我使用它,游戏可能会表现不佳。这一定是真的吗?

禁用垂直同步后,每秒可以获得数百到数千帧。确切的性能取决于您的硬件和场景复杂性 - 我有 300 fps 的简单 3D 地下城爬行器报告,该爬行器使用“RAW”opengl,没有显示列表/顶点缓冲区对象。此外,如果您使用固定帧速率或计时器驱动的引擎,每秒获得的帧数不会超过您要求的数量。

SDL 用于将 Unreal 2004 移植到 Linux。它也被用于 Doom 3/Quake 4 Linux 移植。因此它经过了彻底的测试并且众所周知。

请参阅此列表了解更多信息。

Cross platform hardware accelerated 2d C++ app?

SDL + OpenGL. Maybe also glee or glew if you use shaders and/or extensions.
Glee is easier to use, but it looks like it doesn't support OpenGL versions after 3.0.
Also you might need SDL_image for loading images.

I know there's OpenGL, but I can't seem to find any tutorials on how to use it in a cross platform manner, they all focus on one platform.

    SDL_Init(SDL_INIT_VIDEO);

    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);//disable vsync

    if (SDL_SetVideoMode(scrWidth, scrHeight, 32, SDL_OPENGL) == 0){
        fprintf(stderr, "couldn't set mode %dx%d!\n", 640, 480);
        SDL_Quit();
        return -1;
    }
    glewInit();//if you use glew, shaders, extensions.

    while(gameIsRunning()){//game loop
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
        //OpenGL rendering here 
        glFlush();
        SDL_GL_SwapBuffers();       
    }
    SDL_Quit();

See sdl documentation for more info.

Using SDL is also a possibility, but I'm afraid the game may not perform as well if I use it. Is this necessarily true?

With vsync disabled, you can get from few hundreds to thousand frames per second. Exact performance depends on your hardware and scene complexity - I had 300 fps reports for simple 3D dungeon crawler that used "RAW" opengl without display lists/vertex buffer objects. Also, if you use fixed framerate or timer-driven engine, you won't get more frames per second than you asked for.

SDL was used for porting Unreal 2004 to Linux. It also was used in Doom 3/Quake 4 linux port. So it is thoroughly tested and well known.

See this list for more info.

别再吹冷风 2024-09-19 05:15:23

如果您希望以跨平台方式使用 OpenGL,您可以使用 QGLWidget 将其嵌入到 Qt 应用程序中。

If you want OpenGL in a cross platform manner you can embed it in a Qt application using QGLWidget.

不羁少年 2024-09-19 05:15:23

还有 wxWidgets,它有 OpenGl 支持

There's also wxWidgets, which has OpenGl support.

羞稚 2024-09-19 05:15:23

对我来说,问题不在于知道是否应该使用库。这是您应该使用的库。如果您想编写游戏,请找到可以为您解决大多数可移植性问题的库。它会让您专注于最重要的事情:游戏本身。其他人给出了更多我本可以给你的图书馆建议。

我认为在项目开始之前过度担心性能是错误的。像开发过程中遇到的任何其他问题一样解决性能问题。设计您的程序以将库隔离在与其余程序逻辑分开的层中。如果需要的话,切换实现会更容易。这甚至允许尝试不同的实现。

IE:

// separate files

class LowLevelGraphicStuff {
  // abstract
};

class LowLevelGraphicStuff_SFML : public LowLevelGraphicsStuff {
  // actual SFML implementation
};

class LowLevelGraphicsStuff_OGL : public LowLevelGraphicsStuff  {
  // actual OpenGL implementation
};

// main

// Run the game with the SFML implementation.
gameLoop(new LowLevelGraphicsStuff_SFML());
// Run the game with the OpenGL implementation.
gameLoop(new LowLevelGraphicsStuff_OGL());

To me the question is not to know if you should use libraries or not. It is what library you should use. If you want to write a game, then find libraries that will solve most portability issues for you. It will let you concentrate on what matters the most: the game itself. Others gave more library suggestions that I could have given you.

I think it is a mistake to overly worry about performance before you have even started your project. Work on performance issues like any other problem you face during development. Design your program to isolate libraries in a layer separate from the rest of your program logic. It will be easier to switch implementation if needed. That will even allow to experiment different implementations.

Ie:

// separate files

class LowLevelGraphicStuff {
  // abstract
};

class LowLevelGraphicStuff_SFML : public LowLevelGraphicsStuff {
  // actual SFML implementation
};

class LowLevelGraphicsStuff_OGL : public LowLevelGraphicsStuff  {
  // actual OpenGL implementation
};

// main

// Run the game with the SFML implementation.
gameLoop(new LowLevelGraphicsStuff_SFML());
// Run the game with the OpenGL implementation.
gameLoop(new LowLevelGraphicsStuff_OGL());
复古式 2024-09-19 05:15:23

是的,选择 SFML。 SDL 本身并不是硬件加速的。

OpenGL 确实是可移植的,但它确实具有用于创建 OpenGL 上下文的特定于平台的方法。 SFML 解决了这个问题

Yes, go with SFML. SDL is not inherently hardware accelerated.

OpenGL is indeed portable but it does have platform-specific methods for creating the OpenGL context. SFML solves that.

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