简体   繁体   English

如何在 xlib 的 window 中显示 xmp 图像文件? (我正在用 C 编码)

[英]How can I display an xmp image file in a window in xlib? (I'm coding in C)

I am creating Chess in C, and I need to print the board after every move.我在 C 中创建国际象棋,每走一步都需要打印棋盘。 I have managed to print the board using strings as pieces but I would like to replace the strings with images of actual pieces.我已经设法使用字符串作为片段来打印电路板,但我想用实际片段的图像替换字符串。

I haven't been able to find a solution for displaying an image in an X11 window.我还没有找到在 X11 window 中显示图像的解决方案。

for(i=0;i<8;i++){
for(j=0;j<8;j++){
    if(board[i][j]>0){
        if(board[i][j]==100)
            strcpy(text,"Pawn");
        else if(board[i][j]==300)
            strcpy(text,"Knight");
        else if(board[i][j]==400)
            strcpy(text,"Bishop");
        else if(board[i][j]==500)
            strcpy(text,"Rook");
        else if(board[i][j]==900)
            strcpy(text,"Queen");
        else if(board[i][j]==10000)
            strcpy(text,"King");
XSetForeground(dpy,gc,whiteColor);
XDrawString(dpy,w,gc,j*100+20,i*100+50, text, strlen(text));

This snippet displays the white pieces using text in white color.此代码段使用白色文本显示白色部分。

Drawing XMP file is not supported directly with X11. X11 不直接支持绘制 XMP 文件。 There is built-in support for png , jpeg and the older format (pixmap, bitmap, etc).内置支持pngjpeg和旧格式(像素图、bitmap 等)。 First step will be to convert the XMP into fixed size image (from the code, looks like 20*50).第一步是将 XMP 转换为固定大小的图像(从代码来看,看起来像 20*50)。

In general, you want to preload the images (from files):通常,您要预加载图像(从文件):

#include <Xm/Xm.h>

 static unsigned char data[] = {
                0x13, 0x00, 0x15, 0x00, 0x93, 0xcd, 0x55, 0xa5, 0x93, 0xc5, 0x00, 0x80,
0x00, 0x60

        } ;
Screen s = DefaultScreenOfDisplay(d) ;
Pixel fg = XWhitePixelOfScreen(s) ;
Pixel bg = XBlackPixelOfScreen(s) ;
// Pixmap king_pm = XmGetPixmap(s, "king.png", fg, bg) ;
// Pixmap queen_pm = XmGetPixmap(s, "queen.png", fg, bg) ;

Pixmap king_pm = XCreatePixmapFromBitmapData(d, RootWindowOfScreen(s), data, 8, 8, fg, bg, 8) ;

Then drawing is based on COPYING the pixmap into the display (using the indices as above).然后绘制基于将像素图复制到显示器中(使用上述索引)。 For example, to draw the king.例如,画国王。

   XCopyArea(dpy, king_pm, gc, 0, 0, 20, 50, j*100+20,i*100+50) ;

Disclaimer: I do not have a way to test this code.免责声明:我没有办法测试此代码。 You might need to troubleshoot/fix as needed.您可能需要根据需要进行故障排除/修复。

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

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