davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: HDMI graphic lib: openvg syntax questions

Tue Nov 03, 2015 10:12 am

ps, edit:
of course I tried it already passing the macro to the function call but that does not work

no one around here who did this before, by passing color names to the Stroke or Fill functions and so on?
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
Paeryn
Posts: 3174
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: HDMI graphic lib: openvg syntax questions

Tue Nov 03, 2015 2:31 pm

davenull wrote:next question:

what would be the best way to pass "real color names" to the Stroke(r, g, b, x) function?

#define BLACK {0, 0, 0, 1}
#define WHITE {255,255,255, 1}

is this correct?
Stroke( WHITE );

analoguesly, e.g. for:
#define RED ...
#define BLUE
#define GREEN
#define YELLOW
#define MAGENTA
#define BROWN
#define LIME
#define GRAY

- is there already a list of common widely used colors predefined in openvg ?
Since Stroke() is expecting 4 parameters your code would have to be

Code: Select all

#define WHITE 255, 255, 255, 1

Stroke( WHITE );
OpenVG doesn't have any pre-defined colours, the idea is you pick exactly the ones you want.
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: HDMI graphic lib: openvg syntax questions

Tue Nov 03, 2015 6:22 pm

thank you,
I never expected
#define WHITE 255, 255, 255, 1
to work lising 5 parameters without a brace around the last 4 ones :o

are those r,g,b,x coded colors a common color code? Is there a name for this color code like it's common for RGB (or RGBA or CMYK - or is this actually one of these)?
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
Paeryn
Posts: 3174
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: HDMI graphic lib: openvg syntax questions

Tue Nov 03, 2015 7:01 pm

davenull wrote:thank you,
I never expected
#define WHITE 255, 255, 255, 1
to work lising 5 parameters without a brace around the last 4 ones :o

are those r,g,b,x coded colors a common color code? Is there a name for this color code like it's common for RGB (or RGBA or CMYK - or is this actually one of these)?
#define when used like that just replaces all occurences of the first identifier with a list of the remaining identifers.

Where did you get X for the last value, it's A. Standard RGBA although I do find ajstarks' usage of int [0, 255] for RGB and then float [0.0, 1.0] for alpha to be confusing, OpenVG uses float [0.0, 1.0] for all four.
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: HDMI graphic lib: openvg syntax questions

Tue Nov 03, 2015 7:19 pm

indeed, I didn't understand that
int [0, 255] for RGB and then float [0.0, 1.0] for alpha
was actually a defamiliarized RGBA coding. That's why I paraphrased the float parameter by an "x" which is often used in analysis for Real numbers, opposite to Natural numbers.

So by Google search I found the keyword "CSS3 RGBA" which is using the same parameters as ajstarks is using, and additionally color palette tools:

http://www.css3-generator.de/rgba.html

https://developer.mozilla.org/de/docs/W ... l_werkzeug

Using this tool it should be possible to generate customized color name codes (I hope).

BROWN 100, 50, 15, 1
VIOLET 200, 20, 130, 1

nevertheless, having standardized name-code relations would be very much appreciated!

edit: found some:

http://www.december.com/html/spec/colorrgbadec.html
http://www.december.com/html/spec/colordec.html
http://www.colorcombos.com/light-blue-c ... hemes.html

Code: Select all

// defining just RGB values one could use a variable alpha luminiscence value by e.g., 
Stroke( RED, 0.5);
 
    #define RED      255, 0, 0
    #define DEEPPINK 255, 20, 147
    #define PINK     255, 192, 203
    #define ROSE     255, 0, 204
    #define MAGENTA  238, 0, 238
    #define FUCHSIA  255, 0, 255
    #define PURPLE   160, 32, 240
    #define YELLOW   255, 255, 0
    #define PAPAYA   255, 255, 126
    #define SIGNYELLOW 255, 209, 22
    #define ORANGE   255, 102, 0
    #define LIME       0, 255, 0
    #define GREEN      0, 128, 0
    #define DARKGREEN 47, 79, 47
    #define CYAN       0, 255, 255
    #define BLUE       0,  0,  255
    #define ULTRAMARINE  18, 10, 143
    #define AQUA       0, 255, 255
    #define BROWN    128, 42, 42
    #define SIGNBROWN 96, 51, 17
    #define OCHRE    204, 119, 34
    #define GRAY25    64, 64, 64
    #define GRAY50   127, 127, 127
    #define GRAY75   191, 191, 191
    #define BLACK      0,  0,  0
some important ones missing >>> ? :ugeek:
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
Paeryn
Posts: 3174
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: HDMI graphic lib: openvg syntax questions

Wed Nov 04, 2015 1:22 am

I've updated my version for ajstatks' library to add in the outlined shapes and also functions to allow setting a window smaller than he screen that can be placed anywhere as long as at least 1 pixel is on screen. The window also has the ability to change it's transparency without clearing the screen like my early hack did.
In the Code he does have colours picked by their names, I could implement a similar system, keep a few common ones already set up as PaintTypes in a list the you could add to do save keeping creatimg and destroying paints.

Documentation is lacking at the moment, I've issued a pull request to ajstarks, my version can be taken from https://github.com/paeryn/openvg
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: HDMI graphic lib: openvg syntax questions

Wed Nov 04, 2015 7:49 am

that sounds amazing! Thank you very much for your efforts!
Now having also a smaller screen besides the other ones on the desktop is really an outstanding feature!

I just had my first look at it, it looks almost as if it's the complete entire openvg system like already ajstarks provided before?

Now if I wish to run your updated version, should I delete all files from ajstarks before?
Or do I only have to copy your files over / into the existing ones?

As working with Linux libs is really completely new to me:
What would you suggest, step by step, :oops: how I can use your new API functions?
I have internet on my raspi, and an USB HD drive for my windows files which I can plug to my raspi too (and mounted there).
You might recognize that not just my Linux skilla but also my English is very poor indeed :?
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
Paeryn
Posts: 3174
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: HDMI graphic lib: openvg syntax questions

Wed Nov 04, 2015 8:35 am

Neither is the complete OpenVG system. They are both layers on top of OpenVG providing a few extra features. Mine looks so similar to ajstarks' because it is an extension of it, I made a few changes and added the extras. I sent a pull request so ajstarks can incorporate as much or as little of my changes to his version.

Since it is a direct replacement you can install it over the top as it will overwrite the exact same files. Reinstalling ajstarks' version will likewise overwrite mine.

