Programação com OpenGL/Android GLUT Wrapper: diferenças entre revisões

[edição não verificada][edição não verificada]
Conteúdo apagado Conteúdo adicionado
Linha 251:
 
== Orientações ==
 
Pela opção:
<source lang="xml">
<activity ...
android:screenOrientation="portrait"
</source>
Seu aplicativo pode trabalhar apenas no modo portrait, independente do seu dispositivo ou das formas. Isto não é recomendado mas é muito usado em alguns jogos.
 
Para manusear as orientção mais eficientemente, você teoricamente precisa checar pelo evento <code>onSurfaceChanged</code>.
O manuseador <code>onSurfaceChanged_native</code> no montador(wrapper) <code>android_app_NativeActivity.cpp</code> não parece criar
o evento<code>onNativeWindowResized</code> adequadamente na mudança de orientação, Então vamos monitora-lo regularmente:
<source lang="cpp">
/* glutMainLoop */
 
int32_t lastWidth = -1;
int32_t lastHeight = -1;
 
// loop waiting for stuff to do.
while (1) {
 
...
 
int32_t newWidth = ANativeWindow_getWidth(engine.app->window);
int32_t newHeight = ANativeWindow_getHeight(engine.app->window);
if (newWidth != lastWidth || newHeight != lastHeight) {
lastWidth = newWidth;
lastHeight = newHeight;
onNativeWindowResized(engine.app->activity, engine.app->window);
// Process new resize event :)
continue;
}
</source>
Agora nos podemos processar o evento:
<source lang="cpp">
static void onNativeWindowResized(ANativeActivity* activity, ANativeWindow* window) {
struct android_app* android_app = (struct android_app*)activity->instance;
LOGI("onNativeWindowResized");
// Sent an event to the queue so it gets handled in the app thread
// after other waiting events, rather than asynchronously in the
// native_app_glue event thread:
android_app_write_cmd(android_app, APP_CMD_WINDOW_RESIZED);
}
</source>
 
Nota: é possivel processar pelo evento <code>APP_CMD_CONFIG_CHANGED</code>, mas isto só acontece depois que tela é redimensionada, isto é muito cedo para pegar o novo tamanho da tela.
 
O Android pode apenas detectar o nova resolução e depois da troca de buffer, então vamos abusar de outro engancho para obter o evento de redimensionamento:
<source lang="cpp">
/* android_main */
state_param->activity->callbacks->onContentRectChanged = onContentRectChanged;
 
...
 
static void onContentRectChanged(ANativeActivity* activity, const ARect* rect) {
LOGI("onContentRectChanged: l=%d,t=%d,r=%d,b=%d", rect->left, rect->top, rect->right, rect->bottom);
// Make Android realize the screen size changed, needed when the
// GLUT app refreshes only on event rather than in loop. Beware
// that we're not in the GLUT thread here, but in the event one.
glutPostRedisplay();
}
</source>
 
== Referencia ==