Dobra, sorry, trochę mnie poniosło.
Ale w takim razie w tym miejscu przy konstruktorze muszę wyrzucić to thr(&Ped::update, this) muszę wyrzucic, w funkcji za każdym wywołaniem muszę umieścić thr = new sf::Thread(&Ped::goToCoords, p); a sf::Thread thr w ped.h muszę zrobić sf::Thread *thr; żeby wszystko było w porządku, żebym mógł za każdym razem przeinicjować ten thr. No i od razu zamiast wywoływać funkcję GoToCoords(blablabla); zrobić thr.launch();, wiec mniej więcej.
ped.h:
#ifndef FPED
#define FPED
#include
#include
#include
#include <errno.h>
#include
#ifndef _WIN32
#include <unistd.h>
#include <semaphore.h>
#include <speech-dispatcher/libspeechd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <wait.h>
#include <signal.h>
#endif
#include
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <audio.h>
#include <system.h>
#include <logger.h>
#include <helpers.h>
class Ped
{
public:
enum PED_TYPE
{
CAT,
COW,
DOG,
HUMAN
};
struct arg
{
int index;
float x;
float y;
float z;
PED_TYPE t;
};
Ped();
~Ped(void);
void installAudio(Audio& a);
void launchThread();
void terminateThread();
void createRandomPedsAt3DCoords(int c, float x, float y, float z);
void del(int index);
void setCoords(int index, float& x, float& y, float& z);
int getSize();
void setHealth(int index, int h);
int getHealth(int index);
void update(void);
void goToCoords(arg a);
PED_TYPE getType(int index);
Vector3D getCoords(int index);
private:
struct Pedd
{
sf::Vector2f speed;
bool big;
float x;
float y;
float z;
PED_TYPE t;
int health;
sf::Clock clk[2];
sf::Time tim[2];
int jump;
int interval;
bool missionflag;
};
vector a_ped;
struct 5arg
{
int index;
float x;
float y;
float z;
PED_TYPE t;
};
Audio* audio;
sf::Clock clock[1024];
sf::Time czas[1024];
sf::Thread* thr;
sf::Mutex mu;
};
#endif
następnie:
#include <ped.h>
Ped::Ped()
{
logmsg(“Ped: spawn”);
createRandomPedsAt3DCoords(1000, 0, 0, 0);
}
Ped::~Ped(void)
{
logmsg(“Ped: destroy”);
for (int i = 0; i < getSize(); i++) {
del(i);
}
}
void
Ped::installAudio(Audio& a)
{
this->audio = &a;
}
void
Ped::createRandomPedsAt3DCoords(int peds, float _x, float _y, float _z)
{
logmsg(“Ped: creating %i peds at %.1f %.1f %.1f”, peds, _x, _y, _z);
srand(time(NULL));
for (int i = 0; i < peds; i++) {
Pedd pd;
pd.big = false;
pd.health = 100;
pd.jump = 1;
if (_x == 0 && _y == 0 && _z == 0) {
if (rand() % 10 > 4)
pd.big = true;
else
pd.big = false;
pd.x = rand() % 6000;
pd.y = rand() % 6000;
pd.z = 0;
} else {
pd.x = _x;
pd.y = _y;
pd.z = _z;
}
if (i <= 100) {
pd.t = CAT;
pd.interval = 10;
} else if (i > 100 && i <= 200) {
pd.t = COW;
pd.interval = 20;
} else if (i > 200 && i <= 300) {
pd.t = DOG;
pd.interval = 10;
} else if (i > 300 && i <= peds) {
pd.t = HUMAN;
pd.interval = 22;
}
int radius = 40;
if (pd.big)
radius *= 1.5;
float vx, vy;
vx = (rand() % 2) + 1;
vy = (rand() % 2) + 1;
if (rand() % 2 == 0)
vx *= -1;
if (rand() % 2 == 0)
vy *= -1;
pd.speed = sf::Vector2f(vx / 1.5, vy / 1.5);
a_ped.push_back(pd);
}
logmsg(“Ped: done”);
}
void
Ped::del(int in)
{
for (int i = in; i < a_ped.size() - 1; i++) {
swap(a_ped[i], a_ped[i + 1]);
}
a_ped.resize(a_ped.size() - 1);
}
int
Ped::getSize()
{
return a_ped.size();
}
Ped::PED_TYPE
Ped::getType(int index)
{
return a_ped[index].t;
}
void
Ped::setHealth(int index, int h)
{
a_ped[index].health = h;
}
int
Ped::getHealth(int index)
{
return a_ped[index].health;
}
void
Ped::setCoords(int ind, float& xx, float& yy, float& zz)
{
a_ped[ind].x = xx;
a_ped[ind].y = yy;
a_ped[ind].z = zz;
}
void
Ped::update()
{
srand(time(NULL));
if (getSize() > 0) {
for (int d = 0; d < getSize(); d++) {
logmsg(“Updating PED %i”, d);
if ((a_ped[d].t == CAT)) {
czas[0] = clock[0].getElapsedTime();
int los1 = rand() % 60;
int los2 = rand() % 60;
if (czas[0].asSeconds() <= los1) {
a_ped[d].tim[2] = a_ped[d].clk[2].getElapsedTime();
if (a_ped[d].tim[2].asSeconds() >= 5) {
audio->play3DSound(audio->MEOF01, a_ped[d].x, a_ped[d].y,
a_ped[d].z);
a_ped[d].clk[2].restart();
}
if (a_ped[d].x <= -2000) {
int cx = rand() % 20;
// goToCoords(d, a_ped[d].x + cx, a_ped[d].y, 0, CAT);
arg ar = { d, a_ped[d].x + cx, a_ped[d].y, a_ped[d].z, CAT };
thr = new sf::Thread(&Ped::goToCoords, &ar);
thr->launch();
} else if (a_ped[d].x >= 2000) {
int cx = rand() % 20;
// goToCoords(d, a_ped[d].x - cx, a_ped[d].y, 0, CAT);
arg ar = { d, a_ped[d].x - cx, a_ped[d].y, a_ped[d].z, CAT };
thr = new sf::Thread(&Ped::goToCoords, &ar);
thr->launch();
}
if (a_ped[d].y <= -2000) {
int cy = rand() % 20;
// goToCoords(d, a_ped[d].x, a_ped[d].y + cy, 0, CAT);
arg ar = { d, a_ped[d].x, a_ped[d].y + cy, a_ped[d].z, CAT };
thr = new sf::Thread(&Ped::goToCoords, &ar);
thr->launch();
} else if (a_ped[d].y >= 2000) {
int cy = rand() % 20;
// goToCoords(d, a_ped[d].x, a_ped[d].y - cy, 0, CAT);
arg ar = { d, a_ped[d].x, a_ped[d].y - cy, a_ped[d].z, CAT };
thr = new sf::Thread(&Ped::goToCoords, &ar);
thr->launch();
}
} else if (czas[0].asSeconds() > los1 && czas[0].asSeconds() <= los2) {
a_ped[d].tim[1] = a_ped[d].clk[1].getElapsedTime();
if (a_ped[d].tim[1].asSeconds() >= 5) {
audio->play3DSound(audio->PURE01, a_ped[d].x, a_ped[d].y,
a_ped[d].z);
audio->setSoundLoop(audio->PURE01, true);
a_ped[d].clk[1].restart();
}
if (a_ped[d].x <= -2000) {
int cx = rand() % 20;
// goToCoords(d, a_ped[d].x + cx, a_ped[d].y, 0, CAT);
arg ar = { d, a_ped[d].x + cx, a_ped[d].y, a_ped[d].z, CAT };
thr = new sf::Thread(&Ped::goToCoords, &ar);
thr->launch();
} else if (a_ped[d].x >= 2000) {
int cx = rand() % 20;
// goToCoords(d, a_ped[d].x - cx, a_ped[d].y, 0, CAT);
arg ar = { d, a_ped[d].x - cx, a_ped[d].y, a_ped[d].z, CAT };
thr = new sf::Thread(&Ped::goToCoords, &ar);
thr->launch();
}
if (a_ped[d].y <= -2000) {
int cy = rand() % 20;
// goToCoords(d, a_ped[d].x, a_ped[d].y + cy, 0, CAT);
arg ar = { d, a_ped[d].x, a_ped[d].y + cy, a_ped[d].z, CAT };
thr = new sf::Thread(&Ped::goToCoords, &ar);
thr->launch();
} else if (a_ped[d].y >= 2000) {
int cy = rand() % 20;
// goToCoords(d, a_ped[d].x, a_ped[d].y - cy, 0, CAT);
arg ar = { d, a_ped[d].x, a_ped[d].y - cy, a_ped[d].z, CAT };
thr = new sf::Thread(&Ped::goToCoords, &ar);
thr->launch();
}
clock[0].restart();
}
} else if ((a_ped[d].t = COW)) {
czas[1] = clock[1].getElapsedTime();
int los1 = rand() % 60;
int los2 = rand() % 120;
if (czas[1].asSeconds() >= los1) {
a_ped[d].tim[2] = a_ped[d].clk[2].getElapsedTime();
if (a_ped[d].tim[2].asSeconds() > 5) {
int sound0 = rand() % 2;
if (sound0 == 0) {
audio->play3DSound(audio->M01, a_ped[d].x, a_ped[d].y,
a_ped[d].z);
} else if (sound0 != 0) {
audio->play3DSound(audio->M02, a_ped[d].x, a_ped[d].y,
a_ped[d].z);
}
a_ped[d].clk[2].restart();
}
if (a_ped[d].x <= -3000) {
int cx = rand() % 20;
// goToCoords(d, a_ped[d].x + cx, a_ped[d].y, 0, COW);
arg ar = { d, a_ped[d].x + cx, a_ped[d].y, a_ped[d].z, COW };
thr = new sf::Thread(&Ped::goToCoords, &ar);
thr->launch();
} else if (a_ped[d].x >= 3000) {
int cx = rand() % 20;
// goToCoords(d, a_ped[d].x - cx, a_ped[d].y, 0, COW);
arg ar = { d, a_ped[d].x - cx, a_ped[d].y, a_ped[d].z, COW };
thr = new sf::Thread(&Ped::goToCoords, &ar);
thr->launch();
}
if (a_ped[d].y <= -3000) {
int cy = rand() % 20;
// goToCoords(d, a_ped[d].x, a_ped[d].y + cy, 0, COW);
arg ar = { d, a_ped[d].x, a_ped[d].y + cy, a_ped[d].z, COW };
thr = new sf::Thread(&Ped::goToCoords, &ar);
thr->launch();
} else if (a_ped[d].y >= 3000) {
int cy = rand() % 20;
// goToCoords(d, a_ped[d].x, a_ped[d].y - cy, 0, COW);
arg ar = { d, a_ped[d].x, a_ped[d].y - cy, a_ped[d].z, COW };
thr = new sf::Thread(&Ped::goToCoords, &ar);
thr->launch();
}
} else if (czas[2].asSeconds() > los2) {
a_ped[d].tim[1] = a_ped[d].clk[1].getElapsedTime();
if (a_ped[d].tim[1].asSeconds() > 5) {
audio->play3DSound(audio->M03, a_ped[d].x, a_ped[d].y, a_ped[d].z);
a_ped[d].clk[1].restart();
}
clock[1].restart();
}
} else if ((a_ped[d].t == DOG)) {
czas[2] = clock[2].getElapsedTime();
int los1 = rand() % 60;
int los2 = rand() % 60;
if (czas[2].asSeconds() <= los1) {
a_ped[d].tim[2] = a_ped[d].clk[2].getElapsedTime();
if (a_ped[d].tim[2].asSeconds() >= 5) {
int sound1 = rand() % 2;
if (sound1 == 0)
audio->play3DSound(audio->DOG01, a_ped[d].x, a_ped[d].y,
a_ped[d].z);
else if (sound1 != 0)
audio->play3DSound(audio->DOG02, a_ped[d].x, a_ped[d].y,
a_ped[d].z);
a_ped[d].clk[2].restart();
}
if (a_ped[d].x <= -2000) {
int cx = rand() % 20;
// goToCoords(d, a_ped[d].x + cx, a_ped[d].y, 0, DOG);
arg ar = { d, a_ped[d].x + cx, a_ped[d].y, a_ped[d].z, DOG };
thr = new sf::Thread(&Ped::goToCoords, &ar);
thr->launch();
} else if (a_ped[d].x >= 2000) {
int cx = rand() % 20;
// goToCoords(d, a_ped[d].x - cx, a_ped[d].y, 0, DOG);
arg ar = { d, a_ped[d].x - cx, a_ped[d].y, a_ped[d].z, DOG };
thr = new sf::Thread(&Ped::goToCoords, &ar);
thr->launch();
}
if (a_ped[d].y <= -2000) {
int cy = rand() % 20;
// goToCoords(d, a_ped[d].x, a_ped[d].y + cy, 0, DOG);
arg ar = { d, a_ped[d].x, a_ped[d].y + cy, a_ped[d].z, DOG };
thr = new sf::Thread(&Ped::goToCoords, &ar);
thr->launch();
} else if (a_ped[d].y >= 2000) {
int cy = rand() % 20;
// goToCoords(d, a_ped[d].x, a_ped[d].y - cy, 0, DOG);
arg ar = { d, a_ped[d].x, a_ped[d].y - cy, a_ped[d].z, DOG };
thr = new sf::Thread(&Ped::goToCoords, &ar);
thr->launch();
}
} else if (czas[2].asSeconds() > los1 && czas[2].asSeconds() <= los2) {
a_ped[d].tim[1] = a_ped[d].clk[1].getElapsedTime();
if (a_ped[d].tim[1].asSeconds() >= 5) {
audio->play3DSound(audio->DOG03, a_ped[d].x, a_ped[d].y,
a_ped[d].z);
a_ped[d].clk[1].restart();
}
if (a_ped[d].x <= -2000) {
int cx = rand() % 20;
// goToCoords(d, a_ped[d].x + cx, a_ped[d].y, 0, DOG);
arg ar = { d, a_ped[d].x + cx, a_ped[d].y, a_ped[d].z, DOG };
thr = new sf::Thread(&Ped::goToCoords, &ar);
thr->launch();
} else if (a_ped[d].x >= 2000) {
int cx = rand() % 20;
// goToCoords(d, a_ped[d].x - cx, a_ped[d].y, 0, DOG);
arg ar = { d, a_ped[d].x - cx, a_ped[d].y, a_ped[d].z, DOG };
thr = new sf::Thread(&Ped::goToCoords, &ar);
thr->launch();
}
if (a_ped[d].y <= -2000) {
int cy = rand() % 20;
// goToCoords(d, a_ped[d].x, a_ped[d].y + cy, 0, DOG);
arg ar = { d, a_ped[d].x, a_ped[d].y + cy, a_ped[d].z, DOG };
thr = new sf::Thread(&Ped::goToCoords, &ar);
thr->launch();
} else if (a_ped[d].y >= 2000) {
int cy = rand() % 20;
// goToCoords(d, a_ped[d].x, a_ped[d].y - cy, 0, DOG);
arg ar = { d, a_ped[d].x, a_ped[d].y - cy, a_ped[d].z, DOG };
thr = new sf::Thread(&Ped::goToCoords, &ar);
thr->launch();
}
clock[2].restart();
}
}
if (a_ped[d].health == 0) {
del(d);
}
if (a_ped[d].x > 11000) {
a_ped[d].x = 11000;
} else if (a_ped[d].y > 11000) {
a_ped[d].y = 11000;
}
}
}
}
void
Ped::goToCoords(Ped::arg a)
{
int ind = a.index;
PED_TYPE t = a.t;
do {
a_ped[ind].tim[0] = a_ped[ind].clk[0].getElapsedTime();
if (a_ped[ind].tim[0].asMilliseconds() >= a_ped[ind].interval) {
if (a_ped[ind].x < a.x) {
a_ped[ind].x += 1;
} else if (a_ped[ind].x > a.x) {
a_ped[ind].x -= 1;
}
if (a_ped[ind].y < a.y) {
a_ped[ind].y += 1;
} else if (a_ped[ind].y > a.y) {
a_ped[ind].y -= 1;
}
if (t == CAT || t == DOG) {
audio->play3DSound(audio->STEP7, a_ped[ind].x, a_ped[ind].y,
a_ped[ind].z);
} else if (t == HUMAN) {
audio->play3DSound(audio->STEP6, a_ped[ind].x, a_ped[ind].y,
a_ped[ind].z);
} else if (t == COW) {
audio->play3DSound(audio->STEP8, a_ped[ind].x, a_ped[ind].y,
a_ped[ind].z);
}
a_ped[ind].clk[0].restart();
}
} while (a_ped[ind].x != a.x || a_ped[ind].y != a.y || a_ped[ind].z != a.z);
}
void
Ped::launchThread()
{
thr->launch();
}
void
Ped::terminateThread()
{
thr->terminate();
}
Vector3D
Ped::getCoords(int index)
{
return { a_ped[index].x, a_ped[index].y, a_ped[index].z };
}
ale
In file included from /usr/include/SFML/System/Thread.hpp:193:0,
from /usr/include/SFML/System.hpp:43,
from /usr/include/SFML/Window.hpp:32,
from /usr/include/SFML/Graphics.hpp:32,
from ./headers/ped.h:19,
from ped.cpp:1:
/usr/include/SFML/System/Thread.inl: In instantiation of ‘void sf::priv::ThreadFunctorWithArg<F, A>::run() [with F = void (Ped::)(Ped::arg); A = Ped::arg]’:
ped.cpp:433:1: required from here
/usr/include/SFML/System/Thread.inl:48:25: error: must use ‘.’ or ‘->’ to call pointer-to-member function in ‘((sf::priv::ThreadFunctorWithArg<void (Ped::)(Ped::arg),
Ped::arg>)this)->sf::priv::ThreadFunctorWithArg<void (Ped::)(Ped::arg), Ped::arg*>::m_function (…)’, e.g. ‘(… ->* ((sf::priv::ThreadFunctorWithArg<void (Ped::)(
Ped::arg), Ped::arg>)this)->sf::priv::ThreadFunctorWithArg<void (Ped::)(Ped::arg), Ped::arg*>::m_function) (…)’
virtual void run() {m_function(m_arg);}
^~~~~~~~~~