Sorry about the lack of documentation, it was bed time and I forgot to add it in. I'll try and do it later today but basically (off the top of my head) the extra functions are :-

Code: Select all

// request window size and position on screen,
// call before init(), size cannot be altered after opening.
initWindowSize(x, y, w, h); 

// clears the window to the last colour given to Background() so
// call that once right at the start (or whenever you want to change
// the background colour)..
// Use in place of Start() to clear the full window and nothing else.
WindowClear();

// Move window to given coordinates, -ve values upto -width,-height allow the window to be partially off the left/top of the screen.
WindowPosition(x, y);

// Set opacity of window, ranges between
// 0 = fully transparent and 255 = fully opaque.
WindowOpacity(alpha);

// All shapes that didn't have outline versions now do by adding Outline e.g.
// Rect() now has equivalent RectOutline()
Edited - I originally wrote WindowMove() whereas the function is called WindowPosition().
Last edited by Paeryn on Wed Nov 04, 2015 4:31 pm, edited 1 time in total.
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: HDMI graphic lib: openvg syntax questions

Wed Nov 04, 2015 8:47 am

hey,
the last install procedure for your extended libs were:

Code: Select all

wget https://www.dropbox.com/s/h37po1q305eab17/shapes_plus.tar.gz
tar xvf shapes_plus.tar.gz
sudo rm shapes_plus.tar.gz
cd shape_demo
make
sudo make install
for the compiler: additionally
-lshape_plus
for the program: additionally
#include "shapes_plus.h"


what has to be changed now for a new install?


----------------------------------------------------

BTW,
what do you think about adding
vg********
before each of the openvg function calls?
e.g.

Code: Select all

vgInitWindowSize
vgWindowMove(x, y);
IMO that will make the API functions more clear to be related to openvg libs, opposite to different libs doing something important for different "windows".
Additionally, for having

Code: Select all

#define vgHidewindow  HideWindow
#define vgShowwindow  End
#define vgcls()       ClearWindowRGB(_scrwidth_, _scrheight_, 0, 0, 0);   
#define vgStart       Start
#define vgInit        init
#define vgCleanup     finish
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
Paeryn
Posts: 3174
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: HDMI graphic lib: openvg syntax questions

Wed Nov 04, 2015 11:45 am

You don't need shapes_plus anymore, everything is in libshapes.

Personally I'm against prefixing with vg as they denote openvg's libs.
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: HDMI graphic lib: openvg syntax questions

Wed Nov 04, 2015 12:47 pm

so I can delete the shapes_plus folder, but now how to install your libs instead?
I don't understand what to do unfortunately.
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
Paeryn
Posts: 3174
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: HDMI graphic lib: openvg syntax questions

Wed Nov 04, 2015 3:39 pm

davenull wrote:so I can delete the shapes_plus folder, but now how to install your libs instead?
I don't understand what to do unfortunately.
Same way as before, from within the (new) openvg directory

Code: Select all

make
sudo make install
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: HDMI graphic lib: openvg syntax questions

Wed Nov 04, 2015 3:53 pm

I don't even have the new openvg on my SD card - how do I get it there? (sorry, for Windows it would be easy, just
download zip,
unzip,
copy unzipped folder over existing openvg folder

but by linux ???
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
Paeryn
Posts: 3174
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: HDMI graphic lib: openvg syntax questions

Wed Nov 04, 2015 4:27 pm

davenull wrote:I don't even have the new openvg on my SD card - how do I get it there? (sorry, for Windows it would be easy, just
download zip,
unzip,
copy unzipped folder over existing openvg folder

but by linux ???
You can do that on linux as well, if you're doing it from the command line you can download it with

Code: Select all

wget https://github.com/paeryn/openvg/archive/master.zip
You can copy it over the original if you want, personally I'd have it in a different directory just so it's easy to re-install the original just in case there are any issues (and in the future there may be other files that are in one version but not the other).

I'll try and keep my version as compatible as possible to the extent that any code that uses his original library will work without modification if linked with my fork. The main intention is that ajstarks can easily incorporate any modifications I make into his main version if he wishes to do so.

Just to clarify about the windowed mode in my version - if you're using the desktop it's not a window that the X window manager knows about in the sense that you can't move it on the desktop with your mouse and there won't be any title bar or borders. Movement can only be done directly by calling the WindowPosition() function in your own program.
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: HDMI graphic lib: openvg syntax questions

Wed Nov 04, 2015 4:47 pm

ok, I made a copy of openvg called openvg_sav

then

Code: Select all

pi@raspberrypi ~ $ wget https://github.com/paeryn/openvg/archive/master.zip
ok

now: master.zip is in the pi folder.

how to get it to the correct folder openvg ?
what next?

Linux is really the 7th hell to me... :evil:
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
Paeryn
Posts: 3174
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: HDMI graphic lib: openvg syntax questions

Wed Nov 04, 2015 10:58 pm

Unzip it with (I'm sure the rpi has unzip by default)

Code: Select all

unzip master.zip
That should create the new openvg directory with everything in it.
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: HDMI graphic lib: openvg syntax questions

Thu Nov 05, 2015 8:01 am

and the old directory of the same name (openvg) which is still existing has to be deleted before or should I just keep it as it is?
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
Paeryn
Posts: 3174
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: HDMI graphic lib: openvg syntax questions

Thu Nov 05, 2015 12:54 pm

You could delete the old one as long as you copied everything including subdirectories. But you should be fine just letting it overwrite the current.
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: HDMI graphic lib: openvg syntax questions

Thu Nov 05, 2015 2:27 pm

thank you, I recall:

Code: Select all

pi@raspberrypi ~ $ wget https://github.com/paeryn/openvg/archive/master.zip

unzip master.zip

cd openvg

make

sudo make install

correct? (dude, how I LOVE Windows!)
;)
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: HDMI graphic lib: openvg syntax questions

Thu Nov 05, 2015 2:46 pm

after
unzip master.zip I suddenly have a
openvg-master
dirctory :o
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
Paeryn
Posts: 3174
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: HDMI graphic lib: openvg syntax questions

Thu Nov 05, 2015 2:52 pm

Looks good to me.

You can't compare the linux command line to windows (unless you compare it to windows' command line). :)

Edit: okay, it's added the branch name to the directory name... I didn't realise github would do that.
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: HDMI graphic lib: openvg syntax questions

Thu Nov 05, 2015 2:55 pm

the fontinfo files are not in your folder, but in the old one.
I'll copy just your files over the other ones... ;)
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: HDMI graphic lib: openvg syntax questions

