nsxivc7860b6
Stricter object encapsulation
+25 −31
5 files changed
| @@ -28,20 +28,22 @@ int zl_cnt; | |||
| 28 | 28 | float zoom_min; | |
| 29 | 29 | float zoom_max; | |
| 30 | 30 | ||
| 31 | − | void imlib_init(win_t *win) { | |
| 32 | − | if (!win) | |
| 33 | − | return; | |
| 34 | − | ||
| 35 | − | imlib_context_set_display(win->env.dpy); | |
| 36 | − | imlib_context_set_visual(win->env.vis); | |
| 37 | − | imlib_context_set_colormap(win->env.cmap); | |
| 38 | − | ||
| 31 | + | void img_init(img_t *img, win_t *win) { | |
| 39 | 32 | zl_cnt = sizeof(zoom_levels) / sizeof(zoom_levels[0]); | |
| 40 | 33 | zoom_min = zoom_levels[0] / 100.0; | |
| 41 | 34 | zoom_max = zoom_levels[zl_cnt - 1] / 100.0; | |
| 35 | + | ||
| 36 | + | if (img) | |
| 37 | + | img->zoom = 1.0; | |
| 38 | + | ||
| 39 | + | if (win) { | |
| 40 | + | imlib_context_set_display(win->env.dpy); | |
| 41 | + | imlib_context_set_visual(win->env.vis); | |
| 42 | + | imlib_context_set_colormap(win->env.cmap); | |
| 43 | + | } | |
| 42 | 44 | } | |
| 43 | 45 | ||
| 44 | − | void imlib_destroy() { | |
| 46 | + | void img_free(img_t* img) { | |
| 45 | 47 | if (imlib_context_get_image()) | |
| 46 | 48 | imlib_free_image(); | |
| 47 | 49 | } | |
| @@ -104,7 +106,7 @@ void img_render(img_t *img, win_t *win) { | |||
| 104 | 106 | img->re = 1; | |
| 105 | 107 | ||
| 106 | 108 | /* set zoom level to fit image into window */ | |
| 107 | − | if (img->scalemode != SCALE_ZOOM) { | |
| 109 | + | if (SCALE_MODE != SCALE_ZOOM) { | |
| 108 | 110 | zw = (float) win->w / (float) img->w; | |
| 109 | 111 | zh = (float) win->h / (float) img->h; | |
| 110 | 112 | img->zoom = MIN(zw, zh); | |
| @@ -114,7 +116,7 @@ void img_render(img_t *img, win_t *win) { | |||
| 114 | 116 | else if (img->zoom > zoom_max) | |
| 115 | 117 | img->zoom = zoom_max; | |
| 116 | 118 | ||
| 117 | − | if (img->scalemode == SCALE_DOWN && img->zoom > 1.0) | |
| 119 | + | if (SCALE_MODE == SCALE_DOWN && img->zoom > 1.0) | |
| 118 | 120 | img->zoom = 1.0; | |
| 119 | 121 | } | |
| 120 | 122 | ||
| @@ -21,15 +21,14 @@ | |||
| 21 | 21 | ||
| 22 | 22 | #include "window.h" | |
| 23 | 23 | ||
| 24 | − | typedef enum scalemode_e { | |
| 24 | + | enum scalemode { | |
| 25 | 25 | SCALE_DOWN = 0, | |
| 26 | 26 | SCALE_FIT, | |
| 27 | 27 | SCALE_ZOOM | |
| 28 | − | } scalemode_t; | |
| 28 | + | }; | |
| 29 | 29 | ||
| 30 | 30 | typedef struct img_s { | |
| 31 | 31 | float zoom; | |
| 32 | − | scalemode_t scalemode; | |
| 33 | 32 | unsigned char re; | |
| 34 | 33 | int x; | |
| 35 | 34 | int y; | |
| @@ -37,8 +36,8 @@ typedef struct img_s { | |||
| 37 | 36 | int h; | |
| 38 | 37 | } img_t; | |
| 39 | 38 | ||
| 40 | − | void imlib_init(win_t*); | |
| 41 | − | void imlib_destroy(); | |
| 39 | + | void img_init(img_t*, win_t*); | |
| 40 | + | void img_free(img_t*); | |
| 42 | 41 | ||
| 43 | 42 | int img_load(img_t*, const char*); | |
| 44 | 43 | void img_render(img_t*, win_t*); | |
| @@ -83,14 +83,8 @@ int main(int argc, char **argv) { | |||
| 83 | 83 | exit(1); | |
| 84 | 84 | } | |
| 85 | 85 | ||
| 86 | − | img.zoom = 1.0; | |
| 87 | − | img.scalemode = SCALE_MODE; | |
| 88 | − | ||
| 89 | − | win.w = WIN_WIDTH; | |
| 90 | − | win.h = WIN_HEIGHT; | |
| 91 | − | ||
| 92 | 86 | win_open(&win); | |
| 93 | − | imlib_init(&win); | |
| 87 | + | img_init(&img, &win); | |
| 94 | 88 | ||
| 95 | 89 | img_load(&img, filenames[fileidx]); | |
| 96 | 90 | img_render(&img, &win); | |
| @@ -107,7 +101,7 @@ void cleanup() { | |||
| 107 | 101 | static int in = 0; | |
| 108 | 102 | ||
| 109 | 103 | if (!in++) { | |
| 110 | − | imlib_destroy(); | |
| 104 | + | img_free(&img); | |
| 111 | 105 | win_close(&win); | |
| 112 | 106 | } | |
| 113 | 107 | } | |
| @@ -32,16 +32,14 @@ void win_open(win_t *win) { | |||
| 32 | 32 | ||
| 33 | 33 | if (!win) | |
| 34 | 34 | return; | |
| 35 | − | ||
| 36 | − | e = &win->env; | |
| 37 | 35 | ||
| 36 | + | e = &win->env; | |
| 38 | 37 | if (!(e->dpy = XOpenDisplay(NULL))) | |
| 39 | 38 | DIE("could not open display"); | |
| 40 | − | ||
| 39 | + | ||
| 41 | 40 | e->scr = DefaultScreen(e->dpy); | |
| 42 | 41 | e->scrw = DisplayWidth(e->dpy, e->scr); | |
| 43 | 42 | e->scrh = DisplayHeight(e->dpy, e->scr); | |
| 44 | − | ||
| 45 | 43 | e->vis = DefaultVisual(e->dpy, e->scr); | |
| 46 | 44 | e->cmap = DefaultColormap(e->dpy, e->scr); | |
| 47 | 45 | e->depth = DefaultDepth(e->dpy, e->scr); | |
| @@ -50,6 +48,8 @@ void win_open(win_t *win) { | |||
| 50 | 48 | &bgcol, &bgcol)) | |
| 51 | 49 | DIE("could not allocate color: %s", BG_COLOR); | |
| 52 | 50 | ||
| 51 | + | win->w = WIN_WIDTH; | |
| 52 | + | win->h = WIN_HEIGHT; | |
| 53 | 53 | if (win->w > e->scrw) | |
| 54 | 54 | win->w = e->scrw; | |
| 55 | 55 | if (win->h > e->scrh) | |
| @@ -66,10 +66,9 @@ void win_open(win_t *win) { | |||
| 66 | 66 | XSelectInput(e->dpy, win->xwin, | |
| 67 | 67 | StructureNotifyMask | KeyPressMask); | |
| 68 | 68 | ||
| 69 | − | win->pm = 0; | |
| 70 | − | ||
| 71 | 69 | gcval.foreground = bgcol.pixel; | |
| 72 | 70 | win->bgc = XCreateGC(e->dpy, win->xwin, GCForeground, &gcval); | |
| 71 | + | win->pm = 0; | |
| 73 | 72 | ||
| 74 | 73 | win_set_title(win, "sxiv"); | |
| 75 | 74 | ||
| @@ -33,8 +33,8 @@ typedef struct win_env_s { | |||
| 33 | 33 | typedef struct win_s { | |
| 34 | 34 | Window xwin; | |
| 35 | 35 | win_env_t env; | |
| 36 | − | Pixmap pm; | |
| 37 | 36 | GC bgc; | |
| 37 | + | Pixmap pm; | |
| 38 | 38 | ||
| 39 | 39 | int w; | |
| 40 | 40 | int h; | |