Shows a moving image
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Tue, 12 Aug 2008 04:59:06 +0000 (01:59 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Tue, 12 Aug 2008 07:50:47 +0000 (04:50 -0300)
It shows 30 frames per second of a piece of a image moving from its
top-left corner to its bottom-right corner.

movie.c

diff --git a/movie.c b/movie.c
index 8bba5ac..f6a20b9 100644 (file)
--- a/movie.c
+++ b/movie.c
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+#define WIDTH 800
+#define HEIGHT 600
 
 #include <SDL.h>
 #include <SDL_image.h>
 
+SDL_Rect *
+GetNextPoint (void)
+{
+  static SDL_Rect rect = {0, 0, WIDTH, HEIGHT};
+  rect.x = (rect.x++) % WIDTH;
+  rect.y = (rect.y++) % HEIGHT;
+  return &rect;
+}
+
+void
+ShowPoint (SDL_Surface *screen, SDL_Surface *image, SDL_Rect *rect)
+{
+  SDL_BlitSurface (image, rect, screen, NULL);
+  SDL_UpdateRect (screen, 0, 0, 0, 0);
+}
+
+Uint32
+ShowNext (Uint32 interval, void *data)
+{
+  SDL_UserEvent event;
+  event.type = SDL_USEREVENT;
+  event.code = 0;
+  SDL_PushEvent ((SDL_Event *) &event);
+  return 33;
+}
+
 int
 main (int argc, char **argv)
 {
-  SDL_Surface *image;
   SDL_Surface *screen;
+  SDL_Surface *image;
   SDL_Event event;
   SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER);
   screen = SDL_SetVideoMode (800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
   image = IMG_Load ("/home/cascardo/fotos/debconf.jpg");
-  SDL_BlitSurface (image, NULL, screen, NULL);
-  SDL_UpdateRect (screen, 0, 0, 0, 0);
+  SDL_AddTimer (0, ShowNext, NULL);
   while (SDL_WaitEvent (&event))
   {
     if (event.type == SDL_KEYDOWN)
       break;
+    else if (event.type == SDL_USEREVENT)
+      ShowPoint (screen, image, GetNextPoint ());
   }
   SDL_FreeSurface (image);
   SDL_Quit ();