Thu Nov 05, 2015 3:13 pm

indeed, how would I love to have a setup.exe program which installs all the files, all the folders, all the settings and all the drivers... :twisted:

now this program don't work any more (it compiles fine though):

Code: Select all

//
// shapedemo: testbed for OpenVG APIs
// Anthony Starks (ajstarks@gmail.com)
//
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include "VG/openvg.h"
#include "VG/vgu.h"
#include "fontinfo.h"
#include "shapes.h"

// randcolor returns a random number 0..255
unsigned int randcolor() {
   return (unsigned int)(drand48() * 255.0);
}

// randf returns a floating point number bounded by n
VGfloat randf(int n) {
   return drand48() * n;
}

// coordpoint marks a coordinate, preserving a previous color
void coordpoint(VGfloat x, VGfloat y, VGfloat size, VGfloat pcolor[4]) {
   Fill(128, 0, 0, 0.3);
   Circle(x, y, size);
   setfill(pcolor);
}

// grid draws a grid
void grid(VGfloat x, VGfloat y, int n, int w, int h) {
   VGfloat ix, iy;
   Stroke(128, 128, 128, 0.5);
   StrokeWidth(2);
   for (ix = x; ix <= x + w; ix += n) {
      Line(ix, y, ix, y + h);
   }

   for (iy = y; iy <= y + h; iy += n) {
      Line(x, iy, x + w, iy);
   }
}
// gradient demos linear and radial gradients
void gradient(int width, int height) {
   VGfloat x1, y1, x2, y2, cx, cy, fx, fy, r;
   VGfloat w = (VGfloat)width;
   VGfloat h = (VGfloat)height;
   VGfloat dotcolor[4] = {0, 0, 0, 0.3};
   
   
   VGfloat stops[] = {
      0.0, 1.0, 1.0, 1.0, 1.0,
      0.5, 0.5, 0.5, 0.5, 1.0,
      1.0, 0.0, 0.0, 0.0, 1.0
   };
            
   
   x1 = w/8;
   x2 = (w*3)/8;
   y1 = h/3;
   y2 = (h*2)/3;
   cx = (w*3)/4;
   cy = (h/2);
   r = (x2-x1);
   fx = cx + (r/4);
   fy = cy + (r/4);
   Start(w, h);
   Background(128, 128, 128);
   

   FillLinearGradient(x1, y1, x2, y2, stops, 3);
   Rect(x1, y1, x2-x1, y2-y1);
   FillRadialGradient(cx, cy, fx, fy, r, stops, 3);
   Circle(cx, cy, r);
   
   RGBA(.5, 0, 0, 0.3, dotcolor);
   setfill(dotcolor);
   Circle(x1, y1, 10);
   Circle(x2, y2, 10);
   Circle(cx, cy, 10);
   Circle(cx+r/2, cy, 10);
   Circle(fx, fy, 10);
   
   RGB(0,0,0,dotcolor);
   setfill(dotcolor);
   TextMid(x1, y1-20, "(x1, y1)", SansTypeface, 18);
   TextMid(x2, y2+10, "(x2, y2)", SansTypeface, 18);
   TextMid(cx, cy, "(cx, cy)", SansTypeface, 18);
   TextMid(fx, fy, "(fx, fy)", SansTypeface, 18);
   TextEnd(cx+(r/2)+20, cy, "r", SansTypeface, 18);
   
   
   TextMid(x1+((x2-x1)/2), h/6, "Linear Gradient", SansTypeface, 36);
   TextMid(cx, h/6, "Radial Gradient", SansTypeface, 36);
   
   
   End();

}
// makepi draws the Raspberry Pi
void makepi(VGfloat x, VGfloat y, int w, int h) {
   // dimensions
   VGfloat socw = h / 5,
       compw = h / 5,
       cjw = h / 10,
       cjh = h / 8,
       audw = h / 5,
       aujw = h / 10,
       aujh = cjh / 2,
       hdw = w / 6,
       hdh = w / 10,
       gpw = w / 3,
       gph = h / 8,
       pw = h / 10,
       usw = w / 5,
       ush = h / 5,
       etw = w / 5,
       eth = h / 5,
       sdw = w / 6, sdh = w / 4, offset = (w / 2) / 10, w34 = (w * 3) / 4, w2 = w / 2, h2 = h / 2, h40 = (h * 2) / 5;

   Fill(0, 128, 0, 1);
   Rect(x, y, w, h);               // board

   Fill(255, 255, 0, 1);
   Rect(x + w2, (y + h) - compw, compw, compw);      // composite
   Fill(192, 192, 192, 1);
   Rect(x + w2 + (cjw / 2), y + h, cjw, cjh);      // composite jack

   Fill(0, 0, 0, 1);
   Rect(x + w34, y + h - audw, audw, audw);      // audio
   Rect(x + w34 + (aujw / 2), y + h, aujw, aujh);      // audio jack

   Fill(192, 192, 192, 1);
   Rect(x + w2, y, hdw, hdh);            // HDMI
   Rect((x + w) - etw, y, etw, eth);         // Ethernet
   Rect((x + w + offset) - usw, y + h40, usw, ush);   // USB
   Rect(x, y, pw, pw);               // Power

   Fill(0, 0, 0, 1);
   Rect(x + (w * 2) / 5, y + h40, socw, socw);      // SoC
   Rect(x, (y + h) - gph, gpw, gph);         // GPIO
   Fill(0, 0, 255, 1);
   Rect(x - sdw, (y + h2) - (sdh / 2), sdw, sdh);      // SD card
}

// raspi draws the raspberry pi, scaled to the screen dimensions
void raspi(int w, int h, char *s) {
   VGfloat midx = w / 2, midy = h / 2;
   int rw = midx, rh = (rw * 2) / 3, fontsize = w * 0.03;

   Start(w, h);
   Background(255, 255, 255);
   makepi(midx - (rw / 2), midy - (rh / 2), rw, rh);
   Fill(128, 0, 0, 1);
   TextMid(midx, midy - (rh / 2) - (fontsize * 2), s, SansTypeface, fontsize);
   End();
}

typedef struct {
   Fontinfo font;
   VGfloat tw;
   int fontsize;
} FW;

void textbbox(char *s, Fontinfo f, int pointsize) {

}

// adjust the font to fit within a width
void fitwidth(int width, int adj, char *s, FW * f) {
   f->tw = TextWidth(s, f->font, f->fontsize);
   while (f->tw > width) {
      f->fontsize -= adj;
      f->tw = TextWidth(s, f->font, f->fontsize);
   }
}

