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, SDL_Surface* cur){ if (x1 > 0 && x1 < bg->w-cur->w && y1 > 0 && y1 < bg->h-cur->h) return true; return false;}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;}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; 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] = IMG_Load (va_arg (ags, char*)); 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; if (okay (x1,y1, find_current())) { x=x1; y=y1;} else { x =0; y =0;}}void character::moveRight (int a){ if (state==standing || state == falling || state == jumping) { if (okay (x+a, y, find_current())) if (x+a != x) { bool tmp = left; left = false; if (find_current()) { SDL_Rect rmt, rmf; /*if (!tmp) { rmt.x = rmf.y = x-2; rmt.y = rmf.y = y-4; rmf.w = a+4; rmf.h = find_current()->h+2; } else {*/ rmt.x = rmf.y = x-2; rmt.y = rmf.y = y-2; rmf.w = find_current()->w+2; rmf.h = find_current()->h+2; //} SDL_BlitSurface (bg, &rmf, screen, &rmt); x+=a; up=1; } } } else if (state == crouching && left) { left=false; up=1; }}void character::moveLeft (int a){ if (state==standing || state == falling || state == jumping) { if (okay (x-a, y, find_current())) if (x-a != x) { bool tmp = left; left = true; if (find_current()) { SDL_Rect rmt, rmf; /*if (tmp) { rmt.x = rmf.y = x-2; rmt.y = rmf.y = y-2; rmf.w = a+4; rmf.h = find_current()->h+4; } else {*/ rmt.x = rmf.y = x-2; rmt.y = rmf.y = y-2; rmf.w = find_current()->w+2; rmf.h = find_current()->h+2; //} SDL_BlitSurface (bg, &rmf, screen, &rmt); x-=a; up=1; } } } else if (state == crouching && !left) { left=true; up=1; }}void character::crouch (){ if (state == standing) { state = crouching; SDL_Rect rmt, rmf; rmt.x = rmf.y = x-2; rmt.y = rmf.y = y-2; rmf.w = find_current()->w+2; rmf.h = find_current()->h+2; SDL_BlitSurface (bg, &rmf, screen, &rmt); 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 if (state==crouching) { SDL_Rect rmt, rmf; rmt.x = rmf.y = x-2; rmt.y = rmf.y = y-2; rmf.w = find_current()->w+2; rmf.h = find_current()->h+2; SDL_BlitSurface (bg, &rmf, screen, &rmt); state=standing; } else up=0; }}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-100, 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(); Uint8 *keys; 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; keys = SDL_GetKeyState (0); if (keys[SDLK_DOWN]) mn.crouch(); if (keys[SDLK_LEFT]) mn.moveLeft (5); else if (keys[SDLK_RIGHT]) mn.moveRight (5); mn.update(); SDL_Flip (screen); } return 0;}