Finally a busy past a period of time, you can write a good summary, this week wrote a lot of SDL's small projects;
Here's one by one sticking out:
1. Wizard Diagram problem
/* predefine.h*/#ifndef __pre_define_h #define __PRE_DEFINE_H #include <stdio.h> #ifn def false #define FALSE 0 #endif #ifndef true #define TRUE 1 #endif #ifndef error #define ER Ror-1 #endif #endif/* global.h*/#ifndef __global_h #define __GLOBAL_H #i Nclude <stdio.h> #include <SDL.h> #ifdef __cplusplus extern "C" {#endif sdl_surface *
Obtainscreen (void);
void Registerscreen (Sdl_surface *screen);
Sdl_surface *obtainscreenimage (void);
void Registerscreenimage (Sdl_surface *screen); #ifdef __cplusplus} #endif #endif/* global.c*/#include "Global.h" static Sdl_surface
*surf;
Static Sdl_surface *surfimage;
Sdl_surface *obtainscreen (void) {return surf;
} void Registerscreen (Sdl_surface *screen) {surf = screen; } sdl_surface *OBTAInscreenimage (void) {return surfimage;
} void Registerscreenimage (Sdl_surface *screen) {surfimage = screen; }/* surface.h*/#ifndef __suface_h #define __SUFACE_H #include <SDL.h> #i Nclude <SDL_gfxPrimitives.h> #include <SDL_image.h> #include <SDL_rotozoom.h> #include &l T sdl_ttf.h> #include <stdio.h> #include "preDefine.h" #include "Global.h" #include "tslib.h" typedef
struct Btninfo {sdl_rect Rect;
Char *btntitle;
int Flag;
}btninfo;
#ifdef __cplusplus extern "C" {#endif//sdl initialized,//successfully returns TRUE, Failure returns false int sdlinit ();
Free resource//successfully returns TRUE, Failure returns false int freesdl ();
The initialization interface//The first parameter is the coordinates of the button to be drawn, the second parameter is the button name//Success returns TRUE, and the failure returns false int drawbtn (Sdl_rect myrect, char *btntitle); Depict pattern//The first parameter is the event object and the button coordinates, the second parameter is the button name, and the third parameter is the button bouncing down mark///successful return True, failure returns false int Draw (Sdl_rect myrect, char *btntitle, int Flag);
The mouse down-handled event//parameter returns True for the event object//, and the failure returns false int OnMouseDown (Sdl_event event);
The mouse-bouncing event//parameter returns True for the event object//, and the failure returns false int OnMouseUp (Sdl_event event);
Event handling void *event (void *junk);
Gets the user input control information void *getctrlmessage (void *junk);
Compares the touch-screen position to which button corresponds, the return value is the number int comparison (struct ts_sample, Btninfo btnarray[]) when the button is generated; #ifdef __cplusplus} #endif #endif/* surface.c*/#include "Surface.h" Btninfo
BTNARRAY[10];
Uint32 Beginticks, Endticks;
static int playerstarts = 0;
static int playerindex = 0;
Sdl_rect prect;
static int btncount = 0;
static int state_event = 4;
struct Tsdev *ts;
struct Ts_sample sample;
int Sdlinit () {Sdl_rect Brect = {0, 0, 32, 48}; if (Sdl_init (Sdl_init_video) < 0 | | | Ttf_init () < 0) {printf ("Init error\n");
return false;
Sdl_surface *screen = Sdl_setvideomode (640, sdl_swsurface);
if (!screen) {printf ("Init video mode error\n");
return false;
} sdl_surface *playerimage = Sdl_loadbmp ("./player.bmp");
if (playerimage = = NULL) {fprintf (stderr, "couldn ' t load player,%s\n", Sdl_geterror ());
return error;
//Read the first pixel Uint8 key = * ((UINT8 *) playerimage->pixels);
Set the color key Sdl_setcolorkey (Playerimage, Sdl_srccolorkey, key);
Registerscreenimage (Playerimage); prect.x = 0;
Initializes a picture of the animated display.
Prect.y = 0;
PRECT.W = 32;
PRect.h = 48; Paste the test surface if (Sdl_blitsurface (Playerimage, &prect, screen, &brect) < 0) {FPR intf (stderr, "Blitsurface Error:%s\n", Sdl_geterror ()); //Look at the hint. return error;
Registerscreen (screen);
Sdl_updaterects (Screen,1,&screen->clip_rect);
return true;
int freesdl () {sdl_quit ();
return true; int drawbtn (Sdl_rect myrect, char *btntitle) {Ttf_font * Font = Ttf_openfont ("Simfang.ttf",
20);
Sdl_color Color = {255, 255, 255};
Sdl_rect Temprect;
Sdl_surface *screen = Obtainscreen ();
if (!screen) return;
Sdl_fillrect (screen, &myrect, 0X9EABFF);
Hlinergba (screen, myrect.x, Myrect.x + MYRECT.W, Myrect.y, 0xFF, 0xFF, 0xFF, 0xff);
Vlinergba (screen, Myrect.x, Myrect.y, Myrect.y + myrect.h, 0xFF, 0xFF, 0xFF, 0xff);
Hlinergba (screen, Myrect.x + 1, myrect.x + myrect.w-1, Myrect.y + myrect.h-1, 0x00, 0x00, 0x00, 0xff); Vlinergba (screen, Myrect.x + myrect.w-1, Myrect.y + 1, Myrect.y + myrect.h-1, 0x00, 0x, 0x00, 0xff);
Sdl_surface *text = ttf_renderutf8_solid (font, btntitle, color);
Temprect.x = Myrect.x + 5;
Temprect.y = Myrect.y + 10;
TEMPRECT.W = myrect.w-10;
Temprect.h = myrect.h-20;
Sdl_blitsurface (text, 0, screen, &temprect);
Registerscreen (screen);
Sdl_updaterect (screen, Myrect.x, Myrect.y, MYRECT.W, myrect.h);
Each additional BTN saves its button information in a global variable btnarray[btncount].rect = myrect; Btnarray[btncount].
Btntitle = (char *) malloc (sizeof (char) * (strlen (btntitle) +1)); strcpy (Btnarray[btncount].
Btntitle, Btntitle); Btnarray[btncount].
Flag = true;
btncount++;
Ttf_closefont (font);
return true;
int Draw (Sdl_rect myrect, char *btntitle, int Flag) {Sdl_rect temprect;
Ttf_font * Font = Ttf_openfont ("Simfang.ttf", 20);
Sdl_color Color = {255, 255, 255}; Sdl_surface *screen = OBTAInscreen ();
if (!screen) return;
Sdl_fillrect (SCREEN,&MYRECT,0X9EABFF);
if (Flag) {Hlinergba (screen,myrect.x,myrect.x + myrect.w,myrect.y,0xff,0xff,0xff,0xff);
Vlinergba (Screen,myrect.x,myrect.y,myrect.y + myrect.h,0xff,0xff,0xff,0xff);
Hlinergba (screen,myrect.x + 1,myrect.x + myrect.w-1,myrect.y + myrect.h-1,0x00,0x00,0x00,0xff);
Vlinergba (screen,myrect.x + myrect.w-1,myrect.y + 1,myrect.y + myrect.h-1,0x00,0x00,0x00,0xff);
Sdl_surface *text = ttf_renderutf8_solid (font, btntitle, color);
Temprect.x = Myrect.x + 5;
Temprect.y = Myrect.y + 10;
TEMPRECT.W = myrect.w-10;
Temprect.h = myrect.h-20;
Sdl_blitsurface (text, 0, screen, &temprect); else {Hlinergba (screen,myrect.x,myrect.x + myrect.w,myrect).Y,0X00,0X00,0X00,0XFF);
Vlinergba (Screen,myrect.x,myrect.y,myrect.y + myrect.h,0x00,0x00,0x00,0xff);
Hlinergba (screen,myrect.x + 1,myrect.x + myrect.w-1,myrect.y + myrect.h-1,0xff,0xff,0xff,0xff);
Vlinergba (screen,myrect.x + myrect.w-1,myrect.y + 1,myrect.y + myrect.h-1,0xff,0xff,0xff,0xff);
Sdl_surface *text = ttf_renderutf8_solid (font, btntitle, color);
Temprect.x = myrect.x + 7;
Temprect.y = Myrect.y + 13;
TEMPRECT.W = myrect.w-10;
Temprect.h = myrect.h-20;
Sdl_blitsurface (text, 0, screen, &temprect);
} sdl_updaterect (Screen,myrect.x,myrect.y,myrect.w,myrect.h);
return true;
The int OnMouseDown (sdl_event Event) {if (state_event = 4) return false;
Playerstarts = state_event+1; Btnarray[state_event].
Flag = false; Draw (Btnarray[state_event].rect, btnarray[state_event). Btntitle, Btnarray[state_event].
Flag);
return true; int OnMouseUp (sdl_event Event) {if (state_event = 4) ret
Urn false;
playerstarts = 0; Btnarray[state_event].
Flag = true; Draw (Btnarray[state_event].rect, btnarray[state_event). Btntitle, Btnarray[state_event].
Flag);
return true;
} void *event (void *junk) {sdl_surface *screen = Obtainscreen ();
Sdl_rect Brect = {0, 0, 32, 48};
Beginticks = Sdl_getticks ();
while (true) {Sdl_mousebuttonevent event;
Sdl_event event1;
if (sample.pressure) {event.type = Sdl_mousebuttondown; Event.staTe = sdl_pressed;
else {event.type = Sdl_mousebuttonup;
Event.state = sdl_released;
} event.x = sample.x;
Event.y = Sample.y;
Event.which = 0;
Event.button = 0;
Event1.button = event;
Sdl_pushevent (&EVENT1);
Sdl_event Event2;
Sdl_waitevent (&event2);
Sdl_delay (50); Switch (event2.type) {case sdl_mousebuttondown:onmoused
Own (EVENT2);
Break
Case Sdl_mousebuttonup:onmouseup (EVENT2);
Break
Case Sdl_quit:goto Exit;
Break } endticks = Sdl_getticks ();//Use Timer
to the 50MS beginticks = endticks; Sdl_fillrect (screen, &brect, 0);
Clears the previous picture. if (playerstarts) {switch (playerstarts) {Case 1:/
/Move up.
if ((brect.y-5) > 0) {brect.y = 5; else {brect.y = 0;
Set the position at the top. } prect.x = Playerindex * 32; Use the ordinal to calculate the display that animated picture prect.y = 144; Up the 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-brect.h;
} prect.x = Playerindex * 32;
Prect.y = 0;
Playerindex + +;
if (Playerindex > 2) playerindex = 0;
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-brect.w;
} prect.x = Playerindex * 32;
PRECT.Y = 96;
Playerindex + +;
if (Playerindex > 2) playerindex = 0;
Break
} sdl_surface *playerimage = Obtainscreenimage (); if (Sdl_blitsurface (Playerimage, &prect, screen, &brect) < 0) {fprintf (St Derr, "BlitsurfAce Error:%s\n ", Sdl_geterror ());
return NULL;
Sdl_flip (screen);
} exit:freesdl ();
int comparison (struct Ts_sample sample, Btninfo btnarray[]) {int i = 0; for (; i < Btncount; i++) {if (sample.x >= btnarray[i].rect.x && sample.x <= (btnar RAY[I].RECT.X+BTNARRAY[I].RECT.W) && (sample.y >= btnarray[i].rect.y && sample.y
<= (btnarray[i].rect.y+btnarray[i].rect.h))) return i;
}/* thread T_c, for Pickup user action command/void *getctrlmessage (void *junk) { int fangdou_flag = 1; Stabilization signs.
1: Processing touch point information license, 0: Processing touch point information prohibit int choice;
ts = Ts_open ("/dev/event1", 0);
if (!ts) {printf ("Open device error!\n"); ReturnNULL;
} if (Ts_config (ts)) {perror ("ts_config\n");
return NULL;
for (;;)
{if (Ts_read (TS, &sample, 1)) {//* process only the first touch point information * *
if ((sample.pressure) && (Fangdou_flag = 1)) {
Choice = comparison (sample, Btnarray);
Switch (choice) {case 0:state_event = 0;
Break
Case 1:state_event = 1;
Break
Case 2:state_event = 2;
Break
Case 3:state_event = 3;
Break Default:state_event = 4;
Break } fangdou_flag = 0; Prevents processing of the touch point information generated by jitter if (sample
. Pressure = = 0) Fangdou_flag = 1;
}/* Close touch screen device file/ts_close (TS);
/*main.c*/#include "Surface.h" #include <pthread.h> int main (int argc, char *argv[])
{pthread_t t_a;
pthread_t T_b;
Sdl_rect Myrect = {290, 385, 60, 40};
Sdl_rect Myrect1 = {290, 435, 60, 40};
Sdl_rect Myrect2 = {220, 435, 60, 40};
Sdl_rect myrect3 = {360, 435, 60, 40};
Sdlinit ();
DRAWBTN (Myrect, "Shang");
DRAWBTN (Myrect1, "Xia");
DRAWBTN (Myrect2, "left");
DRAWBTN (Myrect3, "right"); Sdl_showcursor (0);
Pthread_create (&t_a, NULL, Event, (void *) null);
Pthread_create (&t_b, NULL, Getctrlmessage, (void *) null);
Pthread_join (T_a, NULL);
FREESDL ();
return 0; }
This article is from the "Late Evening" blog, please be sure to keep this source http://yiluohuanghun.blog.51cto.com/3407300/863971