#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <math.h>
#include "CRM32Pro.h"
#include "SpacePong.h"
#ifdef _WINDOWS
#include <conio.h>
#include <windows.h>
#elif defined(_LINUX) || defined(_MACOSX)
#include <math.h>
char *_itoa(int i,char *a,size_t len)
{
sprintf(a,"%d",i % static_cast<int>(pow(10,len-1-(i<0))));
return a;
}
#endif
int main(int argc,char *argv[])
{
Uint8 done;
int iRet;
if(CRM32Pro.
Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO) < 0)
{
ILogSystem.
Msg(
LOG_NORMAL,
" ·[PONG Init Error] - Couldnīt initialize CRM32Pro: %s\n",SDL_GetError());
exit(1);
}
{
ILogSystem.
Msg(
LOG_NORMAL,
" ·[PONG Init Error] - Couldnīt load 'setup.xml'. Please, run 'Setup.exe'\n");
exit(1);
}
CRM32Pro.
Config.
Title = PONG_VERSION;
CRM32Pro.Config.
Icon = IImage->
Load(GFX_RESOURCE,
"icono");
{
return 1;
}
if(iRet == -1) ILogSystem.
Msg(
LOG_NORMAL,
" ·[PONG Sound System] - Couldnīt init sound system: %s\n",SDL_GetError());
else if(iRet == 1) LoadAudio();
LoadGraphics();
sGameDat.iTipo = GAME_GOAL;
done = PONG_ENTRY;
while(done != PONG_QUIT)
{
if(done == PONG_ENTRY)
{
bID = -1;
done = PONG_LOOP;
}
if(bID == bExitID)
{
while(ISoundFX->
IsPlaying(1)) SDL_Delay(200);
done = PONG_QUIT;
}
else if(bID == bPlayer1ID)
{
sGameDat.iModo = MODE_1PLAYER;
GameLoop();
done = PONG_ENTRY;
}
else if(bID == bPlayer2ID)
{
sGameDat.iModo = MODE_2PLAYER;
GameLoop();
done = PONG_ENTRY;
}
else if(bID == bAutoID)
{
sGameDat.iModo = MODE_COMPUTER;
GameLoop();
done = PONG_ENTRY;
}
else if(bID == bNormalID)
{
sGameDat.iTipo = GAME_NORMAL;
}
else if(bID == bGoalID)
{
sGameDat.iTipo = GAME_GOAL;
}
else if(bID == bTimeID)
{
sGameDat.iTipo = GAME_TIME;
}
}
SDL_Delay(250);
FreeAudio();
FreeGraphics();
return 1;
}
void RenderInMenu(int bLogicUpdate)
{
{
rRect.x = 27;rRect.y = 20;rRect.w = sMenuTitle->w; rRect.h = sMenuTitle->h;
CRM32Pro.
Blit(sMenuTitle,NULL,CRM32Pro.
screen,&rRect);
rRect.x = 133;rRect.y = 578;rRect.w = sMenuInfo->w; rRect.h = sMenuInfo->h;
CRM32Pro.
Blit(sMenuInfo,NULL,CRM32Pro.
screen,&rRect);
}
}
void GameLoop()
{
Uint8 done;
Uint32 iTmp = 0;
srand(SDL_GetTicks());
sGameDat.iWinP1 = 0; sGameDat.iWinP2 = 0;
sPadder[0].x = 384.0f; sPadder[0].size = 64; sPadder[0].speed=0.0f; sPadder[0].random=64/2;
sPadder[1].x = 384.0f; sPadder[1].size = 64; sPadder[1].speed=0.0f; sPadder[1].random=64/2;
InitBall(0);
sBall.spr->SetPosition((int)sBall.x,(int)sBall.y);
sPadder[0].spr->SetPosition((int)sPadder[0].x,Y_POSITION_1);
sPadder[1].spr->SetPosition((int)sPadder[1].x,Y_POSITION_2);
while(ISoundFX->
IsPlaying(1)) SDL_Delay(200);
switch(sGameDat.iTipo)
{
case GAME_TIME:
strcpy(sCad,"TIME"); break;
case GAME_GOAL:
strcpy(sCad,"GOAL"); break;
case GAME_NORMAL:
strcpy(sCad,"INFI"); break;
}
done = PONG_ENTRY;
while(done < PONG_QUIT)
{
if(CRM32Pro.
GetKeystate(SDLK_ESCAPE)) done = PONG_QUIT;
sBall.x = sBall.x + (sBall.sx * sBall.speed);
sBall.y = sBall.y + (sBall.sy * sBall.speed);
if(sBall.x < LEFT_BAR)
{
sBall.x = LEFT_BAR;
sBall.sx = sBall.sx * (-1.0f); ISoundFX->
SoundPlay(2,pared,0);
}
else if(sBall.x + X_SIZE_BALL > RIGHT_BAR)
{
sBall.x = RIGHT_BAR - X_SIZE_BALL;
sBall.sx = sBall.sx * (-1.0f); ISoundFX->
SoundPlay(2,pared,0);
}
if(sBall.y < UP_LIMIT)
{
InitBall(1);
sGameDat.iWinP1++;
SDL_Delay(250);
}
else if(sBall.y > DOWN_LIMIT)
{
InitBall(2);
sGameDat.iWinP2++;
SDL_Delay(250);
}
switch(sGameDat.iModo)
{
case MODE_COMPUTER:
AIPadder(0);
AIPadder(1);
break;
case MODE_2PLAYER:
break;
case MODE_1PLAYER:
AIPadder(1);
break;
}
for(iTmp = 0; iTmp < 2; iTmp++)
{
if(sPadder[iTmp].speed > PADDER_MAXSPEED) sPadder[iTmp].speed = PADDER_MAXSPEED;
if(sPadder[iTmp].speed < -PADDER_MAXSPEED) sPadder[iTmp].speed = -PADDER_MAXSPEED;
if(sPadder[iTmp].speed > 0.0f)
{
sPadder[iTmp].speed -= PADDER_ACEL / 4;
}
if(sPadder[iTmp].speed < 0.0f)
{
sPadder[iTmp].speed += PADDER_ACEL / 4;
}
}
for(iTmp = 0; iTmp < 2; iTmp++) sPadder[iTmp].x = sPadder[iTmp].x + sPadder[iTmp].speed;
for(iTmp = 0; iTmp < 2; iTmp++)
{
if(sPadder[iTmp].x > RIGHT_BAR - sPadder[iTmp].size)
{
sPadder[iTmp].x = RIGHT_BAR - sPadder[iTmp].size;
sPadder[iTmp].speed = 0.0f;
}
if(sPadder[iTmp].x < LEFT_BAR)
{
sPadder[iTmp].x = LEFT_BAR;
sPadder[iTmp].speed = 0.0f;
}
}
if((sBall.y + Y_SIZE_BALL >= Y_POSITION_1) && (sBall.y + Y_SIZE_BALL <= (Y_POSITION_1 + 12))) PadderHitBall(0);
if((sBall.y <= Y_POSITION_2 + Y_SIZE_PADDER) && (sBall.y >= (Y_POSITION_2 + Y_SIZE_PADDER - 12))) PadderHitBall(1);
sBall.spr->SetPosition((int)sBall.x,(int)sBall.y,1);
sPadder[0].spr->SetPosition((int)sPadder[0].x,Y_POSITION_1,1);
sPadder[1].spr->SetPosition((int)sPadder[1].x,Y_POSITION_2,1);
switch(sGameDat.iTipo)
{
case GAME_TIME:
if(iAux == 5) done = PONG_ENDMATCH;
break;
case GAME_GOAL:
if((sGameDat.iWinP1 == 10) || (sGameDat.iWinP2 == 10)) done = PONG_ENDMATCH;
break;
}
}
if(done == PONG_QUIT)
{
if(sGameDat.iTipo != GAME_NORMAL) iTmp = 1;
else
{
if(sGameDat.iWinP1 > sGameDat.iWinP2) iTmp = 0;
else if(sGameDat.iWinP1 < sGameDat.iWinP2) iTmp = 2;
else iTmp = 1;
}
}
else
{
if(sGameDat.iWinP1 > sGameDat.iWinP2) iTmp = 0;
else if(sGameDat.iWinP1 < sGameDat.iWinP2) iTmp = 2;
}
switch(iTmp)
{
case 0:
fFont->PutString(CRM32Pro.
screen,260,290,
"PLAYER 1 WINS !!");
break;
case 1:
fFont->PutString(CRM32Pro.
screen,300,290,
"TIE GAME !!");
break;
case 2:
fFont->PutString(CRM32Pro.
screen,260,290,
"PLAYER 2 WINS !!");
break;
}
ILogSystem.
Msg(
LOG_NORMAL,
"\n · Player 1 wins: %d - Player 2 wins: %d\n",sGameDat.iWinP1,sGameDat.iWinP2);
SDL_Delay(2000);
SDL_Delay(250);
}
void RenderInGame(int bLogicUpdate)
{
sBall.spr->Draw();
sPadder[0].spr->Draw();
sPadder[1].spr->Draw();
DrawInterfaz(1);
DrawInterfaz(2);
fFont->PutString(CRM32Pro.
screen,36,463,sCad);
fFont->PutString(CRM32Pro.
screen,685,463,sCad);
}
void DrawInterfaz(int m)
{
char sAux[4], sCad[8];
Uint32 iTime = 0;
switch(m)
{
case 1:
rRect.x = 25; rRect.y = 199; rRect.w = sRestoreBg[0]->w; rRect.h = sRestoreBg[0]->h;
SDL_BlitSurface(sRestoreBg[0],NULL,CRM32Pro.
screen,&rRect);
_itoa(sGameDat.iWinP1,sAux,10);
strcpy(sCad,"");
if(sGameDat.iWinP1 < 10) strcpy(sCad,"0");
strcat(sCad,sAux);
fFont->PutString(CRM32Pro.
screen,55,203,sCad);
strcpy(sCad,"");
iAux = iTime / 60;
_itoa(iAux,sAux,10);
strcpy(sCad,sAux);
strcat(sCad,":");
_itoa(iTime - (iAux * 60),sAux,10);
if(iTime - (iAux * 60) < 10) strcat(sCad,"0");
strcat(sCad,sAux);
fFont->PutString(CRM32Pro.
screen,44,264,sCad);
break;
case 2:
rRect.x = 685; rRect.y = 201; rRect.w = sRestoreBg[1]->w; rRect.h = sRestoreBg[1]->h;
SDL_BlitSurface(sRestoreBg[1],NULL,CRM32Pro.
screen,&rRect);
_itoa(sGameDat.iWinP2,sAux,10);
strcpy(sCad,"");
if(sGameDat.iWinP2 < 10) strcpy(sCad,"0");
strcat(sCad,sAux);
fFont->PutString(CRM32Pro.
screen,704,203,sCad);
strcpy(sCad,"");
iAux = iTime / 60;
_itoa(iAux,sAux,10);
strcpy(sCad,sAux);
strcat(sCad,":");
_itoa(iTime - (iAux * 60),sAux,10);
if(iTime - (iAux * 60) < 10) strcat(sCad,"0");
strcat(sCad,sAux);
fFont->PutString(CRM32Pro.
screen,692,264,sCad);
break;
}
}
void AIPadder(int m)
{
if(((!m) && (sBall.y < 100)) || ((m) && (sBall.y > 500)))
{
if(sPadder[m].x < 368) { SET_SPEED(m,1); }
else if(sPadder[m].x > 368) { SET_SPEED(m,0); }
}
else
{
if(((sBall.x + 8)) < (sPadder[m].x + sPadder[m].random))
{
if(sPadder[m].x > LEFT_BAR) { SET_SPEED(m,0); }
}
else if(((sBall.x + 8)) > (sPadder[m].x + sPadder[m].random))
{
if(sPadder[m].x <= RIGHT_BAR - sPadder[m].size) { SET_SPEED(m,1); }
}
}
}
void InitBall(int mode)
{
sBall.sx = 0.0;
sBall.x = 400.0;
sBall.y = 300.0;
sBall.speed = 0.9;
switch(mode)
{
case 0:
if((rand() % 10) >= 5) sBall.sy = -4.0;
else sBall.sy = 4.0;
break;
case 1:
sBall.sy = -4.0;
break;
case 2:
sBall.sy = 4.0;
break;
}
}
void PadderHitBall(int m)
{
int iTmp;
float mod = -1.0;
if(m == 0) mod = 1.0;
if(((sBall.x + 12) >= sPadder[m].x) && ((sBall.x + 4) <= (sPadder[m].x + 64)))
{
iTmp = 24 + (int)sBall.x - (int)sPadder[m].x;
if(iTmp <= 18)
{
sBall.sx = -5;sBall.sy = -4 * mod;
}
else if((iTmp > 18) && (iTmp <= 24))
{
sBall.sx = -4.0f;sBall.sy = -4.0f * mod;
}
else if((iTmp > 24) && (iTmp <= 40))
{
sBall.sx = -2.0f;sBall.sy = -6.0f * mod;
}
else if((iTmp > 40) && (iTmp <= 56))
{
sBall.sx = 0.0f;sBall.sy = -8.0f * mod;
}
else if((iTmp > 56) && (iTmp <= 72))
{
sBall.sx = 2.0f;sBall.sy = -6.0f * mod;
}
else if((iTmp > 72) && (iTmp <= 78))
{
sBall.sx = 4.0f;sBall.sy = -4.0f * mod;
}
else if(iTmp >= 79)
{
sBall.sx = 5.0f;sBall.sy = -4.0f * mod;
}
else ILogSystem.
Msg(
LOG_NORMAL,
"Padder[%d] - Error %d\n",m + 1,iTmp);
sBall.speed = sBall.speed + 0.03f;
if(sBall.speed >= MAX_BALL_SPEED) sBall.speed = MAX_BALL_SPEED;
sPadder[m].random=rand()%sPadder[m].size;
}
}
void LoadAudio()
{
thunder = ISoundFX->
SoundLoad(MUS_RESOURCE,
"thunder");
end = ISoundFX->
SoundLoad(MUS_RESOURCE,
"endplay");
gol = ISoundFX->
SoundLoad(MUS_RESOURCE,
"goal");
p1 = ISoundFX->
SoundLoad(MUS_RESOURCE,
"padder1");
p2 = ISoundFX->
SoundLoad(MUS_RESOURCE,
"padder2");
pared = ISoundFX->
SoundLoad(MUS_RESOURCE,
"pared");
click = ISoundFX->
SoundLoad(MUS_RESOURCE,
"click");
click2 = ISoundFX->
SoundLoad(MUS_RESOURCE,
"click2");
music_menu = ISoundFX->
MusicLoad(MUS_RESOURCE,
"game");
music_game = ISoundFX->
MusicLoad(MUS_RESOURCE,
"game");
}
void FreeAudio()
{
}
void LoadGraphics()
{
sGameBg = IImage->
Load(GFX_RESOURCE,
"gamebg");
sRestoreBg[0] = SDL_CreateRGBSurface(CRM32Pro.
screen->flags,100,95,sGameBg->format->BitsPerPixel,
sGameBg->format->Rmask,sGameBg->format->Gmask,sGameBg->format->Bmask,
sGameBg->format->Amask);
rRect.x = 25;rRect.y = 199;rRect.w = 100;rRect.h = 95;
SDL_BlitSurface(sGameBg,&rRect,sRestoreBg[0],NULL);
sRestoreBg[1] = SDL_CreateRGBSurface(CRM32Pro.
screen->flags,100,90,sGameBg->format->BitsPerPixel,
sGameBg->format->Rmask,sGameBg->format->Gmask,sGameBg->format->Bmask,
sGameBg->format->Amask);
rRect.x = 685;rRect.y = 201;rRect.w = 100;rRect.h = 90;
SDL_BlitSurface(sGameBg,&rRect,sRestoreBg[1],NULL);
sMenuBg = IImage->
Load(GFX_RESOURCE,
"menubg");
sMenuTitle = IImage->
Load(GFX_RESOURCE,
"menutitle");
sMenuInfo = IImage->
Load(GFX_RESOURCE,
"menuinfo");
sPadder[0].spr->Load(GFX_RESOURCE,"SPR_PADDER1");
sPadder[1].spr->Load(GFX_RESOURCE,"SPR_PADDER2");
sBall.spr->Load(GFX_RESOURCE,"SPR_BALL");
fFont->
Load(GFX_RESOURCE,
"FVerde");
bExitID = IButton->
Load(GFX_RESOURCE,
"bExit");
bPlayer1ID = IButton->
Load(GFX_RESOURCE,
"bPlayer1");
bPlayer2ID = IButton->
Load(GFX_RESOURCE,
"bPlayer2");
bAutoID = IButton->
Load(GFX_RESOURCE,
"bAuto");
bTimeID = IButton->
Load(GFX_RESOURCE,
"bTime");
bGoalID = IButton->
Load(GFX_RESOURCE,
"bGoal");
bNormalID = IButton->
Load(GFX_RESOURCE,
"bNormal");
cCursor = ICursor->
Load(GFX_RESOURCE,
"cursor");
}
void FreeGraphics()
{
delete sPadder[0].spr;
delete sPadder[1].spr;
delete sBall.spr;
delete fFont;
SDL_FreeSurface(sGameBg);
SDL_FreeSurface(sMenuBg);
SDL_FreeSurface(sMenuTitle);
SDL_FreeSurface(sMenuInfo);
SDL_FreeSurface(sRestoreBg[0]);
SDL_FreeSurface(sRestoreBg[1]);
}