There is no line drawing functions other than DrawLine and DrawLines in
dfb. DrawLines might help to you. Additionally, I had to write my own
draw poly and drawarc routines. I have taken most of the code from
gpsdrive and dfb packages and done some minor changes. You can find my
routines within attached file..
Hope it helps.
BR
Aykut KOÇAK
Nathanael D. Noblet wrote:
Is there no DrawArc type functions in DirectFB, or am I just blind?
/***************************************************************************
* Copyright (C) 2005 by root *
* [EMAIL PROTECTED] *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <directfb.h>
#include <shapefil.h>
#include "dfb_utils.h"
extern long int lrint();
IDirectFB *dfb=NULL;
IDirectFBDisplayLayer *layer=NULL;
IDirectFBSurface *primary=NULL;
IDirectFBFont *font=NULL;
IDirectFBVideoProvider *provider=NULL;
IDirectFBEventBuffer *events=NULL;
IDirectFBWindow *window=NULL;
DFBSurfaceDescription dsc;
DFBFontDescription fontdsc;
int ScreenW, ScreenH;
#define myabs( x,y ) (( x>y) ? x-y:y-x )
static unsigned int rand_pool = 0x12345678;
static unsigned int rand_add = 0x87654321;
long cl_util_time_ms( void )
{
struct timeval tv;
long ret;
gettimeofday(&tv, ( struct timezone *) NULL);
ret = tv.tv_sec * 1000 + tv.tv_usec / 1000;
return ret;
}
unsigned int myrand()
{
rand_pool ^= ((rand_pool << 7) | (rand_pool >> 25));
rand_pool += rand_add;
rand_add += rand_pool;
return rand_pool;
}
void cl_load_image( IDirectFBSurface** in, const char* file )
{
IDirectFBImageProvider *image;
char filename[255];
sprintf( filename, "%s", file );
dfb->CreateImageProvider( dfb, filename, &image );
image->RenderTo( image, (*in), NULL );
(*in)->Flip( (*in), NULL, DSFLIP_ONSYNC );
image->RenderTo( image, (*in), NULL );
image->Release( image );
}
int cl_read( int fd, char* ptr, long sz, long timeout )
{
long cnt;
int sta;
cnt = cl_util_time_ms() + timeout;
do
{
sta = read( fd, ptr, sz );
}
while ( ( cl_util_time_ms() < cnt ) && ( sta < 0 ) );
return sta;
}
void cl_create_window( IDirectFBWindow** in, IDirectFBSurface** in2, int x, int y, int w, int h )
{
DFBWindowDescription desc;
desc.flags = ( DWDESC_POSX | DWDESC_POSY |
DWDESC_WIDTH | DWDESC_HEIGHT ); //| DWDESC_CAPS );
desc.posx = x;
desc.posy = y;
desc.width = w;
desc.height = h;
// desc.caps = DWCAPS_ALPHACHANNEL;
DFBCHECK( layer->CreateWindow( layer, &desc, in ) );
(*in)->GetSurface( *in, in2 );
(*in)->SetOpacity( *in, 0xff );
// (*in)->SetColorKey( *in, 0x00, 0x00, 0x00 );
// (*in)->SetOptions( *in, DWOP_COLORKEYING ); //| DWOP_ALPHACHANNEL );
}
void cl_destroy_window( IDirectFBWindow** in )
{
(*in)->Close( *in );
(*in)->Destroy( *in );
}
int dfb_init( int argc, char* argv[] )
{
fprintf( stderr, "Starting DirectFB\t: " );
DFBCHECK( DirectFBInit( &argc, &argv ) );
DFBCHECK( DirectFBCreate( &dfb ) );
DFBCHECK( dfb->GetDisplayLayer( dfb, 0, &layer ) ); // 0 primary layer...
DFBCHECK( layer->SetCooperativeLevel( layer, DLSCL_EXCLUSIVE ) );
DFBCHECK( layer->GetSurface(layer, &primary) );
DFBCHECK( primary->GetSize( primary, &ScreenW, &ScreenH ) );
printf("Screen Res. = %04d,%04d\n", ScreenW, ScreenH );
DFBCHECK( primary->Clear( primary, 0,0,0,0 ) );
DFBCHECK( primary->Flip( primary, NULL, DSFLIP_ONSYNC) );
DFBCHECK( primary->Clear( primary, 0,0,0,0 ) );
DFBCHECK( primary->SetColor( primary, 0xff, 0xff, 0xff, 0xff ) );
DFBCHECK( dfb->CreateInputEventBuffer( dfb, DICAPS_KEYS, DFB_TRUE, &events ) );
#ifdef DEBUG
fprintf( stderr, "%s() : OK\n", __FUNCTION__ );
#endif
/* eveything is fine, bye!: */
return 0;
}
void dfb_release( void )
{
events->Reset( events );
DFBCHECK( events->Release( events ) );
DFBCHECK( primary->Release( primary ) );
DFBCHECK( layer->Release( layer ) );
assert( dfb != NULL );
DFBCHECK( dfb->Release( dfb ) );
fprintf( stderr, "DirectFB exited\n\n" );
}
int dfb_keypress( void )
{
static DFBEvent evnt;
// events->Reset( events );
// events->WaitForEvent( events );
if ( events->WaitForEventWithTimeout ( events,0,250) == DFB_OK )
{
events->GetEvent( events, &evnt );
// events->Reset( events );
if ( evnt.clazz != DFEC_INPUT )
return -1;
if ( evnt.input.type != DIET_KEYRELEASE )
return -1;
return evnt.input.key_id;
}
return -1;
}
DFBResult df_drawPixel( IDirectFBSurface *surface, int x, int y, __u8 r, __u8 g, __u8 b )
{
DFBResult ret;
void *data;
int pitch;
int width, height;
DFBSurfacePixelFormat format;
uint8_t *dst8;
uint16_t *dst16;
uint32_t *dst32;
surface->GetSize( surface, &width, &height );
surface->GetPixelFormat( surface, &format );
fprintf ( stderr, "pixel : %x\n", format );
ret = surface->Lock( surface, DSLF_WRITE, &data, &pitch );
if (ret)
{
DirectFBError( "IDirectFBSurface::Lock() failed", ret );
return ret;
}
switch (format)
{
case DSPF_RGB32:
dst32 = data + y * pitch;
dst32[x] = PIXEL_RGB32( r, g, b );
break;
case DSPF_RGB16:
dst16 = data + y * pitch;
dst16[x] = PIXEL_RGB16( r, g, b );
break;
case DSPF_ARGB1555:
dst16 = data + y * pitch;
dst16[x] = PIXEL_ARGB1555( 0xff, r, g, b );
break;
case DSPF_ARGB:
dst32 = data + y * pitch;
dst32[x] = PIXEL_ARGB( 0xff, r, g, b );
break;
case DSPF_RGB332:
dst8 = data + y * pitch;
dst8[x] = PIXEL_RGB332( r, g, b );
break;
default:
fprintf( stderr, "Unhandled pixel format 0x%08x!\n", format );
break;
}
surface->Unlock( surface );
return DFB_OK;
}
static int gfxPrimitivesCompareInt(const void *a, const void *b)
{
return (*(const int *) a) - (*(const int *) b);
}
static void hLine( IDirectFBSurface* surface, int x1, int x2, int y )
{
assert( surface != NULL );
if ( x1 < 0 )
x1=0;
if ( x2 < 0 )
x2=0;
if ( y < 0 )
y=0;
DFBCHECK( surface->DrawLine( surface, x1, y, x2, y ) );
}
void df_drawArc( IDirectFBSurface* surface, int * vx, int * vy, int n)
{
int i;
assert( surface != NULL );
assert( vx != NULL );
assert( vy != NULL );
if ( n < 2 )
return;
for ( i=0;i< (n - 1);i++)
{
surface->DrawLine( surface, vx[i],vy[i],vx[i+1],vy[i+1] );
}
}
void df_drawPoly( IDirectFBSurface* surface, int * vx, int * vy, int n)
{
int result;
int i;
int y;
int miny, maxy;
int x1, y1;
int x2, y2;
int ind1, ind2;
int ints;
int *gfxPrimitivesPolyInts=NULL;
int gfxPrimitivesPolyAllocated;
/*
* Sanity check
*/
if (n < 3)
{
return ;
}
/*
* Allocate temp array, only grow array
*/
gfxPrimitivesPolyInts = (int *) malloc(sizeof(int) * n);
gfxPrimitivesPolyAllocated = n;
/*
* Determine Y maxima
*/
miny = vy[0];
maxy = vy[0];
for (i = 1; (i < n); i++)
{
if (vy[i] < miny)
{
miny = vy[i];
}
else if (vy[i] > maxy)
{
maxy = vy[i];
}
}
/*
* Draw, scanning y
*/
result = 0;
for (y = miny; (y <= maxy); y++)
{
ints = 0;
for (i = 0; (i < n); i++)
{
if (!i)
{
ind1 = n - 1;
ind2 = 0;
}
else
{
ind1 = i - 1;
ind2 = i;
}
y1 = vy[ind1];
y2 = vy[ind2];
if (y1 < y2)
{
x1 = vx[ind1];
x2 = vx[ind2];
}
else if (y1 > y2)
{
y2 = vy[ind1];
y1 = vy[ind2];
x2 = vx[ind1];
x1 = vx[ind2];
}
else
{
continue;
}
if ((y >= y1) && (y < y2))
{
gfxPrimitivesPolyInts[ints++] = (y - y1) * (x2 - x1) / (y2 - y1) + x1;
}
else if ((y == maxy) && (y > y1) && (y <= y2))
{
gfxPrimitivesPolyInts[ints++] = (y - y1) * (x2 - x1) / (y2 - y1) + x1;
}
}
qsort(gfxPrimitivesPolyInts, ints, sizeof(int), gfxPrimitivesCompareInt);
for (i = 0; (i < ints); i += 2)
{
hLine( surface, gfxPrimitivesPolyInts[i], gfxPrimitivesPolyInts[i + 1], y);
}
}
free( gfxPrimitivesPolyInts );
}
IDirectFB* df_get_dfb( void )
{
return dfb;
}
IDirectFBDisplayLayer* df_get_layer( void )
{
return layer;
}
IDirectFBSurface* df_get_surface( void )
{
return primary;
}
IDirectFBFont* df_get_font( void)
{
return font;
}
IDirectFBEventBuffer* df_get_event( void )
{
return events;
}
IDirectFBWindow* df_get_window( void )
{
return window;
}
int df_get_screen_width( void )
{
return ScreenW;
}
int df_get_screen_height( void )
{
return ScreenH;
}
/***************************************************************************
* Copyright (C) 2005 by root *
* [EMAIL PROTECTED] *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef DFB_UTILS_H
#define DFB_UTILS_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <directfb.h>
#define BX 0
#define BY 1
#define BZ 2
#define BM 3
#define DFBCHECK(x...) \
{ \
err = x; \
if (err != DFB_OK) { \
fprintf( stderr, "%s <%d>:\n\t", __FILE__, __LINE__ ); \
DirectFBErrorFatal( #x, err ); \
} \
}
int err;
char dirname[512];
#define datadir( x ) { \
sprintf ( dirname, "%s%s", DATADIR, x);\
}
/* pixel packing */
#define PIXEL_RGB332(r,g,b) ( (((r)&0xE0) ) | \
(((g)&0xE0) >> 3) | \
(((b)&0xC0) >> 6) )
#define PIXEL_ARGB1555(a,r,g,b)( (((a)&0x80) << 8) | \
(((r)&0xF8) << 7) | \
(((g)&0xF8) << 2) | \
(((b)&0xF8) >> 3) )
#define PIXEL_ARGB2554(a,r,g,b)( (((a)&0xC0) << 8) | \
(((r)&0xF8) << 6) | \
(((g)&0xF8) << 1) | \
(((b)&0xF0) >> 4) )
#define PIXEL_ARGB4444(a,r,g,b)( (((a)&0xF0) << 8) | \
(((r)&0xF0) << 4) | \
(((g)&0xF0) ) | \
(((b)&0xF0) >> 4) )
#define PIXEL_RGB16(r,g,b) ( (((r)&0xF8) << 8) | \
(((g)&0xFC) << 3) | \
(((b)&0xF8) >> 3) )
#define PIXEL_RGB32(r,g,b) ( ((r) << 16) | \
((g) << 8) | \
(b) )
#define PIXEL_ARGB(a,r,g,b) ( ((a) << 24) | \
((r) << 16) | \
((g) << 8) | \
(b) )
#define PIXEL_AiRGB(a,r,g,b) ( (((a) ^ 0xff) << 24) | \
((r) << 16) | \
((g) << 8) | \
(b) )
#ifdef WORDS_BIGENDIAN
#define PIXEL_YUY2(y,u,v) ( ((u) << 24) | \
((y) << 16) | \
((v) << 8) | \
(y) )
#define PIXEL_UYVY(y,u,v) ( ((y) << 24) | \
((u) << 16) | \
((y) << 8) | \
(v) )
#else /* little endian */
#define PIXEL_YUY2(y,u,v) ( ((v) << 24) | \
((y) << 16) | \
((u) << 8) | \
(y) )
#define PIXEL_UYVY(y,u,v) ( ((y) << 24) | \
((v) << 16) | \
((y) << 8) | \
(u) )
#endif
long cl_util_time_ms( void );
unsigned int myrand();
void cl_create_window( IDirectFBWindow** in, IDirectFBSurface** in2, int x, int y, int w, int h );
void cl_destroy_window( IDirectFBWindow** in );
void cl_load_image( IDirectFBSurface** in, const char* file );
int cl_read( int fd, char* ptr, long sz, long timeout );
int dfb_init( int argc, char* argv[] );
void dfb_release( void );
int dfb_keypress( void );
DFBResult df_drawPixel( IDirectFBSurface *surface, int x, int y, __u8 r, __u8 g, __u8 b );
void df_drawArc( IDirectFBSurface* surface, int * vx, int * vy, int n);
void df_drawPoly( IDirectFBSurface* surface, int * vx, int * vy, int n);
IDirectFB* df_get_dfb( void );
IDirectFBDisplayLayer* df_get_layer( void );
IDirectFBSurface* df_get_surface( void );
IDirectFBFont* df_get_font( void);
IDirectFBEventBuffer* df_get_event( void );
IDirectFBWindow* df_get_window( void );
int df_get_screen_width( void );
int df_get_screen_height( void );
#endif
_______________________________________________
directfb-users mailing list
[email protected]
http://mail.directfb.org/cgi-bin/mailman/listinfo/directfb-users