NMT:directfb-sample
/* DirectFB Tutorials (c) Copyright 2000-2002 convergence integrated media GmbH. (c) Copyright 2002 convergence GmbH. All rights reserved. Written by Denis Oliver Kropp <dok@directfb.org>, Andreas Hundt <andi@fischlustig.de> and Sven Neumann <neo@directfb.org>. This file is subject to the terms and conditions of the MIT License: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** * sprite.c * * Moving a sprite with the cursor keys (without input buffer) */ #include <stdio.h> #include <unistd.h> #include <directfb.h> /* You might need to change this */ #define DATADIR "/music" /* * (Globals) */ static IDirectFB *dfb = NULL; static IDirectFBSurface *primary = NULL; static IDirectFBSurface *tux = NULL; static int screen_width = 0; static int screen_height = 0; #define DFBCHECK(x...) \ { \ DFBResult err = x; \ \ if (err != DFB_OK) \ { \ fprintf( stderr, "%s <%d>:\n\t", __FILE__, __LINE__ ); \ DirectFBErrorFatal( #x, err ); \ } \ } /* * Our interface to the keyboard. */ static IDirectFBInputDevice *keyboard = NULL; int main (int argc, char **argv) { /* * (Locals) */ DFBSurfaceDescription dsc; IDirectFBImageProvider *provider; /* * To quit the application when escape is pressed we store the key state here. */ DFBInputDeviceKeyState escape = DIKS_UP; /* * Here we store the current position of the sprite on the screen * and the maximum x/y coordinates the sprite can have without being clipped. */ int sprite_x, sprite_y, max_x, max_y; /* * (Initialize) */ DFBCHECK (DirectFBInit (&argc, &argv)); DFBCHECK (DirectFBCreate (&dfb)); DFBCHECK (dfb->SetCooperativeLevel (dfb, DFSCL_FULLSCREEN)); dsc.flags = DSDESC_CAPS; dsc.caps = DSCAPS_PRIMARY | DSCAPS_FLIPPING; DFBCHECK (dfb->CreateSurface( dfb, &dsc, &primary )); DFBCHECK (primary->GetSize (primary, &screen_width, &screen_height)); /* * Retrieve an interface to the keyboard so we can query key states. * DIDID_KEYBOARD is the device id of the primary keyboard. */ DFBCHECK (dfb->GetInputDevice (dfb, 3, &keyboard)); /* * (Load the sprite) */ /* tux.png is in your DirectFB directory */ DFBCHECK (dfb->CreateImageProvider (dfb, DATADIR"/tux.png", &provider)); DFBCHECK (provider->GetSurfaceDescription (provider, &dsc)); DFBCHECK (dfb->CreateSurface (dfb, &dsc, &tux)); DFBCHECK (provider->RenderTo (provider, tux, NULL)); provider->Release (provider); /* * Calculate maximum x/y coordinates depending on screen resolution and sprite size. */ max_x = screen_width - dsc.width; max_y = screen_height - dsc.height; /* * Initialize the sprite position by centering it on the screen. */ sprite_x = (screen_width - dsc.width) / 2; sprite_y = (screen_height - dsc.height) / 2; DFBCHECK (primary->Blit (primary, tux, NULL, sprite_x, sprite_y)); DFBCHECK (primary->Flip (primary, NULL, DSFLIP_WAITFORSYNC)); /* * Loop through until the escape key is pressed. */ static IDirectFBEventBuffer *buffer = NULL; DFBCHECK (keyboard->CreateEventBuffer (keyboard, &buffer)); int quit = 0; while (!quit) { /* * Structure which stores a DirectFB input event from an input buffer. */ DFBInputEvent event; /* * This makes the current thread wait idle for the next event. */ DFBCHECK (buffer->WaitForEvent (buffer)); /* * Fetch all events from buffer one by one and process them. */ while (buffer->GetEvent (buffer, DFB_EVENT(&event)) == DFB_OK) { /* * If any key went up, we clear the screen. if (event.type == DIET_KEYRELEASE) DFBCHECK (primary->FillRectangle (primary, 0, 0, screen_width, screen_height)); */ /* * If a key has been pressed and it's the escape key, we quit. * Otherwise we put a foot print somewhere. */ if (event.type == DIET_KEYPRESS) { printf("%x\n",event.key_symbol); switch (event.key_symbol) { case DIKS_CURSOR_RIGHT: sprite_x+=10; break; case DIKS_CURSOR_LEFT: sprite_x-=10; break; case DIKS_CURSOR_UP: sprite_y-=10; break; case DIKS_CURSOR_DOWN: sprite_y+=10; break; case DIKS_HOME: case DIKS_AB: sprite_x = (screen_width - dsc.width) / 2; sprite_y = (screen_height - dsc.height) / 2; break; case DIKS_STOP: quit = 1; break; } /* * Clear the screen. */ DFBCHECK (primary->FillRectangle (primary, 0, 0, screen_width, screen_height)); /* * Blit the sprite at its current position. */ DFBCHECK (primary->Blit (primary, tux, NULL, sprite_x, sprite_y)); /* * Flip the front and back buffer, but wait for the vertical retrace to avoid tearing. */ DFBCHECK (primary->Flip (primary, NULL, DSFLIP_WAITFORSYNC)); /* * Now make sure we didn't move the sprite out of the screen. */ 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; } } } /* * Release the keyboard. */ keyboard->Release (keyboard); /* * (Release) */ tux->Release (tux); primary->Release (primary); dfb->Release (dfb); return 23; }