Implementation of image movement and windmill rotation based on SDL

Source: Internet
Author: User
Tags error handling touch visibility

This is my two week course design topic, this week the school will shut down the system, record this program, for future learning SDL can do some reference

The things you need to prepare are as follows: 1. Library support required by this procedure

Which calls the SDL sdl_image sdl_gfx tslib Library

Contains files with SDL header files, cross-compiled executable file A, some link library files

libsdl-1.2.so.0.11.3, libsdl-1.2.so.0, libsdl.so 、---------------〉sdl Some of the link libraries

Some link libraries for libsdl_gfx.so, libsdl_gfx.so.13, libsdl_gfx.so.13.9.0, libsdl_gfx.so.13.9.1,----------〉SDL_GFX

Some link libraries for libsdl_image-1.2.so.0, libsdl_image-1.2.so.0.8.0, libsdl_image.so,---------------〉sdl_image

libts-0.0.so.0, libts-0.0.so.0.1.1, libts.so,---------------. Some of the link libraries for the touch screen 2. Picture required for this procedure

Background picture: bg.bmp   Moving Pictures: player.bmp   3. Code and header files for this procedure main.c      TS.C   & Nbsp;ts.h     tslib.h    move.h   movebutton.h     judge.h

The code is as follows:   1.-------------------------main.c 

#include "SDL.h" #include "stdio.h" #include "judge.h" #include "movebutton.h" #include <stdlib.h> static void Sig (I


