Handle expose events

This commit is contained in:
Bert 2011-01-20 15:39:08 +01:00
parent 822ef72657
commit 00f32120a6
3 changed files with 39 additions and 22 deletions

47
image.c
View File

@ -57,10 +57,8 @@ void img_load(img_t *img, const char *filename) {
img->h = imlib_image_get_height(); img->h = imlib_image_get_height();
} }
void img_render(img_t *img, win_t *win) { void img_display(img_t *img, win_t *win) {
float zw, zh; float zw, zh;
unsigned int sx, sy, sw, sh;
unsigned int dx, dy, dw, dh;
if (!img || !win || !imlib_context_get_image()) if (!img || !win || !imlib_context_get_image())
return; return;
@ -84,30 +82,43 @@ void img_render(img_t *img, win_t *win) {
img->x = (win->w - img->w * img->zoom) / 2; img->x = (win->w - img->w * img->zoom) / 2;
img->y = (win->h - img->h * img->zoom) / 2; img->y = (win->h - img->h * img->zoom) / 2;
if (img->x < 0) { win_clear(win);
sx = -img->x / img->zoom;
sw = (img->x + win->w) / img->zoom; img_render(img, win, 0, 0, win->w, win->h);
dx = 0; }
dw = win->w;
void img_render(img_t *img, win_t *win, int x, int y, int w, int h) {
int sx, sy, sw, sh;
int dx, dy, dw, dh;
if (!img || !win || !imlib_context_get_image())
return;
if (img->x < x) {
sx = (x - img->x) / img->zoom;
sw = MIN(w / img->zoom, img->w - sx);
dx = x;
dw = sw * img->zoom;
} else { } else {
sx = 0; sx = 0;
sw = img->w; sw = MIN((w - img->x + x) / img->zoom, img->w);
dx = img->x; dx = img->x;
dw = img->w * img->zoom; dw = sw * img->zoom;
} }
if (img->y < 0) { if (img->y < y) {
sy = -img->y / img->zoom; sy = (y - img->y) / img->zoom;
sh = (img->y + win->h) / img->zoom; sh = MIN(h / img->zoom, img->h - sy);
dy = 0; dy = y;
dh = win->h; dh = sh * img->zoom;
} else { } else {
sy = 0; sy = 0;
sh = img->h; sh = MIN((h - img->y + y) / img->zoom, img->h);
dy = img->y; dy = img->y;
dh = img->h * img->zoom; dh = sh * img->zoom;
} }
win_clear(win); if (sw < 0 || sh < 0)
return;
imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh); imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
} }

View File

@ -30,16 +30,17 @@ typedef enum scalemode_e {
typedef struct img_s { typedef struct img_s {
float zoom; float zoom;
scalemode_t scalemode; scalemode_t scalemode;
int w;
int h;
int x; int x;
int y; int y;
int w;
int h;
} img_t; } img_t;
void imlib_init(win_t*); void imlib_init(win_t*);
void imlib_destroy(); void imlib_destroy();
void img_load(img_t*, const char*); void img_load(img_t*, const char*);
void img_render(img_t*, win_t*); void img_display(img_t*, win_t*);
void img_render(img_t*, win_t*, int, int, int, int);
#endif /* IMAGE_H */ #endif /* IMAGE_H */

7
main.c
View File

@ -69,7 +69,7 @@ int main(int argc, char **argv) {
imlib_init(&win); imlib_init(&win);
img_load(&img, options->filenames[fileidx]); img_load(&img, options->filenames[fileidx]);
img_render(&img, &win); img_display(&img, &win);
run(); run();
@ -113,4 +113,9 @@ void on_configurenotify(XEvent *ev) {
} }
void on_expose(XEvent *ev) { void on_expose(XEvent *ev) {
if (!ev)
return;
img_render(&img, &win, ev->xexpose.x, ev->xexpose.y,
ev->xexpose.width, ev->xexpose.height);
} }