如何在Xlib中立即更改显示的场景?

如何解决如何在Xlib中立即更改显示的场景?

我正在用C语言编写一个简单的游戏,并且我不想使用任何高级库(例如OpenGL)。最烦人的问题是更改显示场景的问题。不久前,有人告诉我您在Windows中使用SwapBuffers()函数。我试图在我的PaintEngine中编写类似的代码,从而产生了以下功能:

ECode PaintEngine_SwapBuffers (PaintEngine PE) {
    PaintEngine_Platform *platform = &(PE->platform);

    XCopyArea (platform->display,platform->pixmap,platform->window,platform->gc,PE->width,PE->height,0);
    return ECode_OK;
}

平台结构定义如下:

typedef struct {
    Display *display;
    int      screen;
    Window   window;
    GC       gc;
    int      depth;
    Colormap colormap;
    Visual  *visual;
    Pixmap   pixmap;
} PaintEngine_Platform;

每次调整窗口大小时,像素图都会更改:

platform->pixmap     = XCreatePixmap (platform->display,XRootWindow(platform->display,platform->screen),platform->depth);

因此,我在像素图上进行了所有绘制,并且在事件处理周期中一次调用了PaintEngine_SwapBuffers()。

我的问题:

  1. 持续闪烁。一切都闪烁得很明显
  2. 有时候,当我调整窗口大小时,像素图似乎没有被调整大小。

我知道,这种方法可能还可以,问题出在我的PaintEngine的其余部分中,所以这里是完整的代码:

#if defined(__linux__) || defined(__FreeBSD__)
#include <X11/Xlib.h>
#endif

#include <time.h>

extern void *malloc (size_t);
extern void  free   (void *);

// Type definitions

typedef u_int8_t  ECode;
#define           ECode_OK    (char) 0
#define           ECode_ERROR (char) 1
#define           ECode_QUIT  (char) 2

#if defined(__linux__) || defined(__FreeBSD__)

typedef struct {
    Display *display;
    int      screen;
    Window   window;
    GC       gc;
    int      depth;
    Colormap colormap;
    Visual  *visual;
    Pixmap   pixmap;
} PaintEngine_Platform;

#endif

typedef struct _PaintEngine_Mouse {
    u_int32_t x;
    u_int32_t y;
    u_int32_t x_prev;
    u_int32_t y_prev;
    u_int8_t  press   [5];
    u_int8_t  moves;
} PaintEngine_Mouse;

#define Mouse_Left       0
#define Mouse_Middle     1
#define Mouse_Right      2
#define Mouse_ScrollUp   4
#define Mouse_ScrollDown 5

typedef struct _PaintEngine {
    u_int32_t            width;
    u_int32_t            height;
    u_int32_t            pixel_size;
    u_int8_t             full_screen;
    u_int32_t          (*OnCreate ) (struct _PaintEngine *);
    u_int32_t          (*OnDestroy) (struct _PaintEngine *);
    u_int32_t          (*OnUpdate ) (struct _PaintEngine *,float);
    u_int32_t            active;
    clock_t              t1,t2;
    PaintEngine_Mouse    mouse;

    PaintEngine_Platform platform;

    u_int32_t          (*OnButtonPress)   (struct _PaintEngine *);
    u_int32_t          (*OnMotion)        (struct _PaintEngine *);
    u_int32_t          (*OnButtonRelease) (struct _PaintEngine *);
} *PaintEngine;

PaintEngine PaintEngine_Create          (u_int32_t,u_int32_t,u_int8_t);
ECode       PaintEngine_Start           (PaintEngine);
u_int32_t   PaintEngine_OnCreate        (PaintEngine);
u_int32_t   PaintEngine_OnDestroy       (PaintEngine);
u_int32_t   PaintEngine_OnUpdate        (PaintEngine,float);
ECode       PaintEngine_CreateWindow    (PaintEngine);
u_int32_t   PaintEngine_ProcessCycle    (PaintEngine);
ECode       PaintEngine_ProcessLoop     (PaintEngine);
ECode       PaintEngine_ProcessLoopInit (PaintEngine);
ECode       PaintEngine_CleanUp         (PaintEngine);
void        PaintEngine_ProcessEvents   (PaintEngine);
u_int32_t   PaintEngine_OnButtonPress   (PaintEngine);
u_int32_t   PaintEngine_OnButtonRelease (PaintEngine);
u_int32_t   PaintEngine_OnMotion        (PaintEngine);
ECode       PaintEngine_SwapBuffers     (PaintEngine);
ECode       PaintEngine_ClearWindow     (PaintEngine);

