1
General / Re: Better way to create particle as Vertex Array?
« on: February 05, 2015, 04:54:13 pm »
.h
#ifndef PIXELCREATOR_HPP
#define PIXELCREATOR_HPP
#include <SFML\Graphics.hpp>
#include <iostream>
class Pixelcreator
{
public:
Pixelcreator(int posx, int posy);
~Pixelcreator();
void update(float frametime);
void handle();
void render(sf::RenderWindow *rnd);
const bool getIsAlive() {return mIsAlive;};
void setIsAlive(bool alive) {mIsAlive = alive;};
void set_alpha(float alph) {iAlpha = alph;};
const float get_ran_0() {return ran[0];};
void set_ran_0(float ran_0) {ran[0] = ran_0;};
const float get_ran_1() {return ran[1];};
void set_ran_1(float ran_1) {ran[1] = ran_1;};
const bool get_bX() {return bX;};
const bool get_bY() {return bY;};
void set_bX(bool bx) {bX = bx;};
void set_bY(bool by) {bY = by;};
const float get_grav() {return fgrav;};
void set_grav(float gra) {fgrav = gra;};
void set_default() {ran[0] = defran_0; ran[1] = defran_1;};
const float get_horiz() {return horiz;};
void set_horiz(float hor) {horiz = hor;};
const float get_vert() {return vert;};
void set_vert(float ver) {vert = ver;};
private:
sf::VertexArray pointmap;
sf::Clock *pClock;
float defran_0, defran_1;
bool mIsAlive;
int iAlpha;
int irand;
float horiz;
float vert;
float fgrav;
bool bX;
bool bFirst;
bool bY;
float ran[];
float rand_dom(float a, float b)
{
return ((b-a)*((float)rand()/RAND_MAX))+a;
}
};
#endif
.cpp
#include "pixelcreator.h"
using namespace std;
template<class T>
T roundd(T a){
if(a < 0.05) {a = 0;
}
else
if(a > 0.05 && a < 0.01) {a = 0.01;
}
else
if(a > 0.01 && a < 0.02) {a = 0.02;
}
else
if(a > 0.02 && a < 0.03) {a = 0.03;
}
else
if(a > 0.03 && a < 0.04) {a = 0.04;
}
else
if(a > 0.04 && a < 0.05) {a = 0.05;
}
else
if(a > 0.05 && a < 0.06) {a = 0.06;
}
else
if(a > 0.06 && a < 0.07) {a = 0.07;
}
else
if(a > 0.07 && a < 0.08) {a = 0.08;
}
else
if(a > 0.08 && a < 0.09) {a = 0.09;
}
T fin = a;
return fin;
}
Pixelcreator::Pixelcreator(int posx, int posy){
pClock = new sf::Clock();
pClock->restart();
setIsAlive(true);
horiz = posx;
vert = posy;
for( int a = 0; a < 16; a++) {
pointmap.append(sf::Vertex(sf::Vector2f((a % 4) + horiz, (a / 4) + vert), sf::Color::Green));
pointmap[a].color.a = static_cast<sf::Uint8>(iAlpha);
}
fgrav = 0.04;
ran[0] = roundd(ran[0]);
ran[1] = roundd(ran[1]);
ran[0] = rand_dom(0.0,0.09);
ran[1] = rand_dom(0.0,0.09);
defran_0 = ran[0];
defran_1 = ran[1];
switch(rand() % 4)
{
case 0: iAlpha = 255; break;
case 1: iAlpha = 220; break;
case 2: iAlpha = 160; break;
case 3: iAlpha = 150; break;
case 4: iAlpha = 90; break;
}
irand = rand() % 4;
switch(irand)
{
case 0: bX = true; bY = false; break;
case 1: bX = false; bY = true; break;
case 2: bX = false; bY = false; break;
case 3: bX = true; bY = true; break;
}
bFirst = true;
}
Pixelcreator::~Pixelcreator(){
delete pClock;
pClock = nullptr;
}
void Pixelcreator::update(float frametime){
if(ran[1] <= 0 && bY == false) {bY == true;};
if(horiz >= 634 ) {bX = false;}
if(vert >= 634 ) {bY = false;}
if(horiz <= 0 ) {bX = true;}
if(vert <= 0 ) {bY = true;}
if(bX == true) horiz += ran[0];
if(bX == false) horiz -= ran[0];
if(bY == false) { vert -= (ran[1] = ran[1]-((ran[1]*1.5)+fgrav)*frametime);}
if(bY == true) vert += ran[1];
if(pClock->getElapsedTime().asMilliseconds() > 50.f) {iAlpha -= 5; pClock->restart();}
if(iAlpha < 0) iAlpha = 0;
for(int u = 0; u != 16; u++){
//pointmap.append(sf::Vertex(sf::Vector2f((u % 4) + horiz, (u / 4) + vert), sf::Color::Green));
pointmap[u].position.x = (u % 4) + horiz;
pointmap[u].position.y = (u / 4) + vert;
pointmap[u].color.a = static_cast<sf::Uint8>(iAlpha);
}
if(iAlpha <= 20) setIsAlive(false);
if(getIsAlive() == false) pointmap.clear();
}
void Pixelcreator::handle(){
}
void Pixelcreator::render(sf::RenderWindow *rnd){
rnd->draw(pointmap);
}
#ifndef PIXELCREATOR_HPP
#define PIXELCREATOR_HPP
#include <SFML\Graphics.hpp>
#include <iostream>
class Pixelcreator
{
public:
Pixelcreator(int posx, int posy);
~Pixelcreator();
void update(float frametime);
void handle();
void render(sf::RenderWindow *rnd);
const bool getIsAlive() {return mIsAlive;};
void setIsAlive(bool alive) {mIsAlive = alive;};
void set_alpha(float alph) {iAlpha = alph;};
const float get_ran_0() {return ran[0];};
void set_ran_0(float ran_0) {ran[0] = ran_0;};
const float get_ran_1() {return ran[1];};
void set_ran_1(float ran_1) {ran[1] = ran_1;};
const bool get_bX() {return bX;};
const bool get_bY() {return bY;};
void set_bX(bool bx) {bX = bx;};
void set_bY(bool by) {bY = by;};
const float get_grav() {return fgrav;};
void set_grav(float gra) {fgrav = gra;};
void set_default() {ran[0] = defran_0; ran[1] = defran_1;};
const float get_horiz() {return horiz;};
void set_horiz(float hor) {horiz = hor;};
const float get_vert() {return vert;};
void set_vert(float ver) {vert = ver;};
private:
sf::VertexArray pointmap;
sf::Clock *pClock;
float defran_0, defran_1;
bool mIsAlive;
int iAlpha;
int irand;
float horiz;
float vert;
float fgrav;
bool bX;
bool bFirst;
bool bY;
float ran[];
float rand_dom(float a, float b)
{
return ((b-a)*((float)rand()/RAND_MAX))+a;
}
};
#endif
.cpp
#include "pixelcreator.h"
using namespace std;
template<class T>
T roundd(T a){
if(a < 0.05) {a = 0;
}
else
if(a > 0.05 && a < 0.01) {a = 0.01;
}
else
if(a > 0.01 && a < 0.02) {a = 0.02;
}
else
if(a > 0.02 && a < 0.03) {a = 0.03;
}
else
if(a > 0.03 && a < 0.04) {a = 0.04;
}
else
if(a > 0.04 && a < 0.05) {a = 0.05;
}
else
if(a > 0.05 && a < 0.06) {a = 0.06;
}
else
if(a > 0.06 && a < 0.07) {a = 0.07;
}
else
if(a > 0.07 && a < 0.08) {a = 0.08;
}
else
if(a > 0.08 && a < 0.09) {a = 0.09;
}
T fin = a;
return fin;
}
Pixelcreator::Pixelcreator(int posx, int posy){
pClock = new sf::Clock();
pClock->restart();
setIsAlive(true);
horiz = posx;
vert = posy;
for( int a = 0; a < 16; a++) {
pointmap.append(sf::Vertex(sf::Vector2f((a % 4) + horiz, (a / 4) + vert), sf::Color::Green));
pointmap[a].color.a = static_cast<sf::Uint8>(iAlpha);
}
fgrav = 0.04;
ran[0] = roundd(ran[0]);
ran[1] = roundd(ran[1]);
ran[0] = rand_dom(0.0,0.09);
ran[1] = rand_dom(0.0,0.09);
defran_0 = ran[0];
defran_1 = ran[1];
switch(rand() % 4)
{
case 0: iAlpha = 255; break;
case 1: iAlpha = 220; break;
case 2: iAlpha = 160; break;
case 3: iAlpha = 150; break;
case 4: iAlpha = 90; break;
}
irand = rand() % 4;
switch(irand)
{
case 0: bX = true; bY = false; break;
case 1: bX = false; bY = true; break;
case 2: bX = false; bY = false; break;
case 3: bX = true; bY = true; break;
}
bFirst = true;
}
Pixelcreator::~Pixelcreator(){
delete pClock;
pClock = nullptr;
}
void Pixelcreator::update(float frametime){
if(ran[1] <= 0 && bY == false) {bY == true;};
if(horiz >= 634 ) {bX = false;}
if(vert >= 634 ) {bY = false;}
if(horiz <= 0 ) {bX = true;}
if(vert <= 0 ) {bY = true;}
if(bX == true) horiz += ran[0];
if(bX == false) horiz -= ran[0];
if(bY == false) { vert -= (ran[1] = ran[1]-((ran[1]*1.5)+fgrav)*frametime);}
if(bY == true) vert += ran[1];
if(pClock->getElapsedTime().asMilliseconds() > 50.f) {iAlpha -= 5; pClock->restart();}
if(iAlpha < 0) iAlpha = 0;
for(int u = 0; u != 16; u++){
//pointmap.append(sf::Vertex(sf::Vector2f((u % 4) + horiz, (u / 4) + vert), sf::Color::Green));
pointmap[u].position.x = (u % 4) + horiz;
pointmap[u].position.y = (u / 4) + vert;
pointmap[u].color.a = static_cast<sf::Uint8>(iAlpha);
}
if(iAlpha <= 20) setIsAlive(false);
if(getIsAlive() == false) pointmap.clear();
}
void Pixelcreator::handle(){
}
void Pixelcreator::render(sf::RenderWindow *rnd){
rnd->draw(pointmap);
}