1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
// -*- C++ -*-
/*
* Simple MultimEdia LiTerator(SMELT)
* by Chris Xiong 2015
* SMELT system implementation
*
* WARNING: This library is in development and interfaces would be very
* unstable.
*
*/
#include "smelt_internal.hpp"
#include <thread>
#include <chrono>
static const char* SYS_GLFW_SRCFN="smelt/glfw/sys_glfw.cpp";
int refcnt=0;
SMELT_IMPL *pSM=0;
char lasterr[1024];
void glfwErrorHandler(int err,const char* desc);
void glfwFocChangeHandler(GLFWwindow*,int foc);
void glfwCursorEnterHandler(GLFWwindow*,int ent);
void glfwKBEventHandler(GLFWwindow* window,int key,int scancode,int action,int mods);
void glfwMouseButtonHandler(GLFWwindow* w,int btn,int action,int mods);
void glfwMouseMotionHandler(GLFWwindow*,double x,double y);
void glfwMouseWheelHandler(GLFWwindow* w,double x,double y);
SMELT* smGetInterface(int apilevel)
{
if(apilevel==SMELT_APILEVEL)
return(SMELT*)SMELT_IMPL::getInterface();
else return 0;
}
SMELT_IMPL* SMELT_IMPL::getInterface()
{
if(!pSM)pSM=new SMELT_IMPL();
++refcnt;return pSM;
}
void SMELT_IMPL::smRelease()
{
--refcnt;
if(!refcnt)
{
if(pSM->hwnd)pSM->smFinale();
delete pSM;pSM=0;
}
}
bool SMELT_IMPL::smInit()
{
smLog("%s:" SLINE ": Initalizing SMELT...\n",SYS_GLFW_SRCFN);
smLog("%s:" SLINE ": SMELT api version %d\n",SYS_GLFW_SRCFN,SMELT_APILEVEL);
time_t t=time(NULL);
smLog("%s:" SLINE ": Date %s",SYS_GLFW_SRCFN,asctime(localtime(&t)));
#ifdef WIN32
OSVERSIONINFO os_ver; MEMORYSTATUS mem_st;
os_ver.dwOSVersionInfoSize=sizeof(os_ver);
GetVersionEx(&os_ver);
smLog("%s:" SLINE ": OS: Windows %ld.%ld.%ld\n", SYS_GLFW_SRCFN,os_ver.dwMajorVersion,os_ver.dwMinorVersion,os_ver.dwBuildNumber);
int CPUInfo[4]={-1};
__cpuid(CPUInfo,0x80000000);
unsigned int nExIds=CPUInfo[0];
char *cpuName,*loced;cpuName=calloc(0x40,sizeof(char));loced=cpuName;
for(unsigned int i=0x80000000;i<=nExIds;++i)
{
__cpuid(CPUInfo, i);
if(i==0x80000002)
memcpy(cpuName,CPUInfo,sizeof(CPUInfo));
else if(i==0x80000003)
memcpy(cpuName+16,CPUInfo,sizeof(CPUInfo));
else if(i==0x80000004)
memcpy(cpuName+32, CPUInfo, sizeof(CPUInfo));
}
while(*cpuName==' ')++cpuName;
smLog("%s:" SLINE ": CPU: %s\n", SYS_GLFW_SRCFN,cpuName);
free(loced);
GlobalMemoryStatus(&mem_st);
smLog("%s:" SLINE ": Memory: %ldK total, %ldK free\n", SYS_GLFW_SRCFN,mem_st.dwTotalPhys/1024L,mem_st.dwAvailPhys/1024L);
#else
system("uname -svm > /tmp/os.out");
char osv[100];FILE* a=fopen("/tmp/os.out","r");fgets(osv,100,a);fclose(a);
osv[strlen(osv)-1]='\0';
smLog("%s:" SLINE ": OS: %s\n",SYS_GLFW_SRCFN,osv);
system("rm /tmp/os.out");
system("cat /proc/cpuinfo | grep name -m 1 > /tmp/cpu.out");
a=fopen("/tmp/cpu.out","r");fgets(osv,100,a);fclose(a);
osv[strlen(osv)-1]='\0';char *ptr=osv;while(*ptr!=':')++ptr;ptr+=2;
smLog("%s:" SLINE ": CPU: %s\n",SYS_GLFW_SRCFN,osv);
system("rm /tmp/cpu.out");
a=fopen("/proc/meminfo","r");
unsigned totalm,freem;
fscanf(a,"MemTotal: %u kB\n",&totalm);
fscanf(a,"MemFree: %u kB\n",&freem);
smLog("%s:" SLINE ": RAM: %ukB installed, %ukB free\n",SYS_GLFW_SRCFN,totalm,freem);
fclose(a);
#endif
glfwSetErrorCallback(glfwErrorHandler);
if(!glfwInit())
{
smLog("%s:" SLINE ": glfwInit() failed with error %s\n",SYS_GLFW_SRCFN,lasterr);
return false;
}
GLFWmonitor *moninfo=glfwGetPrimaryMonitor();
glfwGetMonitorPhysicalSize(moninfo,&dispw,&disph);
smLog("%s:" SLINE ": Screen: %d x %d\n",SYS_GLFW_SRCFN,dispw,disph);
glfwWindowHint(GLFW_RED_BITS,8);
glfwWindowHint(GLFW_GREEN_BITS,8);
glfwWindowHint(GLFW_BLUE_BITS,8);
glfwWindowHint(GLFW_ALPHA_BITS,8);
glfwWindowHint(GLFW_DEPTH_BITS,16);
glfwWindowHint(GLFW_DOUBLEBUFFER,1);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,1);
GLFWwindow *screen=glfwCreateWindow(windowed?scrw:dispw,windowed?scrh:disph,winTitle,NULL,NULL);
hwnd=(void*)screen;
if(!hwnd)
{
smLog("%s:" SLINE ": glfwCreateWindow() failed with error %s\n",SYS_GLFW_SRCFN,lasterr);
glfwTerminate();
return false;
}
glfwMakeContextCurrent(screen);
glfwSwapInterval(vsync?1:0);
if(!windowed)
{
mouseOver=true;
if(!pSM->Active)pSM->focusChange(true);
}
if(hideMouse)glfwSetInputMode(screen,GLFW_CURSOR,GLFW_CURSOR_HIDDEN);
glfwSetWindowFocusCallback(screen,glfwFocChangeHandler);
glfwSetCursorEnterCallback(screen,glfwCursorEnterHandler);
glfwSetKeyCallback(screen,glfwKBEventHandler);
glfwSetCursorPosCallback(screen,glfwMouseMotionHandler);
glfwSetMouseButtonCallback(screen,glfwMouseButtonHandler);
glfwSetScrollCallback(screen,glfwMouseWheelHandler);
initInput();
if(!initOGL()){smFinale();return false;}
if(!initOAL()){smFinale();return false;}
t0=DWORD(glfwGetTime()*1000.);dt=fcnt=0;fps=.0;
updateFPSDelay=.0;
return true;
}
void SMELT_IMPL::smFinale()
{
smLog("%s:" SLINE ": Cleaning up...\n",SYS_GLFW_SRCFN);
clearQueue();finiOAL();finiOGL();
glfwDestroyWindow((GLFWwindow*)hwnd);glfwTerminate();hwnd=0;
}
void SMELT_IMPL::smMainLoop()
{
if(!hwnd)return smLog("%s:" SLINE ": Error: SMELT is not initialized.\n",SYS_GLFW_SRCFN);
if(!pUpdateFunc&&!updateHandler) return smLog("%s:" SLINE ": UpdateFunc is not defined.\n",SYS_GLFW_SRCFN);
Active=true;
for(;;)
{
glfwPollEvents();
bool loopcont=true;
loopcont=procGLFWEvent();
if(!loopcont)break;
if(Active||noSuspend)
{
DWORD ticks;
do{ticks=DWORD(glfwGetTime()*1000.);dt=ticks-t0;}while(dt<1);
if(dt>=fixDelta)
{
timeDelta=dt/1000.;
if(timeDelta>0.2)timeDelta=fixDelta?fixDelta/1000.:.01;
++fcnt;updateFPSDelay+=timeDelta;t0=ticks;timeS+=timeDelta;
if(updateFPSDelay>1){fps=fcnt/updateFPSDelay;updateFPSDelay=.0;fcnt=0;}
if(pUpdateFunc){if(pUpdateFunc())break;}
if(updateHandler){if(updateHandler->handlerFunc())break;}
for(int i=1;i<=255;++i)keylst[i]=((keyz[i]&4)!=0);
clearQueue();
}
else
if(fixDelta&&dt+3<fixDelta)std::this_thread::sleep_for(std::chrono::milliseconds(1));
}else std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
clearQueue();Active=false;
}
void SMELT_IMPL::smUpdateFunc(smHook func){pUpdateFunc=func;}
void SMELT_IMPL::smUpdateFunc(smHandler* h){updateHandler=h;}
void SMELT_IMPL::smUnFocFunc(smHook func){pUnFocFunc=func;}
void SMELT_IMPL::smUnFocFunc(smHandler* h){unFocHandler=h;}
void SMELT_IMPL::smFocFunc(smHook func){pFocFunc=func;}
void SMELT_IMPL::smFocFunc(smHandler* h){focHandler=h;}
void SMELT_IMPL::smQuitFunc(smHook func){pQuitFunc=func;}
void SMELT_IMPL::smQuitFunc(smHandler* h){quitHandler=h;}
void SMELT_IMPL::smWinTitle(const char *title){strcpy(winTitle,title);}
bool SMELT_IMPL::smIsActive(){return Active;}
void SMELT_IMPL::smNoSuspend(bool para){noSuspend=para;}
void SMELT_IMPL::smVidMode(int resX,int resY,bool _windowed)
{
if(vertexArray)return;
if(!pOpenGLDevice)scrw=resX,scrh=resY,windowed=_windowed;
else if(windowed!=_windowed)
{
windowed=_windowed;
glfwSetWindowSize((GLFWwindow*)hwnd,windowed?scrw:dispw,windowed?scrh:disph);
restOGL();
if(!windowed){mouseOver=true;if(!pSM->Active)pSM->focusChange(true);}
}
}
void SMELT_IMPL::smLogFile(const char* path)
{
strcpy(logFile,path);
FILE *tf=fopen(logFile,"w");
if(!tf)*logFile=0;else fclose(tf);
}
void SMELT_IMPL::smLog(const char* format,...)
{
va_list ap;
va_start(ap,format);vfprintf(stderr,format,ap);va_end(ap);
FILE *logf=fopen(logFile,"a");if(!logf)return;
va_start(ap,format);vfprintf(logf,format,ap);va_end(ap);
fclose(logf);
}
void SMELT_IMPL::smScreenShot(const char* path)
{
if(pOpenGLDevice)
{
//glFinish();
//glReadPixels(0,0,screen->w,screen->h,GL_RGB,GL_UNSIGNED_BYTE,surface->pixels);
}
}
void SMELT_IMPL::smSetFPS(int fps)
{
vsync=(fps==FPS_VSYNC);
if(pOpenGLDevice)glfwSwapInterval(vsync?1:0);
if(fps>0)fixDelta=1000./fps;else fixDelta=0;
}
float SMELT_IMPL::smGetFPS(){return fps;}
float SMELT_IMPL::smGetDelta(){return timeDelta;}
float SMELT_IMPL::smGetTime(){return timeS;}
SMELT_IMPL::SMELT_IMPL()
{
hwnd=NULL;Active=false;memset(curError,0,sizeof(curError));
pUpdateFunc=pUnFocFunc=pFocFunc=pQuitFunc=NULL;
updateHandler=unFocHandler=focHandler=quitHandler=NULL;
Icon=NULL;strcpy(winTitle,"untitled");scrw=dispw=800;scrh=disph=600;
windowed=vsync=false;memset(logFile,0,sizeof(logFile));
limfps=0;hideMouse=true;noSuspend=false;
pOpenGLDevice=NULL;targets=NULL;curTarget=NULL;
textures=NULL;vertexArray=NULL;vertexBuf=NULL;indexBuf=NULL;primTex=0;
pOpenALDevice=NULL;mute=false;inpQueue=NULL;posz=0;posx=posy=.0;
mouseOver=true;captured=false;keymods=0;zbufenabled=false;
timeDelta=updateFPSDelay=fps=.0;fcnt=dt=fixDelta=0;
}
void SMELT_IMPL::focusChange(bool actif)
{
Active=actif;
if(actif){pFocFunc?pFocFunc():0;focHandler?focHandler->handlerFunc():0;}
else {pUnFocFunc?pUnFocFunc():0;unFocHandler?unFocHandler->handlerFunc():0;};
}
bool SMELT_IMPL::procGLFWEvent()
{
if(glfwWindowShouldClose((GLFWwindow*)hwnd))
{
bool accepted=true;
if(pSM->pQuitFunc&&pSM->pQuitFunc())accepted=false;
if(pSM->quitHandler&&quitHandler->handlerFunc())accepted=false;
if(accepted)return false;
}
return true;
}
void glfwErrorHandler(int err,const char* desc)
{
strcpy(lasterr,desc);
}
void glfwFocChangeHandler(GLFWwindow*,int foc)
{
pSM->focusChange(foc);
}
void glfwCursorEnterHandler(GLFWwindow*,int ent)
{
pSM->mouseOver=ent;
}
void glfwKBEventHandler(GLFWwindow* window,int key,int scancode,int action,int mods)
{
if(action==GLFW_RELEASE)
{
pSM->keymods=mods;
pSM->buildEvent(INPUT_KEYUP,key,0,0,-1,-1);
}
else
{
pSM->keymods=mods;
if((pSM->keymods&GLFW_MOD_ALT)&&((key==GLFW_KEY_ENTER)||(key==GLFW_KEY_KP_ENTER)))
pSM->smVidMode(pSM->scrw,pSM->scrh,!pSM->windowed);
pSM->buildEvent(INPUT_KEYDOWN,key,0,0,-1,-1);
}
}
void glfwMouseButtonHandler(GLFWwindow* w,int btn,int action,int mods)
{
double x,y;glfwGetCursorPos(w,&x,&y);
if(action==GLFW_PRESS)
{
if(btn==GLFW_MOUSE_BUTTON_LEFT)
pSM->buildEvent(INPUT_MBUTTONDOWN,SMK_LBUTTON,0,0,x,y);
if(btn==GLFW_MOUSE_BUTTON_RIGHT)
pSM->buildEvent(INPUT_MBUTTONDOWN,SMK_RBUTTON,0,0,x,y);
if(btn==GLFW_MOUSE_BUTTON_MIDDLE)
pSM->buildEvent(INPUT_MBUTTONDOWN,SMK_MBUTTON,0,0,x,y);
}
else
{
if(btn==GLFW_MOUSE_BUTTON_LEFT)
pSM->buildEvent(INPUT_MBUTTONUP,SMK_LBUTTON,0,0,x,y);
if(btn==GLFW_MOUSE_BUTTON_RIGHT)
pSM->buildEvent(INPUT_MBUTTONUP,SMK_RBUTTON,0,0,x,y);
if(btn==GLFW_MOUSE_BUTTON_MIDDLE)
pSM->buildEvent(INPUT_MBUTTONUP,SMK_MBUTTON,0,0,x,y);
}
}
void glfwMouseMotionHandler(GLFWwindow*,double x,double y)
{
pSM->buildEvent(INPUT_MOUSEMOVE,0,0,0,x,y);
}
void glfwMouseWheelHandler(GLFWwindow* w,double x,double y)
{
double cx,cy;glfwGetCursorPos(w,&cx,&cy);
if(y>0)pSM->buildEvent(INPUT_MOUSEWHEEL,1,0,0,cx,cy);
if(y<0)pSM->buildEvent(INPUT_MOUSEWHEEL,-1,0,0,cx,cy);
}
|