2010-08-01 4 views
0

카이로 프로그램을 작성하여 전체 이미지를 검정색으로 채우고 그 안에 다른 사각형을 다른 색으로 그려 봅니다. 결국, 저는 이것을 디지털 시계처럼 보이는 현재 시간의 .png을 생성하는 프로그램으로 만들 것입니다. 당분간, 나는 이것이 끊어지는 곳이다. 문제는이 코드를 함께카이로 그래픽으로 여러 사각형 그리기

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

//on color: 0.6, 1.0, 0 
//off color: 0.2, 0.4, 0 

int prop_number_width; 
int prop_number_height; 

int prop_width; 
int prop_height; 
int prop_space_width; 
int prop_space_height; 

double width; 
double height; 

double w_unit; 
double h_unit; 

void draw_number(cairo_t* cr, int unit_width, int num); 

int main(int argc, char** argv) { 
    /* proportional sizes: 
    * the widths and heights of the diagram 
    */ 
    prop_number_width = 5; //how many spaces the number takes up 
    prop_number_height = 6; 

    prop_space_width = 1; //extra width on each side 
    prop_space_height = 1; //extra height on each side 
    prop_width = 25 + (2 * prop_space_width); //width of diagram 
    prop_height = 6 + (2 * prop_space_height); //height of diagram 

    /* actual sizes: 
    * the pixel value of different sizes 
    */ 
    width = 200.0; 
    height = 100.0; 

    w_unit = width/prop_width; 
    h_unit = height/prop_height; 

    //begin cairo stuff 
    cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, (int)width, (int)height); 
    cairo_t* cr = cairo_create(surface); 

    //black fill 
    cairo_set_source_rgb(cr, 0.0, 0.0, 0.0); 
    cairo_rectangle(cr, 0.0, 0.0, width, height); //cr ref, x, y, width, height 
    cairo_fill_preserve(cr); 

    //draw numbers from left to right 
    draw_number(cr, 0, 1); 
    //draw_number(cr, 6, 3); 
    //draw_number(cr, 14, 3); 
    //draw_number(cr, 20, 7); 

    //draw in colons 

    cairo_destroy(cr); 
    cairo_surface_write_to_png(surface, "test.png"); 
    cairo_surface_destroy(surface); 
    return 0; 
} 

void draw_number(cairo_t* cr, int unit_width, int num) { 
    //determine the box size that the number will be drawn in 
    double box_x = w_unit * (prop_space_width + unit_width); 
    double box_y = h_unit * prop_space_height; 
    double box_width = w_unit * prop_number_width; 
    double box_height = h_unit * prop_number_height; 

    printf("{box_x: %lf box_y: %lf} {box_width: %lf box_height: %lf}\n", box_x, box_y, box_width, box_height); 
    cairo_set_source_rgb(cr, 0.2, 0.4, 0); 
    cairo_rectangle(cr, box_x, box_y, box_width, box_height); 
    cairo_fill_preserve(cr); 
} 

가가의 printf의에서 그것은 단지 작은 부분을 차지한다 전체 이미지를 적용하려면 사각형을 그립니다

여기 내 코드입니다. 아무도 내가이 사각형을 올바른 크기로 표시 할 수있는 방법을 알고 있습니까?

답변

1

API를 좀 더주의 깊게 살펴 보았습니다. cairo_fill_preserve() 대신 cairo_fill()을해야했습니다. 분명히 cairo_fill_preserve()에 대한 첫 번째 호출은 원래 사각형을 유지하면서 항상 그 사각형을 채우는 것이 었습니다.

관련 문제