// testpattern shows a test pattern
void testpattern(int w, int h, char *s) {
   VGfloat midx, midy1, midy2, midy3;
   int fontsize = 256, h2 = h / 2;
   FW tw1 = { MonoTypeface, 0, fontsize };
   FW tw2 = { SansTypeface, 0, fontsize };
   FW tw3 = { SerifTypeface, 0, fontsize };

   Start(w, h);

   // colored squares in the corners
   Fill(255, 0, 0, 1);
   Rect(0, 0, 100, 100);
   Fill(0, 255, 0, 1);
   Rect(0, h - 100, 100, 100);
   Fill(0, 0, 255, 1);
   Rect(w - 100, 0, 100, 100);
   Fill(128, 128, 128, 1);
   Rect(w - 100, h - 100, 100, 100);

   // for each font, (Sans, Serif, Mono), adjust the string to the w
   fitwidth(w, 20, s, &tw1);
   fitwidth(w, 20, s, &tw2);
   fitwidth(w, 20, s, &tw3);

   midx = w / 2;

   // Adjust the baselines to be medial
   midy1 = h2 + 20 + (tw1.fontsize) / 2;
   midy2 = h2 - (tw2.fontsize) / 2;
   midy3 = h2 - 20 - tw2.fontsize - (tw3.fontsize) / 2;

   Fill(128, 128, 128, 1);
   TextMid(midx, midy1, s, tw1.font, tw1.fontsize);
   Fill(128, 0, 0, 1);
   TextMid(midx, midy2, s, tw2.font, tw2.fontsize);
   Fill(0, 0, 128, 1);
   TextMid(midx, midy3, s, tw3.font, tw3.fontsize);
   End();
}

// textlines writes lines of text
void textlines(VGfloat x, VGfloat y, char *s[], Fontinfo f, int fontsize, VGfloat leading) {
   int i;
   for (i = 0;; i++) {
      if (s[i] == NULL) {
         break;
      }
      TextMid(x, y, s[i], f, fontsize);
      y -= leading;
   }
}

// tb draws a block of text
void tb(int w, int h) {
   char *para[] = {
      "For lo,",
      "the winter is past,",
      "the rain is over and gone",
      "the flowers appear on the earth;",
      "the time for the singing of birds is come,",
      "and the voice of the turtle is heard in our land",
      NULL
   };

   VGfloat tmargin = w * 0.5, lmargin = w * 0.10, top = h * .9, mid = h * .6, bot = h * .3;

   int fontsize = 24, leading = 40, lfontsize = fontsize * 2, midb = ((leading * 2) + (leading / 2)) - (lfontsize / 2);

   Start(w, h);
   Fill(49, 79, 79, 1);
   textlines(tmargin, top, para, SerifTypeface, fontsize, leading);
   textlines(tmargin, mid, para, SansTypeface, fontsize, leading);
   textlines(tmargin, bot, para, MonoTypeface, fontsize, leading);
   Text(lmargin, top - midb, "Serif", SansTypeface, lfontsize);
   Text(lmargin, mid - midb, "Sans", SansTypeface, lfontsize);
   Text(lmargin, bot - midb, "Mono", SansTypeface, lfontsize);
   End();
}

void imagetest(int w, int h) {
   int imgw = 422, imgh = 238;
   VGfloat cx = (w / 2) - (imgw / 2), cy = (h / 2) - (imgh / 2);
   VGfloat ulx = 0, uly = h - imgh;
   VGfloat urx = w - imgw, ury = uly;
   VGfloat llx = 0, lly = 0;
   VGfloat lrx = urx, lry = lly;
   Start(w, h);
   Background(0, 0, 0);
   Image(cx, cy, imgw, imgh, "desert1.jpg");
   Image(ulx, uly, imgw, imgh, "desert2.jpg");
   Image(urx, ury, imgw, imgh, "desert3.jpg");
   Image(llx, lly, imgw, imgh, "desert4.jpg");
   Image(lrx, lry, imgw, imgh, "desert5.jpg");
   End();
}

void imagetable(int w, int h) {
   int imgw = 422, imgh = 238;
   char *itable[] = {
      "desert0.jpg",
      "desert1.jpg",
      "desert2.jpg",
      "desert3.jpg",
      "desert4.jpg",
      "desert5.jpg",
      "desert6.jpg",
      "desert7.jpg",
      NULL
   };
   VGfloat left = 50.0;
   VGfloat bot = h - imgh - 50.0;
   VGfloat gutter = 50.0;

   VGfloat x = left;
   VGfloat y = bot;
   int i;
   Start(w, h);
   Background(0, 0, 0);
   for (i = 0; itable[i] != NULL; i++) {
      Image(x, y, imgw, imgh, itable[i]);
      Fill(255, 255, 255, 0.3);
      Rect(x, y, imgw, 32);
      Fill(0, 0, 0, 1);
      TextMid(x + (imgw / 2), y + 10, itable[i], SansTypeface, 16);

      x += imgw + gutter;
      if (x > w) {
         x = left;
         y -= imgh + gutter;
      }
   }
   y = h * 0.1;
   Fill(128, 128, 128, 1);
   TextMid(w / 2, 100, "Joshua Tree National Park", SansTypeface, 48);
   End();
}

// fontrange shows a range of fonts
void fontrange(int w, int h) {
   int *s, sizes[] = { 6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 21, 24, 36, 48, 60, 72, 96, 0 };
   VGfloat x, y = h / 2, spacing = 50, s2 = spacing / 2, len, lx;
   char num[4];

   Start(w, h);
   Background(255, 255, 255);

   // compute the length so we can center
   for (len = 0, s = sizes; *s; s++) {
      len += *s + spacing;
   }
   len -= spacing;
   lx = (w / 2) - (len / 2);            // center point

   // for each size, display a character and label
   for (x = lx, s = sizes; *s; s++) {
      Fill(128, 0, 0, 1);
      TextMid(x, y, "a", SerifTypeface, *s);
      Fill(128, 128, 128, 1);
      snprintf(num, 3, "%d", *s);
      TextMid(x, y - spacing, num, SansTypeface, 16);
      x += *s + spacing;
   }
   // draw a line below the characters, a curve above
   x -= spacing;
   Stroke(150, 150, 150, 0.5);
   StrokeWidth(2);
   Line(lx, y - s2, x, y - s2);
   Fill(255, 255, 255, 1);
   Qbezier(lx, y + s2, x, y + s2, x, y + (spacing * 3));
   End();
}

