[Add] Added a class to read files, wanted something useful for animations. I will commit that soon.

This commit is contained in:
Rtch90 2012-04-14 21:50:36 +01:00
parent 905e040f78
commit 66d01ce735
4 changed files with 144 additions and 15 deletions

View File

@ -55,7 +55,9 @@ HEADERS += ../src/Actor/Player.h \
../src/Collision/TileCollision.h \
../src/Actor/NPC.h \
../src/Sound/SoundEffect.h \
../src/Actor/Actor.h
../src/Actor/Actor.h \
../src/Animation/AnimimationSequence.h \
../src/System/FileReader.h
SOURCES += ../src/Actor/Player.cpp \
../src/Collision/AABB.cpp \
../src/Global/Globals.cpp \
@ -87,5 +89,7 @@ SOURCES += ../src/Actor/Player.cpp \
../src/TMXParser/base64.cpp \
../src/Actor/NPC.cpp \
../src/Sound/SoundEffect.cpp \
../src/Actor/Actor.cpp
../src/Actor/Actor.cpp \
../src/Animation/AnimimationSequence.cpp \
../src/System/FileReader.cpp
OTHER_FILES +=

View File

@ -6,19 +6,6 @@
class Actor {
public:
enum State {
WALKING,
RUNNING,
ATTACKING,
};
enum Facing {
FRONT,
BACK,
LEFT,
RIGHT
};
Actor(void);
~Actor(void);

110
src/System/FileReader.cpp Normal file
View File

@ -0,0 +1,110 @@
#include <stdlib.h>
#include <assert.h>
#include "Debug.h"
#include "FileReader.h"
FileReader::FileReader(void) {
}
FileReader::~FileReader(void) {
}
bool FileReader::Exists(const char* filename) {
// Check to see if _filename is existent in memory,
_file = fopen(filename, "r");
if(_file) {
// Close the file we have.
fclose(_file);
_file = NULL;
return true;
} else {
// Create the file.
_file = fopen(filename, "w");
fclose(_file);
_file = NULL;
return true;
}
return false;
}
void FileReader::OpenFile(const char* filename, const char* accessType) {
// Open _filename into memory, passing in the access type.
_filename = filename;
_accessType = accessType;
_file = fopen(filename, accessType);
}
void FileReader::CloseFile(void) {
if(_file) {
fclose(_file);
_file = NULL;
}
}
void FileReader::Write(const char* buffer) {
if(_file) {
fprintf(_file, "%s", buffer);
} else {
// _filename does not exist or we have the wrong accessType.
assert(false);
}
}
void FileReader::Write(const int buffer) {
if((_file) && (_accessType == "w")) {
fprintf(_file, "%i", buffer);
} else {
// _filename does not exist or we have the wrong accessType.
assert(false);
}
}
void FileReader::Read(const int &value) {
if((_file) && (_accessType == "r")) {
fscanf(_file, "%i", &value);
} else {
// _filename does not exist or we have the wrong accessType.
assert(false);
}
}
void FileReader::WriteBuffer(const char* buffer) {
if((_file) && (_accessType == "w")) {
fwrite(buffer, 1, strlen(buffer), _file);
}
}
void FileReader::WriteBuffer(const char* buffer, int count) {
if((_file) && (_accessType == "w")) {
fwrite(buffer, 1, count, _file);
}
}
void FileReader::ReadBuffer(char* &buffer) {
if((_file) && (_accessType == "r")) {
size_t size = 0;
fseek(_file, 0, SEEK_END);
size = ftell(_file);
rewind(_file);
buffer = (char*)malloc(sizeof(char)* size);
if(buffer != NULL) {
fread(buffer, 1, size, _file);
}
}
}
void FileReader::ReadBuffer(const char* buffer, int count) {
if((_file) && (_accessType == "r")) {
buffer = (char*)malloc(sizeof(char)*count);
if(buffer != NULL) {
fread((char*) buffer, 1, count, _file);
}
}
}

28
src/System/FileReader.h Normal file
View File

@ -0,0 +1,28 @@
#pragma once
#include <string.h>
#include <stdio.h>
class FileReader {
public:
FileReader(void);
~FileReader(void);
void OpenFile(const char* filename, const char* accessType);
void CloseFile(void);
void Write(const char* buffer);
void Write(const int value);
void Read(const char* buffer);
void Read(const int &value);
void WriteBuffer(const char* buffer);
void WriteBuffer(const char* buffer, int count);
void ReadBuffer(char* &buffer);
void ReadBuffer(const char* buffer, int count);
bool Exists(const char* filename);
private:
FILE* _file;
std::string _filename;
std::string _accessType;
};