From 0dc3241a723c1ae93bada75fe3f3d3a3455ff881 Mon Sep 17 00:00:00 2001 From: Edouard Dupin Date: Fri, 9 Dec 2011 13:14:30 +0100 Subject: [PATCH] Android first application working... --- Sources/base/gui.h | 1 + Sources/base/guiAndroid.cpp | 14 +++- Sources/base/guiX11.cpp | 5 ++ Sources/ewol/FontBitmap.cpp | 104 ++++++++++++++++++------ Sources/ewol/OObject/2DText.cpp | 10 ++- Sources/ewol/Texture.cpp | 136 ++++++++++++++++++++++++-------- Sources/ewol/Texture.h | 8 +- Sources/ewol/ewol.cpp | 5 ++ Sources/ewol/ewol.h | 1 + 9 files changed, 217 insertions(+), 67 deletions(-) diff --git a/Sources/base/gui.h b/Sources/base/gui.h index 085e02ba..899103ef 100644 --- a/Sources/base/gui.h +++ b/Sources/base/gui.h @@ -42,6 +42,7 @@ namespace guiAbstraction void StartResizeSystem(void); void StartMoveSystem(void); bool IsPressedInput(int32_t inputID); + void ForceRedrawAll(void); }; //!< must be define in CPP by the application ... diff --git a/Sources/base/guiAndroid.cpp b/Sources/base/guiAndroid.cpp index 907f32c4..48efedcf 100644 --- a/Sources/base/guiAndroid.cpp +++ b/Sources/base/guiAndroid.cpp @@ -37,6 +37,7 @@ #include #include +#include #undef __class__ #define __class__ "AndroidJNI" @@ -74,6 +75,7 @@ extern "C" gAppAlive = 1; sDemoStopped = 0; sTimeOffsetInit = 0; + ewol::TextureOGLContext(true); } @@ -82,6 +84,8 @@ extern "C" m_width = w; m_height = h; EWOL_INFO("Resize w=" << w << " h=" << h); + ewol::TextureOGLContext(false); + ewol::TextureOGLContext(true); if (NULL != m_uniqueWindows) { m_uniqueWindows->CalculateSize((etkFloat_t)m_width, (etkFloat_t)m_height); } @@ -91,6 +95,7 @@ extern "C" void Java_com_example_ewolAbstraction_EwolRenderer_nativeDone( JNIEnv* env ) { EWOL_INFO("Renderer : Close All Application"); + ewol::TextureOGLContext(false); } /* This is called to indicate to the render loop that it should @@ -205,7 +210,7 @@ extern "C" curTime = 0; } } - + Draw(); } @@ -445,6 +450,13 @@ bool guiAbstraction::IsPressedInput(int32_t inputID) //} } +void guiAbstraction::ForceRedrawAll(void) +{ + if (NULL != m_uniqueWindows) { + m_uniqueWindows->CalculateSize((etkFloat_t)m_width, (etkFloat_t)m_height); + } +} + // never had main in android ... /* int main(int argc, char *argv[]) diff --git a/Sources/base/guiX11.cpp b/Sources/base/guiX11.cpp index 61b050df..0ae84272 100644 --- a/Sources/base/guiX11.cpp +++ b/Sources/base/guiX11.cpp @@ -28,6 +28,8 @@ #include #include +#include + #include #include #include @@ -342,11 +344,14 @@ namespace guiAbstraction { EWOL_INFO("XF86 DRI NOT available\n"); } #endif + //ewol::TextureOGLContext(true); return true; } void Draw(void) { + // TODO : set this otherwise + ewol::TextureOGLContext(true); //EWOL_DEBUG("redraw (" << m_width << "," << m_height << ")"); if(NULL == m_uniqueWindows) { //EWOL_DEBUG("Has No Windows set..."); diff --git a/Sources/ewol/FontBitmap.cpp b/Sources/ewol/FontBitmap.cpp index 569a5264..91b71af0 100644 --- a/Sources/ewol/FontBitmap.cpp +++ b/Sources/ewol/FontBitmap.cpp @@ -203,7 +203,7 @@ namespace ewol }; uint32_t GetOglId(void) { - return m_textureId; + return GetTextureGLID(m_textureId); }; int32_t GetHeight(void) { @@ -286,6 +286,8 @@ int32_t ewol::LoadFont(etk::String fontName, int32_t size) return s_listLoadedFonts.Size()-1; } + + void ewol::UnloadFont(int32_t id) { EWOL_TODO("I do not think it was a good idea... will be done later"); @@ -308,6 +310,7 @@ void ewol::DrawText(int32_t fontID, etk::VectorType & listOfElement = s_listLoadedFonts[fontID]->GetRefOnElement(); fontTextureId = s_listLoadedFonts[fontID]->GetOglId(); + EWOL_DEBUG("**************** plop " << fontTextureId); int32_t size = s_listLoadedFonts[fontID]->GetHeight(); etkFloat_t posDrawX = drawPosition.x; @@ -375,6 +378,7 @@ void ewol::DrawText(int32_t fontID, char * tmpVal = (char*)utf8String; fontTextureId = s_listLoadedFonts[fontID]->GetOglId(); + EWOL_DEBUG("**************** plop2 " << fontTextureId); int32_t size = s_listLoadedFonts[fontID]->GetHeight(); etkFloat_t posDrawX = drawPosition.x; @@ -400,29 +404,81 @@ void ewol::DrawText(int32_t fontID, etkFloat_t sizeWidth = listOfElement[charIndex].width; // 0x01 == 0x20 == ' '; if (tmpChar != 0x01) { - // set texture coordonates : - coordTex.PushBack(listOfElement[charIndex].posStart); - texCoord_ts tmpTex; - tmpTex.u = listOfElement[charIndex].posStop.u; - tmpTex.v = listOfElement[charIndex].posStart.v; - coordTex.PushBack(tmpTex); - coordTex.PushBack(listOfElement[charIndex].posStop); - tmpTex.u = listOfElement[charIndex].posStart.u; - tmpTex.v = listOfElement[charIndex].posStop.v; - coordTex.PushBack(tmpTex); - // set display positions : - /*int32_t xxxx = posDrawX; - int32_t yyyy = drawPosition.y;*/ - coord2D_ts tmpCoord; - tmpCoord.x = posDrawX; - tmpCoord.y = drawPosition.y; - coord.PushBack(tmpCoord); - tmpCoord.x = posDrawX + sizeWidth; - coord.PushBack(tmpCoord); - tmpCoord.y = drawPosition.y + size; - coord.PushBack(tmpCoord); - tmpCoord.x = posDrawX; - coord.PushBack(tmpCoord); + #if !defined(__PLATFORM__Android) + // set texture coordonates : + coordTex.PushBack(listOfElement[charIndex].posStart); + texCoord_ts tmpTex; + tmpTex.u = listOfElement[charIndex].posStop.u; + tmpTex.v = listOfElement[charIndex].posStart.v; + coordTex.PushBack(tmpTex); + coordTex.PushBack(listOfElement[charIndex].posStop); + tmpTex.u = listOfElement[charIndex].posStart.u; + tmpTex.v = listOfElement[charIndex].posStop.v; + coordTex.PushBack(tmpTex); + // set display positions : + /*int32_t xxxx = posDrawX; + int32_t yyyy = drawPosition.y;*/ + coord2D_ts tmpCoord; + tmpCoord.x = posDrawX; + tmpCoord.y = drawPosition.y; + coord.PushBack(tmpCoord); + tmpCoord.x = posDrawX + sizeWidth; + coord.PushBack(tmpCoord); + tmpCoord.y = drawPosition.y + size; + coord.PushBack(tmpCoord); + tmpCoord.x = posDrawX; + coord.PushBack(tmpCoord); + #else + // NOTE : Android does not support the Quads elements ... + /* Step 1 : + * ******** + * ****** + * **** + * ** + * + */ + // set texture coordonates : + coordTex.PushBack(listOfElement[charIndex].posStart); + texCoord_ts tmpTex; + tmpTex.u = listOfElement[charIndex].posStop.u; + tmpTex.v = listOfElement[charIndex].posStart.v; + coordTex.PushBack(tmpTex); + coordTex.PushBack(listOfElement[charIndex].posStop); + // set display positions : + coord2D_ts tmpCoord; + tmpCoord.x = posDrawX; + tmpCoord.y = drawPosition.y; + coord.PushBack(tmpCoord); + tmpCoord.x = posDrawX + sizeWidth; + coord.PushBack(tmpCoord); + tmpCoord.y = drawPosition.y + size; + coord.PushBack(tmpCoord); + + /* Step 2 : + * + * ** + * **** + * ****** + * ******** + */ + + // set texture coordonates : + coordTex.PushBack(listOfElement[charIndex].posStart); + coordTex.PushBack(listOfElement[charIndex].posStop); + tmpTex.u = listOfElement[charIndex].posStart.u; + tmpTex.v = listOfElement[charIndex].posStop.v; + coordTex.PushBack(tmpTex); + + // set display positions : + tmpCoord.x = posDrawX; + tmpCoord.y = drawPosition.y; + coord.PushBack(tmpCoord); + tmpCoord.x = posDrawX + sizeWidth; + tmpCoord.y = drawPosition.y + size; + coord.PushBack(tmpCoord); + tmpCoord.x = posDrawX; + coord.PushBack(tmpCoord); + #endif } posDrawX += sizeWidth; } diff --git a/Sources/ewol/OObject/2DText.cpp b/Sources/ewol/OObject/2DText.cpp index 7a92298e..55ea78a8 100644 --- a/Sources/ewol/OObject/2DText.cpp +++ b/Sources/ewol/OObject/2DText.cpp @@ -62,8 +62,6 @@ void ewol::OObject2DText::Draw(void) //EWOL_WARNING("Nothink to draw..."); return; } - // TODO : Android does not support GL_QUADS ... - #if !defined(__PLATFORM__Android) glColor4f(m_textColorFg.red, m_textColorFg.green, m_textColorFg.blue, m_textColorFg.alpha); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, m_FontTextureId); @@ -71,12 +69,16 @@ void ewol::OObject2DText::Draw(void) glEnableClientState( GL_TEXTURE_COORD_ARRAY ); // Enable Texture Coord Arrays glVertexPointer( 2, oglTypeFloat_t, 0, &m_coord[0] ); glTexCoordPointer( 2, oglTypeFloat_t, 0, &m_coordTex[0] ); - glDrawArrays( GL_QUADS, 0, m_coord.Size()); + #if !defined(__PLATFORM__Android) + glDrawArrays( GL_QUADS, 0, m_coord.Size()); + #else + // NOTE : Android does not support the Quads elements ... + glDrawArrays( GL_TRIANGLES, 0, m_coord.Size()); + #endif //EWOL_DEBUG("request draw of " << m_coord.Size() << " elements"); glDisableClientState( GL_VERTEX_ARRAY ); // Disable Vertex Arrays glDisableClientState( GL_TEXTURE_COORD_ARRAY ); // Disable Texture Coord Arrays glDisable(GL_TEXTURE_2D); - #endif } void ewol::OObject2DText::Text(etkFloat_t x, etkFloat_t y, const char* utf8String) diff --git a/Sources/ewol/Texture.cpp b/Sources/ewol/Texture.cpp index 9c0b7941..ef3b4855 100644 --- a/Sources/ewol/Texture.cpp +++ b/Sources/ewol/Texture.cpp @@ -26,6 +26,7 @@ #include #include +#include extern "C" { @@ -314,14 +315,63 @@ class LoadedTexture int32_t m_nbTimeLoaded; int32_t m_imageSize; // must be x=y ... uint32_t m_openGlTextureID; + Bitmap* m_imageData; + bool m_loaded; }; etk::VectorType listLoadedTexture; +static bool OGLContextLoaded=false; #undef __class__ #define __class__ "ewol" +void ewol::TextureOGLContext(bool enable) +{ + if (OGLContextLoaded != enable) { + OGLContextLoaded = enable; + if (true == OGLContextLoaded) { + EWOL_WARNING("POST loading the Texture"); + for (int32_t iii=0; iii < listLoadedTexture.Size(); iii++) { + GLuint textureid; + glGenTextures(1, &textureid); + glBindTexture(GL_TEXTURE_2D, textureid); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + //--- mode nearest + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + //#ifdef __PLATFORM__X11 + //--- Mode linear + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + //#endif + EWOL_WARNING("Set in OpenGl texture =" << listLoadedTexture[iii]->m_imageData->Width() + << "px different of height=" << listLoadedTexture[iii]->m_imageData->Height() + << "px in file:" << listLoadedTexture[iii]->m_filename << " in id OGL : " << textureid); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, + listLoadedTexture[iii]->m_imageData->Width(), + listLoadedTexture[iii]->m_imageData->Height(), + 0, GL_RGBA, GL_UNSIGNED_BYTE, + listLoadedTexture[iii]->m_imageData->Data()); + listLoadedTexture[iii]->m_openGlTextureID = textureid; + listLoadedTexture[iii]->m_loaded = true; + } + ewol::ForceRedrawAll(); + } else { + for (int32_t iii=0; iii < listLoadedTexture.Size(); iii++) { + EWOL_DEBUG("unlink openGL texture ID=" << iii << " file:" << listLoadedTexture[iii]->m_filename); + glDeleteTextures(1,&listLoadedTexture[iii]->m_openGlTextureID); + listLoadedTexture[iii]->m_loaded = false; + listLoadedTexture[iii]->m_openGlTextureID = -1; + } + } + } +} + + + + int32_t ewol::LoadTexture(etk::File fileName) { if (listLoadedTexture.Size()!=0) { @@ -338,39 +388,47 @@ int32_t ewol::LoadTexture(etk::File fileName) EWOL_ERROR("File does not Exist ... " << fileName); return -1; } - Bitmap myBitmap(fileName); - myBitmap.Display(); - if (myBitmap.LoadOK() == true) { - if (myBitmap.Width()!= myBitmap.Height()) { - EWOL_ERROR("Texture can not have Width=" << myBitmap.Width() << "px different of height=" << myBitmap.Height() << "px in file:" << fileName); + Bitmap * myBitmap = new Bitmap(fileName); + myBitmap->Display(); + if (myBitmap->LoadOK() == true) { + if (myBitmap->Width()!= myBitmap->Height()) { + EWOL_ERROR("Texture can not have Width=" << myBitmap->Width() << "px different of height=" << myBitmap->Height() << "px in file:" << fileName); return -1; } - GLuint textureid; - glGenTextures(1, &textureid); - glBindTexture(GL_TEXTURE_2D, textureid); - //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - //--- mode nearest - //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - //--- Mode linear - - #ifdef __PLATFORM__X11 - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - #endif - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, myBitmap.Width(), myBitmap.Height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, myBitmap.Data()); LoadedTexture *tmpTex = new LoadedTexture(); + int32_t outTextureID = -1; + GLuint textureid = 0; if (NULL != tmpTex) { - tmpTex->m_filename = fileName; - tmpTex->m_nbTimeLoaded = 1; - tmpTex->m_imageSize = myBitmap.Width(); - tmpTex->m_openGlTextureID = textureid; + tmpTex->m_filename = fileName; + tmpTex->m_nbTimeLoaded = 1; + tmpTex->m_imageSize = myBitmap->Width(); + tmpTex->m_openGlTextureID = -1; + tmpTex->m_imageData = myBitmap; + tmpTex->m_loaded = false; + if (true == OGLContextLoaded) { + glGenTextures(1, &textureid); + glBindTexture(GL_TEXTURE_2D, textureid); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + //--- mode nearest + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + //#ifdef __PLATFORM__X11 + //--- Mode linear + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + //#endif + EWOL_WARNING("Set in OpenGl texture =" << myBitmap->Width() << "px different of height=" << myBitmap->Height() << "px in file:" << fileName << " in id OGL : " <Width(), myBitmap->Height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, myBitmap->Data()); + tmpTex->m_openGlTextureID = textureid; + tmpTex->m_loaded = true; + } listLoadedTexture.PushBack(tmpTex); + outTextureID = listLoadedTexture.Size()-1; } else { EWOL_ERROR("Allocation ERROR... "); } - return textureid; + return outTextureID; } else { return -1; } @@ -382,22 +440,30 @@ int32_t ewol::LoadTexture(etk::File fileName) void ewol::UnLoadTexture(uint32_t textureID) { - for (int32_t iii=0; iiim_openGlTextureID == textureID) { - listLoadedTexture[iii]->m_nbTimeLoaded--; - if (0 == listLoadedTexture[iii]->m_nbTimeLoaded) { - EWOL_DEBUG("Remove openGL texture ID=" << textureID << " file:" << listLoadedTexture[iii]->m_filename); - glDeleteTextures(1,&listLoadedTexture[iii]->m_openGlTextureID); - delete(listLoadedTexture[iii]); - listLoadedTexture[iii] = NULL; - listLoadedTexture.Erase(iii); + if (textureID>=0 && textureIDm_nbTimeLoaded--; + if (0 == listLoadedTexture[textureID]->m_nbTimeLoaded) { + EWOL_DEBUG("Remove openGL texture ID=" << textureID << " file:" << listLoadedTexture[textureID]->m_filename); + if (true == listLoadedTexture[textureID]->m_loaded) { + glDeleteTextures(1,&listLoadedTexture[textureID]->m_openGlTextureID); } - return; + delete(listLoadedTexture[textureID]); + listLoadedTexture[textureID] = NULL; + listLoadedTexture.Erase(textureID); } + return; } EWOL_CRITICAL("Can not find TextureId=" << (int)textureID << " in the list of texture loaded...==> to remove it ..."); } +uint32_t ewol::GetTextureGLID(uint32_t textureID) +{ + if (textureID>=0 && textureIDm_openGlTextureID; + } + return -1; +} + int32_t ewol::GetTextureSize(uint32_t textureID) { diff --git a/Sources/ewol/Texture.h b/Sources/ewol/Texture.h index 485ce427..aeb4d675 100644 --- a/Sources/ewol/Texture.h +++ b/Sources/ewol/Texture.h @@ -39,9 +39,11 @@ namespace ewol int32_t GetSize(uint32_t textureID); }; */ - int32_t LoadTexture(etk::File fileName); - void UnLoadTexture(uint32_t textureID); - int32_t GetTextureSize(uint32_t textureID); + int32_t LoadTexture(etk::File fileName); + void UnLoadTexture(uint32_t textureID); + int32_t GetTextureSize(uint32_t textureID); + uint32_t GetTextureGLID(uint32_t textureID); + void TextureOGLContext(bool enable); }; #endif diff --git a/Sources/ewol/ewol.cpp b/Sources/ewol/ewol.cpp index e399cb4d..5ba92d4e 100644 --- a/Sources/ewol/ewol.cpp +++ b/Sources/ewol/ewol.cpp @@ -111,3 +111,8 @@ bool ewol::IsPressedInput(int32_t inputID) return guiAbstraction::IsPressedInput(inputID); } +void ewol::ForceRedrawAll(void) +{ + guiAbstraction::ForceRedrawAll(); +} + diff --git a/Sources/ewol/ewol.h b/Sources/ewol/ewol.h index fc97d9de..2c1b603a 100644 --- a/Sources/ewol/ewol.h +++ b/Sources/ewol/ewol.h @@ -43,6 +43,7 @@ namespace ewol { void StartResizeSystem(void); void StartMoveSystem(void); bool IsPressedInput(int32_t inputID); + void ForceRedrawAll(void); };