Last time I explained up to the setting of OpenGL ES, this time I thought I would proceed, but I found that C / C ++ code can be used on Android So, take a detour.
The OpenGL code is overwhelmingly C / C ++, so I expected that the asset could be used, but the usage was more difficult than I had imagined. I haven't figured out everything yet, but here's a summary of what I understand.
I don't have much information on the latest Android Studio, so I'm trying to find something wrong, so I'd appreciate it if you could point out any mistakes.
In Android Studio 2.2, the check box "Do you want to use C / C ++?" Is displayed when creating a new project, so turn it ON first.
Check the configuration of the new project. Change the project structure on the left from "Android" to "Project" so that you can see all the files.
It is OK if you have the following files.
Once you build it, you should see a TextView on the screen. You can see that the code in cpp / native-lib.c is called from MainActivity.java.
public class MainActivity extends AppCompatActivity {
// native-lib.c seems to be a library at compile, and I'm loading it here.
//Is it something like import?
static {
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setText(stringFromJNI());
}
/**
* native-lib.It seems to define the function implemented in c.
*/
public native String stringFromJNI();
}
I think the following code is written by default. Looking at this code, It seems that the package, project name, and function name are connected with an underscore. Maybe this is the format.
It seems that it is defined by the description. The return value is also in the form of "jstring". I think this was an Android NDK or a JNI (Java native interface) type.
#include <jni.h>
#include <string>
extern "C"
jstring
Java_com_test_opengl_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
First, modify as follows so that extern "C" is applied to the whole.
extern "C" {
//Put the function here.
}
Write a test function as appropriate. The arguments are standard, so enter the same ones.
extern "C" {
//The description of stringFromJNI is omitted.
void
Java_quad_jp_opengl_MainActivity_sayHello(JNIEnv *env, jobject){
std::cout << "Hello World" << std::endl;
}
}
Of course, the function is also defined for MainActivity.
public native void sayHello();
When I built it, it ended without any errors, I didn't know where to see the output of cout ... orz
For the time being, I know how to execute the process on the C side, so let's modify the previous code. I will add it here later.
Android NDK (official page has detailed setting method)
Some pages have been translated, but the translation is not good enough and it is a little confusing. Adding C / C ++ code to your project
Recommended Posts