• Register

I am a programmer, and one of the 3 founders of mionet. Mionet is a small site intended for indie game dev and discussion. I hope to be able to contribute a lot to the indie game dev scene in the coming years.

  • View media
  • View media
  • View media
  • View media
RSS My Blogs

Recoding is good

miotatsu Blog

Yesterday I began work on the Player Creation screen for Meiro and I knew that I needed to switch the state to it if the player presses enter when a file of the name "New Player" is selected. At the time the file names were loaded into arrays and manipulated like plain raw memory, I did not append a null character at the end of the names but instead a newline and so I was not able to use c-string related functions. Because of this I had implemented some of that functionality specifically for my newline terminated version of a string. I had done all this early on and did not realize at the time that it was a terrible idea and the features that were available in the cstring library. So I was writing code to check if the name was "New Player" and while doing so I realized my cArraylen function (my equivalent of strlen) was not returning the correct size. I decided to cout each array after it was loaded into memory to see that it was loading them correctly and discovered that in fact it was not. It is a miracle (or rather a curse) that those bugs did not show themselves much earlier. I decided it would be easier to recode the state from scratch than to try to fix all those bugs.
Today I did just that. The original .cpp file for the save selection state was 1100 lines, was extremely inefficient, and as I already explained - very buggy. It also took me 2 months to write (on and off). This new implementation took me one day to write, is readable, and only 396 lines. I rewrote it from scratch and so a lot of things changed, especially with the rendering. The notable change relating to this post is how I went about handling the file names. This time around I steered away from raw memory (as well as c-strings) and loaded them into std::strings. I think this experience really goes to show how much I am learning from coding meiro. Next up I will be working on the player creation screen however I am going to be on vacation until Tuesday (I think) and won't have internet until I am back, so I won't be able to keep you guys updated on the progress.

for those interested here is the recoded version of my file loading:
char delim = 0x0A;
inFile.open("data/DAT_000.dat", std::ios::in|std::ios::binary|std::ios::ate);
if (inFile.is_open())
{
Filesize = inFile.tellg();
array = new char[Filesize];
inFile.seekg (0, std::ios::beg);
inFile.read (array, Filesize);
inFile.close();
for (int i = 0; i < Filesize; i++){
if (array[i]!= 0x0A && array[i]!= 0)
array[i]= array[i]+ 31; //my "encryption" is simply to subtract 31 from each char
}
outFile.open("data/TMP_000.txt", std::ios::out);
outFile.write(array, Filesize);
outFile.close();
inFile.open("data/TMP_000.txt", std::ios::in);
std::getline(inFile, sFile1, delim);
std::getline(inFile, sFile2, delim);
std::getline(inFile, sFile3, delim);
inFile.close();

and now when I want to do something like a move operation it is as simple as:
//swap file 2 with file 1
SDL_FreeSurface( pFile2 );
SDL_FreeSurface( pFile1 );
sFile2.swap( sFile1 ); //one-line swap ftw
pFile1 = TTF_RenderText_Solid( TitleFont18, sFile1.c_str(), white );
pFile2 = TTF_RenderText_Solid( TitleFont18, sFile2.c_str(), color[offset] );

:D

Site updates

Site updates

miotatsu Blog
First blog post

First blog post

miotatsu Blog
Start a group Groups
mionet

mionet

5 members Developer

we make indie games mainly for linux, and also windows.

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account:

X