Skip to content

Commit

Permalink
onResume android fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JeyRunner committed Jan 7, 2017
1 parent 7e2fce8 commit aaec29c
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 44 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ before_install:

install:
- sudo apt-get install build-essential
- sudo apt-get install libglew-dev

script: python build.py --android 16 --native

Expand Down
3 changes: 3 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
error = False
packing = True
androidRun = False
androidDevice = ""
androidInstall = False
clean = True
rootBuild = ""
Expand Down Expand Up @@ -56,6 +57,8 @@ def main(argv):
clean = options.clean is not None
androidInstall = options.androidInstall is not None
androidRun = options.androidRun is not None
if androidRun:
androidDevice = options.androidRun

rootSrc = os.getcwd()

Expand Down
6 changes: 3 additions & 3 deletions dependencies/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# compile dependencies
# and download first
execute_process(COMMAND git submodule update --init --recursive)


# -- SDL2 -------------------------------------------------
Expand All @@ -14,14 +12,15 @@ if (SDL2_FOUND)
set(DEPENDENCIES_SDL2_LIB SDL2 PARENT_SCOPE)
else ()
message(STATUS "DEPENDENCY = SDL2 => NOT found => will compile SDL")
execute_process(COMMAND git submodule update --init --recursive dependencies/SDL2)

if (ANDROID)
set(SDL_STATIC ON CACHE BOOL "Build the static SDL library")
set(SDL_SHARED OFF CACHE BOOL "Build the shared SDL library")
set(DEPENDENCIES_SDL2_LIB SDL2 PARENT_SCOPE)
else ()
set(SDL_STATIC OFF CACHE BOOL "Build the static SDL library")
set(SDL_SHARED ON CACHE BOOL "Build the shared SDL library")
set(SDL_SHARED ON CACHE BOOL "Build the shared SDL library")
set(DEPENDENCIES_SDL2_LIB SDL2-2.0 PARENT_SCOPE)
endif ()
set(PTHREADS OFF CACHE BOOL "Pthread support")
Expand All @@ -45,6 +44,7 @@ if (NOT ANDROID)
set(DEPENDENCIES_GLEW_LIB GLEW PARENT_SCOPE)
else ()
message(STATUS "DEPENDENCY = GLEW => NOT found => will compile GLEW")
execute_process(COMMAND git submodule update --init --recursive dependencies/GLEW)

#add_subdirectory(GLEW)

Expand Down
2 changes: 1 addition & 1 deletion platform/android/AndroidManifest.in.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<application android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:allowBackup="true"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:theme="@android:style/Theme.NoTitleBar"
android:hardwareAccelerated="true">
<activity android:name="main"
android:label="${ANDROID_NAME}">
Expand Down
10 changes: 5 additions & 5 deletions source/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ set(SRC)


# files
file(GLOB SRC_FILES
${SOURCE_DIR}*.cpp
${SOURCE_DIR}*.c
${INCLUDE_DIR}*.h)

#file(GLOB SRC_FILES
# ${SOURCE_DIR}*.cpp
# ${SOURCE_DIR}*.c
# ${INCLUDE_DIR}*.h)
set(SRC_FILES source/main.cpp source/SDL_android_main.c)

set(ANDROID_PACKAGE_NAME "com.blueprint.cmake")

Expand Down
92 changes: 57 additions & 35 deletions source/app/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ GLfloat fps = 0;
void renderLoop();
void renderScene();
void createShader();
void setViewport(float w, float h);
void checkShaderError(GLuint shader, bool isProgram, GLint flag, string message);


Expand Down Expand Up @@ -109,6 +110,8 @@ bool createWindow()

// create context
context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, context);
log("context: " + to_string(context));


SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
Expand Down Expand Up @@ -142,11 +145,22 @@ int main(int argc, char *argv[])

// start render loop
renderLoop();


// Delete our OpengL context
SDL_GL_DeleteContext(context);

// Destroy our window
SDL_DestroyWindow(window);

// Shutdown SDL 2
SDL_Quit();
}


