triangle group code reorganization
This commit is contained in:
81
initcairo.c
81
initcairo.c
@@ -12,28 +12,11 @@ static Bool alwaysTruePredicate(Display *display, XEvent *event, XPointer arg)
|
||||
return true;
|
||||
}
|
||||
|
||||
// this computes center, radius and scalefactor out of ctx->matrix
|
||||
void updateDimensions(DrawingContext *ctx)
|
||||
{
|
||||
double det = ctx->matrix.xx * ctx->matrix.yy - ctx->matrix.xy * ctx->matrix.yx;
|
||||
double cx = (double)ctx->width/2;
|
||||
double cy = (double)ctx->height/2;
|
||||
double r = 2*sqrt((cx*cx + cy*cy)/det); // this is not safe anymore if we have non-uniform scaling
|
||||
|
||||
// don't use cairo_device_to_user() since the matrix might not be our CTM yet
|
||||
cx -= ctx->matrix.x0;
|
||||
cy -= ctx->matrix.y0;
|
||||
ctx->center_x = ( ctx->matrix.yy * cx - ctx->matrix.xy * cy) / det;
|
||||
ctx->center_y = (- ctx->matrix.yx * cx + ctx->matrix.xx * cy) / det;
|
||||
ctx->radius = r;
|
||||
ctx->scalefactor = sqrt(det);
|
||||
}
|
||||
|
||||
GraphicsInfo *initCairo(int screen, int mask, int width, int height, const char *name)
|
||||
{
|
||||
GraphicsInfo *info = malloc(sizeof(GraphicsInfo));
|
||||
DrawingContext *ctx = malloc(sizeof(DrawingContext));
|
||||
info->context = ctx;
|
||||
DimensionsInfo *dim = malloc(sizeof(DimensionsInfo));
|
||||
info->dim = dim;
|
||||
|
||||
mask |= StructureNotifyMask | ExposureMask | KeyPressMask | ButtonPressMask | PointerMotionMask;
|
||||
|
||||
@@ -65,10 +48,11 @@ GraphicsInfo *initCairo(int screen, int mask, int width, int height, const char
|
||||
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
|
||||
info->buffer = malloc(stride*height);
|
||||
info->buffer_surface = cairo_image_surface_create_for_data(info->buffer, CAIRO_FORMAT_ARGB32, width, height, stride);
|
||||
info->context->cairo = cairo_create(info->buffer_surface);
|
||||
cairo_matrix_init_identity(&info->context->matrix);
|
||||
info->context->width = width;
|
||||
info->context->height = height;
|
||||
info->buffer_context = cairo_create(info->buffer_surface);
|
||||
cairo_matrix_init_identity(&info->dim->matrix);
|
||||
info->dim->width = width;
|
||||
info->dim->height = height;
|
||||
updateDimensions(info->dim);
|
||||
|
||||
info->wm_delete_window = XInternAtom(info->display, "WM_DELETE_WINDOW", 0);
|
||||
info->wm_protocols = XInternAtom(info->display, "WM_PROTOCOLS", 0);
|
||||
@@ -81,7 +65,7 @@ GraphicsInfo *initCairo(int screen, int mask, int width, int height, const char
|
||||
|
||||
void destroyCairo(GraphicsInfo *info)
|
||||
{
|
||||
cairo_destroy(info->context->cairo);
|
||||
cairo_destroy(info->buffer_context);
|
||||
cairo_destroy(info->front_context);
|
||||
cairo_surface_destroy(info->surface);
|
||||
cairo_surface_destroy(info->buffer_surface);
|
||||
@@ -89,7 +73,7 @@ void destroyCairo(GraphicsInfo *info)
|
||||
XDestroyWindow(info->display, info->win);
|
||||
XFreeColormap(info->display, info->cmap);
|
||||
XCloseDisplay(info->display);
|
||||
free(info->context);
|
||||
free(info->dim);
|
||||
free(info);
|
||||
}
|
||||
|
||||
@@ -119,7 +103,7 @@ void waitUpdateTimer(GraphicsInfo *info)
|
||||
info->frames++;
|
||||
}
|
||||
|
||||
int processStandardEvent(GraphicsInfo *info, XEvent *ev, void (*draw)(DrawingContext *))
|
||||
int processStandardEvent(GraphicsInfo *info, XEvent *ev, void (*draw)(void *))
|
||||
{
|
||||
int state;
|
||||
static int last_x, last_y;
|
||||
@@ -133,17 +117,18 @@ int processStandardEvent(GraphicsInfo *info, XEvent *ev, void (*draw)(DrawingCon
|
||||
|
||||
case ConfigureNotify:
|
||||
printf("ConfigureNotify Event, new dimensions: %d %d %d %d\n", ev->xconfigure.x, ev->xconfigure.y, ev->xconfigure.width, ev->xconfigure.height);
|
||||
info->context->width = ev->xconfigure.width;
|
||||
info->context->height = ev->xconfigure.height;
|
||||
cairo_xlib_surface_set_size(info->surface, info->context->width, info->context->height);
|
||||
info->dim->width = ev->xconfigure.width;
|
||||
info->dim->height = ev->xconfigure.height;
|
||||
updateDimensions(info->dim);
|
||||
cairo_xlib_surface_set_size(info->surface, info->dim->width, info->dim->height);
|
||||
|
||||
cairo_destroy(info->context->cairo);
|
||||
cairo_destroy(info->buffer_context);
|
||||
cairo_surface_destroy(info->buffer_surface);
|
||||
free(info->buffer);
|
||||
stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, info->context->width);
|
||||
info->buffer = malloc(stride*info->context->height);
|
||||
info->buffer_surface = cairo_image_surface_create_for_data(info->buffer, CAIRO_FORMAT_ARGB32, info->context->width, info->context->height, stride);
|
||||
info->context->cairo = cairo_create(info->buffer_surface);
|
||||
stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, info->dim->width);
|
||||
info->buffer = malloc(stride*info->dim->height);
|
||||
info->buffer_surface = cairo_image_surface_create_for_data(info->buffer, CAIRO_FORMAT_ARGB32, info->dim->width, info->dim->height, stride);
|
||||
info->buffer_context = cairo_create(info->buffer_surface);
|
||||
|
||||
return STATUS_REDRAW;
|
||||
|
||||
@@ -160,7 +145,7 @@ int processStandardEvent(GraphicsInfo *info, XEvent *ev, void (*draw)(DrawingCon
|
||||
cairo_matrix_translate(&transform, ev->xbutton.x, ev->xbutton.y);
|
||||
cairo_matrix_scale(&transform, 5.0/4.0, 5.0/4.0);
|
||||
cairo_matrix_translate(&transform, -ev->xbutton.x, -ev->xbutton.y);
|
||||
cairo_matrix_multiply(&info->context->matrix, &info->context->matrix, &transform);
|
||||
cairo_matrix_multiply(&info->dim->matrix, &info->dim->matrix, &transform);
|
||||
status = STATUS_REDRAW;
|
||||
} else if(ev->xbutton.button == 5) {
|
||||
cairo_matrix_t transform;
|
||||
@@ -168,12 +153,13 @@ int processStandardEvent(GraphicsInfo *info, XEvent *ev, void (*draw)(DrawingCon
|
||||
cairo_matrix_translate(&transform, ev->xbutton.x, ev->xbutton.y);
|
||||
cairo_matrix_scale(&transform, 4.0/5.0, 4.0/5.0);
|
||||
cairo_matrix_translate(&transform, -ev->xbutton.x, -ev->xbutton.y);
|
||||
cairo_matrix_multiply(&info->context->matrix, &info->context->matrix, &transform);
|
||||
cairo_matrix_multiply(&info->dim->matrix, &info->dim->matrix, &transform);
|
||||
status = STATUS_REDRAW;
|
||||
}
|
||||
|
||||
last_x = ev->xbutton.x;
|
||||
last_y = ev->xbutton.y;
|
||||
updateDimensions(info->dim);
|
||||
return status;
|
||||
|
||||
case MotionNotify:
|
||||
@@ -184,7 +170,7 @@ int processStandardEvent(GraphicsInfo *info, XEvent *ev, void (*draw)(DrawingCon
|
||||
cairo_matrix_t transform;
|
||||
cairo_matrix_init_identity(&transform);
|
||||
cairo_matrix_translate(&transform, dx, dy);
|
||||
cairo_matrix_multiply(&info->context->matrix, &info->context->matrix, &transform);
|
||||
cairo_matrix_multiply(&info->dim->matrix, &info->dim->matrix, &transform);
|
||||
status = STATUS_REDRAW;
|
||||
} else if(ev->xmotion.state & Button1Mask && ev->xmotion.state & ShiftMask) {
|
||||
double width = (double) cairo_xlib_surface_get_width(info->surface);
|
||||
@@ -199,7 +185,7 @@ int processStandardEvent(GraphicsInfo *info, XEvent *ev, void (*draw)(DrawingCon
|
||||
cairo_matrix_translate(&transform, width/2, height/2);
|
||||
cairo_matrix_rotate(&transform, angle);
|
||||
cairo_matrix_translate(&transform, -width/2, -height/2);
|
||||
cairo_matrix_multiply(&info->context->matrix, &info->context->matrix, &transform);
|
||||
cairo_matrix_multiply(&info->dim->matrix, &info->dim->matrix, &transform);
|
||||
status = STATUS_REDRAW;
|
||||
}
|
||||
|
||||
@@ -219,7 +205,7 @@ int processStandardEvent(GraphicsInfo *info, XEvent *ev, void (*draw)(DrawingCon
|
||||
return STATUS_NOTHING;
|
||||
}
|
||||
|
||||
int checkEvents(GraphicsInfo *info, int (*process)(GraphicsInfo*, XEvent*), void (*draw)(DrawingContext*)) // get any events from the queue and the server, process them if neccessary, quit if wanted
|
||||
int checkEvents(GraphicsInfo *info, int (*process)(GraphicsInfo*, XEvent*), void (*draw)(void *)) // get any events from the queue and the server, process them if neccessary, quit if wanted
|
||||
{
|
||||
XEvent ev;
|
||||
int x11_fd;
|
||||
@@ -264,3 +250,20 @@ int checkEvents(GraphicsInfo *info, int (*process)(GraphicsInfo*, XEvent*), void
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
// this computes center, radius and scalefactor out of ctx->matrix
|
||||
void updateDimensions(DimensionsInfo *dim)
|
||||
{
|
||||
double det = dim->matrix.xx * dim->matrix.yy - dim->matrix.xy * dim->matrix.yx;
|
||||
double cx = (double)dim->width/2;
|
||||
double cy = (double)dim->height/2;
|
||||
double r = 2*sqrt((cx*cx + cy*cy)/det); // this is not safe anymore if we have non-uniform scaling
|
||||
|
||||
// don't use cairo_device_to_user() since the matrix might not be our CTM yet
|
||||
cx -= dim->matrix.x0;
|
||||
cy -= dim->matrix.y0;
|
||||
dim->center_x = ( dim->matrix.yy * cx - dim->matrix.xy * cy) / det;
|
||||
dim->center_y = (- dim->matrix.yx * cx + dim->matrix.xx * cy) / det;
|
||||
dim->radius = r;
|
||||
dim->scalefactor = sqrt(det);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user