samedi 28 mars 2015

C - X11 API: Graphics not Working in a While Loop



I am trying to draw a background dynamically. The graphics work great normally...but:



int width, height;
GC gc_backcolor;
XGCValues gcv_backcolor;

width = c_values.width;
height = c_values.height;

gcv_backcolor.background = c_values.backcolor;
gcv_backcolor.foreground = c_values.backcolor;
gc_backcolor = XCreateGC(display, canvas, GCBackground | GCForeground, &gcv_backcolor);

int x = 0;
int y = 0;

while (y < height) {
x = 0;

while (x < width) {
XDrawPoint(display, canvas, gc_backcolor, x, y);
x++;
}
y++;
}

x = 0;
y = 0;


...for some reason, when I run it in a loop, it will not work. If anyone could explain to me why it is behaving this way, I would be grateful.


Could it be that I am calling this function on the canvas expose event?



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

if (event.xany.window == canvas) {
if (event.type == Expose) {
Canvas_Draw(display, canvas, c_values);
}

}
}


Here is my main code:



#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include "Canvas.h"
#include "Extensions.h"

int main(int argc, char* argv[])
{
Display* display;
int screen;
Window window;
Window canvas;
XEvent event;
GC gc_canvas;

//Canvas* canvas;

display = XOpenDisplay(NULL);

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

screen = DefaultScreen(display);
window = XCreateSimpleWindow(display, RootWindow(display, screen), 0, 0, 640, 480, 1, BlackPixel(display, screen), WhitePixel(display, screen));

XSelectInput(display, window, ExposureMask | KeyPressMask);
XMapWindow(display, window);

/* Create Canvas */

XGCValues gcv_canvas;
CanvasValues c_values;

gcv_canvas.foreground = 0xff00ff;
gcv_canvas.background = 0xff00ff;

canvas = XCreateSimpleWindow(display, window, 0, 0, 256, 256, 1, BlackPixel(display, screen), WhitePixel(display, screen));

gc_canvas = XCreateGC(display, canvas, GCForeground | GCBackground, &gcv_canvas);

XSelectInput(display, canvas, ExposureMask | KeyPressMask);
XMapWindow(display, canvas);

Canvas_Create_Content_Field(display, canvas, c_values);

c_values.backcolor = 0x00ff00;

//Canvas_Draw(display, canvas, c_values); this code only appears to work in the expose event

/* Create Canvas */

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

if (event.xany.window == canvas) {
if (event.type == Expose) {
Canvas_Draw(display, canvas, c_values);
}

}
}

return 0;
}



Aucun commentaire:

Enregistrer un commentaire