Cocos2dx File Processing, cocos2dx

Source: Internet
Author: User

Cocos2dx File Processing, cocos2dx

Question 1: fopen

In vs, fopen is used to process files and run through. However, a major problem occurs when it is transplanted to the android source code, first, you need to understand that the development resources in vs are stored in the same directory of the execution file, and the resources are stored in the assets Directory when transplanted to android, you have tried the following methods before:

 

[Cpp]View plaincopy
  1. Char * fileName = "bg.png ";
  2. String filepath = CCFileUtils: sharedFileUtils ()-> fullPathForFilename (fileName );
  3. FILE * fp = fopen (filepath. c_str (), "r ");


The result is still an error. When the game is entered, it is estimated that it is related to the underlying layer. The solution is to use CCFileUtils of cocos2dx.

 

 

[Cpp]View plaincopy
  1. Char * filename = "bg.png ";
  2. // Obtain the absolute path of the file in the system-assets/bg.png
  3. String filepath = CCFileUtils: sharedFileUtils ()-> fullPathForFilename (filename );
  4. // The number of bytes to read. If the read fails, the value is 0.
  5. Unsigned long len = 0;
  6. // Read content
  7. Unsigned char * data = CCFileUtils: sharedFileUtils ()-> getFileData (filepath. c_str (), "rb", & len );
  8. /*
  9. Perform operations on files here
  10. */
  11. // Remember to release the memory
  12. If (len> 0 & data) delete [] data;
  13. ************** ***************
  14. Static unsigned char * getFileData (const char * pszFileName,
  15. Const char * pszMode,
  16. Unsigned long * pSize
  17. )
  18. PszMode:
  19. R: open the file in read-only mode. The file must exist.
  20. R + can open a file in read/write mode. The file must exist.
  21. Rb + read/write opens a binary file and allows reading data.
  22. Rt + read/write: open a text file and allow reading and writing.
  23. W. Open and write only the file. If the file exists, the file length is 0, indicating that the file content will disappear. If the file does not exist, the file is created.
  24. W + open the readable and writable file. If the file exists, the file length is cleared to zero, that is, the file content disappears. If the file does not exist, the file is created.
  25. A. Open and write-only files as an attachment. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. (EOF reserved)
  26. A + opens readable and writable files by appending them. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. (The original EOF is not retained)
  27. Wb only opens or creates a new binary file; only data can be written.
  28. Wb + enables read/write operations or creates a binary file, allowing read and write operations.
  29. Wt + read/write opens or creates a text file; read/write is allowed.
  30. At + read/write opens a text file, allowing you to read or append data at the end of the text.
  31. Open a binary file through AB + read/write, and allow you to read or append data to the end of the file.
  32. */


Question 2: tinyxml

 

When tinyxml is used to read xml files, my previous practices are as follows:

 

[Cpp]View plaincopy
  1. // Create an animation based on xml data
  2. String xmlFile_path = "bg. xml ";
  3. // Read the XML file of the action
  4. Tinyxml2: XMLDocument * doc = new tinyxml2: XMLDocument ();
  5. Doc-> LoadFile (xmlFile_path.c_str ());
  6. Tinyxml2: XMLElement * ani_node = doc-> RootElement ();
  7. Tinyxml2: XMLElement * action_node = ani_node-> FirstChildElement ("action ");


In vs is run through, but transplanted to android crashes, and later on the Internet to see some solution: Connection http://blog.sina.com.cn/s/blog_6e5f7d400100zlj7.html

 

First read the xml file to a buff, and then let tinyxml parse the buff, so that tinyxml bypasses the bug of File Reading failure.

 

[Cpp]View plaincopy
  1. // Create an animation based on xml data
  2. String xmlFile_path = "bg. xml ";
  3. // Obtain the absolute path of the file in the system
  4. String xmlfilepath = CCFileUtils: sharedFileUtils ()-> fullPathForFilename (xmlFile_path.c_str ());
  5. // The number of bytes to read. If the read fails, the value is 0.
  6. Unsigned long len = 0;
  7. // Read content
  8. Unsigned char * data = CCFileUtils: sharedFileUtils ()-> getFileData (xmlfilepath. c_str (), "r", & len );
  9. // Read the XML file of the action
  10. Tinyxml2: XMLDocument * doc = new tinyxml2: XMLDocument ();
  11. Doc-> Parse (char *) data, len );
  12. Tinyxml2: XMLElement * ani_node = doc-> RootElement ();
  13. Tinyxml2: XMLElement * action_node = ani_node-> FirstChildElement ("action ");

Cocos2dx file processing by the source code souzang network finishing, reprint please indicate the source http://www.codesocang.com/jiaocheng/shoujikaifa/10121.html


How does cocos2dx CCFileUtils or CCDictionary read files?

Is it necessary to use CCFileUtils when loading music and image resources? Recently, I also encountered a similar situation. For example, before loading music, CCFileUtils was used to add music files to SimpleAudioEngine: sharedEngine () -> preloadBackgroundMusic (CCFileUtils: fullPathFromRelativeFile ("Sounds/BGM/play_bgm_long1_"); now you can directly change it to an absolute path to SimpleAudioEngine: sharedEngine () -> preloadBackgroundMusic ("Sounds/BGM/play_bgm_long.pdf"); I hope the landlord will handle the same situation as me. In fact, you only need to carefully check the objects required by the parameters of the called method, we should be able to find the reason to a large extent

How does cocos2dx get all file names in a folder?

Thank you for your reference.
Using System. IO;

String Path = "Images ";
List <string> files = null;
String getFilesFilter = "*. jpg; *. jpeg; *. jpe; *. gif; *. bmp; *. png ;";
String [] arrFilter = getFilesFilter. Split (';');
If (! String. IsNullOrEmpty (Path ))
{
Files = new List <string> ();
Try
{
DirectoryInfo di = new DirectoryInfo ("Images ");
For (int I = 0; I <arrFilter. Length; I ++)
{
If (di. Exists)
{
Foreach (FileInfo fileInfo in di. GetFiles (arrFilter [I])
{
Files. Add (fileInfo. FullName );
If (files. Count> 50)
Break;
}
}
}
}
Catch (IOException ){}
Catch (ArgumentException ){}
Catch (SecurityException ){}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.