From 58cbbdb8a47f87a5d23fa5a8b1a34a021dccc0b1 Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Tue, 30 Sep 2025 01:14:30 +0000 Subject: [PATCH] pixmap works git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@70 b9cfdab3-6d41-4d17-bbe4-086880011989 --- examples/example.c | 2 -- src/frame.c | 20 +--------------- src/x11.c | 59 ++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 50 insertions(+), 31 deletions(-) diff --git a/examples/example.c b/examples/example.c index adb1dcb..f17ebfa 100644 --- a/examples/example.c +++ b/examples/example.c @@ -64,8 +64,6 @@ int main() { MwNbackground, "#66f", NULL); - MwVaCreateWidget(MwFrameClass, "frame", window, 50, 50, 300, 300, NULL); - MwAddUserHandler(window, MwNresizeHandler, resize, NULL); MwAddUserHandler(button, MwNactivateHandler, handler, NULL); MwAddUserHandler(button2, MwNactivateHandler, handler, NULL); diff --git a/src/frame.c b/src/frame.c index e69863d..0fc1889 100644 --- a/src/frame.c +++ b/src/frame.c @@ -1,25 +1,8 @@ /* $Id$ */ #include -MwLLPixmap pixmap; - static void create(MwWidget handle) { MwSetDefault(handle); - - int y, x; - unsigned char* dat = malloc(640 * 480 * 3); - for(y = 0; y < 480; y++) { - for(x = 0; x < 640; x++) { - unsigned char* px = &dat[(y * 640 + x) * 3]; - double c = (double)x / 640 * 255 / 2 + (double)y / 480 * 255 / 2; - - px[0] = c; - px[1] = 0; - px[2] = 0; - } - } - pixmap = MwLLCreatePixmap(handle->lowlevel, dat, 640, 480); - free(dat); } static void draw(MwWidget handle) { @@ -33,8 +16,7 @@ static void draw(MwWidget handle) { MwDrawFrameEx(handle, &r, base, 1, 1); MwDrawFrameEx(handle, &r, base, 0, 1); - MwLLDrawPixmap(handle->lowlevel, &r, pixmap); - // MwDrawRect(handle, &r, base); + MwDrawRect(handle, &r, base); MwLLFreeColor(base); } diff --git a/src/x11.c b/src/x11.c index 7f24e9d..d8ba111 100644 --- a/src/x11.c +++ b/src/x11.c @@ -144,12 +144,19 @@ MwLLPixmap MwLLCreatePixmap(MwLL handle, unsigned char* data, int width, int hei MwLLPixmap r = malloc(sizeof(*r)); char* d = malloc(4 * width * height); int y, x; + int evbase, erbase; r->width = width; r->height = height; r->display = handle->display; r->use_shm = XShmQueryExtension(handle->display) ? 1 : 0; + if(!XRenderQueryExtension(handle->display, &evbase, &erbase)) { + fprintf(stderr, "XRender missing - cannot proceed pixmap creation\n"); + r->image = NULL; + return r; + } + if(r->use_shm) { r->image = XShmCreateImage(handle->display, DefaultVisual(handle->display, DefaultScreen(handle->display)), 24, ZPixmap, NULL, &r->shm, width, height); free(d); @@ -163,7 +170,7 @@ MwLLPixmap MwLLCreatePixmap(MwLL handle, unsigned char* data, int width, int hei } for(y = 0; y < height; y++) { - for(x = 0; x < height; x++) { + for(x = 0; x < width; x++) { unsigned char* px = &data[(y * width + x) * 3]; unsigned long p = 0; p <<= 8; @@ -180,13 +187,15 @@ MwLLPixmap MwLLCreatePixmap(MwLL handle, unsigned char* data, int width, int hei } void MwLLDestroyPixmap(MwLLPixmap pixmap) { - if(pixmap->use_shm) { - XShmDetach(pixmap->display, &pixmap->shm); - } - XDestroyImage(pixmap->image); - if(pixmap->use_shm) { - shmdt(pixmap->shm.shmaddr); - shmctl(pixmap->shm.shmid, IPC_RMID, 0); + if(pixmap->image != NULL) { + if(pixmap->use_shm) { + XShmDetach(pixmap->display, &pixmap->shm); + } + XDestroyImage(pixmap->image); + if(pixmap->use_shm) { + shmdt(pixmap->shm.shmaddr); + shmctl(pixmap->shm.shmid, IPC_RMID, 0); + } } free(pixmap); @@ -194,10 +203,40 @@ void MwLLDestroyPixmap(MwLLPixmap pixmap) { void MwLLDrawPixmap(MwLL handle, MwRect* rect, MwLLPixmap pixmap) { if(pixmap->image != NULL) { + Pixmap px = XCreatePixmap(handle->display, handle->window, pixmap->width, pixmap->height, 24); + XRenderPictFormat* format = XRenderFindStandardFormat(handle->display, PictStandardRGB24); + XRenderPictureAttributes attr; + Picture src, dest; + XTransform m; + double xsc = (double)pixmap->width / rect->width; + double ysc = (double)pixmap->height / rect->height; + + m.matrix[0][0] = XDoubleToFixed(xsc); + m.matrix[0][1] = XDoubleToFixed(0); + m.matrix[0][2] = XDoubleToFixed(0); + + m.matrix[1][0] = XDoubleToFixed(0); + m.matrix[1][1] = XDoubleToFixed(ysc); + m.matrix[1][2] = XDoubleToFixed(0); + + m.matrix[2][0] = XDoubleToFixed(0); + m.matrix[2][1] = XDoubleToFixed(0); + m.matrix[2][2] = XDoubleToFixed(1.0); + + memset(&attr, 0, sizeof(attr)); + if(pixmap->use_shm) { - XShmPutImage(handle->display, handle->window, handle->gc, pixmap->image, 0, 0, rect->x, rect->y, rect->width, rect->height, False); + XShmPutImage(handle->display, px, handle->gc, pixmap->image, 0, 0, 0, 0, pixmap->width, pixmap->height, False); } else { - XPutImage(handle->display, handle->window, handle->gc, pixmap->image, 0, 0, rect->x, rect->y, rect->width, rect->height); + XPutImage(handle->display, px, handle->gc, pixmap->image, 0, 0, 0, 0, pixmap->width, pixmap->height); } + + src = XRenderCreatePicture(handle->display, px, format, 0, &attr); + dest = XRenderCreatePicture(handle->display, handle->window, format, 0, &attr); + + XRenderSetPictureTransform(handle->display, src, &m); + XRenderComposite(handle->display, PictOpSrc, src, 0, dest, 0, 0, 0, 0, rect->x, rect->y, rect->width, rect->height); + + XFreePixmap(handle->display, px); } }