NT argv) {exit (0);
int main () {//Declaration and initialization of Sdl_surface * screen;
Sdl_surface * image;
Sdl_surface * playerimage;


Xtsevent event;
Uint32 beginticks; Declares the area portion of the sprite picture to be transmitted Sdl_rect prect, Brect;
Prect corresponds to the sprite's moving small map position (animate), where the brect corresponds to the sprite's position on the screen.
unsigned char playerstarts = 0;


unsigned char playerindex = 0;


int flag=1; Initialize the SDL if (Sdl_init sdl_init_video |
Sdl_init_timer = = = 1) {fprintf (stderr, "Initialize SDL%s\n", Sdl_geterror ()); return-1;}
Tsinit ();


Signal (SIGINT, SIG);


Initialization successfully set Exit function to be called Sdl_quit atexit (sdl_quit);
Screen = Sdl_setvideomode (640, sdl_swsurface);


if (screen = = NULL) {fprintf (stderr, "Cannot set 640x480x8 video mode%s\n", Sdl_geterror ()); return-1;}
Sdl_wm_setcaption ("Picture Moving", NULL);


Read background picture information, image = Sdl_loadbmp ("./bg.bmp");


if (image = = NULL) {fprintf (stderr, "Cannot download picture,%s\n", Sdl_geterror ()); return-1; Read Picture Playerimage = Sdl_loaDbmp ("./picture.bmp");


Please run the program in the terminal if (playerimage = = NULL) {fprintf (stderr, "Cannot download picture,%s\n", Sdl_geterror ());
Reads the first pixel Uint8 key = * ((UINT8 *) playerimage->pixels);


Set the color key Sdl_setcolorkey (Playerimage, Sdl_srccolorkey, key); if (sdl_blitsurface (image, NULL, screen, NULL) < 0) {//= NULL, the first is displayed according to the size of the image, and the second is the default display fprintf (stderr, "Blitsurfa
CE error:%s\n ", Sdl_geterror ());
return-1; } prect.x = 0;
Initializes a picture of the animated display.
Prect.y = 0;
PRECT.W = 32;


PRect.h = 48; Brect.x = 220;
Initializes the location of the wizard.
BRECT.Y = 200;
BRECT.W = 32;


BRect.h = 48; if (Sdl_blitsurface (Playerimage, &prect, screen, &brect) < 0) {fprintf (stderr, "Blitsurface Error:%s\n", Sdl_
GetError ());
return-1;


//update Display picture sdl_updaterect (screen, 0, 0, image->w, image->h);
Beginticks = Sdl_getticks (); It's really time to listen for touch events on the board while (1) {//message loop tsevent (&event);//if (Tsevent (&event))//{switch (event.type) {CA
Se tsdown:movebutton (screen);
Sdl_updaterect (screen, 0, 0, 0, 0);
Break

Case Tsup:movebutton (screen);
Sdl_updaterect (screen, 0, 0, 0, 0);


Break
} judge (screen, &event, 0, &beginticks, &brect, Prect, &playerindex, &playerstarts); } if (sdl_blitsurface (image, NULL, screen, NULL) < 0) {//null, the first is displayed according to the size of the image, and the second is the default display fprintf (stderr, "Blitsurf
Ace Error:%s\n ", Sdl_geterror ());
return-1; } if (Sdl_blitsurface (Playerimage, &prect, screen, &brect) < 0) {fprintf (stderr, "Blitsurface Error:%s\n",
Sdl_geterror ());
return-1;
Movebutton (screen);
//Quit the program release Picture Resource//sdl_freesurface (image);
Sdl_freesurface (Playerimage);


return 0;
 }


2-----------------------ts.c
#include "ts.h" struct Tsdev *ts;

int Tsinit () {char *tsdevice=null;
        if ((Tsdevice = getenv ("Tslib_tsdevice"))!= NULL) {ts = Ts_open (tsdevice,0);
      }else {ts = Ts_open ("/dev/input/event0", 0);
 
} if (!ts) {perror ("Ts_open"); return-1;}

if (ts_config (ts)) {perror ("Ts_config"); exit (1);}
return 0;

int tsevent (xtsevent *event) {//when recording coordinates static int x1=-1; static int y1=-1; int ret; struct ts_sample;

while (1) {ret = Ts_read (ts, &samp, 1); if (Ret < 0) {perror ("Ts_read"); event->type=-1; return-1;}

if (samp.pressure = = 1) {if (X1 ==-1 | | y1 ==-1) {x1=samp.x;
event->x=samp.x;
event->y=samp.y;
event->type=tsdown;

return 1;

} if (samp.x = = X1 && samp.y = = y1) {continue;
} event->x=samp.x;
event->y=samp.y;

event->type=tsmotion;
return 1;

} if (samp.pressure = = 0) {x1=-1; y1=-1;
event->x=samp.x;
event->y=samp.y;

event->type=tsup;
return 1;

}event->type=-1;
return-1;
 } void Tsclose () {ts_close (TS);}



----------------------3. Ts.h

#ifndef __ts_test_h #define __TS_TEST_H #include "tslib.h" #include <stdio.h> #include <stdlib.h> #include & lt;signal.h> #include <sys/fcntl.h> #include <sys/ioctl.h>//type typedef enum{Tsdown=0,tsup,tsmotion}


Tsevent;

typedef struct {tsevent type; int x; int y;


}xtsevent;
int Tsinit ();
int tsevent (xtsevent *event);


void Tsclose (); #endif----------------------4 tslib.h #ifndef _tslib_h_ #define _TSLIB_H_/* tslib/src/tslib.h * * Copyright (C
 ) 2001 Russell King.
 * * This file is placed under the LGPL.
 * * $Id: tslib.h,v 1.4 2005/02/26 01:47:23 kergoth EXP $ * * Touch screen Library interface definitions.  * * #ifdef __cplusplus extern "C" {#endif/* __cplusplus */#include <stdarg.h> #include <sys/time.h> #ifdef  WIN32 #define Tsimport __declspec (dllimport) #define Tsexport __declspec (dllexport) #define Tslocal #else #define Tsimport #ifdef gcc_hasclassvisibility #define Tsexport __attribute__ (visibIlity ("default")) #define Tslocal __attribute__ ((Visibility ("hidden")) #else #define Tsexport #define TSL ocal #endif #endif #ifdef tslib_internal #define TSAPI tsexport #else #define TSAPI tsimport #endif//Tslib_inter


NAL struct Tsdev;


struct Ts_sample {int x; int y; unsigned int pressure; struct TIMEVALTV;};
 * * Close the touchscreen device.


* * Tsapi int ts_close (struct tsdev *);
 * * Configure the touchscreen device.


* * Tsapi int ts_config (struct tsdev *);
 * * Change this hook to point to your custom error handling function.


* * extern tsapi int (*TS_ERROR_FN) (const char *FMT, va_list AP);
 * * Returns the file descriptor in with the touchscreen device.


* * Tsapi int ts_fd (struct tsdev *);


/* Load a filter/scaling module */tsapi int ts_load_module (struct Tsdev *, const char *mod, const char *params);
 * * Open the touchscreen device.


* * Tsapi struct Tsdev *ts_open (const char *dev_name, int nonblock);* * Return a scaled touchscreen sample.


* * Tsapi int ts_read (struct Tsdev *, struct ts_sample *, int);
 * * Returns a raw, unscaled sample from the touchscreen.


* * Tsapi int Ts_read_raw (struct Tsdev *, struct ts_sample *, int);
 #ifdef __cplusplus} #endif/* __cplusplus */#endif/* _tslib_h_ * *


----------------------5 move.h
#ifndef _move_h_ #define _move_h_ #include "SDL.h"//int move sdl_surface *screen,int i,uint32 *beginticks,sdl_rec
   T *brect,sdl_rect prect,unsigned char *playerindex) {Uint32 endticks;


Endticks = Sdl_getticks ();
if ((Endticks-*beginticks) > 50) {//to 50MS *beginticks = endticks; Sdl_fillrect (screen, brect, 0);


Clears the previous picture.
if (i) {switch (i) {Case 1://Move up.
if (((*brect). y-5) > 0) {//whether the position to be moved is up to the top (*brect). Y-= 5;//coordinates minus 5} else {(*brect). y = 0;//set position at top. } prect.x = (*playerindex) * 32; Use the ordinal to calculate the display that animated picture prect.y = 144; Up Graph Group (*playerindex) + +; Animation serial number plus 1 if ((*playerindex) > 2) (*playerindex) = 0;
Loops show animation break; Case 2:if ((*brect). Y + (*brect). H + 5) < Screen->h) {(*brect). Y + + 5;} else {(*brect). y = screen->h-(*br
ECT). h;
} prect.x = (*playerindex) * 32;
Prect.y = 0;
(*playerindex) + +;
if ((*playerindex) > 2) (*playerindex) = 0;//loop shows animation break; Case 3:if ((*brect). x-5) > 0) {(*brect).x = 5;
} else {(*brect). x = 0;}
Prect.x = (*playerindex) * 32;
Prect.y = 48;
(*playerindex) + +;
if ((*playerindex) > 2) (*playerindex) = 0;
Break Case 4:if ((*brect). x + (*brect). W + 5) < Screen->w) {(*brect). x = + 5;} else {(*brect). x = Screen->w-(*bre
CT). W;
} prect.x = (*playerindex) * 32;
PRECT.Y = 96;
(*playerindex) + +;
if ((*playerindex) > 2) (*playerindex) = 0;


Break #endif}}}}


----------------------6 Movebutton.h
#ifndef _movebutton_h_ #define _MOVEBUTTON_H_ int Movebutton (sdl_surface *screen) {filledtrigoncolor (screen, 5
280, 550,280,530, 0xff8f66);
Filledtrigoncolor (screen, 510,400, 550,400,530, 430, 0xff8f66);
Filledtrigoncolor (screen, 470, 470,360,440, 0xff8f66);
    Filledtrigoncolor (screen, 590, 590,360,620, 0xff8f66);
              Filledcirclecolor (screen, 530, 0xff8f66);
    
              Stringcolor (screen, 520, the "Quit", 0xFFFFFFFF);
              Boxcolor (screen, 490, 180,600, 220,0xff8f66);
Stringcolor (screen, +, "Draw Trigon", 0xFFFFFFFF);
Sdl_flip (screen);
return 0; #endif//Temporary----------------------7 judge.h #ifndef _judge_h_ #define _JUDGE_H_ #include "move.h" #include "ts.h" in T judge (Sdl_surface *screen, xtsevent * event,unsigned char i,uint32 *beginticks,sdl_rect *brect,sdl_rect PRect,unsigned char * playerindex,unsigned char *playerstarts) {//Move up Picture if ((*event) .x<=550&& (*event). x>=510) If((*event). y>=250 && (*event). y<=280) if ((*event). type==tsdown| |
                            (*event). Type==tsup) {move (Screen,1,beginticks,brect,prect,playerindex);
*playerstarts = 1;//Move up. *playerindex = 0;//Animation serial number Clear 0}//Move down the picture if ((*event). x<=550 && (*event). x>=510) if ((*event). y>=400 && Amp (*event). y<=430) if (*event). type==tsdown| |
                              (*event). Type==tsup) {move (Screen,2,beginticks,brect,prect,playerindex); *playerstarts = 2;
    Move Down. *playerindex = 0;//Animation serial number Clear 0}//Left picture if ((*event). x<=470 && (*event). x>=440) if ((*event). y>=320 ;& (*event). y<=360) if ((*event). type==tsdown| |
                              (*event). Type==tsup) {move (Screen,3,beginticks,brect,prect,playerindex); *playerstarts = 3; Move *playerindex = 0;//Animation serial number Clear 0}//Exit if ((*event). x<=620 && (*event). x>=590) if ((*event). y>=3 && (*event). y<=360) if ((*event). type==tsdown| |
                              (*event). Type==tsup) {move (Screen,4,beginticks,brect,prect,playerindex); *playerstarts = 4; Move Right *playerindex = 0;//animation serial number 0} if (*event). x<=570 && (*event). x>=490) if ((*event). Y&gt =300 && (*event). y<=380) if (*event). type==tsdown| |

(*event). Type==tsup) {sdl_quit (); ///Draw Windmill if ((*event). x<=600 && (*event). x>=490) if ((*event). y>=180 && (*event). y<=220) If ((*event). type==tsdown| |
                             (*event). Type==tsup) {filledtrigoncolor (screen, MB, 300,240,340, 240,0xff0623);
                                  Sdl_updaterect (screen, 0, 0,0,0);
                           Sdl_delay (500);
                             Filledtrigoncolor (screen, 280, 340,280,340, 240,0xfbff89);
                                  Sdl_updaterect (screen, 0, 0,0,0);
      Sdl_delay (500);
Filledtrigoncolor (screen, 380, 280, 380,240,340, 240,0X741DFF); Sdl_updAterect (screen, 0, 0,0,0);
                            Sdl_delay (500);
Filledtrigoncolor (screen, 380, 340,200,340, 240,0X5BFF20);
                                  Sdl_updaterect (screen, 0, 0,0,0);


Sdl_delay (1000);
Filledtrigoncolor (screen, the same, the 340,220,340, the 240,0x66b120);
 Sdl_updaterect (screen, 0, 0,0,0);
Sdl_delay (1000);
Filledtrigoncolor (screen, 320,260,340, and 240,0XB12AB1);
 Sdl_updaterect (screen, 0, 0,0,0);
Sdl_delay (1000);
Filledtrigoncolor (screen, the 360,260,340, the 240,0x16b133);
 Sdl_updaterect (screen, 0, 0,0,0);


                            Sdl_delay (1000);
Filledtrigoncolor (screen, 360, 360,220,340, 240,0xff8f66);
 Sdl_updaterect (screen, 0, 0,0,0);


Sdl_delay (1000);
return 0; } #endif


Iv. orders required by this procedure
1, configure the required library command
1. SDL-1.2.14
CD SDL-1.2.14
./configure--prefix=/opt/arm--disable-video-nanox-disable-video-qtopia--disable-static--enable-shared- Disable-video-photon--disable-video-ggi--DISABLE-VIDEO-SVGA--disable-video-aalib--disable-video-dummy-- Disable-video-dga--disable-arts--disable-esd--disable-alsa--disable-video-x11--disable-nasm--disable-joystick- -disable-input-tslib-enable-video-fbcon--host=arm-linux
Make&&make Install 2. sdl_image-1.2.10

CD sdl_image-1.2.10

./configure--prefix=/opt/arm--host=arm-linux--disable-static--enable-shared--with-sdl-prefix=/opt/arm CPPFLAGS= -I/OPT/ARM/INCLUDE/SDL Ldflags=-l/opt/arm/lib

Make&&make Install

3. sdl_gfx-2.0.22

CD sdl_gfx-2.0.22

./configure--prefix=/opt/arm--disable-static--enable-shared--with-sdl-prefix=/opt/arm CPPFLAGS=-I/opt/arm/ INCLUDE/SDL ldflags=-l/opt/arm/lib--host=arm-linux--enable-mmx=no

Make&&make Install

2. Cross-compile command

1. Cross-compile command:
ARM-LINUX-GCC main.c Ts.c-o A-l/opt/arm/lib-i/opt/arm/include/sdl-lsdl-lsdl_image-lsdl_gfx-lts

2. Copy the required link library to 6410 of the Development Board


Mount Command:
Mountnfs 192.168.1.15:/opt/arm/mnt/nfs/

Cd/mnt/nfs
CP A/mnt/yaffs/qtopia/lib
CP *.bmp/mnt/yaffs/qtopia/lib
CP *.so.*/mnt/yaffs/qtopia/lib


Alternatively, change the second step to copy the required link library and SDL header files to a file on your USB drive, and mount a U-disk directly onto the 6410 arm board.

The order is as follows:

Mount/dec/sda1/mnt/nfs

Cd/mnt/nfs

CP A/mnt/yaffs/qtopia/lib
CP *.bmp/mnt/yaffs/qtopia/lib
CP *.so.*/mnt/yaffs/qtopia/lib


./A (cross-compiled executables)

3. The results of the operation are as follows







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.