Skip to content

Commit

Permalink
Start trying to extract the global high scores code so it can be turn…
Browse files Browse the repository at this point in the history
…ed into a generic library.
  • Loading branch information
jeremysrand committed May 26, 2022
1 parent f1a80c7 commit b8b9089
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 35 deletions.
4 changes: 2 additions & 2 deletions BuGS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
9DE37B3E2694E0B0005FC562 /* globalScores.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = globalScores.h; sourceTree = "<group>"; };
9DE37B3F2694E0B0005FC562 /* globals.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; path = globals.s; sourceTree = "<group>"; };
9DE37B402694E0B0005FC562 /* gameSegments.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; path = gameSegments.s; sourceTree = "<group>"; };
9DE37B412694E0B0005FC562 /* score.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; path = score.s; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.asm.orcam; };
9DE37B412694E0B0005FC562 /* score.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; path = score.s; sourceTree = "<group>"; };
9DE37B422694E0B0005FC562 /* gamePlayer.s */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; path = gamePlayer.s; sourceTree = "<group>"; };
9DE37B432694E0B0005FC562 /* Read.Me.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = Read.Me.md; sourceTree = "<group>"; };
9DE37B442694E0B0005FC562 /* global.macros */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = global.macros; sourceTree = "<group>"; };
Expand Down Expand Up @@ -234,9 +234,9 @@
9DE37B412694E0B0005FC562 /* score.s */,
9DE37B482694E0B0005FC562 /* settings.c */,
9DE37B462694E0B0005FC562 /* settings.h */,
9DE37B342694E0B0005FC562 /* tileConvert.s */,
9DE37B5C2694E0B0005FC562 /* sound */,
9DE37B4A2694E0B0005FC562 /* sprites */,
9DE37B342694E0B0005FC562 /* tileConvert.s */,
9DE37AF62694E070005FC562 /* Makefile */,
9DE37AF82694E070005FC562 /* make */,
9DE37B112694E070005FC562 /* Supporting Files */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
<key>DiskImage.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>2</integer>
<integer>1</integer>
</dict>
<key>doNotBuild.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>2</integer>
</dict>
</dict>
</dict>
Expand Down
2 changes: 1 addition & 1 deletion BuGS/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ COPYBOOTDIRS=
# By default, the build uses these arguments with mame:
# apple2gs -skip_gameinfo -mouse -window -resolution 1408x1056 -ramsize 4M -sl7 cffa202
# If you would like to use different arguments, specify that here.
MAMEARGS=apple2gs -skip_gameinfo -mouse -window -resolution 1408x1056 -ramsize 4M -sl3 uthernet -sl7 cffa2
MAMEARGS=apple2gs -skip_gameinfo -mouse -window -resolution 1408x1056 -ramsize 4M -sl3 uthernet -sl7 scsi

# For a desktop application, it can operate in 640x200 or 320x200
# resolution. This setting is used to define which horizontal
Expand Down
4 changes: 4 additions & 0 deletions BuGS/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
extern void game(void);
extern void randInit(void);
extern void waitForVbl(void);
extern void uploadSpin1(void);
extern void uploadSpin2(void);
extern void uploadSpin3(void);
extern void displayConnectionString(void);
extern void swapStereoChannels(void);


Expand Down
45 changes: 26 additions & 19 deletions BuGS/globalScores.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
#include <stdio.h>
#include <stdlib.h>

#include "game.h"
#include "globalScores.h"
#include "tileData.h"


// Defines
Expand Down Expand Up @@ -154,6 +152,7 @@ typedef struct tGameNetworkGlobals {
tHighScoreRequestWithHash highScoreRequest;
Boolean hasHighScoreToSend;
tStatusResponse setHighScoreResponse;
tHighScoreInitParams initParams;
uint16_t errorCode;
uint16_t timeout;
} tGameNetworkGlobals;
Expand All @@ -172,20 +171,21 @@ Boolean hasGlobalHighScores = FALSE;
tScoresResponse highScoreResponse;
Word globalScoreAge = 0;
tSetHighScoreRequestWithHash setHighScoreRequest;
char globalScoreInfo[GAME_NUM_TILES_WIDE + 1];
char globalScoreInfo[26];


