SHARE
TWEET

Untitled

a guest Dec 16th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2. #include <iostream>
  3. #include <SDL_image.h>
  4. #include <SDL.h>
  5. using namespace std;
  6.  
  7.  
  8. class Character
  9. {
  10.     //Variables for the characters current position, and animation frame
  11.     int posX;
  12.     int posY;
  13.     int animationIndex;
  14.     int totalFrames;
  15.     int charWidth;
  16.     int charHeight;
  17.     SDL_Surface* image[2];
  18.     int Smaller_Bigger;
  19.  
  20. public:
  21.     Character();
  22.     ~Character();
  23.  
  24.     SDL_Rect getDestRect();
  25.  
  26.     void processEvents(const SDL_Event &sdlEvent);
  27.     void update(int screenWidth, int screenHeight);
  28.     void draw(SDL_Surface *screen);
  29. };
  30.  
  31.  
  32.  
  33.  
  34. Character::Character() :
  35.     posX(0),
  36.     posY(0),
  37.     animationIndex(0),
  38.     totalFrames(30),
  39.     charWidth(21),
  40.     charHeight(25)
  41.  
  42.  
  43. {
  44.     for(int j=0; j<2; ++j) {
  45.         image[j] = NULL;
  46.     }
  47.     char filename[200];    //create a large enough C-string
  48.     for(int index=0; index<2; ++index) {
  49.         sprintf(filename, "Arial.bmp/Arial_bool", index);
  50.         //Load the image
  51.         image[index] = IMG_Load( filename );
  52.         if(image == NULL) {
  53.             cout << "Error loading image: " << filename << endl;
  54.             exit(1);
  55.         }
  56.     }
  57. }
  58.  
  59.  
  60. Character::~Character()
  61. {
  62.     //Free any loaded images
  63.     for(int index=0; index<2; ++index) {
  64.         SDL_FreeSurface( image[index] );
  65.     }
  66. }
  67.  
  68. SDL_Rect Character::getDestRect()
  69. {
  70.     //Set the desRect based on the position
  71.     SDL_Rect dstRect;
  72.     dstRect.x = posX;
  73.     dstRect.y = posY;
  74.     dstRect.w = charWidth;
  75.     dstRect.h = charHeight;
  76.     return dstRect;
  77. }
  78.  
  79. void Character::update(int screenWidth, int screenHeight)
  80. {
  81.     //move the character
  82.     posX += charWidth;
  83.     posY += charHeight;
  84.  
  85.     //check if the character has moved out the edge of the screen
  86.     if(posX < -charWidth) {
  87.         posX += screenWidth + charWidth;
  88.     } else if(posX >= screenWidth) {
  89.         posX -= (screenWidth + charWidth) + charHeight;
  90.     }
  91.     if(posY < -charHeight) {
  92.         posY += screenHeight + charHeight;
  93.     } else if(posY >= screenHeight) {
  94.         posY -= (screenHeight + charHeight);
  95.     }
  96.  
  97.         //increment and loop the animation index
  98.         animationIndex = (animationIndex + 1) % totalFrames;
  99.    
  100. }
  101.  
  102. void Character::draw(SDL_Surface *screen)
  103. {
  104.     //Set the source rectangle
  105.     SDL_Rect srcRect;
  106.     srcRect.x = (animationIndex % 20) * charWidth;;
  107.     srcRect.y = (animationIndex / 10) * charHeight;
  108.     srcRect.w = charWidth;
  109.     srcRect.h = charHeight;
  110.  
  111.     //Set the desRect based on the position
  112.     SDL_Rect dstRect;
  113.     dstRect.x = posX;
  114.     dstRect.y = posY;
  115.     //Draw the character to the screen
  116.     SDL_BlitSurface( image[Smaller_Bigger], &srcRect, screen, &dstRect );
  117. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top