I'm currently learning to program a game with the SFML library.I dugged up a bunch of SFML tutorials on Youtube and found a really good one by 'Suraj Sharma':
https://www.youtube.com/channel/UC2i39AOpDSlO1Mrn1jQ8Xkg
I'm now making an animation class following his tutorial. Here's the video I'm watching:
https://www.youtube.com/watch?v=pq9OuBHJiVM&list=PL6xSOsbVA1ebkU66okpi-KViAO8_9DJKg&index=27
A C26495 warning:Variable 'AnimCmp::Timer' is uninitialized.Always initialize a member variable(type.6). Blockquote
and a
E0513 error:a value of type "AnimCmp::Anim*" cannot be assigned to an entity of type "AnimCmp::Anim*"
appears right when I was watching the first 8 minutes of the video. Here's my code, compiled in Visual Studio 2019 community (Take notes that all declared classes, functions and variables are an abbreviated version of the video):
AnimCmp.h:
#pragma once
#ifndef ANIM_CMP_H
#define ANIM_CMP_H
//i've put every required headers in a precompiled one
#include "pch.h"
class AnimCmp
{
private:
class Anim {
public:
//Vars
sf::Sprite& Spr;
sf::Texture& TexSheet;
sf::IntRect StartRct;
sf::IntRect CurrRect;
sf::IntRect EndRct;
float Timer;
float AnimTimer;
int w;
int h;
//C26495 at Anim's constuctor
Anim(sf::Sprite& Spr, sf::Texture& TexSheet, float AnimTimer, int stx, int sty, int frx, int fry, int w, int h)
:Spr(Spr), TexSheet(TexSheet),AnimTimer(AnimTimer),w(w),h(h){
this->w = w;
this->h = h;
this->StartRct = sf::IntRect(stx * w, sty * h, w, h);
this->CurrRect = this->StartRct;
this->EndRct = sf::IntRect(frx * w, fry * h, w, h);
this->Spr.setTexture(this->TexSheet, true);
this->Spr.setTextureRect(this->StartRct);
}
//virtual~Anim();
//Functions
//void Pause();
void Reset() {
this->Timer = 0.f;
this->CurrRect = this->StartRct;
}
void Play(const float& dt) {
//Update Timer
this->Timer = 10.f*dt;
if (this->Timer >= this->AnimTimer) {
//Reset Timer
this->Timer = 0.f;
//Animate
if (this->CurrRect != this->EndRct) {
this->CurrRect.left += this->w;
}
else {//Reset
this->CurrRect.left = this->StartRct.left;
}
this->Spr.setTextureRect(this->CurrRect);
}
}
};
sf::Sprite& Spr;
sf::Texture& TexSheet;
std::map<std::string, Anim*> Anims;
public:
AnimCmp(sf::Sprite& Spr,sf::Texture& TexSheet);
virtual~AnimCmp();
//Functions
void AddAnim(const std::string key, sf::Sprite& Spr, sf::Texture& TexSheet,
float AnimTimer, int stx, int sty, int frx, int fry, int w, int h);
void Start(const std::string Anim);
void Pause(const std::string Anim);
void Reset(const std::string Anim);
void Play(const std::string key, const float& dt);
};
#endif // !ANIM_CMP_H
AnimCmp.cpp:
#include "AnimCmp.h"
AnimCmp::AnimCmp(sf::Sprite& Spr, sf::Texture& TexSheet)
:Spr(Spr),TexSheet(TexSheet)
{
}
AnimCmp::~AnimCmp()
{
for (auto& i : this->Anims) {
delete i.second;
}
}
void AnimCmp::AddAnim(const std::string key, sf::Sprite& Spr, sf::Texture& TexSheet,float AnimTimer, int stx, int sty,
int frx, int fry, int w, int h)
{
this->Anims[key] = new Anim(this->Spr, this->TexSheet, AnimTimer, stx, sty, frx, fry, w, h);//E0513
}
//Functions
void AnimCmp::Start(const std::string Anim)
{
}
void AnimCmp::Pause(const std::string Anim)
{
}
void AnimCmp::Reset(const std::string Anim)
{
}
void AnimCmp::Play(const std::string key, const float& dt)
{
this->Anims[key]->Play(dt);
}
The thing is the game still runs even when an error is showing. I'm just not sure how it'll affect the game.