C - 函数仅在循环至少 17 次时才工作

标签 c loops iteration screen x11

在继续回答这个问题之前,我想先说明一下,我正在自学一切,没有大学经验。我买不起。因此,如果有人知道一个好的资源,他们可以向我指出在科学层面上与这个主题相关的内容,我将不胜感激!

我注意到,当我运行一个在屏幕上绘制颜色图的函数时,它在一次调用中不起作用。该调用必须循环至少 17 次才能真正将颜色图绘制到屏幕上。

我正在 X11 中为 Linux 编程,下面是我的代码示例,该代码不起作用 --->

Canvas_Draw(display, &canvas);

不过,当我将其更改为这个时,它就起作用了 --->

unsigned int number = 0;

while (number < 17) {
    Canvas_Draw(display, &canvas);
    number++;
}

只要循环至少 17 次就可以工作。然而,对于加载和解码文件等其他功能来说,情况并非如此,即使它很大......甚至离屏渲染。这是否与该函数正在将图像绘制到屏幕有关?这是被调用函数的代码 --->

GC gc_field;
XGCValues gc_values;
gc_field = XCreateGC(display, canvas->canvas, GCForeground, &gc_values);

XPutImage(display, canvas->canvas, gc_field, canvas->image, 0, 0, 0, 0, canvas->width, canvas->height);
XFlush(display);

任何向我解释这一点的帮助,甚至向我指出解释这一点的有效资源,将不胜感激!谢谢!

编辑

在重新制作此示例后,重要数字发生了变化!现在不再是17岁,而是13岁了!我还注意到,对于不同的图像,它不需要循环该函数,而在第一个测试的图像上仍然需要循环该函数!

请注意,BMP 解码器仅解码 24 位未压缩的 BMP 图像。

这是一个可验证的示例 --->

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>

#define BYTE unsigned char
#define UINT unsigned int
#define ULONG unsigned long
#define COLOR unsigned int
#define ARRAY unsigned char*

typedef struct Position Position;
typedef struct Size Size;
typedef struct Array Array;
typedef struct Pixelmap Pixelmap;
typedef struct BMP BMP;
typedef struct Canvas Canvas;
typedef struct Panel Panel;

struct Position {
    int x, y;
};

struct Size {
    unsigned int width, height;
};

struct Array {
    ARRAY data;
    ULONG dataCount;
};

struct Pixelmap {
    unsigned int* pixels;
    UINT depth;
    Size size;
    UINT pixelCount;
};

struct BMP {
    UINT width;
    UINT height;
    UINT depth;
    UINT compressionType;
    unsigned int* pixels;
};

struct Canvas {
    int loaded;
    int x, y, width, height;
    COLOR forecolor, backcolor;
    Pixelmap *field;
    XImage *image;
    Window canvas;
};

struct Panel {
    int x, y, width, height, forecolor, backcolor;
    Window panel;
};

void Canvas_Load_BMP(Display *display, Canvas *canvas, BMP *bmp);
void Canvas_Render(Display* display, Canvas *canvas);
void Canvas_Draw(Display *display, Canvas *canvas);
BMP *BMPDecoder_Decode(Array *bmpData);
Array *BMPDecoder_Load_File(char* Path);
Pixelmap *Pixelmap_Create(Size size, UINT Depth);
int Pixelmap_Add_Pixel(Pixelmap *pMap, COLOR Pixel);
int ArrayComparator(ARRAY A, ARRAY B, UINT Size);
Array *Array_Create(ULONG Size);
UINT Array_To_UINT(ARRAY Integer);
COLOR Color_Alpha_Blend(COLOR color, COLOR BG);

