Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. using user-defined data type as data-member of a class.

using user-defined data type as data-member of a class.

Scheduled Pinned Locked Moved Managed C++/CLI
announcement
1 Posts 1 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    Here is a class called Animation, and it has a function called Update as shown in the code below.

    class Animation
    {
    private:
    Vector2u imageCount; // contains the number of total images in row and total no. of coloumns , in this case (3, 9)
    Vector2u currentImage; // acts as the limiter.
    float totalTime; // the total time taken by whole animation.
    float switchTime; // the time duration btw to images.

    public:
    // Texture * texture -> takes the whole image.
    // Vector2u imageCount -> counts the number of images in each row, and total number of coloumn.
    // float switchTime -> time btw 2 images.
    Animation(Texture* texture, Vector2u imageCount, float switchTime);
    ~Animation();

    IntRect uvRect;						// sets the height, width, top, and left corner of the image to be printed on the screen.
    
    void Update(int row, float deltaTime, bool faceRight );		// float deltaTime -> gives the FPS of the machine, acc. to which the switch bte images is maintained.
    

    };
    Animation::Animation(Texture* texture, Vector2u imageCount, float switchTime) {
    this->imageCount = imageCount;
    this->switchTime = switchTime;
    totalTime = 0.0f;

    // here we have set the initial image position-> first position
    currentImage.x = 0;
    currentImage.y = 0;
    
    // here we are setting height & width of each image.
    uvRect.width = texture->getSize().x / static\_cast(imageCount.x);
    uvRect.height = texture->getSize().y / static\_cast(imageCount.y);
    

    }

    Animation::~Animation()
    {
    }

    // this is the function i am talking about
    // it takes argument which are the data members of player class.
    void Animation::Update(int row, float deltaTime, bool faceRight) {
    currentImage.y = row;
    totalTime += deltaTime; // deltaTime is added to the totalTime.

    if (totalTime >= switchTime) {
    	totalTime -= switchTime;
    	currentImage.x++;								// every time totalTime >= switchTime, incrementing currentImage.x
    
    	if (currentImage.x >= imageCount.x) {			// when currentImage.x >= imageCount.x, the currentImage.x = 0.	
    		currentImage.x = 0;	
    	}
    }
    
    uvRect.top = currentImage.y \* uvRect.height;
    
    // to turn the player to each side, left or right.
    if (faceRight) {
    	uvRect.left = currentImage.x \* uvRect.width;			// if the faceRight is True.
    	uvRect.width = abs(uvRect.width) ;
    }
    else {
    	uvRect.left = (currentImage.x + 1) \* abs(uvRect.width);		// if the faceRight is false .
    	uvRect.width = -abs(uvRect.width);
    
    }
    

    }

    and the is 2nd class called player, in which i have

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups