Download code

Jump to: navigation, search

Back to Hello_World_(C,_Cairo)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

build.log

1 /tmp/litprog2770024/hello.c:17:18: fatal error: cairo.h: No such file or directory
2 compilation terminated.


Makefile

# The authors of this work have released all rights to it and placed it
# in the public domain under the Creative Commons CC0 1.0 waiver
# (http://creativecommons.org/publicdomain/zero/1.0/).
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# 
# Retrieved from: http://en.literateprograms.org/Hello_World_(C,_Cairo)?oldid=10388

CFLAGS=-Wall -pedantic -g -I/usr/X11R6/include `pkg-config --cflags cairo`
LDFLAGS=-Wall -g `pkg-config --libs cairo` -L/usr/X11R6/lib -lX11

all: hello

hello: hello.o
	cc -o hello ${LDFLAGS} hello.o

hello.o: hello.c
	cc -o hello.o ${CFLAGS} -c hello.c


hijacker
hijacker
hijacker
hijacker

hello.c

  1 /* The authors of this work have released all rights to it and placed it
  2 in the public domain under the Creative Commons CC0 1.0 waiver
  3 (http://creativecommons.org/publicdomain/zero/1.0/).
  4 
  5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 12 
 13 Retrieved from: http://en.literateprograms.org/Hello_World_(C,_Cairo)?oldid=10388
 14 */
 15 
 16 
 17 #include<cairo.h>
 18 #include<cairo-pdf.h>
 19 #include<cairo-ps.h>
 20 #include<cairo-xlib.h>
 21 #include<X11/Xlib.h>
 22 
 23 #include<stdio.h>
 24 #include<stdlib.h>
 25 #include<string.h>
 26 
 27 #define SIZEX 100
 28 #define SIZEY  50
 29 
 30 
 31 void paint(cairo_surface_t *cs)
 32 {
 33 	cairo_t *c;
 34 
 35 	c=cairo_create(cs);
 36 	cairo_rectangle(c, 0.0, 0.0, SIZEX, SIZEY);
 37 	cairo_set_source_rgb(c, 0.0, 0.0, 0.5);
 38 	cairo_fill(c);
 39 
 40 	cairo_move_to(c, 10.0, 10.0);
 41 	cairo_set_source_rgb(c, 1.0, 1.0, 0.0);
 42 	cairo_show_text(c, "Hello World!");
 43 	
 44 	cairo_show_page(c);
 45 
 46 	cairo_destroy(c);
 47 }
 48 
 49 void showxlib()
 50 {
 51 	Display *dpy;
 52 	Window rootwin;
 53 	Window win;
 54 	XEvent e;
 55 	int scr;
 56 	cairo_surface_t *cs;
 57 
 58 	if(!(dpy=XOpenDisplay(NULL))) {
 59 		fprintf(stderr, "ERROR: Could not open display\n");
 60 		exit(1);
 61 	}
 62 
 63 	scr=DefaultScreen(dpy);
 64 	rootwin=RootWindow(dpy, scr);
 65 
 66 	win=XCreateSimpleWindow(dpy, rootwin, 1, 1, SIZEX, SIZEY, 0, 
 67 			BlackPixel(dpy, scr), BlackPixel(dpy, scr));
 68 
 69 	XStoreName(dpy, win, "hello");
 70 	XSelectInput(dpy, win, ExposureMask|ButtonPressMask);
 71 	XMapWindow(dpy, win);
 72 
 73 	cs=cairo_xlib_surface_create(dpy, win, DefaultVisual(dpy, 0), SIZEX, SIZEY);
 74 
 75 	while(1) {
 76 		XNextEvent(dpy, &e);
 77 		if(e.type==Expose && e.xexpose.count<1) {
 78 			paint(cs);
 79 		} else if(e.type==ButtonPress) break;
 80 	}
 81 
 82 	cairo_surface_destroy(cs);
 83 	XCloseDisplay(dpy);
 84 }
 85 
 86 void writepng(const char *fname)
 87 {
 88 	cairo_surface_t *cs;
 89 
 90 	cs=cairo_image_surface_create(CAIRO_FORMAT_ARGB32, SIZEX, SIZEY);
 91 
 92 	paint(cs);
 93 	cairo_surface_write_to_png(cs, fname);
 94 	cairo_surface_destroy(cs);
 95 }
 96 
 97 void writepdf(const char *fname)
 98 {
 99 	cairo_surface_t *cs;
100 
101 	cs=cairo_pdf_surface_create(fname, SIZEX, SIZEY);
102 
103 	paint(cs);
104 
105 	cairo_surface_flush(cs);
106 	cairo_surface_destroy(cs);
107 }
108 
109 void writeps(const char *fname)
110 {
111 	cairo_surface_t *cs;
112 
113 	cs=cairo_ps_surface_create(fname, SIZEX, SIZEY);
114 
115 	paint(cs);
116 
117 	cairo_surface_flush(cs);
118 	cairo_surface_destroy(cs);
119 }
120 
121 int main(int argc, char *argv[])
122 {
123 	char *p;
124 
125 	if(argc>1) {
126 		if((p=strchr(argv[1], '.'))) {
127 			++p;
128 			if(!strcmp(p, "png")) writepng(argv[1]);
129 			else if(!strcmp(p, "pdf")) writepdf(argv[1]);
130 			else if(!strcmp(p, "ps")) writeps(argv[1]);
131 			else writepng(argv[1]);
132 		} else writepng(argv[1]);
133 	} else showxlib();
134 
135 	return 0;
136 }
137 
138 
139 


hijacker
hijacker
hijacker
hijacker