f220d2e9b9
This is pretty major, so there'll probably be something wrong with it... even though I did spend a few hours looking.
NOTES:
* If you've Retrieved it, don't delete it--- Use resources.Release(Whatever).
Textures automatically release subordinate quads.
* Most of the time, use resources.RetrieveQuad to grab a quad. Should handle everything for you.
RetrieveQuad will load the required texture, if needed.
Only managed resources have a resource name ("back", "simon", etc).
Managed resources can be retrieved with GetTexture/GetQuad/GetWhatever.
Non managed quads lookup by position/dimensions, defaulting to the whole texture.
* Use resources.RetrieveTexture only when you need to do something special to it.
Calling retrieve texture with RETRIEVE_MANAGE will permanently add a texture to the manager
RETRIEVE_LOCK and RETRIEVE_VRAM will lock a texture. It will not leave the cache until
Release(JTexture*) is called, or as a last resort during cache overflow.
* Try to only store (as a class member) pointers to textures retrieved with RETRIEVE_MANAGE.
All others may become invalid, although locked textures do have a high degree of stability. It's
pretty safe to store a locked texture if you're not going to load much between uses.
There's a lot going on here, so I might have missed something... but it runs through the test suite alright.
TODO:
* When called without any arguments, RetrieveQuad sometimes leaves a thin border around the image.
This can be bypassed by specifying a quad one or two pixels less than the image size. Why?
* I've had a crash while runing the Demo mode, something to do with receiveEventMinus?
This hasn't exactly reproduced on a clean SVN copy, (being a hang, rather than a crash) so
I've probably done something to worsen the problem somehow? I'll look into it tomorrow.
* Clean up lock/unlock system, memory usage. Streamline interface, consider phasing out calls using GetWhatever() format.
179 lines
4.9 KiB
C++
179 lines
4.9 KiB
C++
#include <iostream>
|
|
#include "../include/GuiMana.h"
|
|
|
|
using std::cout;
|
|
using std::endl;
|
|
|
|
|
|
const float ManaIcon::DESTX = 440;
|
|
const float ManaIcon::DESTY = 20;
|
|
|
|
ManaIcon::ManaIcon(int color, float x, float y) : Pos(x, y, 0.5, 0.0, 255), f(-1), mode(ALIVE), color(color)
|
|
{
|
|
switch (color)
|
|
{
|
|
case Constants::MTG_COLOR_RED :
|
|
particleSys = NEW hgeParticleSystem("graphics/manared.psi", resources.GetQuad("stars"));
|
|
break;
|
|
case Constants::MTG_COLOR_BLUE :
|
|
particleSys = NEW hgeParticleSystem("graphics/manablue.psi", resources.GetQuad("stars"));
|
|
break;
|
|
case Constants::MTG_COLOR_GREEN :
|
|
particleSys = NEW hgeParticleSystem("graphics/managreen.psi", resources.GetQuad("stars"));
|
|
break;
|
|
case Constants::MTG_COLOR_BLACK :
|
|
particleSys = NEW hgeParticleSystem("graphics/manablack.psi", resources.GetQuad("stars"));
|
|
break;
|
|
case Constants::MTG_COLOR_WHITE :
|
|
particleSys = NEW hgeParticleSystem("graphics/manawhite.psi", resources.GetQuad("stars"));
|
|
break;
|
|
default :
|
|
particleSys = NEW hgeParticleSystem("graphics/mana.psi", resources.GetQuad("stars"));
|
|
}
|
|
icon = manaIcons[color];
|
|
|
|
particleSys->FireAt(x, y);
|
|
|
|
zoomP1 = 0.2 + 0.1 * ((float)rand() / (float)RAND_MAX);
|
|
zoomP2 = 0.2 + 0.1 * ((float)rand() / (float)RAND_MAX);
|
|
zoomP3 = 2 * M_PI * ((float)rand() / (float)RAND_MAX);
|
|
zoomP4 = 2 * M_PI * ((float)rand() / (float)RAND_MAX);
|
|
zoomP5 = 0.5 + ((float)rand() / (float)RAND_MAX);
|
|
zoomP6 = 0.5 + ((float)rand() / (float)RAND_MAX);
|
|
|
|
xP1 = 2 * M_PI * ((float)rand() / (float)RAND_MAX);
|
|
xP2 = 5 + 30 * ((float)rand() / (float)RAND_MAX);
|
|
xP3 = 0.5 + ((float)rand() / (float)RAND_MAX);
|
|
yP1 = 2 * M_PI * ((float)rand() / (float)RAND_MAX);
|
|
yP2 = 5 + 10 * ((float)rand() / (float)RAND_MAX);
|
|
yP3 = 0.5 + ((float)rand() / (float)RAND_MAX);
|
|
|
|
actT = 0;
|
|
tP1 = 0;
|
|
}
|
|
|
|
ManaIcon::~ManaIcon()
|
|
{
|
|
SAFE_DELETE(particleSys);
|
|
}
|
|
|
|
void ManaIcon::Render()
|
|
{
|
|
JRenderer* renderer = JRenderer::GetInstance();
|
|
|
|
renderer->SetTexBlend(BLEND_SRC_ALPHA, BLEND_ONE);
|
|
particleSys->Render();
|
|
renderer->SetTexBlend(BLEND_SRC_ALPHA, BLEND_ONE_MINUS_SRC_ALPHA);
|
|
renderer->RenderQuad(icon, actX, actY, actT, actZ + zoomP1 * sinf(M_PI * zoomP3), actZ + zoomP2 * cosf(M_PI * zoomP4));
|
|
}
|
|
void ManaIcon::Update(float dt)
|
|
{
|
|
xP1 += xP3 * dt;
|
|
actX = x + xP2 * sinf(M_PI * xP1);
|
|
zoomP3 += zoomP5 * dt;
|
|
zoomP4 += zoomP6 * dt;
|
|
|
|
switch (mode)
|
|
{
|
|
case DROPPING :
|
|
f += dt * 700;
|
|
actY += f * dt;
|
|
if (actY > SCREEN_HEIGHT * 2) mode = DEAD;
|
|
break;
|
|
case WITHERING :
|
|
actT += dt * 4;
|
|
actZ /= f; zoomP1 /= f; zoomP2 /= f;
|
|
f -= dt;
|
|
actZ *= f; zoomP1 *= f; zoomP2 *= f;
|
|
yP1 += yP3 * dt;
|
|
actY = y + yP2 * sinf(M_PI * yP1);
|
|
if (f < 0) mode = DEAD;
|
|
break;
|
|
case ALIVE :
|
|
x += 10 * dt * (DESTX - x);
|
|
y += 10 * dt * (DESTY - y);
|
|
yP1 += yP3 * dt;
|
|
actY = y + yP2 * sinf(M_PI * yP1);
|
|
break;
|
|
case DEAD :
|
|
break;
|
|
}
|
|
|
|
particleSys->MoveTo(actX, actY);
|
|
particleSys->Update(dt);
|
|
}
|
|
|
|
void ManaIcon::Wither()
|
|
{
|
|
mode = WITHERING;
|
|
f = 1.0;
|
|
particleSys->Stop();
|
|
}
|
|
void ManaIcon::Drop()
|
|
{
|
|
mode = DROPPING;
|
|
if (f < 0) f = 0;
|
|
particleSys->Stop();
|
|
}
|
|
|
|
GuiMana::GuiMana()
|
|
{
|
|
}
|
|
|
|
GuiMana::~GuiMana(){
|
|
for (vector<ManaIcon*>::iterator it = manas.begin(); it != manas.end(); ++it){
|
|
delete(*it);
|
|
}
|
|
}
|
|
|
|
void GuiMana::Render()
|
|
{
|
|
for (vector<ManaIcon*>::iterator it = manas.begin(); it != manas.end(); ++it)
|
|
(*it)->Render();
|
|
}
|
|
bool remove_dead(ManaIcon* m) { return ManaIcon::DEAD != m->mode; }
|
|
void GuiMana::Update(float dt)
|
|
{
|
|
{
|
|
for (vector<ManaIcon*>::iterator it = manas.begin(); it != manas.end(); ++it)
|
|
(*it)->Update(dt);
|
|
}
|
|
vector<ManaIcon*>::iterator it = partition(manas.begin(), manas.end(), &remove_dead);
|
|
if (it != manas.end())
|
|
{
|
|
for (vector<ManaIcon*>::iterator q = it; q != manas.end(); ++q)
|
|
SAFE_DELETE(*q);
|
|
manas.erase(it, manas.end());
|
|
}
|
|
}
|
|
|
|
int GuiMana::receiveEventPlus(WEvent* e)
|
|
{
|
|
if (WEventEngageMana *event = dynamic_cast<WEventEngageMana*>(e))
|
|
{
|
|
if (event->card && event->card->view)
|
|
manas.push_back(NEW ManaIcon(event->color, event->card->view->actX, event->card->view->actY));
|
|
else
|
|
manas.push_back(NEW ManaIcon(event->color, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2));
|
|
return 1;
|
|
}
|
|
else return 0;
|
|
}
|
|
|
|
int GuiMana::receiveEventMinus(WEvent* e)
|
|
{
|
|
if (WEventConsumeMana *event = dynamic_cast<WEventConsumeMana*>(e))
|
|
{
|
|
for (vector<ManaIcon*>::iterator it = manas.begin(); it != manas.end(); ++it)
|
|
if ((event->color == (*it)->color) && (ManaIcon::ALIVE == (*it)->mode)) { (*it)->Wither(); return 1; }
|
|
return 1;
|
|
}
|
|
else if (dynamic_cast<WEventEmptyManaPool*>(e))
|
|
{
|
|
for (vector<ManaIcon*>::iterator it = manas.begin(); it != manas.end(); ++it)
|
|
(*it)->Drop(); //TODO: split according to which manapool was emptied...
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|