PaintEngine PaintEngine_Create (u_int32_t width,u_int32_t height,u_int32_t pixel_size,u_int8_t full_screen) {

    PaintEngine PE = malloc(sizeof(struct _PaintEngine));

    PE->width           = width;
    PE->height          = height;
    PE->pixel_size      = pixel_size;
    PE->full_screen     = full_screen;
    PE->OnCreate        = PaintEngine_OnCreate;
    PE->OnDestroy       = PaintEngine_OnDestroy;
    PE->OnUpdate        = PaintEngine_OnUpdate;
    
    PE->OnMotion        = PaintEngine_OnMotion;
    PE->OnButtonPress   = PaintEngine_OnButtonPress;
    PE->OnButtonRelease = PaintEngine_OnButtonRelease;

    return PE;
}

#if defined(__linux__) || defined(__FreeBSD__)

ECode PaintEngine_Start (PaintEngine PE) {
    ECode ecode;
    if ((ecode = PaintEngine_CreateWindow (PE)) != ECode_OK)
        return ecode;
    if ((ecode = PaintEngine_ProcessLoop  (PE)) != ECode_OK)
        return ecode;
    return       PaintEngine_CleanUp      (PE);
}

#endif

u_int32_t PaintEngine_OnCreate        (PaintEngine PE)               { return 1; }
u_int32_t PaintEngine_OnDestroy       (PaintEngine PE)               { return 1; }
u_int32_t PaintEngine_OnUpdate        (PaintEngine PE,float unused) { return 1; }
u_int32_t PaintEngine_OnMotion        (PaintEngine PE)               { return 1; }
u_int32_t PaintEngine_OnButtonPress   (PaintEngine PE)               { return 1; }
u_int32_t PaintEngine_OnButtonRelease (PaintEngine PE)               { return 1; }

#if defined(__linux__) || defined(__FreeBSD__)

ECode PaintEngine_CreateWindow (PaintEngine PE) {
    PaintEngine_Platform *platform = &(PE->platform);
    XSetWindowAttributes  wa;
    u_int32_t             display_width,display_height;

    XInitThreads();

    platform->display    = XOpenDisplay   (0);
    platform->screen     = DefaultScreen  (platform->display);
    platform->depth      = DefaultDepth   (platform->display,platform->screen);
    platform->visual     = DefaultVisual  (platform->display,platform->screen);
    wa.background_pixel  = XBlackPixel    (platform->display,platform->screen);
    wa.override_redirect = False;
    wa.event_mask        = ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask |
                           ButtonReleaseMask | PointerMotionMask | FocusChangeMask | StructureNotifyMask;

    display_width        = DisplayWidth   (platform->display,platform->screen);
    display_height       = DisplayHeight  (platform->display,platform->screen);

    if (PE->full_screen) {
        PE->width  = DisplayWidth     (platform->display,platform->screen);
        PE->height = DisplayHeight    (platform->display,platform->screen);
    #if defined(__linux__)
        wa.override_redirect = True;
    #endif
    }

    platform->window     = XCreateWindow (platform->display,XRootWindow (platform->display,(display_width  - PE->width)  / 2,(display_height - PE->height) / 2,platform->depth,InputOutput,platform->visual,CWBackPixel | CWEventMask | CWOverrideRedirect,&wa);
    
    XMapWindow (platform->display,platform->window);

//  platform->colormap   = XCreateColormap (platform->display,AllocAll);
    platform->gc         = XCreateGC (platform->display,0);
    platform->pixmap     = XCreatePixmap (platform->display,platform->depth);

    XSetForeground (platform->display,XWhitePixel(platform->display,platform->screen));
    XSetBackground (platform->display,platform->screen));

    XFlush(platform->display);

    return ECode_OK;
}

#endif

