Rename GP_Context -> GP_Pixmap
[gfxprim.git] / demos / c_simple / zip_container.c
blob4cf77fd9aef1d96eecb4c299888ff68d69ff99e9
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2013 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
25 Simple example that loads and show image from zip container into X11 window.
29 #include <stdio.h>
30 #include <errno.h>
31 #include <string.h>
33 #include <GP.h>
35 static GP_Backend *backend;
36 static GP_Pixmap *image;
37 static GP_Container *container;
40 * Try to load next image in container, if image has different size than the
41 * window request resize and blit it at the resize event in the main loop.
43 * Note that the resize event needs not to be granted so we really should have
44 * plan b (which is omited here for the sake of simplicity).
46 static void load_next(void)
48 GP_PixmapFree(image);
50 image = GP_ContainerLoadNext(container, NULL);
52 if (image == NULL)
53 return;
55 if (image->w != backend->pixmap->w ||
56 image->h != backend->pixmap->h) {
57 GP_BackendResize(backend, image->w, image->h);
58 return;
61 GP_Blit_Clipped(image, 0, 0, image->w, image->h, backend->pixmap, 0, 0);
62 GP_BackendFlip(backend);
65 int main(int argc, char *argv[])
68 if (argc != 2) {
69 fprintf(stderr, "Takes path to zip or cbz as an argument\n");
70 return 1;
73 /* Open zip container */
74 container = GP_OpenZip(argv[1]);
76 if (container == NULL) {
77 fprintf(stderr, "Failed to open container: %s\n", strerror(errno));
78 return 1;
81 /* Load image */
82 image = GP_ContainerLoadNext(container, NULL);
84 if (image == NULL) {
85 fprintf(stderr, "Failed to load image %s\n", strerror(errno));
86 return 1;
89 /* Initalize backend */
90 backend = GP_BackendX11Init(NULL, 0, 0, image->w, image->h, argv[1], 0);
92 if (backend == NULL) {
93 fprintf(stderr, "Failed to initalize backend\n");
94 return 1;
97 /* Blit image into the window and show it */
98 GP_Blit_Clipped(image, 0, 0, image->w, image->h, backend->pixmap, 0, 0);
99 GP_BackendFlip(backend);
101 /* Wait for events */
102 for (;;) {
103 GP_Event ev;
105 GP_BackendWaitEvent(backend, &ev);
107 switch (ev.type) {
108 case GP_EV_KEY:
109 if (!ev.code == GP_EV_KEY_DOWN)
110 continue;
112 switch (ev.val.val) {
113 case GP_KEY_Q:
114 GP_BackendExit(backend);
115 return 0;
116 break;
117 case GP_KEY_SPACE:
118 load_next();
119 break;
121 break;
122 case GP_EV_SYS:
123 if (ev.code == GP_EV_SYS_RESIZE) {
124 GP_BackendResizeAck(backend);
125 GP_Blit_Clipped(image, 0, 0, image->w, image->h,
126 backend->pixmap, 0, 0);
127 GP_BackendFlip(backend);
129 break;
133 return 0;