// refcard shows a reference card of shapes
void refcard(int width, int height) {
   char *shapenames[] = {
      "Circle",
      "Ellipse",
      "Rectangle",
      "Rounded Rectangle",
      "Line",
      "Polyline",
      "Polygon",
      "Arc",
      "Quadratic Bezier",
      "Cubic Bezier",
      "Image"
   };
   VGfloat shapecolor[4];
   RGB(202, 225, 255, shapecolor);
   VGfloat top = height * .95, sx = 500, sy = top, sw = width * .05, sh = height * .045, dotsize = 7, spacing = 2.0;

   int i, ns = sizeof(shapenames) / sizeof(char *), fontsize = height * .033;
   Start(width, height);
   sx = width * 0.10;

   Fill(128, 0, 0, 1);
   TextEnd(width - 20, height / 2, "OpenVG on the Raspberry Pi", SansTypeface, fontsize + (fontsize / 2));
   Fill(0, 0, 0, 1);
   for (i = 0; i < ns; i++) {
      Text(sx + sw + sw / 2, sy, shapenames[i], SansTypeface, fontsize);
      sy -= sh * spacing;
   }
   sy = top;
   VGfloat cx = sx + (sw / 2), ex = sx + sw;
   setfill(shapecolor);
   Circle(cx, sy, sw);
   coordpoint(cx, sy, dotsize, shapecolor);
   sy -= sh * spacing;
   Ellipse(cx, sy, sw, sh);
   coordpoint(cx, sy, dotsize, shapecolor);
   sy -= sh * spacing;
   Rect(sx, sy, sw, sh);
   coordpoint(sx, sy, dotsize, shapecolor);
   sy -= sh * spacing;
   Roundrect(sx, sy, sw, sh, 20, 20);
   coordpoint(sx, sy, dotsize, shapecolor);
   sy -= sh * spacing;

   StrokeWidth(1);
   Stroke(204, 204, 204, 1);
   Line(sx, sy, ex, sy);
   coordpoint(sx, sy, dotsize, shapecolor);
   coordpoint(ex, sy, dotsize, shapecolor);
   sy -= sh;

   VGfloat px[5] = { sx, sx + (sw / 4), sx + (sw / 2), sx + ((sw * 3) / 4), sx + sw };
   VGfloat py[5] = { sy, sy - sh, sy, sy - sh, sy };

   Polyline(px, py, 5);
   coordpoint(px[0], py[0], dotsize, shapecolor);
   coordpoint(px[1], py[1], dotsize, shapecolor);
   coordpoint(px[2], py[2], dotsize, shapecolor);
   coordpoint(px[3], py[3], dotsize, shapecolor);
   coordpoint(px[4], py[4], dotsize, shapecolor);
   sy -= sh * spacing;

   py[0] = sy;
   py[1] = sy - sh;
   py[2] = sy - (sh / 2);
   py[3] = py[1] - (sh / 4);
   py[4] = sy;
   Polygon(px, py, 5);
   sy -= (sh * spacing) + sh;

   Arc(sx + (sw / 2), sy, sw, sh, 0, 180);
   coordpoint(sx + (sw / 2), sy, dotsize, shapecolor);
   sy -= sh * spacing;

   VGfloat cy = sy + (sh / 2), ey = sy;
   Qbezier(sx, sy, cx, cy, ex, ey);
   coordpoint(sx, sy, dotsize, shapecolor);
   coordpoint(cx, cy, dotsize, shapecolor);
   coordpoint(ex, ey, dotsize, shapecolor);
   sy -= sh * spacing;

   ey = sy;
   cy = sy + sh;
   Cbezier(sx, sy, cx, cy, cx, sy, ex, ey);
   coordpoint(sx, sy, dotsize, shapecolor);
   coordpoint(cx, cy, dotsize, shapecolor);
   coordpoint(cx, sy, dotsize, shapecolor);
   coordpoint(ex, ey, dotsize, shapecolor);

   sy -= (sh * spacing * 1.5);
   Image(sx, sy, 100, 100, "starx.jpg");

   End();
}

// rotext draws text, rotated around the center of the screen, progressively faded
void rotext(int w, int h, int n, char *s) {
   VGfloat fade = (100.0 / (VGfloat) n) / 100.0;
   VGfloat deg = 360.0 / n;
   VGfloat x = w / 2, y = h / 2;
   VGfloat alpha = 1.0;   // start solid
   int i, size = w / 8;

   Start(w, h);
   Background(0, 0, 0);
   Translate(x, y);
   for (i = 0; i < n; i++) {
      Fill(255, 255, 255, alpha);
      Text(0, 0, s, SerifTypeface, size);
      alpha -= fade;               // fade
      size += n;               // enlarge
      Rotate(deg);
   }
   End();
}

// rseed seeds the random number generator from the random device
void rseed(void) {
   unsigned char d[sizeof(long int)];
   long int s;
   int fd;

   // read bytes from the random device,
   // pack them into a long int.
   fd = open("/dev/urandom", O_RDONLY);
   if (fd < 0) {
      srand48(1);
      return;
   }
   read(fd, (void *)d, (size_t) sizeof(long int));
   s = d[3] | (d[2] << 8) | (d[1] << 16) | (d[0] << 24);
   srand48(s);
   close(fd);
}

// rshapes draws shapes with random colors, strokes, and sizes.
void rshapes(int width, int height, int n) {
   int i, j, np = 10;
   VGfloat sx, sy, cx, cy, px, py, ex, ey, pox, poy;
   VGfloat polyx[np], polyy[np];
   rseed();
   Start(width, height);
   for (i = 0; i < n; i++) {
      Fill(randcolor(), randcolor(), randcolor(), drand48());
      Ellipse(randf(width), randf(height), randf(200), randf(100));
      Circle(randf(width), randf(height), randf(100));
      Rect(randf(width), randf(height), randf(200), randf(100));
      Arc(randf(width), randf(height), randf(200), randf(200), randf(360), randf(360));

      sx = randf(width);
      sy = randf(height);
      Stroke(randcolor(), randcolor(), randcolor(), 1);
      StrokeWidth(randf(5));
      Line(sx, sy, sx + randf(200), sy + randf(100));
      StrokeWidth(0);

      sx = randf(width);
      sy = randf(height);
      ex = sx + randf(200);
      ey = sy;
      cx = sx + ((ex - sx) / 2.0);
      cy = sy + randf(100);
      Qbezier(sx, sy, cx, cy, ex, ey);

      sx = randf(width);
      sy = randf(height);
      ex = sx + randf(200);
      ey = sy;
      cx = sx + ((ex - sx) / 2.0);
      cy = sy + randf(100);
      px = cx;
      py = sy - randf(100);
      Cbezier(sx, sy, cx, cy, px, py, ex, ey);

      pox = randf(width);
      poy = randf(height);
      for (j = 0; j < np; j++) {
         polyx[j] = pox + randf(200);
         polyy[j] = poy + randf(100);
      }
      Polygon(polyx, polyy, np);

      pox = randf(width);
      poy = randf(height);
      for (j = 0; j < np; j++) {
         polyx[j] = pox + randf(200);
         polyy[j] = poy + randf(100);
      }
      Polyline(polyx, polyy, np);
   }
   Fill(128, 0, 0, 1);
   Text(20, 20, "OpenVG on the Raspberry Pi", SansTypeface, 32);
   End();
}

