如何使用xlib创建游戏循环 [英] How to create a game loop with xlib

查看:237
本文介绍了如何使用xlib创建游戏循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为xlib窗口创建一个游戏循环,但是我不能正确地绘制窗口。现在我使用XCreateSimpleWindow(...)创建一个窗口,并使用for循环来绘制所有像素一次一个。 (这些像素的颜色是从一个大整数数组中读取的,现在我已将所有像素设置为蓝色。)实际的游戏循环现在是以下:

I am trying to create a game loop for an xlib window, but I cannot manage to paint the window correctly. Right now I am creating a window with XCreateSimpleWindow(...), and using a for loop to paint all pixels one at a time. (The color of these pixels is read from a large integer array, for now I've set all pixels to be blue.) The actual game loop right now is the following:

void loop() {
    while (true) {
        // Clear the window (the background color is set to white)
        XClearWindow(dsp, win);

        // Loop through all pixels of the 800*600 window
        for (int j = 0; j < 600; j++) {
            for (int i = 0; i < 800; i++) {
                // Read the color from the pixels array (always blue for now)
                long int color = pixels[i + 800*j];
                // Set the foreground color for drawing
                XSetForeground(dsp, gc, color);
                // Draw the pixel
                XDrawPoint(dsp, win, gc, i, j);
            }
        }

        // Flush the output buffer
        XFlush();
    }
}

定义变量dsp,win,pixels,gc

The variables dsp, win, pixels, gc are defined globally.

现在,当我编译和执行二进制文件时,具有低y坐标的行大部分是蓝色的,但是具有高y坐标的行大多是白色的。在它之间很容易看到它简单地需要太多的时间来绘制所有的像素一次。我期望这种效果是因为首先绘制顶行(低y),这意味着这些像素的XClearWindow()和XDrawPoint()之间的短延迟。 (我也测试了fps,需要大约7毫秒来运行while(true)循环一次。)

Now when I compile and execute the binary file, the rows with low y coordinate are mostly blue, but the rows with high y coordinate are mostly white. In between it is easy to see how it simply takes too much time to draw all pixels at once. I expect that this effect is because the top rows (low y) are drawn first, which means a short delay between XClearWindow() and XDrawPoint() for those pixels. (I also tested the fps, it takes around 7 milliseconds to run the while(true) loop once.)

我做了一些研究,解决这个问题。我没有遵循一个关于双缓冲与xlib(Xdbe)的指南,但它似乎并没有解决问题。有没有更快的绘制xlib的方式,而不是循环遍历所有的像素?

I did some research, and read about how double buffering might solve this issue. I did follow a guide on double buffering with xlib (Xdbe), but it does not seem to solve the issue. Is there a faster way of drawing with xlib than just looping through all pixels? Is double buffering not supposed to solve this, or am I implementing it incorrectly?

推荐答案

直接与Xlib进行通信是如此的早期90年代(阅读:没有人这样做,除非他是一个框架设计器,在过去20年!)

Communicating directly with Xlib is so early-90s (read: noone has done this, unless he's a framework designer, in the last 20 years!).

你是对的,循环通过像素更新他们屏幕难以置信地慢。这就是为什么几乎所有的现代GUI框架都使用他们自己的Xbuffer,他们只是吸引和指示X渲染。

You're right, looping through pixels to update them on the screen is incredibly slow. That's why almost all modern GUI Frameworks use their own Xbuffers that they just draw on and instruct X to render.

作为对你的方法的评论:
游戏开发对原始X没有最低限度的意义,因为有更多的可爱,更好的性能,更容易使用,小,经过良好测试的库。以SDL为例。

As a comment on your approach: Game development on raw X doesn't make the least sense, as there are more protable, better performing, easier to use, small, well-tested libraries. Take SDL as an example.

这篇关于如何使用xlib创建游戏循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