Krolyxon
Commit c7860b6

Branches

Refs →

nsxivc7860b6

Stricter object encapsulation

Author
Bert
Date
2011-01-21 12:57 +0100
Parent
629d373
Commit
c7860b690b687492f90202e77d575f5855f04433

+25 −31

5 files changed

  1. M image.c +13 −11
  2. M image.h +4 −5
  3. M main.c +2 −8
  4. M window.c +5 −6
  5. M window.h +1 −1
M image.c View
@@ -28,20 +28,22 @@ int zl_cnt;
2828 float zoom_min;
2929 float zoom_max;
3030
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) {
3932 zl_cnt = sizeof(zoom_levels) / sizeof(zoom_levels[0]);
4033 zoom_min = zoom_levels[0] / 100.0;
4134 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+ }
4244 }
4345
44−void imlib_destroy() {
46+void img_free(img_t* img) {
4547 if (imlib_context_get_image())
4648 imlib_free_image();
4749 }
@@ -104,7 +106,7 @@ void img_render(img_t *img, win_t *win) {
104106 img->re = 1;
105107
106108 /* set zoom level to fit image into window */
107− if (img->scalemode != SCALE_ZOOM) {
109+ if (SCALE_MODE != SCALE_ZOOM) {
108110 zw = (float) win->w / (float) img->w;
109111 zh = (float) win->h / (float) img->h;
110112 img->zoom = MIN(zw, zh);
@@ -114,7 +116,7 @@ void img_render(img_t *img, win_t *win) {
114116 else if (img->zoom > zoom_max)
115117 img->zoom = zoom_max;
116118
117− if (img->scalemode == SCALE_DOWN && img->zoom > 1.0)
119+ if (SCALE_MODE == SCALE_DOWN && img->zoom > 1.0)
118120 img->zoom = 1.0;
119121 }
120122
M image.h View
@@ -21,15 +21,14 @@
2121
2222 #include "window.h"
2323
24−typedef enum scalemode_e {
24+enum scalemode {
2525 SCALE_DOWN = 0,
2626 SCALE_FIT,
2727 SCALE_ZOOM
28−} scalemode_t;
28+};
2929
3030 typedef struct img_s {
3131 float zoom;
32− scalemode_t scalemode;
3332 unsigned char re;
3433 int x;
3534 int y;
@@ -37,8 +36,8 @@ typedef struct img_s {
3736 int h;
3837 } img_t;
3938
40−void imlib_init(win_t*);
41−void imlib_destroy();
39+void img_init(img_t*, win_t*);
40+void img_free(img_t*);
4241
4342 int img_load(img_t*, const char*);
4443 void img_render(img_t*, win_t*);
M main.c View
@@ -83,14 +83,8 @@ int main(int argc, char **argv) {
8383 exit(1);
8484 }
8585
86− img.zoom = 1.0;
87− img.scalemode = SCALE_MODE;
88−
89− win.w = WIN_WIDTH;
90− win.h = WIN_HEIGHT;
91−
9286 win_open(&win);
93− imlib_init(&win);
87+ img_init(&img, &win);
9488
9589 img_load(&img, filenames[fileidx]);
9690 img_render(&img, &win);
@@ -107,7 +101,7 @@ void cleanup() {
107101 static int in = 0;
108102
109103 if (!in++) {
110− imlib_destroy();
104+ img_free(&img);
111105 win_close(&win);
112106 }
113107 }
M window.c View
@@ -32,16 +32,14 @@ void win_open(win_t *win) {
3232
3333 if (!win)
3434 return;
35−
36− e = &win->env;
3735
36+ e = &win->env;
3837 if (!(e->dpy = XOpenDisplay(NULL)))
3938 DIE("could not open display");
40−
39+
4140 e->scr = DefaultScreen(e->dpy);
4241 e->scrw = DisplayWidth(e->dpy, e->scr);
4342 e->scrh = DisplayHeight(e->dpy, e->scr);
44−
4543 e->vis = DefaultVisual(e->dpy, e->scr);
4644 e->cmap = DefaultColormap(e->dpy, e->scr);
4745 e->depth = DefaultDepth(e->dpy, e->scr);
@@ -50,6 +48,8 @@ void win_open(win_t *win) {
5048 &bgcol, &bgcol))
5149 DIE("could not allocate color: %s", BG_COLOR);
5250
51+ win->w = WIN_WIDTH;
52+ win->h = WIN_HEIGHT;
5353 if (win->w > e->scrw)
5454 win->w = e->scrw;
5555 if (win->h > e->scrh)
@@ -66,10 +66,9 @@ void win_open(win_t *win) {
6666 XSelectInput(e->dpy, win->xwin,
6767 StructureNotifyMask | KeyPressMask);
6868
69− win->pm = 0;
70−
7169 gcval.foreground = bgcol.pixel;
7270 win->bgc = XCreateGC(e->dpy, win->xwin, GCForeground, &gcval);
71+ win->pm = 0;
7372
7473 win_set_title(win, "sxiv");
7574
M window.h View
@@ -33,8 +33,8 @@ typedef struct win_env_s {
3333 typedef struct win_s {
3434 Window xwin;
3535 win_env_t env;
36− Pixmap pm;
3736 GC bgc;
37+ Pixmap pm;
3838
3939 int w;
4040 int h;