How can i reach files from Android NDK? - android

I never make a android project and i don't know java. I am trying to port my c++ sdl opengl project to android. I am using android studio. I opened sdl sample android project and now i can manage project from main.cpp . How do i know this because I changed the color of the window and it worked. But when i try to load shader.text file with ifstream program crashed. I understand that this will not happen. After this i searched but I couldn't find a complete code or an example.
When i put files in assets folder and build apk. apk size grows as files. Therefore I need to kepth files in assets folder ?
I think i need to use asset manager but of course i don't know.
What files should I write what code? I mean what i need to add main.cpp and I need to add code java side also ?
main.cpp :
int main()
{
//lets say i have filePath like these
char file_vert[100];
snprintf(file_vert,100,"shaders/vertexShader.text");
char file_Texture[100];
snprintf(file_Texture,100,"textures/image.jpg");
//i am using them in cpp project
//shader text file like this
std::ifstream vShaderFile;
vShaderFile.open(file_vert);
//and i am loading texture file with "stbi" library
unsigned char *data = stbi_load(file_Texture, &width, &height, &nrComponents, 0);
}

Related

cocos2dx- 3.7 and 3.7.1 cocos studio don't work

Hello I'm working on cocos2dx-3.7.1 and I want to create an Scene on cocos studio, when I try to import the csb file only whit the template Helloworld.png works, but if I add a new Image and place it on the Scene(cocos studio) and publish, when I run the program I only see the helloworld.png.
Here is my code:
#include "GS_MissionSelect.h"
#include "Game.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
USING_NS_CC;
bool GS_MissionSelect::init()
{
//////////////////////////////
// 1. super init first
if (!Layer::init())
{
return false;
}
auto rootNode = CSLoader::createNode("CS_MainMenu/MainScene.csb");
this->addChild(rootNode);
return true;
}
I don't know what I need to do, I can't add more resources on Android studio and publish, only see Helloworld.png (default img of cocostudio).
I happen to meet same situation.
I deleted cocos 3.7.1 and have downgraded cocos 2dx to 3.4.
It solved publish problem.
I have had this problem before, and it happened because I only copied the .csb file published in the res folder, instead of copying all the files in the res folder as it is. You have to stick to the structure of your cocostudio project within your resource folder.
Just check the following please. Have you copied your entire publish folder into your resources folder? If not you should. Also could you edit your answer and put your directory structure for your published folder as well as your cocostudio content folder within the cocostudio editor and also the directory structure of your resource folder. It's just to rule out that your structure is correct and the problem lies in cocos.

android NDK. libzip stopped to sub folder

I use libzip to read APK content in native code.
assets/sounds/voice folder "disappeared" for libzip functions only.
libzip still see other files.
I still can see it in assets/sounds/voice in project folder.
I did clean many times.
I still can unzip APK file from bin folder and I see all required files. (including files from assets/sounds/voice folder)
I added small logging command and it does not print files from assets/sounds/voice folder. All other files are present.
int inum = zip_get_num_files(apkzip);
for(int i = 0; i < inum; i++){
const char * name = zip_get_name(apkzip, i, 0);
__android_log_print(ANDROID_LOG_INFO,"FILEZIP", "zip: %s", name);
}
I have no idea what at what step it is wrong. Had eclipse compressed ZPK with new format that my libzip does not able to read? or what could be a reason.
This is not an answer for libzip, but why you have to use libzip to read apk file in NDK application? In Android NDK, you can use AAsset APIs to get file from the apk. Take a look at "How To Get File In Assets From Android NDK".
EDITED
It is not available in android-5 that I still use
Ok. So how about Cocos2d-x's ZipFile? It is based on Minizip.
How to use ZipFile from cocos2dx/platform/android/CCFileUtilsAndroid.cpp
s_pZipFile = new ZipFile(resourcePath, "assets/");
pData = s_pZipFile->getFileData(fullPath.c_str(), pSize);

c++ copy file for ios and android

I am familiar with objective-c to copy file using cocoatouch sdk.
But I hope add copy file function to my game (cocos2dx c++).
Is there a common way for ios and android to copy file using c++?
Your comment welcome
int main()
{
std::ifstream src("from.ogv", std::ios::binary);
std::ofstream dst("to.ogv", std::ios::binary);
dst << src.rdbuf();
}
Taken from here.
Of course in Android you need NDK for that.

Ouput to text file using C++ STL library in Android

I have an Android application that use JNI. In JNI I used C++ STL library to output some data into a text file.
This is the snippet code.
#include <iostream>
#include <fstream>
float mydata[4] = {0.0f};
ofstream file;
file.open("Data.txt", ios::app);
// For example
file<<mydata[1];
file.close();
when I ran ndk-build, I did not face any errors and the program seems run properly. But I did not found any text file in my Application directory.
does anybody know what could be the issue? Please help.
Thanks
Your code works, but you apparently try to access a path which is forbidden.
Firstly, be sure that your Android application asks for storage permissions.
Secondly, try to replace "Data" by an absolute path you know you have access to (e.g. "/sdcard/Pictures/Data.txt").
Also, as a better practice, you should test if the file has been opened before writing to it.

Android JNI, how to load library with soname libxx.so.1.2.3

Need to use a shared lib for android from 3rd party, the lib's soname and file name are same, in format libxx.so.1.2.3, which is common on linux. I rename the lib file to libxx.so, and link libxx.so in libmyjni.so using ndk-build. In my java code, before calling the functions in libmyjni.so, I load them like this:
System.load("/data/local/tmp/libxx.so.1.2.3");
System.loadLibrary("myjni");
I have to manually copy libxx.so.1.2.3 to /data/local/tmp/. It works well in this way, after above loading, I can call functions in libmyjni.so.
In code "System.loadLibrary("myjni");", system always trying to get libxx.so.1.2.3 from somewhere. I want to know, in the real world, how could I copy libxx.so.1.2.3 to a specific location on android device during installation? so that I can System.load() it.
Or android has official way to install self made lib to /system/lib/?
If libxx.so.1.2.3 is in format libxx.so then I can use System.loadLibrary("xx") to load it.
Here is finally what I did, keep the libxx.so.1.2.3 untouched and do all stuff in Java:
Put libxx.so.1.2.3 in android assets.
In MainApp.OnCreate(), copy the file to private file folder and load it from there:
AssetManager am = applicationContext.getAssets();
in = am.open(OPENSSL_SO_LIB_NAME); // source instream
File privateStorageDir = applicationContext.getFilesDir();
String libPath = privateStorageDir.getAbsolutePath(); // copy the lib to here ...
System.load(libPath + "/" + SO_LIB_NAME);

Categories

Resources