rendered paste body#include <cstdlib>#include <SDL/SDL.h>#include <SDL/SDL_image.h>#include <string.h>#include <stdarg.h>SDL_Surface *screen, *bg;typedef enum { standing=0,jumping,falling,crouching } action;struct acdc { action state; bool isleft;};class character{ private: SDL_Surface **images, *cur; //The images to use for the character int num, up, x, y; //Number of images, if update, x and y coords action state; bool left; struct acdc *table; public: character (); //shouldn't be used character (int,int,int, ...); //pretty freaking important SDL_Surface *find_current(); void moveRight (int); void moveLeft (int); void crouch (); //void jump (); void update ();};bool okay (int x1, int y1){ if (x1 > 0 && x1 < bg->w && y1 > 0 && y1 < bg->h) return true; return false;}character::character (){ images = 0; num = 0; up = 0; table = 0; x = 0; y = 0; left = false; state = standing;}character::character (int x1, int y1, int num1, ...){ va_list ags; va_start (ags, num1); num=num1; if (okay (x1,y1)) { x=x1; y=y1;} else { x =0; y =0;} images = (SDL_Surface**) malloc (sizeof (SDL_Surface*)*num); table = (struct acdc*) malloc (sizeof (struct acdc)*num); for (int a = 0; a < num; ++a) { images [a] = va_arg (ags, SDL_Surface*); SDL_SetColorKey(images[a], SDL_SRCCOLORKEY,SDL_MapRGB(images[a]->format, 255, 0, 255)); table [a].state = (action) va_arg (ags, int); table [a].isleft = va_arg (ags, int); } va_end (ags); up = 1; state = standing; left = true;}SDL_Surface *character::find_current(){ for (int a=0; a < num; ++a) if (table[a].state == state && table[a].isleft == left) return images[a]; return 0;}void character::moveRight (int a){ if (okay (x+a, y)) if (x+a != x) { left = false; if (find_current()) { SDL_Rect rmt, rmf; rmt.x = rmf.y = x; rmt.y = rmf.y = y; rmf.w = a; rmf.h = find_current()->h; SDL_BlitSurface (bg, &rmf, screen, &rmt); x+=a; up=1; } }}void character::moveLeft (int a){ if (okay (x-a, y)) if (x-a != x) { left = true; if (find_current()) { SDL_Rect rmt, rmf; rmt.x = rmf.y = x; rmt.y = rmf.y = y; rmf.w = a; rmf.h = find_current()->h; SDL_BlitSurface (bg, &rmf, screen, &rmt); x-=a; up=1; } }}void character::crouch (){ if (state == standing) up=1;}void character::update (){ if (up) { SDL_Rect fm; fm.x = x; fm.y = y; SDL_BlitSurface (find_current(), 0, screen, &fm); SDL_Flip (screen); //this really shouldn't be called now up = 0; if (state==crouching) state=standing; }}main (){ if (SDL_Init (SDL_INIT_VIDEO|SDL_INIT_AUDIO)<0) return 1; atexit (SDL_Quit); if (!(screen = SDL_SetVideoMode (800,600,32,SDL_HWSURFACE|SDL_DOUBLEBUF))) return 1; SDL_Surface *cham = IMG_Load ("Stand-right.png"); bg = IMG_Load ("sky.png"); SDL_SetColorKey(cham, SDL_SRCCOLORKEY,SDL_MapRGB(cham->format, 255, 0, 255)); SDL_Rect rft; rft.x = rft.y = 0; SDL_BlitSurface (bg,0,screen,&rft); rft.x = bg->w/2; rft.y = bg->h/2; SDL_BlitSurface (cham,0,screen,&rft); SDL_Flip (screen);// character mn (bg->w/2, bg->h/2, 6, "Stand-right.png", standing, false, "Stand-left.png", standing, true,// "Jump-left.png", jumping, true, "Jump-right.png", jumping, false, "Crouch-left.png", crouching, true,// "Crouch-right.png", crouching, false); int done=0;// mn.update(); SDL_Event event; while (!done) { while (SDL_PollEvent (&event)) if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) done = 1; SDL_Flip (screen); } return 0;}