int main(int argc, char* argv[])
{
    Display* display;
    Screen* screen;
    int screenIndex;
    Size screenSize;
    Window window;
    XEvent event;
    GC gc_canvas;
    int x, y, width, height;
    Position w_position;
    Size w_size;
    int rendered = 0;
    const char* programName = "Image Display Test";

    display = XOpenDisplay(NULL);
    screen = DefaultScreenOfDisplay(display);

    screenSize.width = screen->width;
    screenSize.height = screen->height;

    if (display == NULL) {
        fprintf(stderr, "Failed to open display!\n");
        exit(1);
    }

    width = 800;
    height = 600;
    x = (screenSize.width/2) - (width/2);
    y = (screenSize.height/2) - (height/2);
    w_position.x = x;
    w_position.y = y;
    w_size.width = width;
    w_size.height = height;

    screenIndex = DefaultScreen(display);
    window = XCreateSimpleWindow(display, RootWindow(display, screenIndex), x, y, width, height, 1, 0, 0xFFFFFF);

    XSelectInput(display, window, KeyPressMask | PropertyChangeMask | StructureNotifyMask | PointerMotionMask | ButtonPressMask);
    XMapWindow(display, window);
    XFlush(display);

    XStoreName(display, window, programName);

    XWindowChanges changes;
    changes.x = w_position.x;
    changes.y = w_position.y;
    XConfigureWindow(display, window, CWX | CWY, &changes);

    Panel cPanel;

    cPanel.x = 0;
    cPanel.y = 0;
    cPanel.width = width;
    cPanel.height = height;

    cPanel.panel = XCreateSimpleWindow(display, window, 0, 0, width, height, 0, 0x000000, 0xFF0000);

    XSelectInput(display, cPanel.panel, KeyPressMask | ButtonPressMask | StructureNotifyMask);
    XMapWindow(display, cPanel.panel);
    XFlush(display);

    int created = 0;

    Canvas canvas;

    int c_width = width/2;
    int c_height = height/2;

    canvas.canvas = XCreateSimpleWindow(display, cPanel.panel, width/2-(c_width/2), width/2-(c_width/2), c_width, c_height, 0, 0, 0x000000);

    canvas.x = x;
    canvas.y = y;
    canvas.width = width;
    canvas.height = height;

    canvas.loaded = 0;

    XSelectInput(display, canvas.canvas, KeyPressMask | KeyReleaseMask | ButtonPressMask | StructureNotifyMask);
    XMapWindow(display, canvas.canvas);
    XFlush(display);

    Size size;
    size.width = canvas.width;
    size.height = canvas.height;

    canvas.field = Pixelmap_Create(size, 4);

    UINT index = 0;

    while (index < canvas.width*canvas.height) {
        Pixelmap_Add_Pixel(canvas.field, 0x00000000);
        index++;
    }

    canvas.backcolor = 0x00000000;
    XSetWindowBackground(display, canvas.canvas, 0x000000);

    Array *bmpData = BMPDecoder_Load_File("/home/user/Example 1.bmp");
    BMP *bmp = BMPDecoder_Decode(bmpData);

    Canvas_Load_BMP(display, &canvas, bmp);

    Canvas_Render(display, &canvas);

    UINT number = 0;

    while(1) {
        XNextEvent(display, &event);

        if (number < 13) {
            Canvas_Draw(display, &canvas);
            number++;
        }

        if (event.xany.window == window) {
            if (event.type == ConfigureNotify) {
                XWindowAttributes attributes;
                XGetWindowAttributes(display, window, &attributes);

                Size np_size;
                np_size.width = attributes.width;
                np_size.height = attributes.height;

                if (np_size.width > 0 && np_size.height > 0) {
                    cPanel.width = np_size.width;
                    cPanel.height = np_size.height;

                    XWindowChanges changes;
                    changes.width = np_size.width;
                    changes.height = np_size.height;
                    XConfigureWindow(display, cPanel.panel, CWWidth | CWHeight, &changes);
                }

                Position nc_position;
                nc_position.x = cPanel.width/2-(canvas.width/2);
                nc_position.y = cPanel.height/2-(canvas.height/2);

                canvas.x = nc_position.x;
                canvas.y = nc_position.y;

                XWindowChanges changes;
                changes.x = nc_position.x;
                changes.y = nc_position.y;
                XConfigureWindow(display, canvas.canvas, CWX | CWY, &changes);

                Canvas_Draw(display, &canvas);
            }
        }
    }

    return 0;
}

void Canvas_Load_BMP(Display *display, Canvas *canvas, BMP *bmp) {
    canvas->width = bmp->width;
    canvas->height = bmp->height;

    Size size;
    size.width = bmp->width;
    size.height = bmp->height;

    canvas->field = Pixelmap_Create(size, 4);

    char* data = calloc(bmp->width*bmp->height, 4);
    canvas->image = XCreateImage(display, DefaultVisual(display, 0), DefaultDepth(display, 0), ZPixmap, 0, data, bmp->width, bmp->height, 32, 0);

    XWindowChanges changes;
    changes.width = size.width;
    changes.height = size.height;
    XConfigureWindow(display, canvas->canvas, CWWidth | CWHeight, &changes);

    int x = 0;
    int y = bmp->height-1;
    UINT increment = 0;

    while (y > -1) {
        while (x < bmp->width) {
            Pixelmap_Add_Pixel(canvas->field, bmp->pixels[bmp->width*y+x]);
            x++;
        }
        x = 0;
        y--;
    }

    canvas->loaded = 1;
}