// Implementation


segment "highscores";

void initNetwork(void)
void initNetwork(tHighScoreInitParams * params)
{
networkGlobals = NULL;

if ((NETWORK_SERVER == NULL) ||
(NETWORK_SERVERPORT == 0))
if ((params->scoreServer == NULL) ||
(params->scorePort == 0) ||
(params->waitForVbl == NULL))
return;

LoadOneTool(54, 0x200); // Load Marinetti
Expand Down Expand Up @@ -223,15 +223,17 @@ void initNetwork(void)

}

memcpy(&(networkGlobals->initParams), params, sizeof(networkGlobals->initParams));

networkGlobals->networkStartedConnected = TCPIPGetConnectStatus();
if (networkGlobals->networkStartedConnected) {
networkGlobals->gameNetworkState = GAME_NETWORK_CONNECTED;
} else {
networkGlobals->gameNetworkState = GAME_NETWORK_UNCONNECTED;
}

networkGlobals->secrets[0] = NETWORK_SERVERSECRET1;
networkGlobals->secrets[1] = NETWORK_SERVERSECRET2;
networkGlobals->secrets[0] = params->secret1;
networkGlobals->secrets[1] = params->secret2;

networkGlobals->hasHighScoreToSend = FALSE;

Expand Down Expand Up @@ -275,7 +277,7 @@ void disconnectNetwork(void)
}

while (networkGlobals->gameNetworkState > GAME_NETWORK_TCP_UNCONNECTED) {
waitForVbl();
networkGlobals->initParams.waitForVbl();
pollNetwork();
}
}
Expand Down Expand Up @@ -380,7 +382,8 @@ void pollNetwork(void)
break;

