mirror of
https://gitlab.freedesktop.org/cairo/cairo.git
synced 2026-02-03 14:50:30 +01:00
Move the magic header files to be down in an include directory and more out of the way.
Look for the headers in -I./include. Make the circle track the width and height of the window dynamically. Remove boring expander example. Add more interesting LCA exmaple. Add some notes about how to use this stuff.
This commit is contained in:
parent
9abe21ac70
commit
dc64fcc68c
11 changed files with 178 additions and 30 deletions
28
ChangeLog
28
ChangeLog
|
|
@ -1,3 +1,31 @@
|
|||
2006-01-26 Carl Worth <cworth@cworth.org>
|
||||
|
||||
* doc/tutorial/src/cairo-tutorial.h:
|
||||
* doc/tutorial/src/cairo-tutorial-gtk.h:
|
||||
* doc/tutorial/src/cairo-tutorial-pdf.h:
|
||||
* doc/tutorial/src/cairo-tutorial-png.h:
|
||||
* doc/tutorial/src/cairo-tutorial-xlib.h:
|
||||
* doc/tutorial/src/include/cairo-tutorial.h:
|
||||
* doc/tutorial/src/include/cairo-tutorial-gtk.h:
|
||||
* doc/tutorial/src/include/cairo-tutorial-pdf.h:
|
||||
* doc/tutorial/src/include/cairo-tutorial-png.h:
|
||||
* doc/tutorial/src/include/cairo-tutorial-xlib.h:
|
||||
Move the magic header files to be down in an include directory and
|
||||
more out of the way.
|
||||
|
||||
* doc/tutorial/src/Makefile: Look for the headers in -I./include.
|
||||
|
||||
* doc/tutorial/src/circle.c: (draw): Make the circle track the
|
||||
width and height of the window dynamically.
|
||||
|
||||
* doc/tutorial/src/expander.c: Remove boring expander example.
|
||||
|
||||
* doc/tutorial/src/lca.c: (draw): Add more interesting LCA
|
||||
exmaple.
|
||||
|
||||
* doc/tutorial/src/README: Add some notes about how to use this
|
||||
stuff.
|
||||
|
||||
2006-01-26 Carl Worth <cworth@cworth.org>
|
||||
|
||||
* doc/tutorial/slides/cairo-large-content.svg: Here's the actual
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
MYCFLAGS=$(shell pkg-config --cflags --libs cairo gtk+-2.0) -Wall -g -Wpointer-arith -Wmissing-prototypes -Wmissing-declarations -Wnested-externs -fno-strict-aliasing
|
||||
MYCFLAGS=-I./include $(shell pkg-config --cflags --libs cairo gtk+-2.0) -Wall -g -Wpointer-arith -Wmissing-prototypes -Wmissing-declarations -Wnested-externs -fno-strict-aliasing
|
||||
|
||||
# If you don't want to/can't compile all of these targets, then trim
|
||||
# this list.
|
||||
|
|
@ -7,25 +7,25 @@ all: gtk xlib png
|
|||
|
||||
GTK_EXAMPLES=$(patsubst %.c,%-gtk,$(wildcard *.c))
|
||||
gtk: $(GTK_EXAMPLES)
|
||||
%-gtk:%.c cairo-tutorial-gtk.h
|
||||
%-gtk:%.c ./include/cairo-tutorial-gtk.h
|
||||
$(CC) -DCAIRO_TUTORIAL_GTK $(CFLAGS) $(MYCFLAGS) -o $@ $<
|
||||
|
||||
XLIB_EXAMPLES=$(patsubst %.c,%-xlib,$(wildcard *.c))
|
||||
xlib: $(XLIB_EXAMPLES)
|
||||
%-xlib:%.c cairo-tutorial-xlib.h
|
||||
%-xlib:%.c ./include/cairo-tutorial-xlib.h
|
||||
$(CC) -DCAIRO_TUTORIAL_XLIB $(CFLAGS) $(MYCFLAGS) -o $@ $<
|
||||
|
||||
PDF_EXAMPLES=$(patsubst %.c,%-pdf,$(wildcard *.c))
|
||||
pdf: $(PDF_EXAMPLES)
|
||||
%-pdf:%.c cairo-tutorial-pdf.h
|
||||
%-pdf:%.c ./include/cairo-tutorial-pdf.h
|
||||
$(CC) -DCAIRO_TUTORIAL_PDF $(CFLAGS) $(MYCFLAGS) -o $@ $<
|
||||
|
||||
PNG_EXAMPLES=$(patsubst %.c,%-png,$(wildcard *.c))
|
||||
png: $(PNG_EXAMPLES)
|
||||
%-png:%.c cairo-tutorial-png.h
|
||||
%-png:%.c ./include/cairo-tutorial-png.h
|
||||
$(CC) -DCAIRO_TUTORIAL_PNG $(CFLAGS) $(MYCFLAGS) -o $@ $<
|
||||
|
||||
clean:
|
||||
rm -f $(GTK_EXAMPLES) $(XLIB_EXAMPLES) $(PDF_EXAMPLES) $(PNG_EXAMPLES)
|
||||
rm -f $(GTK_EXAMPLES) $(XLIB_EXAMPLES) $(PDF_EXAMPLES) $(PNG_EXAMPLES) *.png
|
||||
|
||||
.PHONY: all gtk xlib pdf png clean
|
||||
|
|
|
|||
66
doc/tutorial/src/README
Normal file
66
doc/tutorial/src/README
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
Welcome to the cairo tutorial:
|
||||
|
||||
+--------------------------------+
|
||||
| How to Recognize Ugly Graphics |
|
||||
|(and what you can do about them)|
|
||||
+--------------------------------+
|
||||
|
||||
This directory is your personal playground for following along with
|
||||
the examples. In order for you to make use of these files you will
|
||||
need to have cairo and its header files installed. You can find
|
||||
instructions for doing this at:
|
||||
|
||||
http://cairographics.org/tutorial
|
||||
|
||||
Notice that there are a few .c files in this directory.
|
||||
|
||||
You should start out by just typing "make" which will turn each .c
|
||||
file into several different programs. Go ahead and run each of the
|
||||
programs and see what they do. Some of them will open up new X windows
|
||||
while others will simply write their output to files (such .png or
|
||||
.pdf).
|
||||
|
||||
After you play with those a bit, go ahead and take a look at the
|
||||
contents of the .c files. You'll see that each file contains a draw()
|
||||
function that does all of the drawing.
|
||||
|
||||
You might be surprised to notice that there is no main() function in
|
||||
any of the files. Instead, main is hidden away by means of
|
||||
cairo-tutorial.h. This rather non-conventional style is used to allow
|
||||
you to focus on the actual drawing code involved in using cairo, while
|
||||
not having to worry about the setup semantics. We don't recommend that
|
||||
you follow this style for real projects.
|
||||
|
||||
As you follow along during the tutorial and get some ideas for things
|
||||
to draw, you'll want to start making your own .c files. You can copy
|
||||
an existing file or make your own by following this simple minimal
|
||||
template:
|
||||
|
||||
#include "cairo-tutorial.h"
|
||||
|
||||
static void
|
||||
draw (cairo_t *cr, int width, int height)
|
||||
{
|
||||
/* Put your drawing code here. */
|
||||
}
|
||||
|
||||
Any new file you create will automatically get picked up by the
|
||||
Makefile so that "make" will compile your file into several different
|
||||
programs, just like the existing examples.
|
||||
|
||||
If you'd like to control the initial size of the output, you may
|
||||
define WIDTH and HEIGHT before including cairo-tutorial.h like so:
|
||||
|
||||
#define WIDTH 100
|
||||
#define HEIGHT 100
|
||||
|
||||
#include "cairo-tutorial.h"
|
||||
|
||||
If you would like to change the set of cairo-backend target programs
|
||||
that are compiled, you may edit the "all" target in the Makefile.
|
||||
|
||||
Have fun!
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -3,17 +3,20 @@
|
|||
static void
|
||||
draw (cairo_t *cr, int width, int height)
|
||||
{
|
||||
cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
|
||||
cairo_paint (cr);
|
||||
int radius;
|
||||
|
||||
cairo_move_to (cr, 47.5, 25);
|
||||
cairo_arc (cr, 25, 25, 22.5,
|
||||
if (width < height)
|
||||
radius = width/2 - 4;
|
||||
else
|
||||
radius = height/2 - 4;
|
||||
|
||||
cairo_move_to (cr, width/2 + radius, height/2);
|
||||
cairo_arc (cr, width/2, height/2, radius,
|
||||
0.0, 2 * M_PI);
|
||||
|
||||
cairo_set_source_rgb (cr, 0.6, 0.8, 1.0);
|
||||
cairo_fill_preserve (cr);
|
||||
|
||||
cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
|
||||
cairo_set_line_width (cr, 1.0);
|
||||
cairo_stroke (cr);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +0,0 @@
|
|||
#include "cairo-tutorial.h"
|
||||
|
||||
static void
|
||||
draw (cairo_t *cr, int width, int height)
|
||||
{
|
||||
cairo_translate (cr, 24.5, 25);
|
||||
cairo_move_to (cr, 5, -2.5);
|
||||
cairo_line_to (cr, 0, 2.5);
|
||||
cairo_line_to (cr, -5, -2.5);
|
||||
cairo_close_path (cr);
|
||||
cairo_set_line_width (cr, 1);
|
||||
cairo_stroke (cr);
|
||||
}
|
||||
|
|
@ -54,17 +54,20 @@ Colormap colormap;
|
|||
Visual *visual;
|
||||
int depth;
|
||||
unsigned int width = WIDTH, height = HEIGHT;
|
||||
Window win;
|
||||
Pixmap pix;
|
||||
GC gc;
|
||||
|
||||
static void
|
||||
draw (cairo_t *cr, int width, int height);
|
||||
|
||||
static void
|
||||
handle_expose (Display *dpy, Drawable d)
|
||||
draw_to_pixmap (Display *dpy, Pixmap pix)
|
||||
{
|
||||
cairo_surface_t *surface;
|
||||
cairo_t *cr;
|
||||
|
||||
surface = cairo_xlib_surface_create (dpy, d, visual,
|
||||
surface = cairo_xlib_surface_create (dpy, pix, visual,
|
||||
width, height);
|
||||
cr = cairo_create (surface);
|
||||
|
||||
|
|
@ -74,13 +77,33 @@ handle_expose (Display *dpy, Drawable d)
|
|||
cairo_surface_destroy (surface);
|
||||
}
|
||||
|
||||
static void
|
||||
handle_configure (Display *dpy, XConfigureEvent *cev)
|
||||
{
|
||||
width = cev->width;
|
||||
height = cev->height;
|
||||
|
||||
XFreePixmap(dpy, pix);
|
||||
pix = XCreatePixmap(dpy, win, width, height, depth);
|
||||
XFillRectangle(dpy, pix, gc, 0, 0, width, height);
|
||||
draw_to_pixmap (dpy, pix);
|
||||
}
|
||||
|
||||
static void
|
||||
handle_expose (Display *dpy, XExposeEvent *eev)
|
||||
{
|
||||
XCopyArea (dpy, pix, win, gc,
|
||||
eev->x, eev->y,
|
||||
eev->width, eev->height,
|
||||
eev->x, eev->y);
|
||||
}
|
||||
|
||||
int
|
||||
main (argc, argv)
|
||||
int argc;
|
||||
char **argv;
|
||||
{
|
||||
Display *dpy;
|
||||
Window win;
|
||||
Window root = 0;
|
||||
char **init_argv = argv;
|
||||
XSetWindowAttributes attr;
|
||||
|
|
@ -98,6 +121,7 @@ main (argc, argv)
|
|||
XTextProperty wm_name, icon_name;
|
||||
Atom wm_delete_window;
|
||||
unsigned long gc_mask;
|
||||
XGCValues gcv;
|
||||
char quit_string[10];
|
||||
unsigned long window_mask;
|
||||
int has_colormap = 0;
|
||||
|
|
@ -173,13 +197,18 @@ main (argc, argv)
|
|||
}
|
||||
attr.background_pixel = WhitePixel (dpy, scr);
|
||||
attr.border_pixel = 0;
|
||||
attr.event_mask = ExposureMask|KeyPressMask|KeyReleaseMask;
|
||||
attr.event_mask = ExposureMask|KeyPressMask|KeyReleaseMask|StructureNotifyMask;
|
||||
wm_delete_window = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
|
||||
win = XCreateWindow (dpy, root, x, y, width, height, border_width,
|
||||
depth, InputOutput,
|
||||
visual,
|
||||
window_mask,
|
||||
&attr);
|
||||
pix = XCreatePixmap (dpy, win, width, height, depth);
|
||||
gcv.foreground = WhitePixel (dpy, scr);
|
||||
gc = XCreateGC (dpy, pix, GCForeground, &gcv);
|
||||
XFillRectangle(dpy, pix, gc, 0, 0, width, height);
|
||||
draw_to_pixmap (dpy, pix);
|
||||
XSetWMProperties (dpy, win,
|
||||
&wm_name, &icon_name,
|
||||
init_argv, argc,
|
||||
|
|
@ -190,15 +219,18 @@ main (argc, argv)
|
|||
XNextEvent (dpy, &ev);
|
||||
if (HasExpose && ev.type != Expose) {
|
||||
HasExpose = 0;
|
||||
handle_expose (dpy, eev.xexpose.window);
|
||||
handle_expose (dpy, &eev.xexpose);
|
||||
}
|
||||
switch (ev.type) {
|
||||
case ConfigureNotify:
|
||||
handle_configure (dpy, &ev.xconfigure);
|
||||
break;
|
||||
case Expose:
|
||||
if (QLength(dpy)) {
|
||||
eev = ev;
|
||||
HasExpose = 1;
|
||||
} else if (ev.xexpose.count == 0) {
|
||||
handle_expose (dpy, ev.xexpose.window);
|
||||
handle_expose (dpy, &ev.xexpose);
|
||||
}
|
||||
break;
|
||||
case KeyPress:
|
||||
32
doc/tutorial/src/lca.c
Normal file
32
doc/tutorial/src/lca.c
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#define WIDTH 750
|
||||
#define HEIGHT 360
|
||||
|
||||
#include "cairo-tutorial.h"
|
||||
|
||||
static void
|
||||
draw (cairo_t *cr, int width, int height)
|
||||
{
|
||||
cairo_save (cr);
|
||||
|
||||
cairo_translate (cr, 60, 60);
|
||||
cairo_scale (cr, 3, 3);
|
||||
|
||||
cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
|
||||
cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
|
||||
cairo_set_line_width (cr, 20);
|
||||
|
||||
/* L */
|
||||
cairo_move_to (cr, 0, 0);
|
||||
cairo_line_to (cr, 0, 80);
|
||||
cairo_line_to (cr, 50, 80);
|
||||
|
||||
/* C */
|
||||
cairo_move_to (cr, 110 + 40 * cos (M_PI / 3), 40 + 40 * sin(M_PI / 3));
|
||||
cairo_arc (cr, 110, 40, 40, M_PI / 3, -M_PI / 3);
|
||||
|
||||
/* A */
|
||||
cairo_move_to (cr, 160, 80);
|
||||
cairo_curve_to (cr, 160, -30, 210, -30, 210, 80);
|
||||
|
||||
cairo_stroke (cr);
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue