FBC's Cairo clock example error

For other topics related to the FreeBASIC project or its community.
dodicat
Posts: 5913
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FBC's Cairo clock example error

Postby dodicat » Jun 22, 2019 11:32

Thanks knatterton, I'll experiment with these new (to me) procedures.
Knatterton
Posts: 139
Joined: Apr 19, 2019 19:03

Re: FBC's Cairo clock example error

Postby Knatterton » Jun 22, 2019 12:40

Maybe you have an idea what do do with these meshes. Seems it is meant to arrange them in a pattern.
Knatterton
Posts: 139
Joined: Apr 19, 2019 19:03

Re: FBC's Cairo clock example error

Postby Knatterton » Jun 22, 2019 19:11

Anotherone showing "Gradients":

Code: Select all

#include once "cairo/cairo.bi"

Const SCREEN_W = 512
Const SCREEN_H = 512

ScreenRes SCREEN_W, SCREEN_H, 32
windowtitle "Gradients"

' Create a cairo drawing context, using the FB screen as surface.
Dim As cairo_surface_t Ptr surface = cairo_image_surface_create_for_data(ScreenPtr(), _
      CAIRO_FORMAT_ARGB32, SCREEN_W, SCREEN_H, SCREEN_W * 4 )

Dim As cairo_t Ptr cr = cairo_create(surface)
dim as cairo_pattern_t ptr radpat, linpat

ScreenLock

cairo_scale (cr, 512, 512)

' draw the entire context dark
cairo_set_source_rgba(cr, 0.1, 0.9, 0, 0)
cairo_paint(cr)

radpat = cairo_pattern_create_radial (0.50, 0.50, 0.1,  0.5, 0.5, 0.5)
cairo_pattern_add_color_stop_rgb (radpat, 0,  0.9, 0.1, 0.1)
cairo_pattern_add_color_stop_rgb (radpat, 0.8,  0.1, 0.1, 0.6)

' draw background rectangles
for i as short = 1 to 9
  for j as short = 1 to 9
    cairo_rectangle (cr, i/10.0 - 0.04, j/10.0 - 0.04, 0.08, 0.08)
  next j
next i

cairo_set_source (cr, radpat)
cairo_fill (cr)

linpat = cairo_pattern_create_linear (0.25, 0.35, 0.75, 0.65)
cairo_pattern_add_color_stop_rgba (linpat, 0.00,  1, 1, 1, 0)
cairo_pattern_add_color_stop_rgba (linpat, 0.25,  0.9, 0.9, 0, 0.8)
cairo_pattern_add_color_stop_rgba (linpat, 0.50,  1, 1, 1, 0)
cairo_pattern_add_color_stop_rgba (linpat, 0.75,  0.9, 0.9, 0, 0.8)
cairo_pattern_add_color_stop_rgba (linpat, 1.00,  1, 1, 1, 0)

cairo_rectangle (cr, 0.0, 0.0, 1, 1)
cairo_set_source (cr, linpat)
cairo_fill (cr)

ScreenUnlock

' Clean up the cairo context
cairo_destroy(cr)
cairo_surface_destroy(surface)

Sleep


edit: made colors more clear
Knatterton
Posts: 139
Joined: Apr 19, 2019 19:03

Re: FBC's Cairo clock example error

Postby Knatterton » Jun 24, 2019 18:38

Just finished translation of "Text Glyphs":

Code: Select all

#include once "cairo/cairo.bi"

Const SCREEN_W = 560
Const SCREEN_H = 400

ScreenRes SCREEN_W, SCREEN_H, 32
windowtitle "Text Glyphs"

' Create a cairo drawing context, using the FB screen as surface.
Dim As cairo_surface_t Ptr surface = cairo_image_surface_create_for_data(ScreenPtr(), _
      CAIRO_FORMAT_ARGB32, SCREEN_W, SCREEN_H, SCREEN_W * 4 )

Dim As cairo_t Ptr cr = cairo_create(surface)

Dim as long n_glyphs = 20 * 35, i = 0, x, y
Dim as cairo_glyph_t glyphs(n_glyphs)

ScreenLock

' draw the entire context white
cairo_set_source_rgba(cr, 1, 1, 1, 1)
cairo_paint(cr)

' black
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0)

cairo_select_font_face(cr, "Serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL)
cairo_set_font_size(cr, 13)
 
  for y = 0 to 19
      for x=0 to 34
          glyphs(i).index = i
          glyphs(i).x = x*15 + 20
          glyphs(i).y = y*18 + 20
          i += 1
      next x
  next y
 
cairo_show_glyphs(cr, @glyphs(0),i)

ScreenUnlock

' Clean up the cairo context
cairo_destroy(cr)
cairo_surface_destroy(surface)

Sleep


It shows the first glyphs of a given font.
badidea
Posts: 1461
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: FBC's Cairo clock example error

Postby badidea » Jun 24, 2019 20:05

Nice, you are the Cairo expert here now :-)
Knatterton
Posts: 139
Joined: Apr 19, 2019 19:03

Re: FBC's Cairo clock example error

Postby Knatterton » Jun 24, 2019 21:56

Ha, ha, i really dont't know. Started once with Cairo when i have read it may become standard lib for C/C++. Then i found it perfectly matches gtk which has the same crazy logic.

The biggest advantage to me is, you can save .pdf, .png and .ps with almost no extra cost. That's useful for my work.

Still have few examples like "Glob Lines":

Code: Select all

' cairo_glob_lines.bas

' cairo example for FreeBASIC 1.06
' public domain by Knatterton

#include once "cairo/cairo.bi"

Const SCREEN_W = 512
Const SCREEN_H = 512

CONST M_PI = 4 * ATN(1)

ScreenRes SCREEN_W, SCREEN_H, 32
windowtitle " Cairo Glob Lines"

' Create a cairo drawing context, using the FB screen as surface.

Dim As cairo_surface_t Ptr surface = cairo_image_surface_create_for_data(ScreenPtr(), _
      CAIRO_FORMAT_ARGB32, SCREEN_W, SCREEN_H, SCREEN_W * 4 )

Dim As cairo_t Ptr cr = cairo_create(surface)
dim as long i, j,globnr = 27

type tglob
  as long count, x, y
end type

dim as tglob glob(globnr)

randomize

do
  ScreenLock
 
' draw the entire context blue
  cairo_set_source_rgb(cr, 0.1, 0.1, 0.9)
  cairo_paint(cr)
   
' color white   
  cairo_set_source_rgb(cr, 1, 1, 1)
  cairo_set_line_width(cr, 0.5)
 
   for i = 0 to globnr
     'glob(i).count = i
     glob(i).x = int(rnd * 512)
     glob(i).y = int(rnd * 512)
   next i
 
  for i = 0 to globnr
      for j = 0 to globnr
          cairo_move_to(cr, glob(i).x, glob(i).y)
          cairo_line_to(cr, glob(j).x, glob(j).y)
      next j
  next i
 
  cairo_stroke(cr)   
  ScreenUnlock
  sleep 1
 
loop until INKEY = CHR(27)

' Clean up the cairo context
cairo_destroy(cr)
cairo_surface_destroy(surface)

Sleep



Strange here, the breaks between drawings sometimes is short, sometimes verrry long. With moving mouse to the left and right of the screen over the cairo window you can speed up. Seems, the OS gives cairo more timeslices then.

Edit: Inserted "Sleep 1" after badideas hint.
Last edited by Knatterton on Jun 24, 2019 22:41, edited 1 time in total.
badidea
Posts: 1461
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: FBC's Cairo clock example error

Postby badidea » Jun 24, 2019 22:05

Add a sleep 1 (after screenunlock)
Knatterton
Posts: 139
Joined: Apr 19, 2019 19:03

Re: FBC's Cairo clock example error

Postby Knatterton » Jun 24, 2019 22:36

Tried, works. Now we know who is cairo-master. Its you!

Return to “Community Discussion”

Who is online

Users browsing this forum: No registered users and 5 guests