Add two gpanel examples: color palette and speed test.

This commit is contained in:
Serge Vakulenko
2015-10-09 17:44:46 -07:00
parent 8b5e30f416
commit a78f66c220
3 changed files with 208 additions and 1 deletions

View File

@@ -1,6 +1,6 @@
CC = cc
LIBS = -lgpanel
PROG = tft tftetris pixel line rect fill circle font
PROG = tft tftetris pixel line rect fill circle font color speed
FONTS = 5x7.o 6x9.o digits20.o digits32.o lucidasans11.o lucidasans15.o \
lucidasans7.o lucidasans9.o verdana7.o
@@ -31,6 +31,12 @@ fill: fill.c
circle: circle.c
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) $? $(LIBS)
color: color.c
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) $? $(LIBS)
speed: speed.c lucidasans15.o
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) speed.c lucidasans15.o $(LIBS)
font: font.c $(FONTS)
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) font.c $(FONTS) $(LIBS)

View File

@@ -0,0 +1,65 @@
/*
* Display a color palette.
* Assume RGB 5-6-5 color format.
* Color is a 16-bit value: rrrrrggggggbbbbb, where rrrrr,
* gggggg and bbbbb are red, green and blue components.
*/
#include <stdio.h>
#include <sys/gpanel.h>
int xsize, ysize;
int scale_red(int x, int y)
{
int r;
r = 32 * y * (xsize-x-1) / xsize / ysize;
if (r > 31)
r = 31;
return r;
}
int scale_green(int x, int y)
{
int g;
g = 64 * x / xsize;
if (g > 63)
g = 63;
return g;
}
int scale_blue(int x, int y)
{
int b;
b = 32 * (ysize-y-1) * (xsize-x-1) / xsize / ysize;
if (b > 31)
b = 31;
return b;
}
int main()
{
char *devname = "/dev/tft0";
int x, y, r, g, b, color;
if (gpanel_open(devname) < 0) {
printf("Cannot open %s\n", devname);
exit(-1);
}
gpanel_clear(0, &xsize, &ysize);
printf("Screen size %u x %u.\n", xsize, ysize);
printf("Display color palette.\n");
for (y=0; y<ysize; y++) {
for (x=0; x<xsize; x++) {
r = scale_red(x, y);
g = scale_green(x, y);
b = scale_blue(x, y);
color = r << 11 | g << 5 | b;
gpanel_pixel(color, x, y);
}
}
return 0;
}

View File

@@ -0,0 +1,136 @@
/*
* Test the gpanel functions for speed.
*/
#include <stdio.h>
#include <sys/gpanel.h>
#include <sys/time.h>
#define NPIXELS 100000
#define NLINES 3000
#define NRECT 1000
#define NCIRCLES 1000
#define NCHARS 10000
extern const struct gpanel_font_t font_lucidasans15;
int xsize, ysize;
/*
* Get current time in milliseconds.
*/
unsigned current_msec()
{
struct timeval t;
gettimeofday(&t, 0);
return t.tv_sec * 1000 + t.tv_usec / 1000;
}
/*
* Compute elapsed time in milliseconds.
*/
unsigned elapsed_msec(unsigned t0)
{
struct timeval t1;
unsigned msec;
gettimeofday (&t1, 0);
msec = t1.tv_sec * 1000 + t1.tv_usec / 1000;
msec -= t0;
if (msec < 1)
msec = 1;
return msec;
}
int main()
{
char *devname = "/dev/tft0";
int x, y, x1, y1, color, i, r, sym;
unsigned t0, msec;
if (gpanel_open(devname) < 0) {
printf("Cannot open %s\n", devname);
exit(-1);
}
gpanel_clear(0, &xsize, &ysize);
printf("Screen size %u x %u.\n", xsize, ysize);
/* Use fixed seed, for repeatability. */
srand(1);
printf("Graphics speed:\n");
sync();
usleep(500000);
/*
* Pixels.
*/
t0 = current_msec();
for (i=0; i<NPIXELS; i++) {
x = rand() % xsize;
y = rand() % ysize;
color = rand() << 1;
gpanel_pixel(color, x, y);
}
msec = elapsed_msec(t0);
printf (" %u pixels/second\n", NPIXELS*1000U / msec);
/*
* Lines.
*/
t0 = current_msec();
for (i=0; i<NLINES; i++) {
x = rand() % xsize;
y = rand() % ysize;
x1 = rand() % xsize;
y1 = rand() % ysize;
color = rand() << 1;
gpanel_line(color, x, y, x1, y1);
}
msec = elapsed_msec(t0);
printf (" %u lines/second\n", NLINES*1000U / msec);
/*
* Filled rectangles.
*/
t0 = current_msec();
for (i=0; i<NRECT; i++) {
x = rand() % xsize;
y = rand() % ysize;
x1 = rand() % xsize;
y1 = rand() % ysize;
color = rand() << 1;
gpanel_fill(color, x, y, x1, y1);
}
msec = elapsed_msec(t0);
printf (" %u rectangles/second\n", NRECT*1000U / msec);
/*
* Circles.
*/
t0 = current_msec();
for (i=0; i<NCIRCLES; i++) {
x = rand() % xsize;
y = rand() % ysize;
r = rand() % ysize;
color = rand() << 1;
gpanel_circle(color, x, y, r);
}
msec = elapsed_msec(t0);
printf (" %u circles/second\n", NCIRCLES*1000U / msec);
/*
* Characters.
*/
t0 = current_msec();
for (i=0; i<NCHARS; i++) {
x = rand() % xsize;
y = rand() % ysize;
sym = '!' + rand() % ('~' - ' ');
color = rand() << 1;
gpanel_char(&font_lucidasans15, color, -1, x, y, sym);
}
msec = elapsed_msec(t0);
printf (" %u characters/second\n", NCHARS*1000U / msec);
return 0;
}