• Content count

  • Joined

  • Last visited

Community Reputation

119 Neutral

About SpaceEmotion

  • Rank
  1. I was following [url=""]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][/img] Thank you all in advance! EDIT: because < > are recognized as HTML tags I added a space to the include tags! ResourceFile.hpp: [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 : ResourceFile(); ~ResourceFile(); bool createFile (vector<string> files, string destination); bool readFile (string source); char* getResource (string filename); long int getSizeOf (string filename); }; }[/CODE] ResourceFile.cpp: [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; (files[i].c_str(), ifstream::in | ifstream::binary); if (file.is_open()) { memset(&entry, 0, sizeof(RESFileEntry) ); // Fill with zeros memcpy(, 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 m_entries.push_back(entry); } 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 (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++) { (m_entries[i].name, ifstream::in | ifstream::binary); if (file.is_open()) { file.seekg (0, ios::beg); while ( (ioBuffer, 1)) resFile.write (ioBuffer, 1); file.close(); } file.clear(); } resFile.close(); 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, ifstream::in | ifstream::binary); if (resFile.is_open()) { resFile.seekg (0, ios::beg); ((char*)&m_header, sizeof(RESFileHeader)); // Read all entries for (unsigned int i=0;i<m_header.nb_files;i++) { ((char*)&entry, sizeof(RESFileEntry)); // Read a File Entry m_entries.push_back(entry); // Push it to vector } m_filename = src; } else { resFile.close(); 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 (m_filename.c_str(), ifstream::in | ifstream::binary); if (resFile.is_open()) { resFile.seekg (m_entries[i].offset, ios::beg); (m_buffer, m_entries[i].size); resFile.close(); 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] main.cpp: [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; files.push_back("logo.png"); files.push_back("small_bg.png"); files.push_back("txt_loading.png"); files.push_back("demo"); if (!write_test.createFile(files, "assets.res")) return EXIT_FAILURE; ResourceFile read_test; char* buffer; if(!read_test.readFile("assets.res")) { std::cout << "Could not open file" << std::endl; return EXIT_FAILURE; } 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; return EXIT_FAILURE; } 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) { app.close(); break; } } app.clear(); app.draw(logo); app.display(); } return EXIT_SUCCESS; }[/code]
  2. Thank you rip-off, that solved my issue!! However I now have problems starting my program. It somehow does not recognizes the main function. Does it have to do something with the SDLMain.m and SDLMain.h files I need for mac applications? (Error: Undefined symbols for architecture x86_64: "_main", referenced from: start in crt1.10.6.o). My function is currently "int Main(int argc, char* argv[])", when I change "Main" to "main" it also does not work [img][/img] Edit: I used Netbeans, I tried XCode insteaad and it worked! Thanks for the support - can be closed now -
  3. [quote name='rip-off' timestamp='1343215586' post='4962899'] You have declared your static variable Console::prefix, but you have yet to define it. The definition goes in a single source file (the obviously candidate being console.cpp) and looks like this: [code] string Console::prefix; [/code] You may want to initialise it with a default prefix: [code] string Console::prefix = "[hello, world]"; [/code] [/quote] I now added the following lines to the Console class in console.cpp (Under "public:"): [CODE] Console() { prefix = "[Log]"; } [/CODE] Tried to build and got the following error now: [CODE] Undefined symbols for architecture x86_64: "_main", referenced from: start in crt1.10.6.o "Console::prefix", referenced from: Console::setPrefix(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)in console.o Console::log(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)in console.o ld: symbol(s) not found for architecture x86_64 [/CODE] Does is has to do something with the strings maybe? (I personally don't think so though) Or does it has to be something with the way how I use the fields of the class?
  4. I recently started learning C++ (I already know a lot about PHP and Java and have some knowledge in C) to create some games and discovered the SDL framework. After watching some tutorials in youtube I wanted to start myself making some basic stuff so I started setting up the environment using Netbeans. First test were successfully (SDL_Init() and stuff) so I wanted to create some static classes (like i know them in PHP or Java) to create some Console output (using a prefix, date/time and so on) so I had three files in the end (console.h, console.cpp and main.cpp). I got no compile errors but the linker said the following: [CODE] Undefined symbols for architecture x86_64: "Console::prefix", referenced from: Console::setPrefix(std::string) in console.o Console::log(std::string) in console.o ld: symbol(s) not found for architecture x86_64 [/CODE] Because I am a mac user I also tried using XCode (version 4), the same errors again. I don't know if its my code or just another problem, I really hope someone can help me in here. For reference, this is the code I am using: main.cpp ( the char** was added due to a tip in stackoverflow when I googled my problem, it is from my test with XCode) [CODE] #include <SDL/SDL.h> #include <iostream> #include "console.h" int main(int argc, char** argv) { Console::setPrefix("[Game]"); Console::log("Initializing SDL"); SDL_Init(SDL_INIT_EVERYTHING); Console::log("Shutting down SDL"); SDL_Quit(); return 0; } [/CODE] console.h [CODE] #ifndef CONSOLE_H #define CONSOLE_H #include <string> using namespace std; class Console { private: /** * The prefix used in the log output */ static string prefix; public: /** * Sets the prefix for the output * @param msg */ static void setPrefix(string msg); /** * Echoes the message with the prefix to the console * @param msg */ static void log(string msg); }; #endif [/CODE] console.cpp [CODE] #include <iostream> #include <string> #include "console.h" using namespace std; void Console::setPrefix(string msg) { prefix = msg; } void Console::log(string msg) { std::cout << prefix << " " << msg; } [/CODE] (I don't know if its right to place this topic in here but since its a "beginners" problem I think this would be the best place in the forums.)