void Canvas_Render(Display* display, Canvas *canvas) {
    if (canvas->loaded == 0)
        return;

    int x = 0;
    int y = 0;

    while (y < canvas->height) {
        while (x < canvas->width) {
            XPutPixel(canvas->image, x, y, Color_Alpha_Blend(canvas->field->pixels[(canvas->width*y)+x], canvas->backcolor));
            x++;
        }
        x = 0;
        y++;
    }

    XFlush(display);
}

void Canvas_Draw(Display *display, Canvas *canvas) {
    if (canvas->loaded == 0)
        return;

    GC gc_field;
    XGCValues gc_values;
    gc_field = XCreateGC(display, canvas->canvas, GCForeground, &gc_values);

    XPutImage(display, canvas->canvas, gc_field, canvas->image, 0, 0, 0, 0, canvas->width, canvas->height);
    XFlush(display);
}

BMP *BMPDecoder_Decode(Array *bmpData) {
    BMP *bmp = malloc(sizeof(BMP));

    BYTE bheader[] = { 0x42, 0x4D };
    BYTE fSizeBytes[] = { bmpData->data[5], bmpData->data[4], bmpData->data[3], bmpData->data[2] };
    BYTE dataOffsetBytes[] = { bmpData->data[13], bmpData->data[12], bmpData->data[11], bmpData->data[10] };
    BYTE widthBytes[] = { bmpData->data[21], bmpData->data[20], bmpData->data[19], bmpData->data[18] };
    BYTE heightBytes[] = { bmpData->data[25], bmpData->data[24], bmpData->data[23], bmpData->data[22] };
    BYTE depthBytes[] = { 0x00, 0x00, bmpData->data[29], bmpData->data[28] };
    BYTE compressionTypeBytes[] = { bmpData->data[33], bmpData->data[32], bmpData->data[31], bmpData->data[30] };
    BYTE bitmapSizeBytes[] = { bmpData->data[37], bmpData->data[36], bmpData->data[35], bmpData->data[34] };

    ARRAY fheader = calloc(2, sizeof(BYTE));
    fheader[0] = bmpData->data[0];
    fheader[1] = bmpData->data[1];

    if (!ArrayComparator(fheader, bheader, 2)) {
        free(fheader);
        return NULL;
    }

    free(fheader);

    UINT fSize = Array_To_UINT(fSizeBytes);
    UINT dataOffset = Array_To_UINT(dataOffsetBytes);
    UINT width = Array_To_UINT(widthBytes);
    UINT height = Array_To_UINT(heightBytes);
    UINT depth = Array_To_UINT(depthBytes);
    UINT compressionType = Array_To_UINT(compressionTypeBytes);
    UINT bitmapSize = Array_To_UINT(bitmapSizeBytes);

    if (fSize != bmpData->dataCount)
        return NULL;

    if (fSize-dataOffset != bitmapSize)
        return NULL;

    int padding = (width*(depth/8))%4;

    if (padding != 0) {
        padding -= 4;
        padding *= -1;
    }

    bmp->width = width;
    bmp->height = height;
    bmp->depth = depth;
    bmp->compressionType = compressionType;

    bmp->pixels = calloc(width*height, sizeof(COLOR));

    int x = 0;
    int y = 0;

    BYTE colorBytes[depth/8];

    while (y < height) {
        while (x < width) {
            colorBytes[0] = bmpData->data[dataOffset+(width*(depth/8))*y+y*padding+x*(depth/8)+2];
            colorBytes[1] = bmpData->data[dataOffset+(width*(depth/8))*y+y*padding+x*(depth/8)+1];
            colorBytes[2] = bmpData->data[dataOffset+(width*(depth/8))*y+y*padding+x*(depth/8)];

            bmp->pixels[width*y+x] = (0xFF & colorBytes[0]) << 24 | (0xFF & colorBytes[1]) << 16 | (0xFF & colorBytes[2]) << 8 | 0xFF;
            x++;
        }
        x = 0;
        y++;
    }

    return bmp;
}

Array *BMPDecoder_Load_File(char* Path) {
    FILE *fileptr;
    ARRAY buffer;
    ULONG filelen;
    UINT i = 0;

    fileptr = fopen(Path, "rb");
    fseek(fileptr, 0, SEEK_END);
    filelen = ftell(fileptr);
    rewind(fileptr);
    buffer = calloc(filelen, sizeof(BYTE));

    Array *data = Array_Create(filelen);

    while (i < filelen) {
        fread(data->data+i, 1, 1, fileptr);
        i++;
    }

    if (data->dataCount <= 0)
        return NULL;

    return data;
}