ECode PaintEngine_ProcessLoop (PaintEngine PE) {
    PE->active = PE->OnCreate(PE);

    PaintEngine_ProcessLoopInit (PE);

    while (PE->active) {
        while (PE->active)
            PaintEngine_ProcessCycle (PE);
        if (!PE->OnDestroy (PE))
            PE->active = 1;
    }

    PaintEngine_CleanUp (PE);
    
    return ECode_OK;
}

ECode PaintEngine_ProcessLoopInit (PaintEngine PE) {
    int i;
    PE->t1 = clock();
    
    for (i=0; i < 5; i++)
        (PE->mouse).press[i] = 0;
    (PE->mouse).x_prev = 0;
    (PE->mouse).y_prev = 0;

    return ECode_OK;
}

u_int32_t PaintEngine_ProcessCycle (PaintEngine PE) {
    float elapsed_time;

    PE->t2       = clock();
    elapsed_time = (float) (PE->t2 - PE->t1) / CLOCKS_PER_SEC;
    PE->t1       = PE->t2;

    PaintEngine_ProcessEvents (PE);

    PE->active   = PE->OnUpdate(PE,elapsed_time);
    
//  PaintEngine_ClearWindow(PE);
    PaintEngine_SwapBuffers(PE);

    printf("Here\n");
    return 1;
}
#if defined(__linux__) || defined(__FreeBSD__)


void PaintEngine_ProcessEvents (PaintEngine PE) {
    XEvent event;
    PaintEngine_Platform *platform = &(PE->platform);
    PaintEngine_Mouse    *mouse    = &(PE->mouse);

    while (XPending (platform->display)) {
            XNextEvent (platform->display,&event);
            mouse->moves = 0;

            switch (event.type) {
                case Expose:
                    break;

                case ConfigureNotify:
                    PE->width  = event.xconfigure.x;
                    PE->height = event.xconfigure.y;

                    XFreePixmap   (platform->display,platform->pixmap);
                    platform->pixmap = XCreatePixmap (platform->display,platform->depth);
                    break;

                case KeyPress:
                    PaintEngine_SwapBuffers(PE);
                    break;

                case ButtonPress:
                    mouse->x_prev = mouse->x;
                    mouse->y_prev = mouse->y;
                    mouse->x = event.xbutton.x;
                    mouse->y = event.xbutton.y;

                    (mouse->press)[event.xbutton.button-1] = 1;

                    PE->OnButtonPress(PE);
                    break;

                case MotionNotify:
                    mouse->moves  = 1;
                    mouse->x_prev = mouse->x;
                    mouse->y_prev = mouse->y;
                    mouse->x = event.xmotion.x;
                    mouse->y = event.xmotion.y;

                    PE->OnMotion(PE);
                    break;

                case LeaveNotify:
                case ButtonRelease:
                    PE->OnButtonRelease(PE);
                    (mouse->press)[event.xbutton.button-1] = 0;
                    break;

                default:
                    break;
            }
    }
    return;
}

ECode PaintEngine_SwapBuffers (PaintEngine PE) {
    PaintEngine_Platform *platform = &(PE->platform);

    XCopyArea (platform->display,0);
    return ECode_OK;
}

ECode PaintEngine_DrawLine (PaintEngine PE,u_int32_t x1,u_int32_t y1,u_int32_t x2,u_int32_t y2) {
    PaintEngine_Platform *platform = &(PE->platform);

    XDrawLine (platform->display,x1,y1,x2,y2);
    printf("(%d,%d) - (%d,%d)\n",y2);
    return ECode_OK;
}

ECode PaintEngine_ClearWindow (PaintEngine PE) {
    PaintEngine_Platform *platform = &(PE->platform);

    XClearWindow (platform->display,platform->window);
    return ECode_OK;
}

ECode PaintEngine_CleanUp (PaintEngine PE) {
    PaintEngine_Platform *platform = &(PE->platform);

    XFreeGC        (platform->display,platform->gc);
    XDestroyWindow (platform->display,platform->window);
    XFreePixmap    (platform->display,platform->pixmap);
    free           (PE);

    return ECode_OK;
}
#endif

对于其他有关引擎的建议,我也将不胜感激

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res