function instead of WriteFile(). Following is the ReadFile()
function instead of WriteFile(). Following is the ReadFile() function as it is defined in the Win32 API: BOOL ReadFile( HANDLE hFile, LPVOID pBuffer, DWORD dwNumberOfBytesToRead, LPDWORD pNumberOfBytesRead, LPOVERLAPPED pOverlapped); As you can see, the ReadFile() function actually takes the same arguments as the WriteFile() function. However, it uses the arguments a little differently because data is being read into the buffer instead of being written from it. Following is an example of reading an individual high score from a file using the ReadFile() function: char cData[6]; DWORD dwBytesRead; ReadFile(hFile, &cData, 5, &dwBytesRead, NULL); It’s important for the array of characters that holds each score to be null-terminated, which is why the cData variable is declared as being six characters long, instead of just five. In this code, the ReadFile() function reads five characters from the file and stores them in the cData variable. When you’re finished reading from or writing to a file, it’s important to close the file. This is accomplished with the CloseHandle() Win32 function, which is called like this: CloseHandle(hFile); You now have the fundamental knowledge required to store and retrieve high score data to and from a file on disk, which is what you need to add a high score feature to the Space Out game. Building the Space Out 4 Game The final version of the Space Out game that you create in this book is called Space Out 4, and it completes the game by including a high score list that is stored to a file on disk. You’ve learned enough about high scores and file I/O in this hour to handle the task of adding a high score list to the game. So, let’s get started! Writing the Game Code The Space Out 4 game requires only one new global variable, which is the array of integers that stores the high score list. Following is the _iHiScores variable as it appears in the SpaceOut.h header file: int _iHiScores[5]; This variable is pretty straightforward in that it stores away five integers that represent the top five scores for the game. The scores are arranged in order of most to least. Several new functions are required to successfully update, read, and write the high score list in the Space Out 4 game: void UpdateHiScores(); BOOL ReadHiScores(); BOOL WriteHiScores(); The UpdateHiScores() function is used to determine if a new score has made it into the high score list. If so, the function makes sure to insert the score in the correct position and slide the lower scores down a notch to make room for the new score. The ReadHiScores() and WriteHiScores() functions use the _iHiScores array as the basis for reading and writing the high score list from and to the HiScores.dat file. You learn how each of these functions work a little later in the hour, but for now it’s important to see how they are used in the context of the game. The GameStart() function is responsible for initializing the game, which now includes reading the high score list. The only change to this function is the new call to the ReadHiScores() function: ReadHiScores();
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Clan Web Hosting services