# Include <stdio. h>
# Include <unistd. h>
# Include <directfb/directfb. h>
Static idirectfb * DFB = NULL;
Static idirectfbsurface * Primary = NULL;
Static idirectfbsurface * tux = NULL;
Static int screen_width = 0;
Static int screen_height = 0;
// Global variable
Static idirectfbinputdevice * keyboard = NULL;
Int main (INT argc, char ** argv)
{
Dfbsurfacedescription DSC;
Idirectfbimageprovider * provider;
Dfbinputdevicekeystate escape = diks_up; // declare a dfbinputdevicekeystate enumeration variable first,
// Set the value to diks_up, that is, no press is performed by default.
Int sprite_x, sprite_y, max_x, max_y; // defines four integer values. max_x and max_y are used to record the maximum values allowed by the preceding coordinates,
// Sprite_x and sprite_y are used to record the coordinate values in the upper left corner of the image,
// Sprite_x is in the range of [0, max_x] And sprite_y is in the range of [0, max_y]
Directfbinit (& argc, & argv );
Directfbcreate (& DFB );
DFB-> setcooperativelevel (DFB, dfscl_fullscreen );
DSC. Flags = dsdesc_caps;
DSC. Caps = dscaps_primary | dscaps_flipping;
DFB-> createsurface (DFB, & DSC, & Primary );
Primary-> getsize (primary, & screen_width, & screen_height );
DFB-> getinputdevice (DFB, didid_keyboard, & keyboard );
DFB-> createimageprovider (DFB, "./my.jpg", & provider );
Provider-> getsurfacedescription (provider, & DSC );
DFB-> createsurface (DFB, & DSC, & tux); // create a plane tux. The plane size is the image size, and information is recorded in DSC.
Provider-> renderto (provider, tux, null); // render the image to tux. If the size of the image is the same as that of tux, no scaling occurs even if null is used.
Provider-> release (provider );
// Set the value of four int variables to ensure that the image is displayed in the center of the screen
Max_x = screen_width-DSC. width;
Max_y = screen_height-DSC. height;
Sprite_x = (screen_width-DSC. width)/2;
Sprite_y = (screen_height-DSC. Height)/2;
Dfbinputdevicekeystate state;
// Primary-> setcolor (primary, 0x0, 0x0, 0x0, 0xff );
// Primary-> fillrectangle (primary, 0, 0, screen_width, screen_height );
While (Escape = diks_up ){
// Display tux on primary
Primary-> fillrectangle (primary, 0, 0, screen_width, screen_height );
Primary-> bmary (primary, tux, null, sprite_x, sprite_y );
Primary-> flip (primary, null, dsflip_waitforsync );
// Start the image moving policy. If you want to change the moving speed, you can adjust the step value.
Keyboard-> getkeystate (keyboard, diki_left, & State );
If (State = diks_down ){
Printf ("diki_left ---------------/N ");
Sprite_x --;
}
Keyboard-> getkeystate (keyboard, diki_right, & State );
If (State = diks_down ){
Printf ("diki_right -----------------/N ");
Sprite_x ++;
}
Keyboard-> getkeystate (keyboard, diki_up, & State );
If (State = diks_down ){
Printf ("diki_up -----------------/N ");
Sprite_y --;
}
Keyboard-> getkeystate (keyboard, diki_down, & State );
If (State = diks_down ){
Printf ("diki_down -----------------/N ");
Sprite_y ++;
}
// Start to define a policy so that the image is always within the screen range
If (sprite_x <0)
Sprite_x = 0;
Else if (sprite_x> max_x)
Sprite_x = max_x;
If (sprite_y <0)
Sprite_y = 0;
Else if (sprite_y> max_y)
Sprite_y = max_y;
Keyboard-> getkeystate (keyboard, diki_escape, & escape); // check the diki_escape status to prepare for the next loop
// Sleep (1 );
}
// Release resources from small to large
Keyboard-> release (keyboard );
Tux-> release (tux );
Primary-> release (primary );
DFB-> release (DFB );
Return 0;
}