Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
156 lines (122 loc) · 4.73 KB

File metadata and controls

156 lines (122 loc) · 4.73 KB
 
Jun 17, 2010
Jun 17, 2010
1
2
3
4
5
6
7
8
9
/*******************************************************************************
Headers
*******************************************************************************/
#include <jni.h>
#include <sys/time.h>
#include <time.h>
#include <android/log.h>
#include <stdint.h>
Jun 17, 2010
Jun 17, 2010
10
11
12
13
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
Jun 17, 2010
Jun 17, 2010
14
15
#include <pthread.h>
Jun 17, 2010
Jun 17, 2010
16
#include "importgl.h"
Jun 17, 2010
Jun 17, 2010
17
#include "egl.h"
Jun 17, 2010
Jun 17, 2010
18
Jun 17, 2010
Jun 17, 2010
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*******************************************************************************
Globals
*******************************************************************************/
int gAppAlive = 1;
static int sWindowWidth = 320;
static int sWindowHeight = 480;
static int sDemoStopped = 0;
static long _getTime(void){
struct timeval now;
gettimeofday(&now, NULL);
return (long)(now.tv_sec*1000 + now.tv_usec/1000);
}
Jun 17, 2010
Jun 17, 2010
34
Jun 17, 2010
Jun 17, 2010
35
36
37
38
39
40
41
42
43
44
45
/*******************************************************************************
Things used by libsdl
*******************************************************************************/
pthread_mutex_t mSDLRenderMutex;
pthread_cond_t mSDLRenderCondition;
EGLContext mContext;
EGLDisplay mDisplay;
EGLSurface mRead;
EGLSurface mDraw;
Jun 17, 2010
Jun 17, 2010
46
47
48
49
/*******************************************************************************
SDL thread
*******************************************************************************/
pthread_t mSDLThread = 0;
Jun 17, 2010
Jun 17, 2010
50
Jun 17, 2010
Jun 17, 2010
51
52
void* sdlThreadProc(void* args){
__android_log_print(ANDROID_LOG_INFO, "SDL", "Thread Entry");
Jun 17, 2010
Jun 17, 2010
53
54
55
56
57
58
59
if(!eglMakeCurrent(mDisplay, mDraw, mRead, mContext)){
__android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't make current: 0x%x", eglGetError());
return NULL;
}
return (void *)SDL_main();
Jun 17, 2010
Jun 17, 2010
60
61
}
Jun 17, 2010
Jun 17, 2010
62
63
64
65
66
67
68
69
70
/*******************************************************************************
Initialize the graphics state
*******************************************************************************/
void Java_org_libsdl_android_TestRenderer_nativeInit( JNIEnv* env )
{
importGLInit();
gAppAlive = 1;
sDemoStopped = 0;
Jun 17, 2010
Jun 17, 2010
71
72
73
__android_log_print(ANDROID_LOG_INFO, "SDL", "Entry point");
Jun 17, 2010
Jun 17, 2010
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
pthread_mutex_init(&mSDLRenderMutex, NULL);
pthread_cond_init (&mSDLRenderCondition, NULL);
//Get some egl stuff we need
mContext = eglGetCurrentContext();
mDisplay = eglGetCurrentDisplay();
mRead = eglGetCurrentSurface(EGL_READ);
mDraw = eglGetCurrentSurface(EGL_DRAW);
//We need to abandon our context so SDL can have it
if(!eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)){
__android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't abandon context: 0x%x", eglGetError());
return NULL;
}
Jun 17, 2010
Jun 17, 2010
89
90
//Spin up the SDL thread
int r = pthread_create(&mSDLThread, NULL, sdlThreadProc, NULL);
Jun 17, 2010
Jun 17, 2010
91
Jun 17, 2010
Jun 17, 2010
92
93
94
95
96
if(r != 0){
__android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't spawn thread: %d", r);
}else{
__android_log_print(ANDROID_LOG_INFO, "SDL", "Started SDL thread");
}
Jun 17, 2010
Jun 17, 2010
97
Jun 17, 2010
Jun 17, 2010
98
99
100
101
102
103
104
105
106
107
108
109
110
}
/*******************************************************************************
Resize
*******************************************************************************/
void Java_org_libsdl_android_TestRenderer_nativeResize( JNIEnv* env,
jobject thiz,
jint w,
jint h )
{
sWindowWidth = w;
sWindowHeight = h;
__android_log_print(ANDROID_LOG_INFO, "SDL", "resize w=%d h=%d", w, h);
Jun 17, 2010
Jun 17, 2010
111
Jun 17, 2010
Jun 17, 2010
112
113
114
115
116
117
118
119
120
121
122
}
/*******************************************************************************
Finalize (ie: shutdown)
*******************************************************************************/
void Java_org_libsdl_android_TestRenderer_nativeDone( JNIEnv* env )
{
//shut down the app
importGLDeinit();
Jun 17, 2010
Jun 17, 2010
123
124
__android_log_print(ANDROID_LOG_INFO, "SDL", "Finalize");
Jun 17, 2010
Jun 17, 2010
125
126
127
128
129
130
131
132
133
134
}
/*******************************************************************************
Pause (ie: stop as soon as possible)
*******************************************************************************/
void Java_org_libsdl_android_TestGLSurfaceView_nativePause( JNIEnv* env )
{
sDemoStopped = !sDemoStopped;
if (sDemoStopped) {
//we paused
Jun 17, 2010
Jun 17, 2010
135
__android_log_print(ANDROID_LOG_INFO, "SDL", "Pause");
Jun 17, 2010
Jun 17, 2010
136
137
} else {
//we resumed
Jun 17, 2010
Jun 17, 2010
138
__android_log_print(ANDROID_LOG_INFO, "SDL", "Resume");
Jun 17, 2010
Jun 17, 2010
139
140
141
142
143
144
}
}
/*******************************************************************************
Render the next frame
*******************************************************************************/
Jun 17, 2010
Jun 17, 2010
145
Jun 17, 2010
Jun 17, 2010
146
147
148
void Java_org_libsdl_android_TestRenderer_nativeRender( JNIEnv* env )
{
//TODO: Render here
Jun 17, 2010
Jun 17, 2010
149
Jun 17, 2010
Jun 17, 2010
150
151
152
153
154
155
pthread_mutex_lock(&mSDLRenderMutex);
pthread_cond_signal(&mSDLRenderCondition); //wake up the SDL thread
pthread_mutex_unlock(&mSDLRenderMutex);
//__android_log_print(ANDROID_LOG_INFO, "SDL", "Unlocked");
Jun 17, 2010
Jun 17, 2010
156
}