Hi,
I'm trying to write the levels of my game to files. Here's the classes code:
classes.h
class piece // Individual piece
{
public:
int angle; // Initial angle
int sides; // Number of sides
/*
-------------------------------------------------
Sides
-------------------------------------------------
2 = Los
3 = Tri
4 = Quad
6 = Hex
-------------------------------------------------
*/
int position; // Starting random position
int status; // Piece status
/*
-------------------------------------------------
Status
-------------------------------------------------
0 = Unexistent
1 = Playable
2 = Locked
3 = Unlit
4 = Hiden
*/
int layer; // Piece layer for layered puzzles
float X; // X on world
float Y; // Y on world
void create ( int object, int level );
};
class level // Each level
{
public:
int lastPiece;
int time;
int currentLevel;
piece levelPiece [ 100 ];
void loadLevel ();
void initLevel ();
void addPiece ();
};
classes.cpp
// Includes
# pragma once
#include "DarkGDK.h"
#include "stdio.h"
#include "classes.h"
#include "construction.h"
#include "edit.h"
#include "game.h"
#include "levels.h"
#include "menus.h"
#include "test.h"
// Piece functions
void piece::create ( int object, int level )
{
object = ( level * 100 ) + object; // Final Object Number
if ( status != 0 )
{
dbCloneObject ( object, sides );
dbTextureObject ( object, 4 );
dbPositionObject ( object, 0, 0, 0 );
}
}
// Level functions
void level::loadLevel ()
{
// Open file regarding the level
char file [ 512 ];
sprintf ( file, "%d.lvl", currentLevel );
dbOpenToRead ( 1, file ); // Open the file for reading
// Init variables and write it to file
lastPiece = dbReadByte ( 1 );
for ( int a = 0; a < 100; a++ )
{
levelPiece [ a ].X = dbReadFloat ( 1 );
levelPiece [ a ].Y = dbReadFloat ( 1 );
levelPiece [ a ].angle = dbReadByte ( 1 );
levelPiece [ a ].layer = dbReadByte ( 1 );
levelPiece [ a ].sides = dbReadByte ( 1 );
levelPiece [ a ].status = dbReadByte ( 1 );
levelPiece [ a ].position = dbReadByte ( 1 );
levelPiece [ a ].create ( a, currentLevel );
}
dbCloseFile ( 1 );
}
void level::initLevel ()
{
// Open file regarding the level
char file [ 512 ];
sprintf ( file, "%d.lvl", currentLevel );
if ( dbFileExist ( file ) ) dbDeleteFile ( file ); // Delete the file if it exists
dbOpenToWrite ( 1, file ); // Open the file for reading
// Init variables and write it to file
lastPiece = 0; dbWriteByte ( 1, lastPiece );
for ( int a = 0; a < 100; a++ )
{
levelPiece [ a ].X = 0; dbWriteFloat ( 1, levelPiece [ a ].X );
levelPiece [ a ].Y = 0; dbWriteFloat ( 1, levelPiece [ a ].Y );
levelPiece [ a ].angle = 0; dbWriteByte ( 1, levelPiece [ a ].angle );
levelPiece [ a ].layer = 0; dbWriteByte ( 1, levelPiece [ a ].layer );
levelPiece [ a ].sides = 0; dbWriteByte ( 1, levelPiece [ a ].sides );
levelPiece [ a ].status = 0; dbWriteByte ( 1, levelPiece [ a ].status );
levelPiece [ a ].position = 0; dbWriteByte ( 1, levelPiece [ a ].position );
}
dbCloseFile ( 1 );
}
void level::addPiece ()
{
lastPiece++;
while ( levelPiece [ lastPiece ].sides == 0 )
{
int sides = sidesChooser();
switch ( sides )
{
case 2 :levelPiece [ lastPiece ].sides = 2; break;
case 3 :levelPiece [ lastPiece ].sides = 3; break;
case 4 :levelPiece [ lastPiece ].sides = 4; break;
case 5 :levelPiece [ lastPiece ].sides = 6; break;
default :levelPiece [ lastPiece ].sides = 0; break;
}
dbSync();
}
levelPiece [ lastPiece ].create ( lastPiece, currentLevel );
clearMenu();
}
Now a little rundown of what's happening. I've used the level::initLevel to create the files. You can see I wrote ints and floats. The resulting file is a long line of nothing.
I think that, since all values were 0 when I initialized the files, that the line written on the file is a string of NULL.
When I read the file to edit or play a level, using level::loadLevel, all my variables have, instead of 0, initializing values, for instance, level.lastPiece is 643124 instead of 0. That happens to both ints and floats.
Any ideas of what I'm doing wrong?
Thank you in advance.
V
The only lazy people that can complete games are genius. You don't look like a genius, so you better stop being lazy.
Support your local Riker 9 Chapter.