Skip to content

Instantly share code, notes, and snippets.

@edufelipe
Created August 14, 2012 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save edufelipe/3351284 to your computer and use it in GitHub Desktop.
Save edufelipe/3351284 to your computer and use it in GitHub Desktop.
Write the PID on a semi-transparent DirectFB Window.
#include <directfb.h>
#include <stdio.h>
#include <unistd.h>
#define DFBCHECK(x...) \
do { \
DFBResult err = x; \
if (err != DFB_OK) { \
printf("%s <%i>:", __FILE__, __LINE__); \
DirectFBErrorFatal( #x, err ); \
} \
} while (0)
int main(int argc, char **argv) {
// Initialize DirectFB.
DFBCHECK( DirectFBInit(&argc, &argv) );
// Get the main interface.
IDirectFB *dfb = NULL;
DFBCHECK( DirectFBCreate(&dfb) );
// Get the primary layer.
IDirectFBDisplayLayer *layer = NULL;
DFBCHECK( dfb->GetDisplayLayer(dfb, DLID_PRIMARY, &layer) );
DFBCHECK( layer->SetCooperativeLevel(layer, DLSCL_ADMINISTRATIVE) );
// Get the surface of the primary layer.
IDirectFBSurface *surface = NULL;
DFBCHECK( layer->GetSurface(layer, &surface) );
// Load JPG image.
IDirectFBImageProvider *provider = NULL;
DFBCHECK( dfb->CreateImageProvider(dfb,
"/usr/share/directfb-examples/wood_andi.jpg", &provider) );
// Render JPG to surface.
DFBCHECK( provider->RenderTo(provider, surface, NULL) );
// Flip the surface.
DFBCHECK( surface->Flip(surface, NULL, DSFLIP_WAITFORSYNC) );
getchar();
// Cleanup.
DFBCHECK( provider->Release(provider) );
DFBCHECK( layer->Release(layer) );
DFBCHECK( dfb->Release(dfb) );
return 0;
}
#include <directfb.h>
#include <sys/types.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#define DFBCHECK(x...) \
do { \
DFBResult err = x; \
if (err != DFB_OK) { \
printf("%s <%i>:", __FILE__, __LINE__); \
DirectFBErrorFatal( #x, err ); \
} \
} while (0)
int main(int argc, char **argv) {
// Initialize randomness seed.
srand(time(NULL));
// Initialize DirectFB.
DFBCHECK( DirectFBInit(&argc, &argv) );
// Get the main interface.
IDirectFB *dfb = NULL;
DFBCHECK( DirectFBCreate(&dfb) );
// Get the primary layer.
IDirectFBDisplayLayer *layer = NULL;
DFBCHECK( dfb->GetDisplayLayer(dfb, DLID_PRIMARY, &layer) );
DFBCHECK( layer->SetCooperativeLevel(layer, DLSCL_ADMINISTRATIVE) );
// Create a 1280 x 720 window, with alpha.
IDirectFBWindow *window = NULL;
DFBWindowDescription window_desc = {
.flags = DWDESC_POSX | DWDESC_POSY | DWDESC_WIDTH | DWDESC_HEIGHT |
DWDESC_CAPS | DWDESC_SURFACE_CAPS,
.caps = DWCAPS_ALPHACHANNEL | DWCAPS_DOUBLEBUFFER | DWCAPS_NODECORATION,
.surface_caps = DSCAPS_PRIMARY | DSCAPS_PREMULTIPLIED | DSCAPS_VIDEOONLY,
.posx = 0,
.posy = 0,
.width = 1280,
.height = 720
};
DFBCHECK( layer->CreateWindow(layer, &window_desc, &window) );
// Set the window opacity to 50%, so we can see all windows.
DFBCHECK( window->SetOpacity(window, 0x80) );
// Get the primary surface.
IDirectFBSurface *surface;
DFBCHECK( window->GetSurface(window, &surface) );
// Set basic drawing flags.
DFBCHECK( surface->SetPorterDuff(surface, DSPD_SRC_OVER) );
DFBCHECK( surface->SetBlittingFlags(surface, DSBLIT_BLEND_ALPHACHANNEL) );
DFBCHECK( surface->SetDrawingFlags(surface, DSDRAW_BLEND) );
// Clear the screen with black.
DFBCHECK( surface->Clear(surface, 0x0, 0x0, 0x0, 0xFF) );
DFBCHECK( surface->Flip(surface, NULL, DSFLIP_WAITFORSYNC) );
DFBCHECK( surface->Clear(surface, 0x0, 0x0, 0x0, 0xFF) );
// Create a font.
IDirectFBFont *font = NULL;
DFBFontDescription font_dsc = {
.flags = DFDESC_HEIGHT,
.height = 48
};
DFBCHECK( dfb->CreateFont(dfb,
"/usr/share/directfb-examples/fonts/decker.ttf", &font_dsc, &font) );
// Set it as the surface font.
DFBCHECK( surface->SetFont(surface, font) );
// Draw the PID in white.
char pid_string[20] = {0};
snprintf(pid_string, 19, "Process: %i", getpid());
DFBCHECK( surface->SetColor(surface, 0xFF, 0xFF, 0xFF, 0xFF) );
DFBCHECK( surface->DrawString(surface, pid_string, -1, rand() % 1000,
rand() % 600, DSTF_LEFT) );
// Flip the surface.
DFBCHECK( surface->Flip(surface, NULL, DSFLIP_WAITFORSYNC) );
getchar();
// Cleanup.
DFBCHECK( window->Release(window) );
DFBCHECK( layer->Release(layer) );
DFBCHECK( font->Release(font) );
DFBCHECK( dfb->Release(dfb) );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment