From 66d01ce735c486b1d1a42822ebd3d0462dba8888 Mon Sep 17 00:00:00 2001 From: Rtch90 Date: Sat, 14 Apr 2012 21:50:36 +0100 Subject: [PATCH] [Add] Added a class to read files, wanted something useful for animations. I will commit that soon. --- LibDQt/LibDQt.pro | 8 ++- src/Actor/Actor.h | 13 ----- src/System/FileReader.cpp | 110 ++++++++++++++++++++++++++++++++++++++ src/System/FileReader.h | 28 ++++++++++ 4 files changed, 144 insertions(+), 15 deletions(-) create mode 100644 src/System/FileReader.cpp create mode 100644 src/System/FileReader.h diff --git a/LibDQt/LibDQt.pro b/LibDQt/LibDQt.pro index acdf8a4..cbeec78 100644 --- a/LibDQt/LibDQt.pro +++ b/LibDQt/LibDQt.pro @@ -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 += diff --git a/src/Actor/Actor.h b/src/Actor/Actor.h index 3566208..2e29a40 100644 --- a/src/Actor/Actor.h +++ b/src/Actor/Actor.h @@ -6,19 +6,6 @@ class Actor { public: - enum State { - WALKING, - RUNNING, - ATTACKING, - }; - - enum Facing { - FRONT, - BACK, - LEFT, - RIGHT - }; - Actor(void); ~Actor(void); diff --git a/src/System/FileReader.cpp b/src/System/FileReader.cpp new file mode 100644 index 0000000..8c12d62 --- /dev/null +++ b/src/System/FileReader.cpp @@ -0,0 +1,110 @@ +#include +#include + +#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); + } + } +} diff --git a/src/System/FileReader.h b/src/System/FileReader.h new file mode 100644 index 0000000..970f039 --- /dev/null +++ b/src/System/FileReader.h @@ -0,0 +1,28 @@ +#pragma once +#include +#include + +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; +};