Stricter object encapsulation

This commit is contained in:
Bert 2011-01-21 12:57:35 +01:00
parent 629d37376d
commit c7860b690b
5 changed files with 25 additions and 31 deletions

24
image.c
View file

@ -28,20 +28,22 @@ int zl_cnt;
float zoom_min; float zoom_min;
float zoom_max; float zoom_max;
void imlib_init(win_t *win) { void img_init(img_t *img, 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);
zl_cnt = sizeof(zoom_levels) / sizeof(zoom_levels[0]); zl_cnt = sizeof(zoom_levels) / sizeof(zoom_levels[0]);
zoom_min = zoom_levels[0] / 100.0; zoom_min = zoom_levels[0] / 100.0;
zoom_max = zoom_levels[zl_cnt - 1] / 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()) if (imlib_context_get_image())
imlib_free_image(); imlib_free_image();
} }
@ -104,7 +106,7 @@ void img_render(img_t *img, win_t *win) {
img->re = 1; img->re = 1;
/* set zoom level to fit image into window */ /* 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; zw = (float) win->w / (float) img->w;
zh = (float) win->h / (float) img->h; zh = (float) win->h / (float) img->h;
img->zoom = MIN(zw, zh); img->zoom = MIN(zw, zh);
@ -114,7 +116,7 @@ void img_render(img_t *img, win_t *win) {
else if (img->zoom > zoom_max) else if (img->zoom > zoom_max)
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; img->zoom = 1.0;
} }

View file

@ -21,15 +21,14 @@
#include "window.h" #include "window.h"
typedef enum scalemode_e { enum scalemode {
SCALE_DOWN = 0, SCALE_DOWN = 0,
SCALE_FIT, SCALE_FIT,
SCALE_ZOOM SCALE_ZOOM
} scalemode_t; };
typedef struct img_s { typedef struct img_s {
float zoom; float zoom;
scalemode_t scalemode;
unsigned char re; unsigned char re;
int x; int x;
int y; int y;
@ -37,8 +36,8 @@ typedef struct img_s {
int h; int h;
} img_t; } img_t;
void imlib_init(win_t*); void img_init(img_t*, win_t*);
void imlib_destroy(); void img_free(img_t*);
int img_load(img_t*, const char*); int img_load(img_t*, const char*);
void img_render(img_t*, win_t*); void img_render(img_t*, win_t*);

10
main.c
View file

@ -83,14 +83,8 @@ int main(int argc, char **argv) {
exit(1); exit(1);
} }
img.zoom = 1.0;
img.scalemode = SCALE_MODE;
win.w = WIN_WIDTH;
win.h = WIN_HEIGHT;
win_open(&win); win_open(&win);
imlib_init(&win); img_init(&img, &win);
img_load(&img, filenames[fileidx]); img_load(&img, filenames[fileidx]);
img_render(&img, &win); img_render(&img, &win);
@ -107,7 +101,7 @@ void cleanup() {
static int in = 0; static int in = 0;
if (!in++) { if (!in++) {
imlib_destroy(); img_free(&img);
win_close(&win); win_close(&win);
} }
} }

View file

@ -32,16 +32,14 @@ void win_open(win_t *win) {
if (!win) if (!win)
return; return;
e = &win->env;
e = &win->env;
if (!(e->dpy = XOpenDisplay(NULL))) if (!(e->dpy = XOpenDisplay(NULL)))
DIE("could not open display"); DIE("could not open display");
e->scr = DefaultScreen(e->dpy); e->scr = DefaultScreen(e->dpy);
e->scrw = DisplayWidth(e->dpy, e->scr); e->scrw = DisplayWidth(e->dpy, e->scr);
e->scrh = DisplayHeight(e->dpy, e->scr); e->scrh = DisplayHeight(e->dpy, e->scr);
e->vis = DefaultVisual(e->dpy, e->scr); e->vis = DefaultVisual(e->dpy, e->scr);
e->cmap = DefaultColormap(e->dpy, e->scr); e->cmap = DefaultColormap(e->dpy, e->scr);
e->depth = DefaultDepth(e->dpy, e->scr); e->depth = DefaultDepth(e->dpy, e->scr);
@ -50,6 +48,8 @@ void win_open(win_t *win) {
&bgcol, &bgcol)) &bgcol, &bgcol))
DIE("could not allocate color: %s", BG_COLOR); DIE("could not allocate color: %s", BG_COLOR);
win->w = WIN_WIDTH;
win->h = WIN_HEIGHT;
if (win->w > e->scrw) if (win->w > e->scrw)
win->w = e->scrw; win->w = e->scrw;
if (win->h > e->scrh) if (win->h > e->scrh)
@ -66,10 +66,9 @@ void win_open(win_t *win) {
XSelectInput(e->dpy, win->xwin, XSelectInput(e->dpy, win->xwin,
StructureNotifyMask | KeyPressMask); StructureNotifyMask | KeyPressMask);
win->pm = 0;
gcval.foreground = bgcol.pixel; gcval.foreground = bgcol.pixel;
win->bgc = XCreateGC(e->dpy, win->xwin, GCForeground, &gcval); win->bgc = XCreateGC(e->dpy, win->xwin, GCForeground, &gcval);
win->pm = 0;
win_set_title(win, "sxiv"); win_set_title(win, "sxiv");

View file

@ -33,8 +33,8 @@ typedef struct win_env_s {
typedef struct win_s { typedef struct win_s {
Window xwin; Window xwin;
win_env_t env; win_env_t env;
Pixmap pm;
GC bgc; GC bgc;
Pixmap pm;
int w; int w;
int h; int h;