// sunearth shows the relative sizes of the sun and the earth
void sunearth(int w, int h) {
   VGfloat sun, earth, x, y;
   int i;

   rseed();
   Start(w, h);
   Background(0, 0, 0);
   Fill(255, 255, 255, 1);
   for (i = 0; i < w / 4; i++) {
      x = randf(w);
      y = randf(h);
      Circle(x, y, 2);
   }
   earth = (VGfloat) w *0.010;
   sun = earth * 109;
   Fill(0, 0, 255, 1);
   Circle(w / 3, h - (h / 10), earth);
   Fill(255, 255, 224, 1);
   Circle(w, 0, sun);
   End();
}

// advert is an ad for the package
void advert(int w, int h) {
   VGfloat y = h/4;
   int fontsize = (w * 4) / 100;
   char *s = "github.com/ajstarks/openvg";
   char *a = "ajstarks@gmail.com";
   int imw = 110, imh = 110, rw = w/4, rh = (rw*2/3);
   VGfloat tw = TextWidth(s, SansTypeface, fontsize);

   Start(w, h);
   makepi((w/2) - (rw/2), h/2, rw, rh);
   Fill(128, 0, 0, 1);
   Text(w / 2 - (tw / 2), y - (fontsize / 4), s, SansTypeface, fontsize);
   y -= 100;
   tw = TextWidth(a, SansTypeface, fontsize / 3);
   Fill(128, 128, 128, 1);
   Text(w / 2 - (tw / 2), y, a, SansTypeface, fontsize / 3);
   Image((w / 2) - (imw / 2), 20, imw, imh, "starx.jpg");
   End();
}

// demo shows a timed demonstration
void demo(int w, int h, int sec) {
   refcard(w, h);
   sleep(sec);
   rshapes(w, h, 50);
   sleep(sec);
   testpattern(w, h, "OpenVG on RasPi");
   sleep(sec);
   imagetable(w, h);
   sleep(sec);
   rotext(w, h, 30, "Raspi");
   sleep(sec);
   tb(w, h);
   sleep(sec);
   fontrange(w, h);
   sleep(sec);
   sunearth(w, h);
   sleep(sec);
   raspi(w, h, "The Raspberry Pi");
   sleep(sec);
   gradient(w,h);
   sleep(sec);
   advert(w, h);
}

// wait for a specific character
void waituntil(int endchar) {
    int key;

    for (;;) {
        key = getchar();
        if (key == endchar || key == '\n') {
            break;
        }
    }
}
// main initializes the system and shows the picture.
// Exit and clean up when you hit [RETURN].
int main(int argc, char **argv) {
   int w, h, n;
   char *usage =
       "%s [command]\n\tdemo sec\n\tastro\n\ttest ...\n\trand n\n\trotate n ...\n\timage\n\ttext\n\tfontsize\n\traspi\n\tadvert\n\tgradient\n";
   char *progname = argv[0];
   saveterm();
   init(&w, &h);
   rawterm();
   switch (argc) {
   case 2:
      if (strncmp(argv[1], "image", 5) == 0) {
         imagetable(w, h);
      } else if (strncmp(argv[1], "text", 4) == 0) {
         tb(w, h);
      } else if (strncmp(argv[1], "astro", 5) == 0) {
         sunearth(w, h);
      } else if (strncmp(argv[1], "fontsize", 8) == 0) {
         fontrange(w, h);
      } else if (strncmp(argv[1], "advert", 6) == 0) {
         advert(w,h);
      } else if (strncmp(argv[1], "raspi", 5) == 0) {
         raspi(w, h, "The Raspberry Pi");
      } else if (strncmp(argv[1], "gradient", 8) == 0) {
         gradient(w,h);
      } else {
         restoreterm();
         fprintf(stderr, usage, progname);
         return 1;
      }
      break;
   case 3:
      n = atoi(argv[2]);
      if (strncmp(argv[1], "demo", 4) == 0) {
         if (n < 1 || n > 30) {
            n = 5;
         }
         demo(w, h, n);
      } else if (strncmp(argv[1], "rand", 4) == 0) {
         if (n < 1 || n > 1000) {
            n = 100;
         }
         rshapes(w, h, n);
      } else if (strncmp(argv[1], "test", 4) == 0) {
         testpattern(w, h, argv[2]);
      } else {
         restoreterm();
         fprintf(stderr, usage, progname);
         return 1;
      }
      break;

   case 4:
      if (strncmp(argv[1], "rotate", 6) == 0) {
         rotext(w, h, atoi(argv[2]), argv[3]);
      } else {
         restoreterm();
         fprintf(stderr, usage, progname);
         return 1;
      }
      break;

   default:
      refcard(w, h);
   }
   waituntil(0x1b);
   restoreterm();
   finish();
   return 0;
}

#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
Paeryn
Posts: 3174
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: HDMI graphic lib: openvg syntax questions

Thu Nov 05, 2015 3:31 pm

