Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- #include <SDL_image.h>
- #include <SDL.h>
- using namespace std;
- class Character
- {
- //Variables for the characters current position, and animation frame
- int posX;
- int posY;
- int animationIndex;
- int totalFrames;
- int charWidth;
- int charHeight;
- SDL_Surface* image[2];
- int Smaller_Bigger;
- public:
- Character();
- ~Character();
- SDL_Rect getDestRect();
- void processEvents(const SDL_Event &sdlEvent);
- void update(int screenWidth, int screenHeight);
- void draw(SDL_Surface *screen);
- };
- Character::Character() :
- posX(0),
- posY(0),
- animationIndex(0),
- totalFrames(30),
- charWidth(21),
- charHeight(25)
- {
- for(int j=0; j<2; ++j) {
- image[j] = NULL;
- }
- char filename[200]; //create a large enough C-string
- for(int index=0; index<2; ++index) {
- sprintf(filename, "Arial.bmp/Arial_bool", index);
- //Load the image
- image[index] = IMG_Load( filename );
- if(image == NULL) {
- cout << "Error loading image: " << filename << endl;
- exit(1);
- }
- }
- }
- Character::~Character()
- {
- //Free any loaded images
- for(int index=0; index<2; ++index) {
- SDL_FreeSurface( image[index] );
- }
- }
- SDL_Rect Character::getDestRect()
- {
- //Set the desRect based on the position
- SDL_Rect dstRect;
- dstRect.x = posX;
- dstRect.y = posY;
- dstRect.w = charWidth;
- dstRect.h = charHeight;
- return dstRect;
- }
- void Character::update(int screenWidth, int screenHeight)
- {
- //move the character
- posX += charWidth;
- posY += charHeight;
- //check if the character has moved out the edge of the screen
- if(posX < -charWidth) {
- posX += screenWidth + charWidth;
- } else if(posX >= screenWidth) {
- posX -= (screenWidth + charWidth) + charHeight;
- }
- if(posY < -charHeight) {
- posY += screenHeight + charHeight;
- } else if(posY >= screenHeight) {
- posY -= (screenHeight + charHeight);
- }
- //increment and loop the animation index
- animationIndex = (animationIndex + 1) % totalFrames;
- }
- void Character::draw(SDL_Surface *screen)
- {
- //Set the source rectangle
- SDL_Rect srcRect;
- srcRect.x = (animationIndex % 20) * charWidth;;
- srcRect.y = (animationIndex / 10) * charHeight;
- srcRect.w = charWidth;
- srcRect.h = charHeight;
- //Set the desRect based on the position
- SDL_Rect dstRect;
- dstRect.x = posX;
- dstRect.y = posY;
- //Draw the character to the screen
- SDL_BlitSurface( image[Smaller_Bigger], &srcRect, screen, &dstRect );
- }
Add Comment
Please, Sign In to add comment