Skip to content

Commit 918c4aa

Browse files
author
Carlos Une
committed
Add example: chessboard
1 parent 0ac424f commit 918c4aa

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ cairo-fortran = {git = "https://github.com/brocolis/cairo-fortran" }
2222
![test1](F.png)
2323
### Example test2
2424
![test2](axis.png)
25+
### Example test4
26+
![test4](chess.png)

chess.png

1.94 KB
Loading

test/test4.f90

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
! test4
2+
!
3+
! We'll see in this test code
4+
! - Create a 480x480 image
5+
! - Create an internal workspace 8x8
6+
! - Draw board
7+
! - Save image to disk
8+
9+
program test4
10+
use cairo
11+
use cairo_enums
12+
use cairo_types, only: cairo_matrix_t
13+
use iso_c_binding, only: c_ptr, c_int, c_double, c_null_char, c_loc
14+
15+
implicit none
16+
type(c_ptr) :: surface, c
17+
integer(c_int) :: r
18+
real(c_double), parameter :: IMAGE_WIDTH = 480.0d0
19+
real(c_double), parameter :: IMAGE_HEIGHT = IMAGE_WIDTH
20+
real(c_double), parameter :: WORKSPACE_SIZE = 8.d0
21+
real(c_double), parameter :: WORKSPACE_FACTOR = IMAGE_WIDTH/WORKSPACE_SIZE
22+
type(cairo_matrix_t), target :: m
23+
integer :: i, j
24+
25+
! Initialize
26+
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, int(IMAGE_WIDTH), int(IMAGE_HEIGHT))
27+
c = cairo_create(surface)
28+
call cairo_set_antialias(c, CAIRO_ANTIALIAS_BEST)
29+
30+
! Transform image coordinates (480x480) to my internal workspace (8x8)
31+
call cairo_matrix_init(c_loc(m), +WORKSPACE_FACTOR, 0.0d0, 0.0d0, +WORKSPACE_FACTOR, 0.0d0, 0.0d0)
32+
call cairo_transform(c, c_loc(m))
33+
34+
! Draw board
35+
do i = 1, 8, 1
36+
do j = 1, 8, 1
37+
38+
! Which color?
39+
if (mod(i + j, 2) .eq. 0) then
40+
call cairo_set_source_rgb(c, 1.0d0, 0.8d0, 0.7d0)
41+
else
42+
call cairo_set_source_rgb(c, 0.1d0, 0.1d0, 0.1d0)
43+
end if
44+
45+
! Draw square
46+
call cairo_rectangle(c, real(j, kind=c_double) - 1.d0, &
47+
real(i, kind=c_double) - 1.d0, 1.d0, 1.d0)
48+
call cairo_fill(c)
49+
50+
end do
51+
end do
52+
53+
! Write .png
54+
r = cairo_surface_write_to_png(surface, "chess.png"//c_null_char)
55+
56+
! Destroy
57+
call cairo_destroy(c)
58+
call cairo_surface_destroy(surface)
59+
60+
end program test4

0 commit comments

Comments
 (0)