I'll have a look when I get home. Strange the fontinfo files aren't there, it should have all the original files... maybe I made a mistake setting git up (I've only ever used it to download, this is the first time uploading to it).
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: HDMI graphic lib: openvg syntax questions

Thu Nov 05, 2015 3:35 pm

another mistake is with this different former program:
it gives a compile error unless I #include shapes_plus,
and after I did, it stops and waits for a key press after the final test (graph) before showing the benchmark results... strangely ...
I don't see why or if I messed anything up accidentally...?

Code: Select all

// HaWe Brickbench
// benchmark test for NXT/EV3 and similar Micro Controllers
// PL: GCC, Raspi, Raspbian  Linux
// Autor: (C) Helmut Wunder 2013,2014
// ported to Raspi  by "HaWe"
//
// freie Verwendung für private Zwecke
// für kommerzielle Zwecke nur nach schriftlicher Genehmigung durch den Autor.
// protected under the friendly Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// http://creativecommons.org/licenses/by-nc-sa/3.0/
// version 1.09.005  25.10.2015


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>


#include <stdint.h>
#include <time.h>
#include <sys/time.h>

#include "VG/openvg.h"
#include "VG/vgu.h"
#include "fontinfo.h"
#include "shapes.h"
#include "shapes_plus.h"






unsigned long runtime[8];

int a[500], b[500], c[500], t[500];

uint32_t timer()
{
   struct timeval now;
   uint32_t ticks;
   gettimeofday(&now, NULL);
   ticks=now.tv_sec*1000+now.tv_usec/1000;
   return(ticks);
}


//--------------------------------------------
// Mersenne Twister
//--------------------------------------------

unsigned long randM(void) {
   const int M = 7;
   const unsigned long A[2] = { 0, 0x8ebfd028 };

   static unsigned long y[25];
   static int index = 25+1;

   if (index >= 25) {
     int k;
     if (index > 25) {
        unsigned long r = 9, s = 3402;
        for (k=0 ; k<25 ; ++k) {
          r = 509845221 * r + 3;
          s *= s + 1;
          y[k] = s + (r >> 10);
        }
     }
     for (k=0 ; k<25-M ; ++k)
        y[k] = y[k+M] ^ (y[k] >> 1) ^ A[y[k] & 1];
     for (; k<25 ; ++k)
        y[k] = y[k+(M-25)] ^ (y[k] >> 1) ^ A[y[k] & 1];
     index = 0;
   }

   unsigned long e = y[index++];
   e ^= (e << 7) & 0x2b5b2500;
   e ^= (e << 15) & 0xdb8b0000;
   e ^= (e >> 16);
   return e;
}


//--------------------------------------------
// Matrix Algebra
//--------------------------------------------

// matrix * matrix multiplication (matrix product)
 
 void MatrixMatrixMult(int N, int M, int K, double *A, double *B, double *C) {
   int i, j, s;
   for (i = 0; i < N; ++i) {
      for (j = 0; j < K; ++j) {
         C[i*K+j] = 0;
         for (s = 0; s < M; ++s) {
            C[i*K+j] = C[i*K+j] + A[i*N+s] * B[s*M+j];
         }
      }
   }
}


// matrix determinant

double MatrixDet(int N, double A[]) {
   int i, j, i_count, j_count, count = 0;
   double Asub[N - 1][N - 1], det = 0;

   if (N == 1)
      return *A;
   if (N == 2)
      return ((*A) * (*(A+1+1*N)) - (*(A+1*N)) * (*(A+1)));

   for (count = 0; count < N; count++) {
      i_count = 0;
      for (i = 1; i < N; i++) {
         j_count = 0;
         for (j = 0; j < N; j++) {
            if (j == count)
               continue;
            Asub[i_count][j_count] = *(A+i+j*N);
            j_count++;
         }
         i_count++;
      }
      det += pow(-1, count) * A[0+count*N] * MatrixDet(N - 1, &Asub[0][0]);
   }
   return det;
}





//--------------------------------------------
// shell sort
//--------------------------------------------

void shellsort(int size, int* A)
{
  int i, j, increment;
  int temp;
  increment = size / 2;

  while (increment > 0) {
    for (i = increment; i < size; i++) {
      j = i;
      temp = A[i];
      while ((j >= increment) && (A[j-increment] > temp)) {
        A[j] = A[j - increment];
        j = j - increment;
      }
      A[j] = temp;
    }

    if (increment == 2)
       increment = 1;
    else
       increment = (unsigned int) (increment / 2.2);
  }
}

//--------------------------------------------
// gnu quick sort
// (0ptional)
//--------------------------------------------

int compare_int (const int *a, const int *b)
{
  int  temp = *a - *b;

  if (temp > 0)          return  1;
  else if (temp < 0)     return -1;
  else                   return  0;
}

// gnu qsort:
// void qsort (void *a , size_a count, size_a size, compare_function)
// gnu qsort call for a[500] array of int:
// qsort (a , 500, sizeof(a), compare_int)



//--------------------------------------------
// benchmark test procedures
//--------------------------------------------


int test_Int_Add() {
   int i=1, j=11, k=112, l=1111, m=11111, n=-1, o=-11, p=-111, q=-1112, r=-11111;
   int x;
   volatile long s=0;
   for(x=0;x<10000;++x) {
     s+=i; s+=j; s+=k; s+=l; s+=m; s+=n; s+=o; s+=p; s+=q; s+=r;
   }
   return s;
}



long test_Int_Mult() {
  int x,y;
  volatile long s;

  for(y=0;y<2000;++y) {
    s=1;
    for(x=1;x<=13;++x) { s*=x;}
    for(x=13;x>0;--x) { s/=x;}

  }
  return s;
}


#define PI  M_PI


double test_float_math() {

  volatile double s=PI;
  int y;

  for(y=0;y<1000;++y) {
     s*=sqrt(s);
     s=sin(s);
     s=exp(s);
     s*=s;
  }
  return s;
}


long test_rand_MT(){
  volatile unsigned long s;
  int y;

  for(y=0;y<5000;++y) {
     s=randM()%10001;
  }
  return s;
}







double test_matrix_math() {
  int x;

  double A[2][2], B[2][2], C[2][2];
  double S[3][3], T[3][3];
  unsigned long s;

  for(x=0;x<250;++x) {

    A[0][0]=1;   A[0][1]=3;
    A[1][0]=2;   A[1][1]=4;

    B[0][0]=10;  B[0][1]=30;
    B[1][0]=20;  B[1][1]=40;

    MatrixMatrixMult(2, 2, 2, A[0], B[0], C[0]); // <<<<<<<<<<<<<<<<<<<

    A[0][0]=1;   A[0][1]=3;
    A[1][0]=2;   A[1][1]=4;
    MatrixDet(2, A[0]);                          // <<<<<<<<<<<<<<<<<<<

    S[0][0]=1;   S[0][1]=4;  S[0][2]=7;
    S[1][0]=2;   S[1][1]=5;  S[1][2]=8;
    S[2][0]=3;   S[2][1]=6;  S[2][2]=9;

    MatrixDet(3, S[0]);                          // <<<<<<<<<<<<<<<<<<<

  }

  s=(S[0][0]*S[1][1]*S[2][2]);
  return s;
}





// for array copy using void *memcpy(void *dest, const void *src, size_t n);

long test_Sort(){
  unsigned long s;
  int y;
  int t[500];

  for(y=0;y<30;++y) {
    memcpy(t, a, sizeof(a));
    shellsort(500, t);
   
    memcpy(t, a, sizeof(b));
    shellsort(500, t);
   
    memcpy(t, a, sizeof(c));
    shellsort(500, t);
  }

  return y;
}




long test_TextOut(){


  int  y=77;
  char buf[120];

  for(y=0;y<20;++y) {
	  
    Background(0, 0, 0);                    // Black background
    
    //Text(x, y, buf, SerifTypeface, 20); 
    Fill(255, 255, 255, 1);                 // White text
    
    sprintf (buf, "%3d %4d  int_Add",    0, 1000); Text( 20, 200- 20, buf, SerifTypeface, 20); End();
    sprintf (buf, "%3d %4d  int_Mult",   1, 1010); Text( 20, 200- 40, buf, SerifTypeface, 20); End(); 
    sprintf (buf, "%3d %4d  float_op",   2, 1020); Text( 20, 200- 60, buf, SerifTypeface, 20); End();
    sprintf (buf, "%3d %4d  randomize",  3, 1030); Text( 20, 200- 80, buf, SerifTypeface, 20); End();
    sprintf (buf, "%3d %4d  matrx_algb", 4, 1040); Text( 20, 200-100, buf, SerifTypeface, 20); End();
    sprintf (buf, "%3d %4d  arr_sort",   5, 1050); Text( 20, 200-120, buf, SerifTypeface, 20); End();
    sprintf (buf, "%3d %4d  displ_txt",  6, 1060); Text( 20, 200-140, buf, SerifTypeface, 20); End();
    sprintf (buf, "%3d %4d  testing...", 7, 1070); Text( 20, 200-160, buf, SerifTypeface, 20); End();

  }

  return y;
}



long test_graphics(){
    int y=0;
     
    for(y=0;y<100;++y) {
        ClearWindow();      // Colour and size are remembered from the
                            // ClearWindowRGBA() call at the start of the program
        Stroke(255, 255, 255, 1);  // Set these at the start, no need to
        Fill(255,255,255, 1);      // keep calling them if colour hasn't changed

        End();
        
        CircleOutline(50, 40, 10);       // circles
        End();

        Circle(30, 24, 10);
        End();

        Line(10, 10, 60, 60);            // just 2 intersecting lines
        End();
        Line(50, 20, 90, 70);
        End();

        RectOutline(20, 20, 40, 40);     // rectangles
        End();
      
        Rect(65, 25, 20, 30);
        End();

        EllipseOutline(70, 30, 15, 20);  // ellipse
        End();
    }
    return y;
}




inline void displayValues() {

  char buf[120];
  
    ClearWindow();      // Colour and size are remembered from the
                        // ClearWindowRGBA() call at the start of the program

    sprintf (buf, "%3d %7ld  int_Add",    0, runtime[0]); printf(buf); printf("\n");
    sprintf (buf, "%3d %7ld  int_Mult",   1, runtime[1]); printf(buf); printf("\n");
    sprintf (buf, "%3d %7ld  float_op",   2, runtime[2]); printf(buf); printf("\n");
    sprintf (buf, "%3d %7ld  randomize",  3, runtime[3]); printf(buf); printf("\n");
    sprintf (buf, "%3d %7ld  matrx_algb", 4, runtime[4]); printf(buf); printf("\n");
    sprintf (buf, "%3d %7ld  arr_sort",   5, runtime[5]); printf(buf); printf("\n");
    sprintf (buf, "%3d %7ld  displ_txt",  6, runtime[6]); printf(buf); printf("\n");
    sprintf (buf, "%3d %7ld  graphics",   7, runtime[7]); printf(buf); printf("\n");
}



int main(){

  unsigned long time0, x, y;
  float s;
  char  buf[120];
  int width, height;
  char str[3];

  init(&width, &height);                  // Graphics initialization
  Start(width, height);                   // Start the picture
    
  ClearWindowRGB(width, height, 0, 0, 0);
  HideWindow();       // Hide  the picture    

  printf("hw brickbench"); printf("\n");
  printf("initializing..."); printf("\n");
 

  for(y=0;y<500;++y) {
    a[y]=randM()%30000; b[y]=randM()%30000; c[y]=randM()%30000;
  }
  
  time0= timer();
  s=test_Int_Add();
  runtime[0]=timer()-time0;
  sprintf (buf, "%3d %7ld  int_Add",    0, runtime[0]);  printf(buf); printf("\n");

  time0=timer();
  s=test_Int_Mult();
  runtime[1]=timer()-time0;
  sprintf (buf, "%3d %7ld  int_Mult",   0, runtime[1]);  printf(buf); printf("\n");

  time0=timer();
  s=test_float_math();
  runtime[2]=timer()-time0;
  sprintf (buf, "%3d %7ld  float_op",   0, runtime[2]);  printf(buf); printf("\n");

  time0=timer();
  s=test_rand_MT();
  runtime[3]=timer()-time0;
  sprintf (buf, "%3d %7ld  randomize",  0, runtime[3]);  printf(buf); printf("\n");

  time0=timer();
  s=test_matrix_math();
  runtime[4]=timer()-time0;
  sprintf (buf, "%3d %7ld  matrx_algb", 0, runtime[4]);  printf(buf); printf("\n");


  time0=timer();
  s=test_Sort();
  runtime[5]=timer()-time0;
  sprintf (buf, "%3d %7ld  arr_sort",   0, runtime[5]);  printf(buf); printf("\n");
  
  time0=timer();
  s=test_TextOut();
  runtime[6]=timer()-time0;

  time0=timer();
  time0=timer();
  s=test_graphics();
  runtime[7]=timer()-time0;

  //HideWindow();       // Hide  the picture   

  y=0;
  for(x=0;x<8;++x) {y+= runtime[x];}
  printf("\n"); 
  printf("\n");
  

   
  displayValues();

  sprintf (buf, "gesamt ms: %ld ", y);           printf(buf); printf("\n");
  sprintf (buf, "benchmark: %ld ", 50000000/y ); printf(buf); printf("\n");



  fgets(str, 2, stdin);                   // look at the pic, end with [RETURN]
  finish();                               // Graphics cleanup
  exit(0);
}


#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

Return to “C/C++”