git repos / blockattack-game

commit 4f1d27f1

sago007 · 2011-03-18 15:53
4f1d27f1fff7440c1eb9f7bfddb16162668d1b15 patch · browse files
parent 12f2640cbf6f47b0e7e8117479f7b3181ed7af55

A little refactoring


git-svn-id: https://blockattack.svn.sourceforge.net/svnroot/blockattack/trunk@108 9d7177f8-192b-0410-8f35-a16a89829b06

Changed files

M source/code/CppSdl/CppSdlImageHolder.cpp before
M source/code/CppSdl/CppSdlImageHolder.hpp before
M source/code/main.cpp before
diff --git a/source/code/CppSdl/CppSdlImageHolder.cpp b/source/code/CppSdl/CppSdlImageHolder.cpp index 909ffd2..5bc5b62 100644 --- a/source/code/CppSdl/CppSdlImageHolder.cpp +++ b/source/code/CppSdl/CppSdlImageHolder.cpp
@@ -19,7 +19,7 @@ CppSdlImageHolder::CppSdlImageHolder(std::string filename) {
throw e;
}
SDL_GetClipRect(data,&area);
-
+ OptimizeForBlit();
counter=1;
}
@@ -49,6 +49,7 @@ CppSdlImageHolder::CppSdlImageHolder(char* rawdata, int datasize) {
}
SDL_GetClipRect(data,&area);
+ OptimizeForBlit();
counter = 1;
}
@@ -108,4 +109,36 @@ void CppSdlImageHolder::Cut(Uint32 x, Uint32 y, Sint32 w = -1, Sint32 h = -1)
area.h = h;
}
-}
\ No newline at end of file
+void CppSdlImageHolder::PaintTo(SDL_Surface* target, int x, int y) {
+ static SDL_Rect dest; //static for reuse
+ dest.x = x;
+ dest.y = y;
+ SDL_BlitSurface(data,&area, target,&dest);
+}
+
+void CppSdlImageHolder::OptimizeForBlit(bool allowAlpha) {
+ static SDL_Surface tmp;
+ if(allowAlpha)
+ tmp = SDL_DisplayFormatAlpha(data);
+ else
+ tmp = SDL_DisplayFormat(data);
+ SDL_FreeSurface(data);
+ data = tmp;
+}
+
+}
+
+/*
+ void DrawIMG(SDL_Surface *img, SDL_Surface * target, int x, int y, int w, int h, int x2, int y2)
+{
+ SDL_Rect dest;
+ dest.x = x;
+ dest.y = y;
+ SDL_Rect dest2;
+ dest2.x = x2;
+ dest2.y = y2;
+ dest2.w = w;
+ dest2.h = h;
+ SDL_BlitSurface(img, &dest2, target, &dest);
+}
+ */
\ No newline at end of file
diff --git a/source/code/CppSdl/CppSdlImageHolder.hpp b/source/code/CppSdl/CppSdlImageHolder.hpp index 920114c..bdcb9e4 100644 --- a/source/code/CppSdl/CppSdlImageHolder.hpp +++ b/source/code/CppSdl/CppSdlImageHolder.hpp
@@ -24,6 +24,8 @@ public:
void Cut(Uint32 x,Uint32 y,Sint32 w,Sint32 h);
Uint32 GetWidth();
Uint32 GetHeight();
+ void PaintTo(SDL_Surface *target, int x, int y);
+ void OptimizeForBlit(bool allowAlpha = true);
private:
Uint32 counter;
SDL_Rect area;
diff --git a/source/code/main.cpp b/source/code/main.cpp index 470783c..44b7760 100644 --- a/source/code/main.cpp +++ b/source/code/main.cpp
@@ -913,9 +913,7 @@ public:
ballArray[i].update();
if (ballArray[i].getY()>800 || ballArray[i].getX()>xsize || ballArray[i].getX()<-ballSize)
{
- ballArray[i].~aBall();
ballUsed[i] = false;
- //cout << "Ball removed" << endl;
}
}
else
@@ -3003,58 +3001,30 @@ int startSingleVs()
while ( SDL_PollEvent(&event) )
if ( event.type == SDL_KEYDOWN )
{
- if ( event.key.keysym.sym == SDLK_ESCAPE ) {
- done = true;
- }
- if ( event.key.keysym.sym == SDLK_RETURN ) {
- done = true;
- }
- if ( event.key.keysym.sym == SDLK_KP_ENTER ) {
+ if ( event.key.keysym.sym == SDLK_KP_ENTER || event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_RETURN ) {
done = true;
}
- if ( event.key.keysym.sym == SDLK_1 ) {
+ if ( event.key.keysym.sym == SDLK_1 || event.key.keysym.sym == SDLK_KP1 ) {
return 0;
}
- if ( event.key.keysym.sym == SDLK_2 ) {
+ if ( event.key.keysym.sym == SDLK_2 || event.key.keysym.sym == SDLK_KP2 ) {
return 1;
}
- if ( event.key.keysym.sym == SDLK_3 ) {
+ if ( event.key.keysym.sym == SDLK_3 || event.key.keysym.sym == SDLK_KP3 ) {
return 2;
}
- if ( event.key.keysym.sym == SDLK_4 ) {
+ if ( event.key.keysym.sym == SDLK_4 || event.key.keysym.sym == SDLK_KP4 ) {
return 3;
}
- if ( event.key.keysym.sym == SDLK_5 ) {
+ if ( event.key.keysym.sym == SDLK_5 || event.key.keysym.sym == SDLK_KP5 ) {
return 4;
}
- if ( event.key.keysym.sym == SDLK_6 ) {
+ if ( event.key.keysym.sym == SDLK_6 || event.key.keysym.sym == SDLK_KP6 ) {
return 5;
}
- if ( event.key.keysym.sym == SDLK_7 ) {
+ if ( event.key.keysym.sym == SDLK_7 || event.key.keysym.sym == SDLK_KP7 ) {
return 6;
}
- if ( event.key.keysym.sym == SDLK_KP1 ) {
- return 0;
- }
- if ( event.key.keysym.sym == SDLK_KP2 ) {
- return 1;
- }
- if ( event.key.keysym.sym == SDLK_KP3 ) {
- return 2;
- }
- if ( event.key.keysym.sym == SDLK_KP4 ) {
- return 3;
- }
- if ( event.key.keysym.sym == SDLK_KP5 ) {
- return 4;
- }
- if ( event.key.keysym.sym == SDLK_KP6 ) {
- return 5;
- }
- if ( event.key.keysym.sym == SDLK_KP7 ) {
- return 6;
- }
-
}
// If the mouse button is released, make bMouseUp equal true
@@ -3292,6 +3262,61 @@ void StartSinglePlayerEndless() {
strcpy(player2->name, player2name);
}
+void StartSinglePlayerTimeTrial() {
+ player1->NewTimeTrialGame(50,100,SDL_GetTicks());
+ closeAllMenus();
+ twoPlayers =false;
+ player2->SetGameOver();
+ showGame = true;
+ //vsMode = false;
+ strcpy(player1->name, player1name);
+ strcpy(player2->name, player2name);
+}
+
+
+void StarTwoPlayerTimeTrial() {
+ player1->NewTimeTrialGame(50,100,SDL_GetTicks());
+ player2->NewTimeTrialGame(xsize-500,100,SDL_GetTicks());
+ int theTime = time(0);
+ player1->putStartBlocks(theTime);
+ player2->putStartBlocks(theTime);
+ closeAllMenus();
+ twoPlayers = true;
+ player1->setGameSpeed(player1Speed);
+ player2->setGameSpeed(player2Speed);
+ player1->setHandicap(player1handicap);
+ player2->setHandicap(player2handicap);
+ if(player1AI)
+ player1->setAIlevel(player1AIlevel);
+ if(player2AI)
+ player2->setAIlevel(player2AIlevel);
+ strcpy(player1->name, player1name);
+ strcpy(player2->name, player2name);
+}
+
+void StartTwoPlayerVs() {
+ //2 player - VsMode
+ player1->NewVsGame(50,100,player2,SDL_GetTicks());
+ player2->NewVsGame(xsize-500,100,player1,SDL_GetTicks());
+ bNewGameOpen = false;
+ //vsMode = true;
+ twoPlayers = true;
+ b2playersOpen = false;
+ player1->setGameSpeed(player1Speed);
+ player2->setGameSpeed(player2Speed);
+ player1->setHandicap(player1handicap);
+ player2->setHandicap(player2handicap);
+ if(player1AI)
+ player1->setAIlevel(player1AIlevel);
+ if(player2AI)
+ player2->setAIlevel(player2AIlevel);
+ int theTime = time(0);
+ player1->putStartBlocks(theTime);
+ player2->putStartBlocks(theTime);
+ strcpy(player1->name, player1name);
+ strcpy(player2->name, player2name);
+}
+
#if NETWORK
#include "NetworkThing.hpp"
//#include "MenuSystem.h"
@@ -3513,7 +3538,7 @@ int main(int argc, char *argv[])
//Copyright notice:
- cout << "Block Attack - Rise of the Blocks (" << VERSION_NUMBER << ")" << endl << "http://blockattack.sf.net" << endl << "Copyright 2004-2010 Poul Sander" << endl <<
+ cout << "Block Attack - Rise of the Blocks (" << VERSION_NUMBER << ")" << endl << "http://blockattack.sf.net" << endl << "Copyright 2004-2011 Poul Sander" << endl <<
"A SDL based game (see www.libsdl.org)" << endl <<
"The game is availeble under the GPL, see COPYING for details." << endl;
#if defined(_WIN32)
@@ -3661,7 +3686,6 @@ int main(int argc, char *argv[])
//Now sets the icon:
SDL_Surface *icon = IMG_Load2("gfx/icon.png");
SDL_WM_SetIcon(icon,NULL);
- //SDL_FreeSurface(icon);
cout << "Images loaded" << endl;
@@ -3875,14 +3899,7 @@ int main(int argc, char *argv[])
#else
if ((!showOptions)){
#endif
- theGame.NewTimeTrialGame(50,100,SDL_GetTicks());
- closeAllMenus();
- twoPlayers =false;
- theGame2.SetGameOver();
- showGame = true;
- vsMode = false;
- strcpy(theGame.name, player1name);
- strcpy(theGame2.name, player2name);
+ StartSinglePlayerTimeTrial();
}}
if ( event.key.keysym.sym == SDLK_F5 )
{
@@ -3913,24 +3930,7 @@ int main(int argc, char *argv[])
if ((!showOptions))
#endif
{
- theGame.NewVsGame(50,100,&theGame2,SDL_GetTicks());
- theGame2.NewVsGame(xsize-500,100,&theGame,SDL_GetTicks());
- closeAllMenus();
- vsMode = true;
- strcpy(theGame.name, player1name);
- strcpy(theGame2.name, player2name);
- theGame.setGameSpeed(player1Speed);
- theGame2.setGameSpeed(player2Speed);
- theGame.setHandicap(player1handicap);
- theGame2.setHandicap(player2handicap);
- if(player1AI)
- theGame.setAIlevel(player1AIlevel);
- if(player2AI)
- theGame2.setAIlevel(player2AIlevel);
- int theTime = time(0);
- theGame.putStartBlocks(theTime);
- theGame2.putStartBlocks(theTime);
- twoPlayers = true;
+ StartTwoPlayerVs();
}
}
if ( event.key.keysym.sym == SDLK_F4 )
@@ -3941,23 +3941,7 @@ int main(int argc, char *argv[])
if ((!showOptions))
#endif
{
- theGame.NewTimeTrialGame(50,100,SDL_GetTicks());
- theGame2.NewTimeTrialGame(xsize-500,100,SDL_GetTicks());
- int theTime = time(0);
- theGame.putStartBlocks(theTime);
- theGame2.putStartBlocks(theTime);
- closeAllMenus();
- twoPlayers = true;
- theGame.setGameSpeed(player1Speed);
- theGame2.setGameSpeed(player2Speed);
- theGame.setHandicap(player1handicap);
- theGame2.setHandicap(player2handicap);
- if(player1AI)
- theGame.setAIlevel(player1AIlevel);
- if(player2AI)
- theGame2.setAIlevel(player2AIlevel);
- strcpy(theGame.name, player1name);
- strcpy(theGame2.name, player2name);
+ StarTwoPlayerTimeTrial();
}
}
if ( event.key.keysym.sym == SDLK_F7 )
@@ -4405,15 +4389,7 @@ int main(int argc, char *argv[])
else
if ((buttonXsize<mousex) && (mousex<240) && (80<mousey) && (mousey<120) && (b1playerOpen) &&(!networkActive) )
{
- //1 player - time trial
- theGame.NewTimeTrialGame(50,100,SDL_GetTicks());
- bNewGameOpen = false;
- b1playerOpen = false;
- twoPlayers =false;
- theGame2.SetGameOver();
- showGame = true;
- strcpy(theGame.name, player1name);
- strcpy(theGame2.name, player2name);
+ StartSinglePlayerTimeTrial();
}
else
if ((buttonXsize<mousex) && (mousex<240) && (120<mousey) && (mousey<160) && (b1playerOpen)&&(!networkActive) )
@@ -4473,50 +4449,12 @@ int main(int argc, char *argv[])
else
if ((buttonXsize<mousex) && (mousex<240) && (80<mousey) && (mousey<120) && (b2playersOpen))
{
- //2 player - time trial
- theGame.NewTimeTrialGame(50,100,SDL_GetTicks());
- bNewGameOpen = false;
- b2playersOpen = false;
- theGame.NewTimeTrialGame(50,100,SDL_GetTicks());
- theGame2.NewTimeTrialGame(xsize-500,100,SDL_GetTicks());
- int theTime = time(0);
- theGame.putStartBlocks(theTime);
- theGame2.putStartBlocks(theTime);
- theGame.setGameSpeed(player1Speed);
- theGame2.setGameSpeed(player2Speed);
- theGame.setHandicap(player1handicap);
- theGame2.setHandicap(player2handicap);
- if(player1AI)
- theGame.setAIlevel(player1AIlevel);
- if(player1AI)
- theGame2.setAIlevel(player2AIlevel);
- twoPlayers = true;
- strcpy(theGame.name, player1name);
- strcpy(theGame2.name, player2name);
+ StarTwoPlayerTimeTrial();
}
else
if ((buttonXsize<mousex) && (mousex<240) && (120<mousey) && (mousey<160) && (b2playersOpen))
{
- //2 player - VsMode
- theGame.NewVsGame(50,100,&theGame2,SDL_GetTicks());
- theGame2.NewVsGame(xsize-500,100,&theGame,SDL_GetTicks());
- bNewGameOpen = false;
- vsMode = true;
- twoPlayers = true;
- b2playersOpen = false;
- theGame.setGameSpeed(player1Speed);
- theGame2.setGameSpeed(player2Speed);
- theGame.setHandicap(player1handicap);
- theGame2.setHandicap(player2handicap);
- if(player1AI)
- theGame.setAIlevel(player1AIlevel);
- if(player2AI)
- theGame2.setAIlevel(player2AIlevel);
- int theTime = time(0);
- theGame.putStartBlocks(theTime);
- theGame2.putStartBlocks(theTime);
- strcpy(theGame.name, player1name);
- strcpy(theGame2.name, player2name);
+ StartTwoPlayerVs();
}
#if NETWORK
else