static gboolean
path_test_convert_to_cairo_path (CallbackData *data)
{
cairo_surface_t *surface;
cairo_t *cr;
cairo_path_t *cpath;
guint i, j;
ClutterKnot path_start = { 0, 0 }, last_point = { 0, 0 };
/* Create a temporary image surface and context to hold the cairo
path */
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 10, 10);
cr = cairo_create (surface);
/* Convert to a cairo path */
clutter_path_to_cairo_path (data->path, cr);
/* Get a copy of the cairo path data */
cpath = cairo_copy_path (cr);
/* Convert back to a clutter path */
clutter_path_clear (data->path);
clutter_path_add_cairo_path (data->path, cpath);
/* The relative nodes will have been converted to absolute so we
need to reflect this in the node array for comparison */
for (i = 0; i < data->n_nodes; i++)
{
switch (data->nodes[i].type)
{
case CLUTTER_PATH_MOVE_TO:
path_start = last_point = data->nodes[i].points[0];
break;
case CLUTTER_PATH_LINE_TO:
last_point = data->nodes[i].points[0];
break;
case CLUTTER_PATH_CURVE_TO:
last_point = data->nodes[i].points[2];
break;
case CLUTTER_PATH_REL_MOVE_TO:
last_point.x += data->nodes[i].points[0].x;
last_point.y += data->nodes[i].points[0].y;
data->nodes[i].points[0] = last_point;
data->nodes[i].type = CLUTTER_PATH_MOVE_TO;
path_start = last_point;
break;
case CLUTTER_PATH_REL_LINE_TO:
last_point.x += data->nodes[i].points[0].x;
last_point.y += data->nodes[i].points[0].y;
data->nodes[i].points[0] = last_point;
data->nodes[i].type = CLUTTER_PATH_LINE_TO;
break;
case CLUTTER_PATH_REL_CURVE_TO:
for (j = 0; j < 3; j++)
{
data->nodes[i].points[j].x += last_point.x;
data->nodes[i].points[j].y += last_point.y;
}
last_point = data->nodes[i].points[2];
data->nodes[i].type = CLUTTER_PATH_CURVE_TO;
break;
case CLUTTER_PATH_CLOSE:
last_point = path_start;
/* Cairo always adds a move to after every close so we need
to insert one here */
memmove (data->nodes + i + 2, data->nodes + i + 1,
(data->n_nodes - i - 1) * sizeof (ClutterPathNode));
data->nodes[i + 1].type = CLUTTER_PATH_MOVE_TO;
data->nodes[i + 1].points[0] = last_point;
data->n_nodes++;
break;
}
}
/* Free the cairo resources */
cairo_path_destroy (cpath);
cairo_destroy (cr);
cairo_surface_destroy (surface);
return TRUE;
}
int main(int argc, char **argv)
{
cairo_surface_t *surface;
cairo_t *context;
int x, y, i,
width, height, depth,
screen, pressed_key,
png;
double r, b, v;
QTnode *tree;
bool rendering;
struct XWin **xwin;
/* Get the arguments */
if(argc != 3) {
printf("Usage: nbody rendering resultsdir\n");
exit(1);
}
if(*argv[1] == '0') {
rendering = false;
} else {
rendering = true;
}
char buf[strlen(argv[2]) + 10];
/* Set window size */
width = 1024;
height = 1024;
depth = 32;
tree = init_tree(10, NULL);
for (i = 0; i < 100000; i++) {
tree->insert(phys_gen_particle());
if (i % 10000 == 0)
printf("%d\n", i);
}
/* Create the drawing surface */
if(rendering) {
/* Create the X11 window */
xwin = (struct XWin **)calloc(sizeof(struct XWin *), 1);
xwindow_init(width, height, depth, xwin);
surface = cairo_xlib_surface_create((*xwin)->dsp, (*xwin)->win, DefaultVisual((*xwin)->dsp, screen), width, height);
cairo_xlib_surface_set_size(surface, width, height);
} else {
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
}
context = cairo_create(surface);
cairo_scale(context, width, height);
png = 0;
while(1) {
if(rendering) {
if((*xwin)->should_close) {
break;
}
/* Wait on the input (also sync up to disable flickering) */
if(input_ready(xwin)) {
pressed_key = get_key(xwin);
}
}
/* Clear the surface with black */
cairo_set_source_rgb(context, 0.0, 0.0, 0.0);
cairo_paint(context);
std::queue <QTnode *> nodes;
QTnode *t;
nodes.push(tree);
while (!nodes.empty()) {
t = nodes.front();
nodes.pop();
if (!t->children.empty()) {
for (i = 0; i < t->children.size(); i++) {
nodes.push(t->children[i]);
}
} else {
for (std::list <Particle>::iterator p = t->particles.begin(); p != t->particles.end(); p++) {
v = f2_norm((*p).vel);
if(v >= 0.4) {
r = 1.0; b = 0.0;
} else if(v < 0.5) {
b = 1.0; r = 0.0;
}
cairo_set_source_rgba(context, (double)r, 0.0, (double)b, 1.0);
cairo_rectangle(context, (*p).pos.x,
(*p).pos.y, 2e-3, 2e-3);
cairo_fill(context);
cairo_set_source_rgba(context, (double)r, 0.0, (double)b, 0.2);
cairo_rectangle(context, (*p).pos.x - 1e-3,
(*p).pos.y - 1e-3, 4e-3, 4e-3);
cairo_fill(context);
}
}
}
//.........这里部分代码省略.........
开发者ID:BenBrock,项目名称:nbods,代码行数:101,代码来源:nbody.cpp
示例6: if
wxRegion::wxRegion( size_t n, const wxPoint *points,
wxPolygonFillMode fillStyle )
{
#ifdef __WXGTK3__
// Make a cairo path from the points, draw it onto an image surface, use
// gdk_cairo_region_create_from_surface() to get a cairo region
// need at least 3 points to make a useful polygon
if (n < 3)
return;
// get bounding rect
int min_x = points[0].x;
int max_x = min_x;
int min_y = points[0].y;
int max_y = min_y;
size_t i;
for (i = 1; i < n; i++)
{
const int x = points[i].x;
if (min_x > x)
min_x = x;
else if (max_x < x)
max_x = x;
const int y = points[i].y;
if (min_y > y)
min_y = y;
else if (max_y < y)
max_y = y;
}
const int w = max_x - min_x + 1;
const int h = max_y - min_y + 1;
// make surface just big enough to contain polygon (A1 is native format
// for gdk_cairo_region_create_from_surface)
cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_A1, w, h);
memset(cairo_image_surface_get_data(surface), 0, cairo_image_surface_get_stride(surface) * h);
cairo_surface_mark_dirty(surface);
cairo_surface_set_device_offset(surface, -min_x, -min_y);
cairo_t* cr = cairo_create(surface);
cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
if (fillStyle == wxODDEVEN_RULE)
cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
// make path
cairo_move_to(cr, points[0].x, points[0].y);
for (i = 1; i < n; i++)
cairo_line_to(cr, points[i].x, points[i].y);
cairo_close_path(cr);
cairo_fill(cr);
cairo_destroy(cr);
cairo_surface_flush(surface);
m_refData = new wxRegionRefData;
M_REGIONDATA->m_region = gdk_cairo_region_create_from_surface(surface);
cairo_surface_destroy(surface);
#else
GdkPoint *gdkpoints = new GdkPoint[n];
for ( size_t i = 0 ; i < n ; i++ )
{
gdkpoints[i].x = points[i].x;
gdkpoints[i].y = points[i].y;
}
m_refData = new wxRegionRefData();
GdkRegion* reg = gdk_region_polygon
(
gdkpoints,
n,
fillStyle == wxWINDING_RULE ? GDK_WINDING_RULE
: GDK_EVEN_ODD_RULE
);
M_REGIONDATA->m_region = reg;
delete [] gdkpoints;
#endif
}
int
main (int argc, char *argv[])
{
cairo_surface_t *surface = //Sets the Cairo surface context
cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 421, 615); //Creates Cairo image surface, sets image resolution.
cairo_t *cr = //Declare *cr as a Cairo context.
cairo_create (surface); //Sets the Cairo context as a pointer, *cr.
/* Declaration of first triangle pointer */
cairo_surface_t *one;
/* Declaration of second triangle pointer */
cairo_surface_t *two;
/* Declaration of third triangle pointer */
cairo_surface_t *three;
/* Declaration of fourth triangle pointer */
cairo_surface_t *four;
/* Declaration of fifth triangle pointer */
cairo_surface_t *five;
/* Declaration of sixth triangle pointer */
cairo_surface_t *six;
/* Declaration of seventh triangle pointer */
cairo_surface_t *seven;
/* Declaration of text triangle pointer */
cairo_surface_t *text;
/* Assigns the image of first triangle to the declared pointer */
cairo_push_group (cr); //Push the image to a surface
one = cairo_image_surface_create_from_png ("../images/triangle1-color.png"); //Assign generated image to the pointer declared above
cairo_pop_group_to_source (cr); //Pop it to the image you see
/* Assigns the image of second triangle to the declared pointer */
cairo_push_group (cr); //Push the image to a surface
two = cairo_image_surface_create_from_png ("../images/triangle2-color.png"); //Assign generated image to the pointer declared above
cairo_pop_group_to_source (cr); //Pop it to the image you see
/* Assigns the image of third triangle to the declared pointer */
cairo_push_group (cr); //Push the image to a surface
three = cairo_image_surface_create_from_png ("../images/triangle3-color.png"); //Assign generated image to the pointer declared above
cairo_pop_group_to_source (cr); //Pop it to the image you see
/* Assigns the image of fourth triangle to the declared pointer */
cairo_push_group (cr); //Push the image to a surface
four = cairo_image_surface_create_from_png ("../images/triangle4-color.png"); //Assign generated image to the pointer declared above
cairo_pop_group_to_source (cr); //Pop it to the image you see
/* Assigns the image of fifth triangle to the declared pointer */
cairo_push_group (cr); //Push the image to a surface
five = cairo_image_surface_create_from_png ("../images/triangle5-color.png"); //Assign generated image to the pointer declared above
cairo_pop_group_to_source (cr); //Pop it to the image you see
/* Assigns the image of sixth triangle to the declared pointer */
cairo_push_group (cr); //Push the image to a surface
six = cairo_image_surface_create_from_png ("../images/triangle6-color.png"); //Assign generated image to the pointer declared above
cairo_pop_group_to_source (cr); //Pop it to the image you see
/* Assigns the image of seventh triangle to the declared pointer */
cairo_push_group (cr); //Push the image to a surface
seven = cairo_image_surface_create_from_png ("../images/triangle7-color.png"); //Assign generated image to the pointer declared above
cairo_pop_group_to_source (cr); //Pop it to the image you see
/* Assigns the image of the generated logo's text to the declared pointer */
cairo_push_group (cr); //Push the image to a surface
text = cairo_image_surface_create_from_png ("../images/text.png"); //Assign generated image to the pointer declared above
cairo_pop_group_to_source (cr); //Pop it to the image you see
/* Assign pointer to source */
cairo_set_source_surface (cr, one, 0, 0); //Makes sure the pointers appear all together and make the image using their assigned file
cairo_paint (cr); //Transfers these images to the final output that you see, it paints the image that you see when compiled/outpuuted
/* Assign pointer to source */
cairo_set_source_surface (cr, two, 0, 0); //Makes sure the pointers appear all together and make the image using their assigned file
cairo_paint (cr); //Transfers these images to the final output that you see, it paints the image that you see when compiled/outpuuted
/* Assign pointer to source */
cairo_set_source_surface (cr, three, 0, 0); //Makes sure the pointers appear all together and make the image using their assigned file
cairo_paint (cr); //Transfers these images to the final output that you see, it paints the image that you see when compiled/outpuuted
/* Assign pointer to source */
cairo_set_source_surface (cr, four, 0, 0); //Makes sure the pointers appear all together and make the image using their assigned file
cairo_paint (cr); //Transfers these images to the final output that you see, it paints the image that you see when compiled/outpuuted
/* Assign pointer to source */
cairo_set_source_surface (cr, five, 0, 0); //Makes sure the pointers appear all together and make the image using their assigned file
cairo_paint (cr); //Transfers these images to the final output that you see, it paints the image that you see when compiled/outpuuted
/* Assign pointer to source */
cairo_set_source_surface (cr, six, 0, 0); //Makes sure the pointers appear all together and make the image using their assigned file
cairo_paint (cr); //Transfers these images to the final output that you see, it paints the image that you see when compiled/outpuuted
/* Assign pointer to source */
cairo_set_source_surface (cr, seven, 0, 0); //Makes sure the pointers appear all together and make the image using their assigned file
cairo_paint (cr); //Transfers these images to the final output that you see, it paints the image that you see when compiled/outpuuted
//.........这里部分代码省略.........
请发表评论