Cairo Example?

This forum is for general developer support questions.
Post Reply
yoodoo2
Posts: 9
Joined: Wed Aug 31, 2011 9:07 am

Cairo Example?

Post by yoodoo2 »

hi folks,

I'm really struggling to compile even the simplest program using Cairo; diffilculties knowing which includes to, er, include, which libs etc to link and in which order.

Does anyone have a very simple example, please, code and gcc options, for a very simple app such as opening a window and displaying a circle or piece of text.

many thanks

Richard
AmiHyperion
Posts: 28
Joined: Tue Aug 30, 2011 7:26 am

Re: Cairo Example?

Post by AmiHyperion »

This is a sample program that produces an output to png file.
No AmigaOS/Cairo surface is involved at the moment.
Should be a good start

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <cairo.h>

#define WIDTH 1024
#define HEIGHT 768

#define RING_SIZE 16
#define MAX_POINTS 19

static double x[MAX_POINTS], y[MAX_POINTS];
static double r, g, b;

static double dx[MAX_POINTS], dy[MAX_POINTS];
static double dr, dg, db;

static double randdouble (double a, double b)
{
    return (random () / (1. + RAND_MAX)) * (b - a) + a;
}

static void nextvalues (void)
{
    int i;

    for (i = 0; i < MAX_POINTS; i++) {
	x[i] += dx[i];
	y[i] += dy[i];
    }

    r += dr;
    g += dg;
    b += db;
}

static void init_values (void)
{
    int i;

    srandomdev ();

    for (i = 0; i < MAX_POINTS; i++) {
	x[i] = randdouble (0, WIDTH);
	y[i] = randdouble (0, HEIGHT);
	dx[i] = randdouble (-15., 15.);
	dy[i] = randdouble (-15., 15.);
    }

    r = randdouble (0., 1.);
    g = randdouble (0., 1.);
    b = randdouble (0., 1.);

    dr = randdouble (-0.1, 0.1);
    dg = randdouble (-0.1, 0.1);
    db = randdouble (-0.1, 0.1);
}

static void my_line(cairo_surface_t *surface)
{
    cairo_t *cr;
    int i;

    cr = cairo_create (surface);
    cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
    cairo_paint (cr);

    nextvalues ();

    for (i = 0; i < MAX_POINTS; i++)
	cairo_line_to (cr, x[i], y[i]);
    cairo_close_path (cr);

    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
    cairo_set_source_rgb (cr, r, g, b);
    cairo_stroke (cr);

    cairo_destroy (cr);
}

int main (int argc, char **argv)
{
    char filename[256];
    cairo_surface_t *ring[RING_SIZE];
    cairo_surface_t *output;
    cairo_t *cr;
    int i, j, num_outs;

    if (argc > 2)
	num_outs = atoi (argv[1]);
    else
	num_outs = 1;

    init_values ();

    output = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT);

    for (i = 0; i < RING_SIZE; i++) {
	ring[i] = cairo_surface_create_similar (output,
	CAIRO_CONTENT_COLOR_ALPHA,
	WIDTH, HEIGHT);
	my_line (ring[i]);
    }

    for (j = 0; j < num_outs; j++) {
	cr = cairo_create (output);
	cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
	cairo_set_source_surface (cr, ring[j % RING_SIZE], 0, 0);
	cairo_paint (cr);

	cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
	for (i = 1; i < RING_SIZE; i++) {
	    cairo_set_source_surface (cr, ring[(i + j) % RING_SIZE], 0, 0);
	    cairo_paint (cr);
	}

	cairo_destroy (cr);
	sprintf (filename, "output%d.png", j);
	cairo_surface_write_to_png (output, filename);

	my_line (ring[j % RING_SIZE]);
    }

    return 0;
}

this is an hold makefile adapted from another source. It should works. Some flags are unnecessary, so check it if you want to remove something. Remember to to create a file with the name in the makefile (mystyfy in this example).

Makefile:

Code: Select all

CC = ppc-amigaos-gcc
CXX = ppc-amigaos-c++

CFLAGS = -I/SDK/local/common/include/cairo -ggdb -Iinclude
CXXFLAGS = $(CFLAGS)
LDFLAGS = -use-dynld
LIBS = -L/SDK/local/newlib/lib -lcairo -lfreetype -lfontconfig -lpixman-1 -lpng12 -lz -lexpat -lpthread -lauto


SRC = source/main.cpp
OBJ = $(SRC:.cpp=.o)

all: Mystify install

Mystify: $(OBJ)
	$(CXX) $(LDFLAGS) -o Mystify $(OBJ) $(LIBS)
	echo Done

clean:
	rm $(OBJ) Mystify

yoodoo2
Posts: 9
Joined: Wed Aug 31, 2011 9:07 am

