Author: enetheru Date: Thu Jun 25 21:44:51 2009 New Revision: 349 Log: made healthbar shrinkable from directions:up,down,left,right. touched up object class, fixed missing include in menu.cpp Modified: trunk/src/healthbar.cpp trunk/src/healthbar.h trunk/src/menu.cpp trunk/src/newobject.cpp trunk/src/newobject.h trunk/src/testing.cpp Modified: trunk/src/healthbar.cpp ============================================================================== --- trunk/src/healthbar.cpp Thu Jun 25 07:51:57 2009 (r348) +++ trunk/src/healthbar.cpp Thu Jun 25 21:44:51 2009 (r349) @@ -2,6 +2,15 @@ PVN_HealthBar::PVN_HealthBar() { + SetImage(SDL_CreateRGBSurface(SDL_SWSURFACE,64,64,32,0,0,0,0)); + SetFrame(new SDL_Rect); + SetPosition(new SDL_Rect); + newposition = new SDL_Rect; + GetFrame()->x = 0; + GetFrame()->y = 0; + GetFrame()->w = GetImage()->w; + GetFrame()->h = GetImage()->h; + SetPosition2i(0,0); max = 100; current = 100; return; @@ -34,41 +43,94 @@ return current; } -void PVN_HealthBar::Init() +void PVN_HealthBar::Add(int arg_value) +{ + current += arg_value; + return; +} + +void PVN_HealthBar::ReCalcLeft() { - if( GetFrame() == NULL ) + if( current >= max ) GetFrame()->w = GetImage()->w; + else if( current <=0 ) GetFrame()->w = 0; + else { - SetFrame(new SDL_Rect); - GetFrame()->x = GetFrame()->y = 0; + float percent = (float)current / (float)max; + GetFrame()->w = (int)((float)GetImage()->w * percent); + } + newposition->x = GetPosition()->x; + newposition->y = GetPosition()->y; + return; +} + +void PVN_HealthBar::ReCalcRight() +{ + if( current >= max ) + { + newposition->x = GetPosition()->x; + GetFrame()->x = 0; GetFrame()->w = GetImage()->w; - GetFrame()->h = GetImage()->h; } - if( GetPosition() == NULL ) + else if( current <= 0 ) + { + newposition->x = GetPosition()->x + GetImage()->w; + GetFrame()->x = GetImage()->w; + GetFrame()->w = 0; + } + else { - SetPosition(new SDL_Rect); - GetPosition()->x = GetPosition()->y = 0; - GetPosition()->w = GetImage()->w; - GetPosition()->h = GetImage()->h; - } + float percent = (float)current / (float)max; + GetFrame()->w = (int)((float)GetImage()->w * percent); + GetFrame()->x = GetImage()->w - GetFrame()->w; + newposition->x = GetPosition()->x + GetFrame()->x; + } + newposition->y = GetPosition()->y; return; } -void PVN_HealthBar::Add(int arg_value) + +void PVN_HealthBar:: ReCalcUp() { - current += arg_value; + if( current >= max ) GetFrame()->h = GetImage()->h; + else if( current <=0 ) GetFrame()->h = 0; + else + { + float percent = (float)current / (float)max; + GetFrame()->h = (int)((float)GetImage()->h * percent); + } + newposition->x = GetPosition()->x; + newposition->y = GetPosition()->y; return; } -void PVN_HealthBar::ReCalc() +void PVN_HealthBar::ReCalcDown() { - if(current > 0 && current < max) + if( current >= max ) + { + newposition->y = GetPosition()->y; + GetFrame()->y = 0; + GetFrame()->h = GetImage()->h; + } + else if( current <= 0 ) + { + newposition->y = GetPosition()->y + GetImage()->h; + GetFrame()->y = GetImage()->h; + GetFrame()->h = 0; + } + else { float percent = (float)current / (float)max; - GetFrame()->w = (int)((float)GetImage()->w * percent); + GetFrame()->h = (int)((float)GetImage()->h * percent); + GetFrame()->y = GetImage()->h - GetFrame()->h; + newposition->y = GetPosition()->y + GetFrame()->y; } - else if( current <=0 ) GetFrame()->w = 0; - else GetFrame()->w = GetImage()->w; - + newposition->x = GetPosition()->x; return; } - + + +void PVN_HealthBar::Blit(SDL_Surface *arg_destination) +{ + SDL_BlitSurface(GetImage(), GetFrame(), arg_destination, newposition); + return; +} Modified: trunk/src/healthbar.h ============================================================================== --- trunk/src/healthbar.h Thu Jun 25 07:51:57 2009 (r348) +++ trunk/src/healthbar.h Thu Jun 25 21:44:51 2009 (r349) @@ -8,6 +8,7 @@ { private: int max, current; + SDL_Rect *newposition; public: PVN_HealthBar(); ~PVN_HealthBar(); @@ -15,9 +16,12 @@ void SetCurrent(int); int GetMax(); int GetCurrent(); - void Init(); void Add(int); - void ReCalc(); + void ReCalcLeft(); + void ReCalcRight(); + void ReCalcUp(); + void ReCalcDown(); + void Blit(SDL_Surface *); }; #endif /*PVN_HEALTHBAR_H*/ Modified: trunk/src/menu.cpp ============================================================================== --- trunk/src/menu.cpp Thu Jun 25 07:51:57 2009 (r348) +++ trunk/src/menu.cpp Thu Jun 25 21:44:51 2009 (r349) @@ -4,6 +4,7 @@ #include "util.h" #include "fight.h" #include "credits.h" +#include "testing.h" PVN_menu::PVN_menu(PVN_game* game) : PVN_state(game) { Modified: trunk/src/newobject.cpp ============================================================================== --- trunk/src/newobject.cpp Thu Jun 25 07:51:57 2009 (r348) +++ trunk/src/newobject.cpp Thu Jun 25 21:44:51 2009 (r349) @@ -18,19 +18,32 @@ void PVN_Object::SetImage(SDL_Surface *arg_surface) { + SDL_FreeSurface(image); image = arg_surface; } void PVN_Object::SetFrame(SDL_Rect *arg_rect) { + delete frame; frame = arg_rect; } void PVN_Object::SetPosition(SDL_Rect *arg_rect) { + delete position; position = arg_rect; } +void PVN_Object::SetPosition2i(int arg_x, int arg_y) +{ + if(position != NULL) + { + position->x = arg_x; + position->y = arg_y; + } + return; +} + SDL_Surface *PVN_Object::GetImage() { return image; Modified: trunk/src/newobject.h ============================================================================== --- trunk/src/newobject.h Thu Jun 25 07:51:57 2009 (r348) +++ trunk/src/newobject.h Thu Jun 25 21:44:51 2009 (r349) @@ -15,6 +15,7 @@ void SetImage(SDL_Surface *); void SetFrame(SDL_Rect *); void SetPosition(SDL_Rect *); + void SetPosition2i(int, int); SDL_Surface *GetImage(); SDL_Rect *GetFrame(); SDL_Rect *GetPosition(); Modified: trunk/src/testing.cpp ============================================================================== --- trunk/src/testing.cpp Thu Jun 25 07:51:57 2009 (r348) +++ trunk/src/testing.cpp Thu Jun 25 21:44:51 2009 (r349) @@ -11,20 +11,18 @@ player1 = new Player(100,100,128,128,500,200); healthbar1 = new PVN_HealthBar; - healthbar1->SetImage(Image_Load("../data/images/tubereflectionssex.png")); +// healthbar1->SetImage(Image_Load("../data/images/tubereflectionssex.png")); healthbar1->SetMax(100); healthbar1->SetCurrent(100); - healthbar1->Init(); - healthbar1->ReCalc(); + healthbar1->ReCalcLeft(); player2 = new Player(600,100,128,128,500,200); healthbar2 = new PVN_HealthBar; healthbar2->SetImage(Image_Load("../data/images/tubereflectionssex.png")); healthbar2->SetMax(100); healthbar2->SetCurrent(100); - healthbar2->Init(); healthbar2->GetPosition()->x = 300; - healthbar2->ReCalc(); + healthbar2->ReCalcLeft(); } PVN_test::~PVN_test() @@ -138,14 +136,14 @@ { player2->health -= 10; healthbar2->Add(-10); - healthbar2->ReCalc(); + healthbar2->ReCalcRight(); } if( abs((int)player2->yVel) > abs((int)player1->yVel) ) { player1->health -= 10; healthbar1->Add(-10); - healthbar1->ReCalc(); + healthbar1->ReCalcDown(); } if( player1->health <= 0 )player1->Die();