Unable to solve the "error: candidate is:" in cocos2dx - c++

I'm currently having a problem with the following error when I try to run my game. I tried to understand what the meaning of the following error but I find it hard to understand. I believe that there is more than one error but I have no clue where to look. I would love to have some help from you!
[armeabi] Compile++ thumb: MyGame_shared <= BallSprite.cpp
[armeabi] Compile++ thumb: MyGame_shared <= Character.cpp
[armeabi] StaticLibrary : libcocos2d.a
[armeabi] StaticLibrary : libcocostudio.a
[armeabi] StaticLibrary : libcocosbuilder.a
[armeabi] StaticLibrary : libcocos3d.a
jni/../../../Classes/Character.cpp:26:5: error: prototype for 'int Character::getTurnCount()' does not match any in class 'Character'
int Character::getTurnCount()
^
In file included from /Users/michael/Desktop/CocoFolder/Puzzle/cocos2d/cocos/3d/../base/CCAsyncTaskPool.h:28:0,
from /Users/michael/Desktop/CocoFolder/Puzzle/cocos2d/cocos/3d/../cocos2d.h:41,
from jni/../../../Classes/Character.h:4,
from jni/../../../Classes/Character.cpp:1:
/Users/michael/Desktop/CocoFolder/Puzzle/cocos2d/cocos/3d/../platform/CCPlatformMacros.h:153:25: error: candidate is: virtual int Character::getTurnCount() const
public: virtual varType get##funName(void) const;\
^
jni/../../../Classes/Character.h:26:5: note: in expansion of macro 'CC_PROPERTY'
CC_PROPERTY(int, _turnCount, TurnCount);
^
jni/../../../Classes/BallSprite.cpp:62:27: error: prototype for 'BallSprite::PositionIndex BallSprite::getPositionIndex()' does not match any in class 'BallSprite'
BallSprite::PositionIndex BallSprite::getPositionIndex()
^
In file included from /Users/michael/Desktop/CocoFolder/Puzzle/cocos2d/cocos/3d/../base/CCAsyncTaskPool.h:28:0,
from /Users/michael/Desktop/CocoFolder/Puzzle/cocos2d/cocos/3d/../cocos2d.h:41,
from jni/../../../Classes/BallSprite.h:4,
from jni/../../../Classes/BallSprite.cpp:1:
/Users/michael/Desktop/CocoFolder/Puzzle/cocos2d/cocos/3d/../platform/CCPlatformMacros.h:153:25: error: candidate is: virtual BallSprite::PositionIndex BallSprite::getPositionIndex() const
public: virtual varType get##funName(void) const;\
^
jni/../../../Classes/BallSprite.h:53:5: note: in expansion of macro 'CC_PROPERTY'
CC_PROPERTY(PositionIndex, _positionIndex, PositionIndex);
^
make: *** [obj/local/armeabi/objs-debug/MyGame_shared/__/__/__/Classes/BallSprite.o] Error 1
make: *** Waiting for unfinished jobs....
make: *** [obj/local/armeabi/objs-debug/MyGame_shared/__/__/__/Classes/Character.o] Error 1
make: Leaving directory `/Users/michael/Desktop/CocoFolder/Puzzle/proj.android-studio/app'
Error running command, return code: 2.
code for the character
#include "Character.h"
USING_NS_CC;
Character::Character()
: _hp(0)
, _maxHp(0)
, _attack(0)
, _element(Element::None)
, _turnCount(0)
, _remainingTurn(0)
{
}
Character* Character::create()
{
Character *pRet = new Character();
pRet->autorelease();
return pRet;
}
int Character::getTurnCount()
{
return _turnCount;
}
void Character::setTurnCount(int turnCount)
{
_turnCount = turnCount;
_remainingTurn = _turnCount;
}
float Character::getHpPercentage()
{
return _hp * 100.f / _maxHp;
}
bool Character::isAttackTurn()
{
_remainingTurn--;
if (_remainingTurn <= 0)
{
_remainingTurn = _turnCount;
return true;
}
return false;
}
int Character::getDamage(int ballCount, int chainCount, Character* attacker, Character* defender)
{
float baseDamage = ballCount / 3.0 * 100;
float chainBonus = powf(1.1, chainCount - 1);
float elementBonus = getElementBonus(attacker->getElement(), defender->getElement());
return baseDamage * chainBonus * elementBonus;
}
float Character::getElementBonus(Element attackElement, Element defenseElement)
{
switch (attackElement)
{
case Element::Fire:
{
switch (defenseElement)
{
case Element::Wind:return 2;
case Element::Water:return 0.5;
default:return 1;
}
break;
}
case Element::Water:
{
switch (defenseElement)
{
case Element::Fire:return 2;
case Element::Wind:return 0.5;
default:return 1;
}
break;
}
case Element::Wind:
{
switch (defenseElement)
{
case Element::Water:return 2;
case Element::Wind:return 0.5;
default:return 1;
}
break;
}
case Element::Holy:
{
switch (defenseElement)
{
case Element::Shadow:return 2;
default:return 1;
}
break;
}
case Element::Shadow:
{
switch (defenseElement)
{
case Element::Holy:return 2;
default:return 1;
}
break;
}
default:
{
return 1;
}
}
}
Characters header
class Character : public cocos2d::Ref
{
public:
enum class Element
{
Fire,
Water,
Wind,
Holy,
Shadow,
None,
};
protected:
int _remainingTurn;
CC_SYNTHESIZE(int, _hp, Hp);
CC_SYNTHESIZE(int, _maxHp, MaxHp);
CC_SYNTHESIZE(int, _attack, Attack);
CC_SYNTHESIZE(Element, _element, Element);
CC_PROPERTY(int, _turnCount, TurnCount);
public:
Character();
static Character* create();
float getHpPercentage();
bool isAttackTurn();
static int getDamage(int ballCount, int chainCount, Character* attacker, Character* defender);
protected:
static float getElementBonus(Element attackElement, Element defenseElement);
};
#endif
BallSprite.cpp
#include "BallSprite.h"
USING_NS_CC;
BallSprite::BallSprite()
: _removedNo(0)
, _checkedX(false)
, _checkedY(false)
, _fallCount(0)
, _positionIndex(0, 0)
{
}
BallSprite* BallSprite::create(BallType type, bool visible)
{
BallSprite *pRet = new BallSprite();
if (pRet && pRet->init(type, visible))
{
pRet->autorelease();
return pRet;
}
else
{
delete pRet;
pRet = nullptr;
return nullptr;
}
}
bool BallSprite::init(BallType type, bool visible)
{
if (!Sprite::initWithFile(getBallImageFilePath(type)))
return false;
_ballType = type;
setVisible(visible);
return true;
}
void BallSprite::resetParams()
{
_removedNo = 0;
_checkedX = false;
_checkedY = false;
_fallCount = 0;
}
void BallSprite::resetPosition()
{
setPosition(getPositionForPositionIndex(_positionIndex));
}
void BallSprite::getPositionIndex()
{
return _positionIndex;
}
void BallSprite::setPositionIndex(PositionIndex positionIndex)
{
_positionIndex = positionIndex;
setTag(generateTag(_positionIndex));
}
void BallSprite::setPositionIndexAndChangePosition(PositionIndex positionIndex)
{
setPositionIndex(positionIndex);
resetPosition();
}
void BallSprite::removingAndFallingAnimation(int maxRemovedNo)
{
removingAnimation(maxRemovedNo);
fallingAnimation(maxRemovedNo);
}
void BallSprite::removingAnimation(int maxRemovedNo)
{
if (_removedNo > 0)
{
auto delay1 = DelayTime::create(ONE_ACTION_TIME * (_removedNo - 1));
auto fade = FadeTo::create(ONE_ACTION_TIME, 0);
auto delay2 = DelayTime::create(ONE_ACTION_TIME * (maxRemovedNo - _removedNo));
auto removeSelf = RemoveSelf::create(false);
runAction(Sequence::create(delay1, fade, delay2, removeSelf, nullptr));
}
}
void BallSprite::fallingAnimation(int maxRemovedNo)
{
if (_fallCount > 0)
{
setPositionIndex(PositionIndex(_positionIndex.x, _positionIndex.y - _fallCount));
auto delay = DelayTime::create(ONE_ACTION_TIME * maxRemovedNo);
auto show = Show::create();
auto move = MoveTo::create(ONE_ACTION_TIME, getPositionForPositionIndex(getPositionIndex()));
runAction(Sequence::create(delay, show, move, nullptr));
}
}
std::string BallSprite::getBallImageFilePath(BallType type)
{
switch (type)
{
case BallType::Red: return "red.png";
case BallType::Blue: return "blue.png";
default: return "pink.png";
}
}
Point BallSprite::getPositionForPositionIndex(PositionIndex positionIndex)
{
return Point(BALL_SIZE * (positionIndex.x - 0.5) + 1,
BALL_SIZE * (positionIndex.y - 0.5) + 1);
}
int BallSprite::generateTag(PositionIndex positionIndex)
{
return positionIndex.x * 10 + positionIndex.y;
}
BallSprite header
#include "cocos2d.h"
#define BALL_SIZE 106
#define ONE_ACTION_TIME 0.2
class BallSprite : public cocos2d::Sprite
{
public:
enum class BallType
{
Blue,
Red,
Green,
Yellow,
Purple,
Pink,
};
struct PositionIndex
{
PositionIndex()
{
x = 0;
y = 0;
}
PositionIndex(int _x, int _y)
{
x = _x;
y = _y;
}
int x;
int y;
};
BallSprite();
static BallSprite* create(BallType type, bool visible);
virtual bool init(BallType type, bool visible);
CC_SYNTHESIZE(int, _removedNo, RemovedNo);
CC_SYNTHESIZE(bool, _checkedX, CheckedX);
CC_SYNTHESIZE(bool, _checkedY, CheckedY);
CC_SYNTHESIZE(int, _fallCount, FallCount);
CC_SYNTHESIZE_READONLY(BallType, _ballType, BallType);
CC_PROPERTY(PositionIndex, _positionIndex, PositionIndex);
void setPositionIndexAndChangePosition(PositionIndex positionIndex);
void resetParams();
void resetPosition();
void removingAndFallingAnimation(int maxRemovedNo);
static std::string getBallImageFilePath(BallType type);
static cocos2d::Point getPositionForPositionIndex(PositionIndex positionIndex);
static int generateTag(PositionIndex positionIndex);
protected:
void removingAnimation(int maxRemovedNo);
void fallingAnimation(int maxRemovedNo);
};
#endif

CC_PROPERTY(int, _turnCount, TurnCount);
virtual method definition should be like this because in declaration it is constant.
int Character::getTurnCount() const
{
return _turnCount;
}
And there is problem in return type of getter method, it should be like this.
CC_PROPERTY(PositionIndex, _positionIndex, PositionIndex);
PositionIndex BallSprite::getPositionIndex() const
{
return _positionIndex;
}
EDIT
You can't directly access PositionIndex, use it with BallSprite
BallSprite::PositionIndex BallSprite::getPositionIndex() const
{
return _positionIndex;
}

Related

How do I instantiate a class from a member variable pointer pointing to the class in c++?

So I have two classes "PlayerTracking" and "Projectile". Every instance of player tracking should have an associated projectile; the PlayerTracking class has a pointer to a projectile to connect the two. When I call the "getPlayerProjectile" function I would like the constructor in the Projectile class to initialise the projectile but I don't really have any idea what is the best way to go about doing this. Here is the player tracking.h file
#pragma once
#include"GameObject.h"
#include"Projectile.h"
class playerTracking :public GameObject {
public:
playerTracking() {
playerYawOrientation = 0.0f;
playerPitchOrientation = 0.0f;
playerID = 1;
teamID = 0;
IndividualplayerScore = 0;
playerProjectile = new Projectile();
}
void setplayerID(int assignedPlayerID) {
playerID = assignedPlayerID;
}
void setTeamID(int assignedTeamID) {
playerID = assignedTeamID;
}
void AssignPlayerWeapon(gun weponType) {
playerProjectile->setGunType(weponType);
}
Projectile* getPlayerProjectile() {
return playerProjectile;
}
protected:
float playerYawOrientation;
float playerPitchOrientation;
int playerID;
int teamID;
int IndividualplayerScore;
Projectile* playerProjectile;
};
and here is the projectile class.h
#pragma once
#include "Transform.h"
#include "CollisionVolume.h"
#include"GameObject.h"
struct gun {
float radius;
float projectileForce;
float weight;
bool affectedByGravity;
};
static gun pistol{
7,
50,
1,
false,
};
static gun rocket{
20,
20,
5,
true,
};
class NetworkObject;
class RenderObject;
class PhysicsObject;
class Projectile:public GameObject {
public:
/* Projectile() {
setGunType(pistol);
aimingPitch = 0.0f;
aimingYaw = 0.0f;
physicsProjectile = nullptr;
}*/
Projectile();
~Projectile() {
physicsProjectile;
}
void setGunType(gun wepType);
void setExplosionRadius(float newRadius) {
explosionRadius = newRadius;
}
float getExplosionRadius() {
return explosionRadius;
}
void setProjectilePropultionForce(float newForce) {
projectilePropultionForce = newForce;
}
float getPojectilePropultionForce() {
return projectilePropultionForce;
}
void setWieght(float newweight) {
weight = newweight;
}
float getWeight() {
return weight;
}
void setAffectedByGravityTrue() {
AffectedGravity = true;
}
void setAffectedByGravityFalse() {
AffectedGravity = false;
}
void setBulletDirectionVector(Vector3 aimedDirection) {
bulletDirectionVector = aimedDirection.Normalised();
}
Vector3 getBulletDirectionVector() {
return bulletDirectionVector;
}
bool getAffectedGravity() {
return AffectedGravity;
}
protected:
float explosionRadius;
float projectilePropultionForce;
float weight;
float aimingYaw;
float aimingPitch;
//static int TeamID;
//static int personalID;
bool AffectedGravity;
PhysicsObject* physicsProjectile;
Vector3 bulletDirectionVector;
};
and the Projectile.c file
#include"Projectile.h"
Projectile::Projectile(){
setGunType(pistol);
aimingPitch = 0.0f;
aimingYaw = 0.0f;
physicsProjectile = nullptr;
bulletDirectionVector = { 0,0,0 };
}
void Projectile::setGunType(gun wepType) {
setExplosionRadius(wepType.radius);
setProjectilePropultionForce(wepType.projectileForce);
setWieght(wepType.weight);
//SetBoundingVolume(VolumeType::Sphere);
if (wepType.affectedByGravity) {
setAffectedByGravityTrue();
}
else
{
setAffectedByGravityFalse();
}
}
Obviously right now the pointer playerProjectile is just null but I am pretty sure using new would be a bad way to construct one in this case. Thanks a bunch for any help!

C++ undefined reference error in my code?

My code blocks complier give 'undefined reference error to vtable Plant' to my code I have already read oteher articles in this page, but that did not resolved my issue. I tried to add both virtual and normal destructor.
Please review my code and try to run it in Code::Blocks with cpp11 and this topic is not a duplicate!
Here my code :
#include <iostream>
#include <vector>
#include <cstdlib>
#include <fstream>
#include <vector>
#include <time.h>
#include "Plant.h"
using namespace std;
int main(){
vector<Plant*> novenyek;
int novenyszam = 5;
f >> novenyszam;
novenyek.resize(novenyszam);
// novenyek[i].push_back(new Deltafa("a",0,true)); //thhis does not work
for (int i=0; i<novenyszam; i++){
/* string tempname;
string tempfajta;
string temptapanyag;
*/
// string a = "Falank";
// novenyek[i] = new Puffancs(a,7,true);
novenyek[i] = new Deltafa("a",0,true);
//new Puffancs("Falank",7,true);
//novenyek.push_back(new Puffancs("a",7,true));
// novenyek[i] = new Plant(Puffancs("nev",7,true));
// novenyek.push_back();
}
return 0;
}
Plant.h
#pragma once
#include <string>
#include <ostream>
#include <stdlib.h>
enum Sugarzas
{
ALFA,
DELTA,
NONE,
};
struct SugarzasIgeny
{
Sugarzas _sugarzas;
int _mennyiseg;
SugarzasIgeny(Sugarzas sugarzas, int mennyiseg = 0): _mennyiseg(mennyiseg), _sugarzas(sugarzas) {}
};
class Plant
{
public:
///Plant(const std::string &nev, int tapanyag, bool El_e) : _nev(nev), _tapanyag(tapanyag), _El_e(El_e) {}
std::string nev() const { return _nev; }
int tapanyag() const { return _tapanyag; }
bool El_e() const { return _El_e; }
// Sablonfuggveny
virtual SugarzasIgeny nap(Sugarzas sugarzas);
~Plant () {}
protected:
std::string _nev; //noveny neve
int _tapanyag; //tapanyaga
bool _El_e; //ele
Plant(const std::string &nev, int tapanyag, bool El_e) : _nev(nev), _tapanyag(tapanyag), _El_e(El_e) {}
};
class Puffancs : public Plant
{
public:
Puffancs(const std::string &nev, int tapanyag, bool El_e) : Plant(nev, tapanyag, El_e) {}
virtual SugarzasIgeny nap(Sugarzas sugarzas) ;
};
class Deltafa : public Plant
{
public:
Deltafa(const std::string &nev, int tapanyag, bool El_e) : Plant(nev, tapanyag, El_e) {}
virtual SugarzasIgeny nap(Sugarzas sugarzas) ;
};
class Parabokor : public Plant
{
public:
Parabokor(const std::string &nev, int tapanyag, bool El_e) : Plant(nev, tapanyag, El_e) {}
virtual SugarzasIgeny nap(Sugarzas sugarzas) ;
};
Plant.cpp
#include "Plant.h"
SugarzasIgeny Puffancs::nap(Sugarzas sugarzas)
{
switch (sugarzas)
{
case Sugarzas::ALFA :
_tapanyag += 2;
break;
case Sugarzas::DELTA :
_tapanyag -= 2;
break;
case Sugarzas::NONE :
_tapanyag -= 1;
break;
default:
break;
}
if ( _tapanyag > 0 && _tapanyag <=10){
_El_e = false;
}else {
_El_e = true;
}
return SugarzasIgeny(Sugarzas::ALFA, 10);
}
SugarzasIgeny Deltafa::nap(Sugarzas sugarzas)
{
switch (sugarzas)
{
case Sugarzas::ALFA :
_tapanyag -= 3;
break;
case Sugarzas::DELTA :
_tapanyag += 4;
break;
case Sugarzas::NONE :
_tapanyag -= 1;
break;
default:
break;
}
if (_tapanyag > 0)
{_El_e = true;}
else {_El_e = false; }
if (_tapanyag <5 ){
return SugarzasIgeny(Sugarzas::DELTA, 4);
}
else if (_tapanyag >= 5 && _tapanyag <= 10 ){
return SugarzasIgeny(Sugarzas::DELTA, 1);
}else {
return SugarzasIgeny(Sugarzas::NONE, 10);
}
}
SugarzasIgeny Parabokor::nap(Sugarzas sugarzas)
{
switch (sugarzas)
{
case Sugarzas::ALFA :
_tapanyag++;
break;
case Sugarzas::DELTA :
_tapanyag++;
break;
case Sugarzas::NONE :
_tapanyag--;
break;
default:
break;
}
// _El_e = tapanyag > 0;
if (_tapanyag > 0){_El_e = true;}
else {_El_e = false;}
return SugarzasIgeny(Sugarzas::NONE, 10);
}
Just define Plant::nap:
class Plant {
public:
...
// Probably stupid code, but just for example
virtual SugarzasIgeny nap(Sugarzas sugarzas) { return SugarzasIgeny(Sugarzas::ALFA); }
...
};
or make it pure virtual:
class Plant {
public:
...
// Pure virtual, no need for code, but class is then non instanciable
virtual SugarzasIgeny nap(Sugarzas sugarzas)=0;
...
};

'Read access violation' exception in an SFML/C++ project:

I'm recently learning to make a 2d game in SFML using a tutorial series on youtube by Suraj Sharma(Video 57):
https://www.youtube.com/watch?v=kwd_AVCkvXE&list=PL6xSOsbVA1ebkU66okpi-KViAO8_9DJKg&index=57
His Source Code:
https://github.com/Headturna/SFML_RPG
Right now he's teaching me to symplify the game's menu system by making a mini class 'StateData' in the parent class 'State' so any inherited class can access 'State' parameters via 'StateData'(Ex:MainMenu(StData* Stdata){}).
After debugging the game seems to runs fine.But everytime i click on something on the menu(start,settings,etc...) a 'Read access violation:Stdata was nullptr' occurs in the 'State' class constructor.
Here's the code:
State.h:
#pragma once
#ifndef STATE_H
#define STATE_H
#include "Player.h"
#include "GrphSettings.h"
class Player;
class GrphSettings;
class State;
class StData {
public:
StData(){}
//Vars
float GridSize;
sf::RenderWindow* Window;
GrphSettings* GSettings;
std::map<std::string, int>* SupportedKeys;
std::stack<State*>* states;
};
class State
{
private:
protected:
StData* Stdata;
std::stack<State*>* states;
sf::RenderWindow* window;
std::map<std::string, int>* SupportedKeys ;
std::map<std::string, int> Keybinds;
bool quit;
bool pause;
float keyTime;
float keyTimeMax;
float GridSize;
sf::Vector2i MousePosScr;
sf::Vector2i MousePosWind;
sf::Vector2f MousePosView;
//Resources
std::map<std::string,sf::Texture> texture;
//Funcs
virtual void InitKeybinds() = 0;
public:
State(StData* Stdata);
virtual~State();
//Access
const bool getKeytime();
const bool& getquit()const;
//Funcs
void Endstate();
void PauseSt();
void UnPauseSt();
virtual void UpdateInput(const float& dt) = 0;
virtual void UpdateMousePos();
virtual void UpdateKeyTime(const float& dt);
virtual void Update(const float& dt) = 0;
virtual void Render(sf::RenderTarget* target = nullptr) = 0;
};
#endif // !1
State.cpp:
#include "pch.h"
#include "State.h"
State::State(StData* Stdata)
{
this->Stdata = Stdata;
this->window = Stdata->Window;
this->SupportedKeys = Stdata->SupportedKeys;
this->states = Stdata->states;
this->quit = false;
this->pause = false;
this->keyTime = 0.f;
this->keyTimeMax = 10.f;
this->GridSize = Stdata->GridSize;
}
State::~State()
{
}
//Access
const bool State::getKeytime()
{
if (this->keyTime >= this->keyTimeMax) {
this->keyTime = 0.f;
return true;
}
return false;
}
const bool& State::getquit() const
{
// TODO: insert return statement here
return this->quit;
}
//Funcs
void State::Endstate()
{
this->quit = true;
}
void State::PauseSt()
{
this->pause = true;
}
void State::UnPauseSt()
{
this->pause = false;
}
void State::UpdateMousePos()
{
this->MousePosScr = sf::Mouse::getPosition();
this->MousePosWind = sf::Mouse::getPosition(*this->window);
this->MousePosView = this->window->mapPixelToCoords(sf::Mouse::getPosition(*this->window));
}
void State::UpdateKeyTime(const float& dt)
{
if (this->keyTime < this->keyTimeMax)
this->keyTime += 100.f * dt;
}
Every button one the menu(start,exit...)is an inherited state from 'State' class.This is,for example,the 'Edit' state.
Edit.h:
#pragma once
#ifndef EDIT_H
#define EDIT_H
#include "State.h"
#include "Gui.h"
#include "PauseMenu.h"
#include "TileMap.h"
class State;
class Gui;
class PauseMenu;
class TileMap;
class Edit:public State
{
private:
//Vars
sf::Font Fnt;
PauseMenu* PMenu;
std::map<std::string, gui::Button*> buttons;
TileMap Map;
//Functions
void InitVars();
void InitBackGrnd();
void InitFonts();
void InitKeybinds();
void InitPauseMenu();
void InitBtn();
public:
Edit(StData* Stdata);
virtual~Edit();
//Functions
void UpdateInput(const float& dt);
void Update(const float& dt);
void UpdatePButtons();
void UpdateBtn();
void Endstate();
void RenderBtn(sf::RenderTarget& target);
void Render(sf::RenderTarget* target = nullptr);
};
#endif // ! EDIT_H
Edit.cpp:
#include "pch.h"
#include "Edit.h"
void Edit::InitVars()
{
}
void Edit::InitBackGrnd()
{
}
void Edit::InitFonts()
{
if (!this->Fnt.loadFromFile("Fonts/SPACEMAN.ttf")) {
throw("Error::Edit::Couldn't load font");
}
}
void Edit::InitKeybinds()
{
std::ifstream ifs("Config/EditKeys.ini");
if (ifs.is_open()) {
std::string key = "";
std::string key2 = "";
int keyval = 0;
while (ifs >> key >> key2)
{
this->Keybinds[key] = this->SupportedKeys->at(key2);
}
}
ifs.close();
this->Keybinds["Close"] = this->SupportedKeys->at("ESC");
this->Keybinds["Left"] = this->SupportedKeys->at("A");
this->Keybinds["Right"] = this->SupportedKeys->at("D");
this->Keybinds["Up"] = this->SupportedKeys->at("W");
this->Keybinds["Down"] = this->SupportedKeys->at("S");
}
void Edit::InitPauseMenu()
{
this->PMenu = new PauseMenu(*this->window, this->Fnt);
this->PMenu->addButtons("Quit", 800.f, "Quit");
}
void Edit::InitBtn()
{
}
Edit::Edit(StData* Stdata)
:State(Stdata)
{
this->InitVars();
this->InitBackGrnd();
this->InitFonts();
this->InitKeybinds();
this->InitPauseMenu();
this->InitBtn();
}
Edit::~Edit()
{
auto it = this->buttons.begin();
for (it = this->buttons.begin(); it != this->buttons.end(); ++it) {
delete it->second;
}
delete this->PMenu;
}
//Funcs
void Edit::UpdateInput(const float& dt)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key(this->Keybinds.at("Close")))
&& this->getKeytime()) {
if (!this->pause)
this->PauseSt();
else this->UnPauseSt();
}
}
void Edit::Update(const float& dt)
{
this->UpdateMousePos();
this->UpdateKeyTime(dt);
this->UpdateInput(dt);
if (!this->pause) {//Unpaused
this->UpdateBtn();
}
else {//Paused
this->PMenu->Update(this->MousePosView);
this->UpdatePButtons();
}
this->UpdateBtn();
std::cout << this->MousePosView.x << " " << this->MousePosView.y << "\r";
}
void Edit::UpdatePButtons()
{
if (this->PMenu->isPressed("Quit"))
this->Endstate();
}
void Edit::UpdateBtn()
{ //Update buttons and handle their functions
for (auto& it : this->buttons) {
it.second->Update(MousePosView);
}
}
void Edit::Endstate()
{
std::cout << "Ending State" << "\n";
}
void Edit::RenderBtn(sf::RenderTarget& target)
{
for (auto& it : this->buttons) {
it.second->Render(target);
}
}
void Edit::Render(sf::RenderTarget* target)
{
if (!target)
target = this->window;
this->RenderBtn(*target);
this->Map.Render(*target);
if (this->pause) {//Pause Menu
this->PMenu->Render(*target);
}
sf::Text MText;
MText.setPosition(this->MousePosView);
MText.setFont(this->Fnt);
MText.setCharacterSize(12);
std::stringstream ss;
ss << this->MousePosView.x << ' ' << this->MousePosView.y;
MText.setString(ss.str());
target->draw(MText);
}
Can anyone help me ?

SMFL - Only 1 of 2 object gets drawn

I'm having trouble drawing a custom sf::Drawable derived object.
//Textbox.h
#pragma once
#include "Header.h"
#ifndef TEXTBOX_H
#define TEXTBOX_H
class Textbox : public Drawable {
public:
Textbox(int max_chars, bool numeric);
Textbox(int max_chars);
Textbox(bool numeric);
Textbox();
void setTextColor(Color color);
void setPosition(float x, float y);
Vector2f getPosition() {
return m_gshape.getPosition();
}
Vector2f getSize();
String getString();
void setFocus(bool value);
bool isFocused();
void input(Uint32 text_char);
void clear();
private:
virtual void Textbox::draw(sf::RenderTarget& target, sf::RenderStates states) const {
target.draw(m_gshape, states);
target.draw(m_textbox, states);
}
unsigned int max_length;
int min_ascii = 32;
int max_ascii = 127;
bool focus;
string content;
Text m_textbox;
RectangleShape m_gshape;
};
#endif // !TEXTBOX_H
And
//Textbox.cpp
#pragma once
#include "Textbox.h"
Textbox::Textbox(int max_chars, bool numeric) {
max_length = max_chars;
m_gshape.setSize(Vector2f(6 + 15 * max_length, 30));
m_gshape.setFillColor(Color::White);
m_gshape.setOutlineThickness(2);
m_gshape.setOutlineColor(Color(60, 60, 60));
m_gshape.setPosition(0, 0);
m_textbox.setFont(default_font);
m_textbox.setCharacterSize(25);
m_textbox.setFillColor(Color::White);
if (max_chars > 1)
m_textbox.setString(to_string((int)pow(10, max_chars - 1)));
else
m_textbox.setString("0");
if (numeric) {
min_ascii = 47;
max_ascii = 58;
}
}
Textbox::Textbox(int max_chars) : Textbox(max_chars, false) {}
Textbox::Textbox(bool numeric) : Textbox(2, numeric) {}
Textbox::Textbox() : Textbox(2, false) {}
void Textbox::setTextColor(Color color) {
m_textbox.setFillColor(color);
}
void Textbox::setPosition(float x, float y) {
FloatRect textbox_bounds = m_textbox.getGlobalBounds();
m_gshape.setPosition(x, y);
m_textbox.setPosition(m_gshape.getPosition().x + (m_gshape.getSize().x - textbox_bounds.width) / 2 - textbox_bounds.left,
m_gshape.getPosition().y + (m_gshape.getSize().y - textbox_bounds.height) / 2 - textbox_bounds.top);
}
Vector2f Textbox::getSize() {
return m_gshape.getSize();
}
String Textbox::getString() {
return m_textbox.getString();
}
void Textbox::setFocus(bool value) {
focus = true;
}
bool Textbox::isFocused() {
return focus;
}
void Textbox::input(Uint32 text_char) {
content = m_textbox.getString().toAnsiString();
if (text_char == 13) {
focus = false;
return;
}
if (m_textbox.getString().getSize() < max_length) {
if (text_char > min_ascii && text_char < max_ascii) {
m_textbox.setString(m_textbox.getString() + text_char);
}
}
if (text_char == 8 && m_textbox.getString().getSize() > 0) {
content.resize(m_textbox.getString().getSize() - 1);
m_textbox.setString(content);
}
}
void Textbox::clear() {
m_textbox.setString("");
}
Everything works except for the drawing part: while g_shape gets drawn and rendered m_textbox doesn't. I'm sure of this because I can still edit the text, however it's not displayed.
I must admit I didn't fully understand the sf::Drawable inheritance and consequently I'm not sure I overrid draw() correctly.
Thanks to #AlexMeuer I found the solution.
In my header file I had my global font set as extern sf::Font default_font however in my main.cpp I never declared it.

error C2512 in a struct with no explicit constructor

I am receiving error C2512: 'LoadingWorldlet' : no appropriate default constructor available, when I try to compile this file. There is no explicit constructor, so I can not think of a reason for me to be recieving this error.
struct Worldlet {
int x, z;
glm::mat4 worldletMatrix;
std::vector<Voxel> blocks;
};
struct LoadingWorldlet {
int x, z;
std::future<Worldlet> &result;
};
class World {
public:
World();
~World();
void Init();
void InitRenderable();
void UpdateWorldletList(int x, int z);
void Render(Shader* worldShader, Camera *mainPov);
static Worldlet LoadWorldlet(int x, int z, std::ifstream &file);
private:
std::vector<Worldlet> worldlets;
std::vector<LoadingWorldlet> loadingWorldlets;
std::vector<std::string> loadingTitles;
std::vector<int> toRemove;
Renderable cube;
std::string worldName, prefix;
static const float CUBE_SIZE;
static const int LOADLIMIT = 1;
int GetLoadRadius(int r = 0) {
static int i = r;
return i;
}
};
This is the only function in which LoadingWorldlet is used:
void World::UpdateWorldletList(int x, int z) {
static int previousX, previousZ;
for(int index: toRemove) {
worldlets.erase(worldlets.begin() + index);
}
toRemove.clear();
int loaded = 0;
std::vector<int> clearList;
for(auto &loading: loadingWorldlets) {
if(loaded >= LOADLIMIT) break;
worldlets.push_back(loading.result.get());
clearList.push_back(loaded);
loaded++;
}
for(int i: clearList)
loadingWorldlets.erase(loadingWorldlets.begin()+i);
if(previousX != x && previousZ != z) {
int i = 0;
for(auto worldlet: worldlets) {
if(pow(worldlet.x - x, 2) + pow(worldlet.z - z, 2) > GetLoadRadius()) {
toRemove.push_back(i);
}
i++;
}
for(int recX = -GetLoadRadius(); recX < GetLoadRadius(); recX++) {
for(int recZ = -GetLoadRadius(); recZ < GetLoadRadius(); recZ++) {
bool cont = false;
for(auto worldlet: worldlets) {
if (worldlet.x == recX && worldlet.z == recZ) {
cont = true;
break;
}
}
for(auto loading: loadingWorldlets) {
if (loading.x == recX && loading.z == recZ) {
cont = true;
break;
}
}
if(cont || pow(recX - x, 2) + pow(recZ - z, 2) > GetLoadRadius())
continue;
std::ifstream file("./worlds/" + worldName + "/" + prefix + std::to_string(recX) + "X" + std::to_string(recZ) + "Z.json");
if (!file)
continue;
LoadingWorldlet loading;
loading.x = recX;
loading.z = recZ;
loading.result = std::async(LoadWorldlet, recX, recZ, file);
loadingWorldlets.push_back(loading);
}
}
}
}
I have tried adding a default constructor, but then I receive an error about a missing = operator. Doesn't the compiler automatically add in these things? How can I fix the error? If it is important, I am using Visual Studio 2013 preview.
Looking over the code, you'll need to a way to instantiate your reference to std::future<Worldlet> &result;
Typically, this is done through a constructor.
struct LoadingWorldlet
{
LoadingWorldlet( std::future<Worldlet> & inWorldlet ):
result( inWorldlet ) {}
int x, z;
std::future<Worldlet> &result;
};
Otherwise, you could simply not make the data member a reference ( this assumes that other data members don't also have mandatory constructors):
std::future<Worldlet> result;