Re: Cairo Example?

Post by yoodoo2 »

Thanks for that. I've finally managed to actually compile successfully.

There are still unsolved issues (the "not found" warnings for libraries that exist in the expected paths) and in order to actually run the executable, I've had to create copies of libpng.so (ibpng12.so and libpng16.so.16). No idea how to fix these at the moment...
chris
Posts: 562
Joined: Sat Jun 18, 2011 11:05 am
Contact:

Re: Cairo Example?

Post by chris »

yoodoo2 wrote:Thanks for that. I've finally managed to actually compile successfully.

There are still unsolved issues (the "not found" warnings for libraries that exist in the expected paths) and in order to actually run the executable, I've had to create copies of libpng.so (ibpng12.so and libpng16.so.16). No idea how to fix these at the moment...
The libcairo SObj requires libpng.so to be libpng 1.2.
I've heard of a lot of people who have libpng.so as a link to a newer version. This does not work!
Also, don't try to link to both versions of libpng, as that won't work either!
User avatar
salass00
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 530
Joined: Sat Jun 18, 2011 3:12 pm
Location: Finland
Contact:

Re: Cairo Example?

Post by salass00 »

If you only want to be able to draw into a frame buffer and don't need any of the more advanced functions of cairo you can pretty easily build static libraries of pixman and cairo that don't have any external dependencies.

For pixman:
cd pixman-0.28.0
./configure --prefix=/SDK/local/newlib --host=ppc-amigaos --enable-static=yes --enable-shared=no --disable-vmx --enable-timers=no --enable-gtk=no --enable-libpng=no
make

For cairo:
cd cairo-1.12.8
./configure --prefix=/SDK/local/newlib --host=ppc-amigaos --enable-static=yes --enable-shared=no --enable-gtk-doc=no --enable-gtk-doc-html=no --enable-gtk-doc-pdf=no --disable-largefile --enable-gcov=no --disable-valgrind --enable-xlib=no --enable-xlib-xrender=no --enable-xcb=no --enable-xcb-shm=no --enable-png=no --enable-script=no --enable-ft=no --enable-fc=no --enable-ps=no --enable-pdf=no --enable-svg=no --enable-pthread=no --enable-gobject=no --enable-full-testing=no --enable-trace=no --enable-interpreter=no --enable-symbol-lookup=no
make

Replace "./configure" with "sh configure" and "make" with "gmake" if you are compiling natively and not cross-compiling on linux or cygwin.
AmiHyperion
Posts: 28
Joined: Tue Aug 30, 2011 7:26 am

Re: Cairo Example?

Post by AmiHyperion »

@salass00

thank you for tips.
I always wanted to try latest cairo. I'll never use SO, tho, so i don't know how to do this.
Of course this new compiled version of cairo will not be "Hardware accelerated". I remember have read somewhere HJF said that cairo is "partially" hw accelerated. Don't know in what terms.
User avatar
salass00
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 530
Joined: Sat Jun 18, 2011 3:12 pm
Location: Finland
Contact:

Re: Cairo Example?

Post by salass00 »

AmiHyperion wrote: thank you for tips.
I always wanted to try latest cairo. I'll never use SO, tho, so i don't know how to do this.
Of course this new compiled version of cairo will not be "Hardware accelerated". I remember have read somewhere HJF said that cairo is "partially" hw accelerated. Don't know in what terms.
If you follow the instructions I wrote above it will build static libraries only.
AmiHyperion
Posts: 28
Joined: Tue Aug 30, 2011 7:26 am

Re: Cairo Example?

Post by AmiHyperion »

@salass00

i tried your suggestions to compile latest cairo 1.14.2 and pixam-032.6

I have the following error compiling pixman:

pixman-utils.c: In function '_pixman_multiply_overflows_size':
pixman-utils.c:36: error: 'ULONG_MAX' undeclared (first use in this function)
pixman-utils.c:36: error: (Each undeclared identifier is reported only once
pixman-utils.c:36: error: for each function it appears in.)
gmake[2]: *** [pixman-utils.lo] Error 1
gmake[2]: Leaving directory `/OS41/Software/Development/pixman-0.32.6/pixman'
gmake[1]: *** [all-recursive] Error 1
gmake[1]: Leaving directory `/OS41/Software/Development/pixman-0.32.6'
gmake: *** [all] Error 2
Last edited by AmiHyperion on Thu Jun 04, 2015 6:24 pm, edited 1 time in total.
AmiHyperion
Posts: 28
Joined: Tue Aug 30, 2011 7:26 am

Re: Cairo Example?

Post by AmiHyperion »

And, as a conseguence, configuring cairo...

configure fail with one error: configure error: "mandatory image surface backend feature could not be enabled"

Any Hint?
Post Reply