SDL Simple Animation

Source: Internet
Author: User
Tags usleep

Animation is the use of human visual characteristics of the creation of art. The appearance of images at different times and places can produce certain visual effects. SDL programs can make animations. Want to produce the effect of the draw, you need to grasp the number of units in the time of the image of this factor. In general, 24 frames a second can be a smooth animation scene. Then the time interval between the two images remains about 41666 microseconds.
The GIF picture in the example below is made of a GifCam.exe screen. Make a simple animation:
The red circle continues to expand and cycle. The entire window closes automatically after a period of time. The
can use the alarm function to achieve "timings", or sdl_getticks to terminate the time of judgment. Want to draw before the circle disappeared, you can use a different color to paint again, to erase it.

#include <stdio.h> #include <stdlib.h> #include <SDL.h> #include <string.h> #include <math.h > #include <SDL_draw.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int
    Main () {Sdl_surface *screen = Sdl_setvideomode (240,240,16,sdl_swsurface);
        if (screen = = NULL) {fprintf (stderr, "Sdl_setvideomode error%s\n", Sdl_geterror ());
    Exit (1);
        } if (Sdl_init (Sdl_init_video) < 0) {fprintf (stderr, "Sdl_init_video error%s\n", Sdl_geterror ());
    Exit (1);
    } atexit (Sdl_quit);   
    int US = 41666*2;
        while (1) {if (Sdl_getticks () >10000) exit (0);
        int R;
            for (r=0;r<=80;r+=10) {draw_circle (Screen,120,120,r,sdl_maprgb (screen->format,255,0,0));
            Sdl_updaterect (screen,0,0,0,0);
            Draw_circle (Screen,120,120,r-10,sdl_maprgb (screen->format,0,0,0));
            Sdl_updaterect (screen,0,0,0,0);
 Usleep (US);       } draw_circle (Screen,120,120,r-10,sdl_maprgb (screen->format,0,0,0));
        Sdl_updaterect (screen,0,0,0,0);
    Usleep (US);
return 0; }

Compile run:

edemon@ubuntu1:~/workspace$ cat draw_circle.sh
export cflags= "' Sdl-config--cflags '-i/home/edemon/sdl_ Draw-1.2.13/include "
export libs=" ' Sdl-config--libs '/HOME/EDEMON/SDL_DRAW-1.2.13/SRC/.LIBS/LIBSDL_DRAW.A '
gcc-o draw_circle draw_circle.c-wall $CFLAGS $LIBS-lm
edemon@ubuntu1:~/workspace$./draw_cricle

Make the animation: the red ball in the square range of continuous movement, hit the edge of the rebound. The program ends when the user knocks on any key.
Make a BMP bitmap picture of a small ball, the background is white. Then use the Sdl_fillrect function to turn the screen background to white. Keep updating your location at 41666us intervals.

#include <stdio.h> #include <stdlib.h> #include <SDL.h> #include <string.h> #include <math.h > #include <SDL_draw.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> SDL
_surface *image;
    void Handler () {sdl_freesurface (image);
Exit (0);
    int main () {Sdl_surface *screen = Sdl_setvideomode (340,340,16,sdl_swsurface);
    unsigned int color;
    Sdl_event Event;
    Image = Sdl_loadbmp ("Ball.bmp");
        if (screen = = NULL) {fprintf (stderr, "Sdl_setvideomode error%s\n", Sdl_geterror ());
    Exit (1);
        } if (image = = NULL) {fprintf (stderr, "Sdl_loadbmp error%s\n", Sdl_geterror ());
    Exit (1);
        } if (Sdl_init (Sdl_init_video) < 0) {fprintf (stderr, "Sdl_init_video error%s\n", Sdl_geterror ());
    Exit (1);
    color = Sdl_maprgb (screen->format,255,255,255);
    Atexit (Sdl_quit);
    Sdl_rect Rect;
    Rect.x = 100;
    Rect.y = 70;
 RECT.W = image->w;   Rect.h = image->h;
    Sdl_fillrect (Screen,null,color);
    Sdl_blitsurface (Image,null,screen,&rect);
    Sdl_updaterect (screen,0,0,0,0);  int rightleft = 1, UpDown = 1; For Rightleft, 1-means right, 0-means left;
    For UpDown, 1 means down and 0 means up.
    int speed = 4;
        while (1) {if (Sdl_pollevent (&event) && Event.type = = Sdl_keydown) handler ();
        Usleep (41666);
            if (rightleft) {if (rect.x+rect.w+speed<screen->w) {rect.x+=speed;
                else {rect.x-=speed;
            Rightleft = 0;
            } else {if (rect.x-speed>0) {rect.x-=speed;
                else {rect.x+=speed;
            Rightleft = 1;
            } if (UpDown) {if (rect.y+rect.h+speed<screen->h) {rect.y+=speed; else {Rect.y-=speed;
            UpDown = 0;
            } else {if (rect.y-speed>0) {rect.y-=speed;
                else {rect.y+=speed;
            UpDown = 1;
        } sdl_blitsurface (Image,null,screen,&rect);
    Sdl_flip (screen);
    }//sdl_delay (5000);
return 0; }

Compile execution:

edemon@ubuntu1:~/workspace$ cat move.sh
export cflags= "' Sdl-config--cflags '-i/home/edemon/sdl_draw-1.2.13/ Include "
export libs=" ' Sdl-config--libs '/home/edemon/sdl_draw-1.2.13/src/.libs/libsdl_draw.a '
gcc-o move Move.c-wall $CFLAGS $LIBS-lm
edemon@ubuntu1:~/workspace$./move


———————————————————————-
17.04.17 Update:
Note that the rectangular ball picture needs to ensure that the ball and rectangular picture boundaries to retain a certain amount of white space, otherwise you are likely to get this effect:


Of course, you can add a function similar to the following to solve this problem, but there are flashing side effects.

void Deleterec (Sdl_rect *rec) {
    sdl_fillrect (screen,rec,color);     /* Sweep past the trace before * *
    sdl_updaterect (screen, 0, 0, 0, 0);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.