double starttime = 0;
double endtime = 0;
double timeTemp = 0;
float timeGone = 0;
int frame = 0;
// transform matrix
Expand All @@ -156,14 +170,7 @@ glm::mat4 transformMatrix;
void renderLoop()
{
// resize ortho
transformMatrix = glm::ortho(-(float) windowWidth / 2 /* left */,
(float) windowWidth / 2 /* right */,
-(float) windowHeight / 2 /* bottom */,
(float) windowHeight / 2 /* top */,

1.0f /* zNear */,
-1.0f /* zFar */);
glViewport(0, 0, windowWidth, windowHeight);
setViewport(windowWidth, windowHeight);


// sdl event
Expand All @@ -190,7 +197,7 @@ void renderLoop()

bool loop = true;
bool render = true;

float i =0;
// render loop
while (loop)
{
Expand All @@ -203,9 +210,7 @@ void renderLoop()
{
case SDL_QUIT:
log("[END ] close program ");
SDL_DestroyWindow(window);
SDL_Quit();// close SDL
exit(0); // close program
loop = false;
break;

// event from window
Expand All @@ -215,23 +220,8 @@ void renderLoop()
case SDL_WINDOWEVENT_RESIZED:
int w = event.window.data1;
int h = event.window.data2;
//SDL_GetWindowSize(window, &w, &h);
// out
//cout << "[INFO] window height: " << h << " width: " << w << endl;
//cout << "[INEV] window height: " << << " width: " << w << endl;
// reset screen
//SDL_SetVideoMode(w, h, 32, SDL_OPENGL | SDL_RESIZABLE | SDL_DOUBLEBUF);
//SDL_SetWindowSize(window, w, h);
glViewport(0, 0, w, h);

// resize ortho
transformMatrix = glm::ortho(-(float) w / 2 /* left */,
(float) w / 2 /* right */,
-(float) h / 2 /* bottom */,
(float) h / 2 /* top */,

1.0f /* zNear */,
-1.0f /* zFar */);
setViewport(w, h);
break;
}
break;
Expand All @@ -242,9 +232,14 @@ void renderLoop()
break;


case SDL_APP_WILLENTERFOREGROUND:
case SDL_APP_DIDENTERFOREGROUND:
render = true;
log("SDL_APP_WILLENTERFOREGROUND");
context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, context);
log("SDL_APP_DIDENTERFOREGROUND");
log("glcontext make current: " + to_string(context));
//setViewport(windowWidth, windowHeight);
createShader();
break;


Expand All @@ -261,19 +256,22 @@ void renderLoop()
}




if (render)
{
// render
log("render");
//log("render");
renderScene();
sleep(1);
//sleep(1);
}
}
}

/* -- RENDER ------------------------------------ */
void renderScene()
{
starttime = clock();

// vertices
GLfloat vertices[12] = {
Expand Down Expand Up @@ -425,13 +423,13 @@ void renderScene()
// calc fps
endtime = clock();
double timeDiff = (endtime - starttime) / (double) CLOCKS_PER_SEC;
double fpsTemp = 1 / timeDiff;
timeTemp += timeDiff;
starttime = endtime;


if (frame >= 25)
if (frame >= 60)
{
fps = (fpsTemp);
fps = 1 / (timeTemp / frame);

// set window titel
// ostringstream stream;
Expand All @@ -440,11 +438,35 @@ void renderScene()
// SDL_WM_SetCaption(streamO, "");
log("FPS: " + to_string(fps));

timeTemp = 0;
frame = 0;
}
}


void setViewport(float w, float h)
{
log("set glViewport: height: " + to_string(h) + ", width: " + to_string(w));
//SDL_GetWindowSize(window, &w, &h);
// out
//cout << "[INFO] window height: " << h << " width: " << w << endl;
//cout << "[INEV] window height: " << << " width: " << w << endl;
// reset screen
//SDL_SetVideoMode(w, h, 32, SDL_OPENGL | SDL_RESIZABLE | SDL_DOUBLEBUF);
//SDL_SetWindowSize(window, w, h);
glViewport(0, 0, w, h);

// resize ortho
transformMatrix = glm::ortho(-(float) w / 2 /* left */,
(float) w / 2 /* right */,
-(float) h / 2 /* bottom */,
(float) h / 2 /* top */,

1.0f /* zNear */,
-1.0f /* zFar */);
}


/* -- CREATE SHADER ----------------------------- */
void createShader()
{
Expand Down

0 comments on commit aaec29c

Please sign in to comment.