case GAME_NETWORK_UNCONNECTED:
displayConnectionString();
if (networkGlobals->initParams.displayConnectionString != NULL)
networkGlobals->initParams.displayConnectionString();
TCPIPConnect(NULL);
if ((!toolerror()) &&
(TCPIPGetConnectStatus())) {
Expand All @@ -391,7 +394,7 @@ void pollNetwork(void)
break;

case GAME_NETWORK_CONNECTED:
TCPIPDNRNameToIP("\p" NETWORK_SERVER, &(networkGlobals->domainNameResolution));
TCPIPDNRNameToIP(networkGlobals->initParams.scoreServer, &(networkGlobals->domainNameResolution));
if (toolerror()) {
networkGlobals->gameNetworkState = GAME_NETWORK_LOOKUP_FAILED;
networkGlobals->errorCode = toolerror();
Expand All @@ -418,7 +421,7 @@ void pollNetwork(void)
(!networkGlobals->hasHighScoreToSend))
break;

networkGlobals->ipid = TCPIPLogin(myUserId, networkGlobals->domainNameResolution.DNRIPaddress, NETWORK_SERVERPORT, 0, 64);
networkGlobals->ipid = TCPIPLogin(networkGlobals->initParams.userId, networkGlobals->domainNameResolution.DNRIPaddress, networkGlobals->initParams.scorePort, 0, 64);
if (toolerror()) {
networkGlobals->gameNetworkState = GAME_NETWORK_SOCKET_ERROR;
networkGlobals->errorCode = toolerror();
Expand Down Expand Up @@ -686,26 +689,30 @@ BOOLEAN sendHighScore(void)
networkGlobals->gameNetworkState = GAME_NETWORK_TCP_UNCONNECTED;

do {
waitForVbl();
networkGlobals->initParams.waitForVbl();
pollNetwork();
cycleCount++;

if ((cycleCount & 0x7) == 0) {
switch (cycleCount & 0x18) {
case 0x00:
uploadSpin1();
if (networkGlobals->initParams.uploadSpin != NULL)
networkGlobals->initParams.uploadSpin(0);
break;

case 0x08:
uploadSpin2();
if (networkGlobals->initParams.uploadSpin != NULL)
networkGlobals->initParams.uploadSpin(1);
break;

case 0x10:
uploadSpin3();
if (networkGlobals->initParams.uploadSpin != NULL)
networkGlobals->initParams.uploadSpin(2);
break;

case 0x18:
uploadSpin2();
if (networkGlobals->initParams.uploadSpin != NULL)
networkGlobals->initParams.uploadSpin(3);
break;
}
}
Expand All @@ -718,11 +725,11 @@ BOOLEAN sendHighScore(void)
for (cycleCount = strlen(globalScoreInfo); cycleCount < sizeof(globalScoreInfo); cycleCount++) {
globalScoreInfo[cycleCount] = ' ';
}
globalScoreInfo[GAME_NUM_TILES_WIDE] = '\0';
globalScoreInfo[25] = '\0'; // TODO - Get rid of hard coded value...
displayScorePosition();

for (cycleCount = 4 * 60; cycleCount > 0; cycleCount--) {
waitForVbl();
networkGlobals->initParams.waitForVbl();
}

return TRUE;
Expand Down
26 changes: 16 additions & 10 deletions BuGS/globalScores.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,37 @@

#include <types.h>

#include "tileData.h"

typedef struct tHighScore
{
char scoreText[10];
char who[4];
unsigned long score;
} tHighScore;

extern char globalScoreInfo[GAME_NUM_TILES_WIDE + 1];

extern unsigned int myUserId;
extern char globalScoreInfo[26]; /* TODO - Get rid of this global and the hard coded length */

extern void initNetwork(void);
typedef struct tHighScoreInitParams
{
unsigned int userId;
const char * scoreServer; /* Pascal string of the hostname of the score server. */
unsigned int scorePort;
unsigned long secret1;
unsigned long secret2;

void (*displayConnectionString)(void); /* This function should display a message to the user that the network is being brought up. */
void (*waitForVbl)(void); /* This function should wait for the next VBL and is used to poll the network and limit upload time for a high score. */
void (*uploadSpin)(int); /* This argument iterates over 0, 1, 2, 3 and then back to 0, 1, 2, etc and is intended to show some kind of spinner to the user
while uploading a high score. */
} tHighScoreInitParams;

extern void initNetwork(tHighScoreInitParams * params);
extern void disconnectNetwork(void);
extern void pollNetwork(void);
extern void shutdownNetwork(void);
extern BOOLEAN canSendHighScore(void);
extern BOOLEAN sendHighScore(void);

// These are actually assembly functions called from the C code.
extern void uploadSpin1(void);
extern void uploadSpin2(void);
extern void uploadSpin3(void);
extern void displayConnectionString(void);
extern void displayScorePosition(void);


Expand Down
35 changes: 34 additions & 1 deletion BuGS/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,32 @@ word randomMushroomOffset(void)
}


void uploadSpin(int val)
{
switch (val)
{
case 0:
uploadSpin1();
break;

case 1:
uploadSpin2();
break;

case 2:
uploadSpin3();
break;

case 3:
uploadSpin2();
break;
}
}


int main(void)
{
static tHighScoreInitParams highScoreInitParams;
int event;
Ref toolStartupRef;

Expand Down Expand Up @@ -73,7 +96,17 @@ int main(void)
InitMouse(0);
SetMouse(transparent);

initNetwork();
highScoreInitParams.userId = myUserId;
highScoreInitParams.scoreServer = "\p" NETWORK_SERVER;
highScoreInitParams.scorePort = NETWORK_SERVERPORT;
highScoreInitParams.secret1 = NETWORK_SERVERSECRET1;
highScoreInitParams.secret2 = NETWORK_SERVERSECRET2;

highScoreInitParams.displayConnectionString = displayConnectionString;
highScoreInitParams.waitForVbl = waitForVbl;
highScoreInitParams.uploadSpin = uploadSpin;

initNetwork(&highScoreInitParams);

if (!loadSettings())
saveSettings();
Expand Down
2 changes: 2 additions & 0 deletions BuGS/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <types.h>


extern unsigned int myUserId;

extern void saveSettings(void);
BOOLEAN loadSettings(void);
extern void swapStereoSettings(void);
Expand Down

0 comments on commit b8b9089

Please sign in to comment.