From c7860b690b687492f90202e77d575f5855f04433 Mon Sep 17 00:00:00 2001 From: Bert Date: Fri, 21 Jan 2011 12:57:35 +0100 Subject: [PATCH] Stricter object encapsulation --- image.c | 24 +++++++++++++----------- image.h | 9 ++++----- main.c | 10 ++-------- window.c | 11 +++++------ window.h | 2 +- 5 files changed, 25 insertions(+), 31 deletions(-) diff --git a/image.c b/image.c index b6c10bf..ecd7d23 100644 --- a/image.c +++ b/image.c @@ -28,20 +28,22 @@ int zl_cnt; float zoom_min; float zoom_max; -void imlib_init(win_t *win) { - if (!win) - return; - - imlib_context_set_display(win->env.dpy); - imlib_context_set_visual(win->env.vis); - imlib_context_set_colormap(win->env.cmap); - +void img_init(img_t *img, win_t *win) { zl_cnt = sizeof(zoom_levels) / sizeof(zoom_levels[0]); zoom_min = zoom_levels[0] / 100.0; zoom_max = zoom_levels[zl_cnt - 1] / 100.0; + + if (img) + img->zoom = 1.0; + + if (win) { + imlib_context_set_display(win->env.dpy); + imlib_context_set_visual(win->env.vis); + imlib_context_set_colormap(win->env.cmap); + } } -void imlib_destroy() { +void img_free(img_t* img) { if (imlib_context_get_image()) imlib_free_image(); } @@ -104,7 +106,7 @@ void img_render(img_t *img, win_t *win) { img->re = 1; /* set zoom level to fit image into window */ - if (img->scalemode != SCALE_ZOOM) { + if (SCALE_MODE != SCALE_ZOOM) { zw = (float) win->w / (float) img->w; zh = (float) win->h / (float) img->h; img->zoom = MIN(zw, zh); @@ -114,7 +116,7 @@ void img_render(img_t *img, win_t *win) { else if (img->zoom > zoom_max) img->zoom = zoom_max; - if (img->scalemode == SCALE_DOWN && img->zoom > 1.0) + if (SCALE_MODE == SCALE_DOWN && img->zoom > 1.0) img->zoom = 1.0; } diff --git a/image.h b/image.h index 091e834..7a20bd8 100644 --- a/image.h +++ b/image.h @@ -21,15 +21,14 @@ #include "window.h" -typedef enum scalemode_e { +enum scalemode { SCALE_DOWN = 0, SCALE_FIT, SCALE_ZOOM -} scalemode_t; +}; typedef struct img_s { float zoom; - scalemode_t scalemode; unsigned char re; int x; int y; @@ -37,8 +36,8 @@ typedef struct img_s { int h; } img_t; -void imlib_init(win_t*); -void imlib_destroy(); +void img_init(img_t*, win_t*); +void img_free(img_t*); int img_load(img_t*, const char*); void img_render(img_t*, win_t*); diff --git a/main.c b/main.c index e9a2970..bba5fc6 100644 --- a/main.c +++ b/main.c @@ -83,14 +83,8 @@ int main(int argc, char **argv) { exit(1); } - img.zoom = 1.0; - img.scalemode = SCALE_MODE; - - win.w = WIN_WIDTH; - win.h = WIN_HEIGHT; - win_open(&win); - imlib_init(&win); + img_init(&img, &win); img_load(&img, filenames[fileidx]); img_render(&img, &win); @@ -107,7 +101,7 @@ void cleanup() { static int in = 0; if (!in++) { - imlib_destroy(); + img_free(&img); win_close(&win); } } diff --git a/window.c b/window.c index aa39406..fc6a264 100644 --- a/window.c +++ b/window.c @@ -32,16 +32,14 @@ void win_open(win_t *win) { if (!win) return; - - e = &win->env; + e = &win->env; if (!(e->dpy = XOpenDisplay(NULL))) DIE("could not open display"); - + e->scr = DefaultScreen(e->dpy); e->scrw = DisplayWidth(e->dpy, e->scr); e->scrh = DisplayHeight(e->dpy, e->scr); - e->vis = DefaultVisual(e->dpy, e->scr); e->cmap = DefaultColormap(e->dpy, e->scr); e->depth = DefaultDepth(e->dpy, e->scr); @@ -50,6 +48,8 @@ void win_open(win_t *win) { &bgcol, &bgcol)) DIE("could not allocate color: %s", BG_COLOR); + win->w = WIN_WIDTH; + win->h = WIN_HEIGHT; if (win->w > e->scrw) win->w = e->scrw; if (win->h > e->scrh) @@ -66,10 +66,9 @@ void win_open(win_t *win) { XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask); - win->pm = 0; - gcval.foreground = bgcol.pixel; win->bgc = XCreateGC(e->dpy, win->xwin, GCForeground, &gcval); + win->pm = 0; win_set_title(win, "sxiv"); diff --git a/window.h b/window.h index 8728bb9..38d8091 100644 --- a/window.h +++ b/window.h @@ -33,8 +33,8 @@ typedef struct win_env_s { typedef struct win_s { Window xwin; win_env_t env; - Pixmap pm; GC bgc; + Pixmap pm; int w; int h;