• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Problem reading resource from own File Format

0 posts in this topic

I was following [url="http://www.sfml-dev.org/wiki/en/tutorials/formatdat"]this tutorial[/url] over at the old SFML wiki on how to create a resource bundle.
I ported everything to the latest version (SFML 2) and everything works fine, except reading a [b]text[/b] resource (pictures are no problem).

EDIT: Pictures seems also to be a problem: "Failed to load image from memory. Reason : Corrupt PNG"

Every time I write the output to the console it gives me sth like "Demo file conten3\339" (The extra characters are the problem).
Somehow it reads more than it needed and adds some parts of the ram to the console which i do not need of course.
The crazy thing is that this problem only occurs if the text length is odd (1,3,5,...). If the length can be divided by two its just fine.

I don't know where the problem is so I hope that someone else can help me - a search with google returned nothing for me [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]
Thank you all in advance!

EDIT: because < > are recognized as HTML tags I added a space to the include tags!

[CODE]#include < iostream >
#include < vector >

using namespace std;

namespace gs {
/// ResourceFile Header struct
struct RESFileHeader {
char uID[4]; /// Unique ID used to know if this file is a DAT File from this class
char version[1]; /// Version of the RES file format
char offset[3]; /// Offset to files
unsigned int nb_files; /// Number of files in the RES file

/// ResourceFile File Entry
struct RESFileEntry {
char name[128]; /// Name of the file
long size; /// Entry size
long offset; /// Offset to where the file is in the RES file

/// ResourceFile main class
class ResourceFile {
private :
string m_filename; // name of the RES file
RESFileHeader m_header; /// file header
vector<RESFileEntry> m_entries; /// vector of files entries
char* m_buffer; /// Buffer pointing on a file in memory

public :

bool createFile (vector<string> files, string destination);
bool readFile (string source);
char* getResource (string filename);
long int getSizeOf (string filename);
[code]#include < fstream >

#include "ResourceFile.hpp"

namespace gs {
ResourceFile::ResourceFile() {
m_buffer = NULL;

ResourceFile::~ResourceFile() {
if(m_buffer != NULL)
delete m_buffer;

bool ResourceFile::createFile(vector<string> files, string dest) {
// File entry in order to push it in the object's vector
RESFileEntry entry;

// Input and ouput streams
ifstream file;
ofstream resFile;

// Read/Write buffer
char ioBuffer[1];

// Setting the File Header;
memset(&m_header, 0, sizeof(m_header)); // Fill all with zeros
memcpy(m_header.uID, "RESF", 5); // Copy the unique ID
memcpy(m_header.version, "1", 3); // Set file protocol version
m_header.nb_files = (unsigned int)files.size(); // Number of files the file contains

for (unsigned int i = 0; i < files.size(); i++) {
cout << files[i] << endl;

file.open (files[i].c_str(), ifstream::in | ifstream::binary);

if (file.is_open()) {
memset(&entry, 0, sizeof(RESFileEntry) ); // Fill with zeros
memcpy(entry.name, files[i].c_str(), strlen ( files[i].c_str() ) ); // We keep the file name
file.seekg (0, ios::end); // Calculate file size
entry.size = file.tellg(); // Set size
entry.offset = 0;
file.close(); // Close and finish file

//Finally, we add this File Entry in our vector

} else {
cout << "ERROR Writing File " << files[i] << ". Stopping process!" << endl;
return false;

// Once we've got the file offsets, we are setting them correctly
long realOffset = 0;
realOffset += sizeof(RESFileHeader);
realOffset += m_header.nb_files * sizeof(RESFileEntry);

for (unsigned int i = 0; i < m_entries.size(); i++) {
m_entries[i].offset = realOffset;
realOffset += m_entries[i].size;

//And finally, we are writing the DAT file
resFile.open (dest.c_str(), ofstream::out | ofstream::binary);

// Write file header
resFile.write ((char*)&m_header, sizeof(RESFileHeader) );

// Write file entries table
for (unsigned int i=0;i<m_entries.size();i++)
resFile.write ((char*)&m_entries[i], sizeof(RESFileEntry) );

// Write each file
for (unsigned int i = 0; i<m_entries.size(); i++) {
file.open (m_entries[i].name, ifstream::in | ifstream::binary);

if (file.is_open()) {
file.seekg (0, ios::beg);

while (file.read (ioBuffer, 1))
resFile.write (ioBuffer, 1);



return true;

bool ResourceFile::readFile(string src) {
ifstream resFile; // Resource file to read from
RESFileEntry entry; // Raw file entry

memset(&m_header, 0, sizeof(m_header)); // Fill all with zeros

// Open and read file
resFile.open(src.c_str(), ifstream::in | ifstream::binary);

if (resFile.is_open()) {
resFile.seekg (0, ios::beg);
resFile.read ((char*)&m_header, sizeof(RESFileHeader));

// Read all entries
for (unsigned int i=0;i<m_header.nb_files;i++) {
resFile.read ((char*)&entry, sizeof(RESFileEntry)); // Read a File Entry
m_entries.push_back(entry); // Push it to vector

m_filename = src;
} else {
return false;

resFile.close(); // Close file
return true;

char* ResourceFile::getResource(string name) {
ifstream resFile;

// Clean buffer
if (m_buffer != NULL) {
delete (m_buffer);
m_buffer = NULL;

// Find correct file
for (unsigned int i = 0; i < m_header.nb_files; i++) {
if (m_entries[i].name == name) {
m_buffer = new char[(m_entries[i].size)];

if (m_buffer==NULL) return (NULL); // Prevent errors through simple catch

// Open and get file
resFile.open (m_filename.c_str(), ifstream::in | ifstream::binary);
if (resFile.is_open()) {
resFile.seekg (m_entries[i].offset, ios::beg);
resFile.read (m_buffer, m_entries[i].size);

return m_buffer;

// No such file - return NULL
return NULL;

long int ResourceFile::getSizeOf (string name) {
// Find correct file
for (unsigned int i=0; i<m_header.nb_files;i++) {
if (m_entries[i].name == name)
return (m_entries[i].size);

// Return zero
return 0;
[code]#include < SFML/Graphics.hpp >
#include "engine/ResourceFile.hpp"

using namespace gs;

int main (int argc, const char * argv[]) {
ResourceFile write_test;
std::vector<std::string> files;


if (!write_test.createFile(files, "assets.res"))

ResourceFile read_test;
char* buffer;

if(!read_test.readFile("assets.res")) {
std::cout << "Could not open file" << std::endl;

std::cout << read_test.getResource("demo") << std::endl;

// Test the images

sf::RenderWindow app(sf::VideoMode(500, 400), "Hibernate");

sf::Texture tex;

buffer = read_test.getResource("logo.png");
if (buffer==NULL) {
std::cout << "Read error" << std::endl;

tex.loadFromMemory(buffer, read_test.getSizeOf("logo.png"));

sf::Sprite logo = sf::Sprite(tex);
sf::Event event;

while(app.isOpen()) {
while (app.pollEvent(event)) {
if(event.type == sf::Event::Closed) {


}[/code] Edited by SpaceEmotion

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0