Array *Array_Create(ULONG Size) {
    Array *array = malloc(sizeof(Array));

    array->data = calloc(Size, sizeof(BYTE));
    array->dataCount = Size;

    return array;
}

Pixelmap *Pixelmap_Create(Size size, UINT Depth) {
    Pixelmap *pMap = malloc(sizeof(Pixelmap));

    pMap->depth = Depth;
    pMap->size = size;
    pMap->pixels = calloc(1, sizeof(Depth));
    pMap->pixelCount = 0;

    return pMap;
}

int Pixelmap_Add_Pixel(Pixelmap *pMap, COLOR Pixel) {
    UINT pixelCount = pMap->pixelCount+1;

    void *newPixels = realloc(pMap->pixels, sizeof(pMap->depth)*pixelCount);

    if (newPixels != NULL)
        pMap->pixels = newPixels;
    else
        return 0;

    pMap->pixelCount++;

    pMap->pixels[pixelCount-1] = Pixel;

    return 1;
}

int ArrayComparator(ARRAY A, ARRAY B, UINT Size) {
    int index = 0;

    while (index < Size) {
        if (A[index] != B[index])
            return 0;

        index++;
    }

    return 1;
}

UINT Array_To_UINT(ARRAY Integer) {
    return Integer[0] << 24 | Integer[1] << 16 | Integer[2] << 8 | Integer[3];
}

COLOR Color_Alpha_Blend(COLOR color, COLOR BG) {
    COLOR cred = (color >> 24) & 0xFF;
    COLOR cgreen = (color >> 16) & 0xFF;
    COLOR cblue = (color >> 8) & 0xFF;
    COLOR alpha = color & 0xFF;

    COLOR bred = (BG >> 24) & 0xFF;
    COLOR bgreen = (BG >> 16) & 0xFF;
    COLOR bblue = (BG >> 8) & 0xFF;

    COLOR red = ((cred*alpha)/0xFF) + ((bred*(0xFF-alpha))/0xFF);
    COLOR green = ((cgreen*alpha)/0xFF) + ((bgreen*(0xFF-alpha))/0xFF);
    COLOR blue = ((cblue*alpha)/0xFF) + ((bblue*(0xFF-alpha))/0xFF);

    return red << 16 | green << 8 | blue;
}

GCC 编译行 --->

gcc <source-file> -lX11 -o <executable-file>

以下是加载第一个示例图像并且调用 Canvas_Draw() 函数少于 13 次时发生的情况 --->

这是加载第一个示例图像并且调用 Canvas_Draw() 函数 13 次或更多次时发生的情况 --->

这是加载第二个示例图像并且 Canvas_Draw() 函数被调用 1 次或更多次时发生的情况 --->

这里是压缩 zip 文件夹中两个可测试图像的链接 --->

Images.zip

如果有人能找出问题所在并帮助我解决问题,我将不胜感激!谢谢!

最佳答案

您的事件循环已损坏。

首先,您需要为窗口选择曝光事件:

XSelectInput(display, canvas.canvas, KeyPressMask | KeyReleaseMask |
                                     ButtonPressMask | StructureNotifyMask |
                                     ExposureMask); // <===== here

其次,只有当你的事件表明你需要绘制时,你才需要绘制:

while(1) {
    XNextEvent(display, &event);

    if (event.type == Expose &&                   // <=== not on other events
        event.xexpose.window == canvas.canvas) {  // <=== not on events from other windows
      Canvas_Draw(display, &canvas);
      continue;
    }

这会在第一次调用 Canvas_Draw 时显示您的图像。

当第一个暴露事件到达时,窗口就准备好绘制。在此之前尝试绘画是没有意义的。过早的请求可能会也可能不会达到您想要的效果。

之后,您可以在按下按钮或任何您想要的其他内容时自由地重画。

关于C - 函数仅在循环至少 17 次时才工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43996969/

相关文章:

C 给数组加值? `array + value`

检查c中字符串的第一个字母

python:重新启动循环

c - 比赛中的艰难循环

ios - 遍历两个自定义数组并在变量相等时设置值 Swift

python - 找到 N 个组中 N 个项目的所有组合,而没有重复的项目组合(python)?

c - 如何从.dat文件中提取资源文件?

java - 阻止一个 c 程序启动另一个程序

Java问题(需要帮助)

methods - 求解线性方程组的并行迭代算法