| 1 |
From: James Cowgill <[email protected]> |
| 2 |
Date: Wed, 25 Apr 2018 23:08:26 +0200 |
| 3 |
Subject: Port to GLFW3 |
| 4 |
|
| 5 |
This is a very basic port of the dokidoki-support sub-module to GLFW3. I've |
| 6 |
left a number of functions which are not used by pax-britannica unimplemented |
| 7 |
which simplifies the patch. The new versions of some of these functions do not |
| 8 |
match with the current API. The correct way to do this would probably be a full |
| 9 |
rewrite of luaglfw.c with a new API, but on the basis that pax-britannica seems |
| 10 |
to be dead upstream, I think this is acceptable for the time being. |
| 11 |
--- |
| 12 |
dokidoki-support/luaglfw.c | 426 ++++++++++++++++++++++++++------------------- |
| 13 |
dokidoki-support/mixer.c | 31 ++-- |
| 14 |
2 files changed, 261 insertions(+), 196 deletions(-) |
| 15 |
|
| 16 |
diff --git a/dokidoki-support/luaglfw.c b/dokidoki-support/luaglfw.c |
| 17 |
index 8410e06..5192b6e 100644 |
| 18 |
--- a/dokidoki-support/luaglfw.c |
| 19 |
+++ b/dokidoki-support/luaglfw.c |
| 20 |
@@ -29,9 +29,12 @@ |
| 21 |
//======================================================================== |
| 22 |
|
| 23 |
#include <lauxlib.h> |
| 24 |
-#include <GL/glfw.h> |
| 25 |
+#include <GLFW/glfw3.h> |
| 26 |
+#include <errno.h> |
| 27 |
+#include <time.h> |
| 28 |
#include "luaglfw.h" |
| 29 |
|
| 30 |
+static GLFWwindow* window; |
| 31 |
|
| 32 |
//************************************************************************ |
| 33 |
//**** Internal type definitions **** |
| 34 |
@@ -102,19 +105,19 @@ static void pushVideoMode( lua_State *L, const GLFWvidmode *mode) |
| 35 |
{ |
| 36 |
lua_newtable( L ); |
| 37 |
lua_pushstring( L, "Width" ); |
| 38 |
- lua_pushnumber( L, mode->Width ); |
| 39 |
+ lua_pushnumber( L, mode->width ); |
| 40 |
lua_rawset( L, -3 ); |
| 41 |
lua_pushstring( L, "Height" ); |
| 42 |
- lua_pushnumber( L, mode->Height ); |
| 43 |
+ lua_pushnumber( L, mode->height ); |
| 44 |
lua_rawset( L, -3 ); |
| 45 |
lua_pushstring( L, "RedBits" ); |
| 46 |
- lua_pushnumber( L, mode->RedBits ); |
| 47 |
+ lua_pushnumber( L, mode->redBits ); |
| 48 |
lua_rawset( L, -3 ); |
| 49 |
lua_pushstring( L, "GreenBits" ); |
| 50 |
- lua_pushnumber( L, mode->GreenBits ); |
| 51 |
+ lua_pushnumber( L, mode->greenBits ); |
| 52 |
lua_rawset( L, -3 ); |
| 53 |
lua_pushstring( L, "BlueBits" ); |
| 54 |
- lua_pushnumber( L, mode->BlueBits ); |
| 55 |
+ lua_pushnumber( L, mode->blueBits ); |
| 56 |
lua_rawset( L, -3 ); |
| 57 |
} |
| 58 |
|
| 59 |
@@ -130,12 +133,11 @@ static const char * windowclose_name; |
| 60 |
static const char * windowrefresh_name; |
| 61 |
static const char * mousebutton_name; |
| 62 |
static const char * mousepos_name; |
| 63 |
-static const char * mousewheel_name; |
| 64 |
static const char * key_name; |
| 65 |
static const char * char_name; |
| 66 |
|
| 67 |
|
| 68 |
-void GLFWCALL luaglfw_windowsizefun( int w, int h ) |
| 69 |
+void luaglfw_windowsizefun( GLFWwindow* window_dummy, int w, int h ) |
| 70 |
{ |
| 71 |
lua_State *L = callback_lua_state; |
| 72 |
if( L == NULL ) return; |
| 73 |
@@ -149,23 +151,26 @@ void GLFWCALL luaglfw_windowsizefun( int w, int h ) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
-int GLFWCALL luaglfw_windowclosefun( void ) |
| 78 |
+void luaglfw_windowclosefun( GLFWwindow* window_dummy ) |
| 79 |
{ |
| 80 |
lua_State *L = callback_lua_state; |
| 81 |
int do_close = 1; |
| 82 |
- if( L == NULL ) return 1; |
| 83 |
|
| 84 |
- lua_getglobal( L, windowclose_name ); |
| 85 |
- if( lua_isfunction( L, -1 ) ) |
| 86 |
+ if( L != NULL ) |
| 87 |
{ |
| 88 |
- lua_pcall( L, 0, 1, 0 ); |
| 89 |
- do_close = (int) lua_tonumber( L, -1 ); |
| 90 |
- lua_pop( L, 1 ); |
| 91 |
+ lua_getglobal( L, windowclose_name ); |
| 92 |
+ if( lua_isfunction( L, -1 ) ) |
| 93 |
+ { |
| 94 |
+ lua_pcall( L, 0, 1, 0 ); |
| 95 |
+ do_close = (int) lua_tonumber( L, -1 ); |
| 96 |
+ lua_pop( L, 1 ); |
| 97 |
+ } |
| 98 |
} |
| 99 |
- return do_close; |
| 100 |
+ |
| 101 |
+ glfwSetWindowShouldClose(window, do_close); |
| 102 |
} |
| 103 |
|
| 104 |
-void GLFWCALL luaglfw_windowrefreshfun( void ) |
| 105 |
+void luaglfw_windowrefreshfun( GLFWwindow* window_dummy ) |
| 106 |
{ |
| 107 |
lua_State *L = callback_lua_state; |
| 108 |
if( L == NULL ) return; |
| 109 |
@@ -177,7 +182,7 @@ void GLFWCALL luaglfw_windowrefreshfun( void ) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
-void GLFWCALL luaglfw_mousebuttonfun( int button, int action ) |
| 114 |
+void luaglfw_mousebuttonfun( GLFWwindow* window_dummy, int button, int action, int mods ) |
| 115 |
{ |
| 116 |
lua_State *L = callback_lua_state; |
| 117 |
if( L == NULL ) return; |
| 118 |
@@ -191,7 +196,7 @@ void GLFWCALL luaglfw_mousebuttonfun( int button, int action ) |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
-void GLFWCALL luaglfw_mouseposfun( int x, int y ) |
| 123 |
+void luaglfw_mouseposfun( GLFWwindow* window_dummy, double x, double y ) |
| 124 |
{ |
| 125 |
lua_State *L = callback_lua_state; |
| 126 |
if( L == NULL ) return; |
| 127 |
@@ -199,29 +204,19 @@ void GLFWCALL luaglfw_mouseposfun( int x, int y ) |
| 128 |
lua_getglobal( L, mousepos_name ); |
| 129 |
if( lua_isfunction( L, -1 ) ) |
| 130 |
{ |
| 131 |
- lua_pushnumber( L, (lua_Number)x ); |
| 132 |
- lua_pushnumber( L, (lua_Number)y ); |
| 133 |
+ lua_pushnumber( L, (lua_Number)(int)x ); |
| 134 |
+ lua_pushnumber( L, (lua_Number)(int)y ); |
| 135 |
lua_pcall( L, 2, 0, 0 ); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
-void GLFWCALL luaglfw_mousewheelfun( int wheelpos ) |
| 140 |
+void luaglfw_keyfun( GLFWwindow* window_dummy, int key, int scancode, int action, int mods ) |
| 141 |
{ |
| 142 |
lua_State *L = callback_lua_state; |
| 143 |
if( L == NULL ) return; |
| 144 |
|
| 145 |
- lua_getglobal( L, mousewheel_name ); |
| 146 |
- if( lua_isfunction( L, -1 ) ) |
| 147 |
- { |
| 148 |
- lua_pushnumber( L, (lua_Number)wheelpos ); |
| 149 |
- lua_pcall( L, 1, 0, 0 ); |
| 150 |
- } |
| 151 |
-} |
| 152 |
- |
| 153 |
-void GLFWCALL luaglfw_keyfun( int key, int action ) |
| 154 |
-{ |
| 155 |
- lua_State *L = callback_lua_state; |
| 156 |
- if( L == NULL ) return; |
| 157 |
+ // Filter out GLFW_REPEAT actions |
| 158 |
+ if (action == GLFW_REPEAT) return; |
| 159 |
|
| 160 |
lua_getglobal( L, key_name ); |
| 161 |
if( lua_isfunction( L, -1 ) ) |
| 162 |
@@ -232,7 +227,7 @@ void GLFWCALL luaglfw_keyfun( int key, int action ) |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
-void GLFWCALL luaglfw_charfun( int key, int action ) |
| 167 |
+void luaglfw_charfun( GLFWwindow* window_dummy, unsigned int key ) |
| 168 |
{ |
| 169 |
lua_State *L = callback_lua_state; |
| 170 |
if( L == NULL ) return; |
| 171 |
@@ -241,7 +236,7 @@ void GLFWCALL luaglfw_charfun( int key, int action ) |
| 172 |
if( lua_isfunction( L, -1 ) ) |
| 173 |
{ |
| 174 |
lua_pushnumber( L, (lua_Number)key ); |
| 175 |
- lua_pushnumber( L, (lua_Number)action ); |
| 176 |
+ lua_pushnumber( L, (lua_Number)GLFW_PRESS ); |
| 177 |
lua_pcall( L, 2, 0, 0 ); |
| 178 |
} |
| 179 |
} |
| 180 |
@@ -269,6 +264,7 @@ static int glfw_Init( lua_State *L ) |
| 181 |
static int glfw_Terminate( lua_State *L ) |
| 182 |
{ |
| 183 |
glfwTerminate(); |
| 184 |
+ window = NULL; |
| 185 |
return 0; |
| 186 |
} |
| 187 |
|
| 188 |
@@ -287,14 +283,20 @@ static int glfw_GetVersion( lua_State *L ) |
| 189 |
// Window handling |
| 190 |
//======================================================================== |
| 191 |
|
| 192 |
+#define GLFW2_WINDOW 0x00010001 |
| 193 |
+#define GLFW2_FULLSCREEN 0x00010002 |
| 194 |
+ |
| 195 |
static int glfw_OpenWindow( lua_State *L ) |
| 196 |
{ |
| 197 |
lua_Number w, h, r, g, b, a, depth, stencil, mode; |
| 198 |
- int argc, res; |
| 199 |
+ int res; |
| 200 |
|
| 201 |
// Check arguments |
| 202 |
if( badArgs( L, 9, "OpenWindow" ) ) return 0; |
| 203 |
|
| 204 |
+ // Disallow if a window is already open |
| 205 |
+ if (window != NULL) return 0; |
| 206 |
+ |
| 207 |
// Get all arguments to glfwOpenWindow |
| 208 |
w = lua_tonumber( L, 1 ); |
| 209 |
h = lua_tonumber( L, 2 ); |
| 210 |
@@ -306,11 +308,21 @@ static int glfw_OpenWindow( lua_State *L ) |
| 211 |
stencil = lua_tonumber( L, 8 ); |
| 212 |
mode = lua_tonumber( L, 9 ); |
| 213 |
|
| 214 |
- // Call glfwOpenWindow |
| 215 |
+ // Call glfwCreateWindow |
| 216 |
lua_settop( L,0 ); |
| 217 |
- res = glfwOpenWindow( (int)w, (int)h, (int)r, (int)g, (int)b, |
| 218 |
- (int)a, (int)depth, (int)stencil, |
| 219 |
- (int)mode ); |
| 220 |
+ glfwWindowHint(GLFW_RED_BITS, (int)r); |
| 221 |
+ glfwWindowHint(GLFW_GREEN_BITS, (int)g); |
| 222 |
+ glfwWindowHint(GLFW_BLUE_BITS, (int)b); |
| 223 |
+ glfwWindowHint(GLFW_ALPHA_BITS, (int)a); |
| 224 |
+ glfwWindowHint(GLFW_DEPTH_BITS, (int)depth); |
| 225 |
+ glfwWindowHint(GLFW_STENCIL_BITS, (int)stencil); |
| 226 |
+ |
| 227 |
+ GLFWmonitor* monitor = (mode == GLFW2_FULLSCREEN) ? glfwGetPrimaryMonitor() : NULL; |
| 228 |
+ window = glfwCreateWindow((int) w, (int)h, "Pax Britannica", monitor, NULL); |
| 229 |
+ res = (window == NULL) ? GL_FALSE : GL_TRUE; |
| 230 |
+ |
| 231 |
+ if (window != NULL) |
| 232 |
+ glfwMakeContextCurrent(window); |
| 233 |
|
| 234 |
// Return result |
| 235 |
lua_pushnumber( L, (lua_Number)res ); |
| 236 |
@@ -324,13 +336,18 @@ static int glfw_OpenWindowHint( lua_State *L ) |
| 237 |
target = lua_tonumber( L, 1 ); |
| 238 |
hint = lua_tonumber( L, 2 ); |
| 239 |
lua_settop( L, 0 ); |
| 240 |
- glfwOpenWindowHint( (int)target, (int)hint ); |
| 241 |
+ glfwWindowHint( (int)target, (int)hint ); |
| 242 |
return 0; |
| 243 |
} |
| 244 |
|
| 245 |
static int glfw_CloseWindow( lua_State *L ) |
| 246 |
{ |
| 247 |
- glfwCloseWindow(); |
| 248 |
+ if (window != NULL) |
| 249 |
+ { |
| 250 |
+ glfwDestroyWindow(window); |
| 251 |
+ window = NULL; |
| 252 |
+ } |
| 253 |
+ |
| 254 |
return 0; |
| 255 |
} |
| 256 |
|
| 257 |
@@ -338,9 +355,13 @@ static int glfw_SetWindowTitle( lua_State *L ) |
| 258 |
{ |
| 259 |
const char *str; |
| 260 |
if( badArgs( L, 1, "SetWindowTitle" ) ) return 0; |
| 261 |
+ |
| 262 |
+ // Disallow if a window is not open |
| 263 |
+ if (window == NULL) return 0; |
| 264 |
+ |
| 265 |
str = lua_tostring( L, 1 ); |
| 266 |
lua_remove( L, 1 ); |
| 267 |
- glfwSetWindowTitle( str ); |
| 268 |
+ glfwSetWindowTitle(window, str); |
| 269 |
return 0; |
| 270 |
} |
| 271 |
|
| 272 |
@@ -348,17 +369,25 @@ static int glfw_SetWindowSize( lua_State *L ) |
| 273 |
{ |
| 274 |
lua_Number w, h; |
| 275 |
if( badArgs( L, 2, "SetWindowSize" ) ) return 0; |
| 276 |
+ |
| 277 |
+ // Disallow if a window is not open |
| 278 |
+ if (window == NULL) return 0; |
| 279 |
+ |
| 280 |
w = lua_tonumber( L, 1 ); |
| 281 |
h = lua_tonumber( L, 2 ); |
| 282 |
lua_settop( L, 0 ); |
| 283 |
- glfwSetWindowSize( (int)w, (int)h ); |
| 284 |
+ glfwSetWindowSize(window, (int)w, (int)h); |
| 285 |
return 0; |
| 286 |
} |
| 287 |
|
| 288 |
static int glfw_GetWindowSize( lua_State *L ) |
| 289 |
{ |
| 290 |
int w, h; |
| 291 |
- glfwGetWindowSize( &w, &h ); |
| 292 |
+ |
| 293 |
+ // Disallow if a window is not open |
| 294 |
+ if (window == NULL) return 0; |
| 295 |
+ |
| 296 |
+ glfwGetWindowSize(window, &w, &h); |
| 297 |
lua_settop( L, 0 ); |
| 298 |
lua_pushnumber( L, (lua_Number)w ); |
| 299 |
lua_pushnumber( L, (lua_Number)h ); |
| 300 |
@@ -369,32 +398,63 @@ static int glfw_SetWindowPos( lua_State *L ) |
| 301 |
{ |
| 302 |
lua_Number x, y; |
| 303 |
if( badArgs( L, 2, "SetWindowPos" ) ) return 0; |
| 304 |
+ |
| 305 |
+ // Disallow if a window is not open |
| 306 |
+ if (window == NULL) return 0; |
| 307 |
+ |
| 308 |
x = lua_tonumber( L, 1 ); |
| 309 |
y = lua_tonumber( L, 2 ); |
| 310 |
lua_settop( L, 0 ); |
| 311 |
- glfwSetWindowPos( (int)x, (int)y ); |
| 312 |
+ glfwSetWindowPos(window, (int)x, (int)y); |
| 313 |
return 0; |
| 314 |
} |
| 315 |
|
| 316 |
static int glfw_IconifyWindow( lua_State *L ) |
| 317 |
{ |
| 318 |
- glfwIconifyWindow(); |
| 319 |
+ // Disallow if a window is not open |
| 320 |
+ if (window == NULL) return 0; |
| 321 |
+ |
| 322 |
+ glfwIconifyWindow(window); |
| 323 |
return 0; |
| 324 |
} |
| 325 |
|
| 326 |
static int glfw_RestoreWindow( lua_State *L ) |
| 327 |
{ |
| 328 |
- glfwRestoreWindow(); |
| 329 |
+ // Disallow if a window is not open |
| 330 |
+ if (window == NULL) return 0; |
| 331 |
+ |
| 332 |
+ glfwRestoreWindow(window); |
| 333 |
return 0; |
| 334 |
} |
| 335 |
|
| 336 |
+#define GLFW2_OPENED 0x00020001 |
| 337 |
+#define GLFW2_ACCELERATED 0x00020004 |
| 338 |
+ |
| 339 |
static int glfw_GetWindowParam( lua_State *L ) |
| 340 |
{ |
| 341 |
lua_Number n; |
| 342 |
if( badArgs( L, 1, "GetWindowParam" ) ) return 0; |
| 343 |
+ |
| 344 |
+ // Disallow if a window is not open |
| 345 |
+ if (window == NULL) return 0; |
| 346 |
+ |
| 347 |
n = lua_tonumber( L, 1 ); |
| 348 |
lua_settop( L, 0 ); |
| 349 |
- n = glfwGetWindowParam( (int)n ); |
| 350 |
+ |
| 351 |
+ // Handle special cases |
| 352 |
+ switch ((int) n) |
| 353 |
+ { |
| 354 |
+ case GLFW2_OPENED: |
| 355 |
+ n = !glfwWindowShouldClose(window); |
| 356 |
+ break; |
| 357 |
+ case GLFW2_ACCELERATED: |
| 358 |
+ n = GL_TRUE; |
| 359 |
+ break; |
| 360 |
+ default: |
| 361 |
+ n = glfwGetWindowAttrib(window, (int)n); |
| 362 |
+ break; |
| 363 |
+ } |
| 364 |
+ |
| 365 |
lua_pushnumber( L, n ); |
| 366 |
return 1; |
| 367 |
} |
| 368 |
@@ -407,7 +467,11 @@ static int glfw_GetWindowParam( lua_State *L ) |
| 369 |
|
| 370 |
static int glfw_SwapBuffers( lua_State *L ) |
| 371 |
{ |
| 372 |
- glfwSwapBuffers(); |
| 373 |
+ // Disallow if a window is not open |
| 374 |
+ if (window == NULL) return 0; |
| 375 |
+ |
| 376 |
+ glfwSwapBuffers(window); |
| 377 |
+ glfwPollEvents(); |
| 378 |
return 0; |
| 379 |
} |
| 380 |
|
| 381 |
@@ -426,14 +490,10 @@ static int glfw_SwapInterval( lua_State *L ) |
| 382 |
// Video modes |
| 383 |
//======================================================================== |
| 384 |
|
| 385 |
-#define LUAGLFW_MAX_NUM_MODES 256 |
| 386 |
- |
| 387 |
static int glfw_GetVideoModes( lua_State *L ) |
| 388 |
{ |
| 389 |
- GLFWvidmode modes[ LUAGLFW_MAX_NUM_MODES ]; |
| 390 |
int modecount, i; |
| 391 |
- |
| 392 |
- modecount = glfwGetVideoModes( modes, LUAGLFW_MAX_NUM_MODES ); |
| 393 |
+ const GLFWvidmode* modes = glfwGetVideoModes(glfwGetPrimaryMonitor(), &modecount); |
| 394 |
|
| 395 |
lua_newtable( L ); |
| 396 |
for( i = 0; i < modecount; i++ ) |
| 397 |
@@ -447,10 +507,7 @@ static int glfw_GetVideoModes( lua_State *L ) |
| 398 |
|
| 399 |
static int glfw_GetDesktopMode( lua_State *L ) |
| 400 |
{ |
| 401 |
- GLFWvidmode mode; |
| 402 |
- glfwGetDesktopMode( &mode ); |
| 403 |
- |
| 404 |
- pushVideoMode( L, &mode ); |
| 405 |
+ pushVideoMode( L, glfwGetVideoMode(glfwGetPrimaryMonitor()) ); |
| 406 |
|
| 407 |
return 1; |
| 408 |
} |
| 409 |
@@ -483,6 +540,9 @@ static int glfw_GetKey( lua_State *L ) |
| 410 |
|
| 411 |
if( badArgs( L, 1, "GetKey" ) ) return 0; |
| 412 |
|
| 413 |
+ // Disallow if a window is not open |
| 414 |
+ if (window == NULL) return 0; |
| 415 |
+ |
| 416 |
switch( lua_type( L, 1 ) ) |
| 417 |
{ |
| 418 |
case LUA_TSTRING: |
| 419 |
@@ -495,7 +555,7 @@ static int glfw_GetKey( lua_State *L ) |
| 420 |
} |
| 421 |
|
| 422 |
lua_settop( L, 0 ); |
| 423 |
- n = glfwGetKey( n ); |
| 424 |
+ n = glfwGetKey(window, n); |
| 425 |
lua_pushnumber( L, n ); |
| 426 |
return 1; |
| 427 |
} |
| 428 |
@@ -504,16 +564,24 @@ static int glfw_GetMouseButton( lua_State *L ) |
| 429 |
{ |
| 430 |
lua_Number n; |
| 431 |
if( badArgs( L, 1, "GetMouseButton" ) ) return 0; |
| 432 |
+ |
| 433 |
+ // Disallow if a window is not open |
| 434 |
+ if (window == NULL) return 0; |
| 435 |
+ |
| 436 |
n = lua_tonumber( L, 1 ); |
| 437 |
lua_settop( L, 0 ); |
| 438 |
- lua_pushnumber( L, glfwGetMouseButton( (int)n ) ); |
| 439 |
+ lua_pushnumber( L, glfwGetMouseButton(window, (int)n) ); |
| 440 |
return 1; |
| 441 |
} |
| 442 |
|
| 443 |
static int glfw_GetMousePos( lua_State *L ) |
| 444 |
{ |
| 445 |
- int x, y; |
| 446 |
- glfwGetMousePos( &x, &y ); |
| 447 |
+ double x, y; |
| 448 |
+ |
| 449 |
+ // Disallow if a window is not open |
| 450 |
+ if (window == NULL) return 0; |
| 451 |
+ |
| 452 |
+ glfwGetCursorPos(window, &x, &y); |
| 453 |
lua_settop( L, 0 ); |
| 454 |
lua_pushnumber( L, (lua_Number)x ); |
| 455 |
lua_pushnumber( L, (lua_Number)y ); |
| 456 |
@@ -524,29 +592,26 @@ static int glfw_SetMousePos( lua_State *L ) |
| 457 |
{ |
| 458 |
lua_Number x, y; |
| 459 |
if( badArgs( L, 2, "SetMousePos" ) ) return 0; |
| 460 |
+ |
| 461 |
+ // Disallow if a window is not open |
| 462 |
+ if (window == NULL) return 0; |
| 463 |
+ |
| 464 |
x = lua_tonumber( L, 1 ); |
| 465 |
y = lua_tonumber( L, 2 ); |
| 466 |
lua_settop( L, 0 ); |
| 467 |
- glfwSetMousePos( (int)x, (int)y ); |
| 468 |
+ glfwSetCursorPos(window, (double)x, (double)y); |
| 469 |
return 0; |
| 470 |
} |
| 471 |
|
| 472 |
static int glfw_GetMouseWheel( lua_State *L ) |
| 473 |
{ |
| 474 |
- int pos; |
| 475 |
- pos = glfwGetMouseWheel(); |
| 476 |
- lua_settop( L, 0 ); |
| 477 |
- lua_pushnumber( L, (lua_Number)pos ); |
| 478 |
- return 1; |
| 479 |
+ unsupportedFunction(L, "GetMouseWheel"); |
| 480 |
+ return 0; |
| 481 |
} |
| 482 |
|
| 483 |
static int glfw_SetMouseWheel( lua_State *L ) |
| 484 |
{ |
| 485 |
- lua_Number pos; |
| 486 |
- if( badArgs( L, 1, "SetMouseWheel" ) ) return 0; |
| 487 |
- pos = lua_tonumber( L, 1 ); |
| 488 |
- lua_settop( L, 0 ); |
| 489 |
- glfwSetMouseWheel( (int)pos ); |
| 490 |
+ unsupportedFunction(L, "SetMouseWheel"); |
| 491 |
return 0; |
| 492 |
} |
| 493 |
|
| 494 |
@@ -555,6 +620,10 @@ static int glfw_SetMouseWheel( lua_State *L ) |
| 495 |
// Joystick input |
| 496 |
//======================================================================== |
| 497 |
|
| 498 |
+#define GLFW2_PRESENT 0x00050001 |
| 499 |
+#define GLFW2_AXES 0x00050002 |
| 500 |
+#define GLFW2_BUTTONS 0x00050003 |
| 501 |
+ |
| 502 |
static int glfw_GetJoystickParam( lua_State *L ) |
| 503 |
{ |
| 504 |
lua_Number joy, param; |
| 505 |
@@ -563,17 +632,29 @@ static int glfw_GetJoystickParam( lua_State *L ) |
| 506 |
joy = lua_tonumber( L, 1 ); |
| 507 |
param = lua_tonumber( L, 2 ); |
| 508 |
lua_settop( L, 0 ); |
| 509 |
- res = glfwGetJoystickParam( (int)joy, (int)param ); |
| 510 |
+ |
| 511 |
+ switch((int) param) |
| 512 |
+ { |
| 513 |
+ case GLFW2_PRESENT: |
| 514 |
+ res = glfwJoystickPresent(joy); |
| 515 |
+ break; |
| 516 |
+ case GLFW2_AXES: |
| 517 |
+ glfwGetJoystickAxes(joy, &res); |
| 518 |
+ break; |
| 519 |
+ case GLFW2_BUTTONS: |
| 520 |
+ glfwGetJoystickButtons(joy, &res); |
| 521 |
+ break; |
| 522 |
+ default: |
| 523 |
+ res = 0; |
| 524 |
+ } |
| 525 |
+ |
| 526 |
lua_pushnumber( L, (lua_Number)res ); |
| 527 |
return 1; |
| 528 |
} |
| 529 |
|
| 530 |
-#define LUAGLFW_MAX_JOY_AXES 256 |
| 531 |
- |
| 532 |
static int glfw_GetJoystickPos( lua_State *L ) |
| 533 |
{ |
| 534 |
int joy, numaxes, res, i; |
| 535 |
- float pos[ LUAGLFW_MAX_JOY_AXES ]; |
| 536 |
|
| 537 |
// Get arguments |
| 538 |
if( badArgs( L, 2, "GetJoystickPos" ) ) return 0; |
| 539 |
@@ -581,10 +662,10 @@ static int glfw_GetJoystickPos( lua_State *L ) |
| 540 |
numaxes = (int)lua_tonumber( L, 2 ); |
| 541 |
lua_settop( L, 0 ); |
| 542 |
if( numaxes < 1 ) return 0; |
| 543 |
- if( numaxes > LUAGLFW_MAX_JOY_AXES ) numaxes = LUAGLFW_MAX_JOY_AXES; |
| 544 |
|
| 545 |
// Call GLFW funciton |
| 546 |
- res = glfwGetJoystickPos( joy, pos, numaxes ); |
| 547 |
+ const float* pos = glfwGetJoystickAxes(joy, &res); |
| 548 |
+ if (res > numaxes) res = numaxes; |
| 549 |
|
| 550 |
// Create result array |
| 551 |
lua_newtable( L ); |
| 552 |
@@ -598,12 +679,9 @@ static int glfw_GetJoystickPos( lua_State *L ) |
| 553 |
return 1; |
| 554 |
} |
| 555 |
|
| 556 |
-#define LUAGLFW_MAX_JOY_BUTTONS 256 |
| 557 |
- |
| 558 |
static int glfw_GetJoystickButtons( lua_State *L ) |
| 559 |
{ |
| 560 |
int joy, numbuttons, res, i; |
| 561 |
- unsigned char buttons[ LUAGLFW_MAX_JOY_AXES ]; |
| 562 |
|
| 563 |
// Get arguments |
| 564 |
if( badArgs( L, 2, "GetJoystickButtons" ) ) return 0; |
| 565 |
@@ -611,11 +689,10 @@ static int glfw_GetJoystickButtons( lua_State *L ) |
| 566 |
numbuttons = (int)lua_tonumber( L, 2 ); |
| 567 |
lua_settop( L, 0 ); |
| 568 |
if( numbuttons < 1 ) return 0; |
| 569 |
- if( numbuttons > LUAGLFW_MAX_JOY_BUTTONS ) |
| 570 |
- numbuttons = LUAGLFW_MAX_JOY_BUTTONS; |
| 571 |
|
| 572 |
// Call GLFW funciton |
| 573 |
- res = glfwGetJoystickButtons( joy, buttons, numbuttons ); |
| 574 |
+ const unsigned char* buttons = glfwGetJoystickButtons(joy, &res); |
| 575 |
+ if (res > numbuttons) res = numbuttons; |
| 576 |
|
| 577 |
// Create result array |
| 578 |
lua_newtable( L ); |
| 579 |
@@ -653,13 +730,38 @@ static int glfw_SetTime( lua_State *L ) |
| 580 |
return 0; |
| 581 |
} |
| 582 |
|
| 583 |
+#define ONE_BILLION 1000000000 |
| 584 |
+ |
| 585 |
static int glfw_Sleep( lua_State *L ) |
| 586 |
{ |
| 587 |
- lua_Number t; |
| 588 |
+ // This function is no longer implemented by glfw, but is required for |
| 589 |
+ // pax-britannica, so replace it with a nanosleep implementation. |
| 590 |
+ double t; |
| 591 |
if ( badArgs( L, 1, "Sleep" ) ) return 0; |
| 592 |
- t = lua_tonumber( L, 1 ); |
| 593 |
+ t = (double) lua_tonumber( L, 1 ); |
| 594 |
lua_settop( L, 0 ); |
| 595 |
- glfwSleep( (double)t ); |
| 596 |
+ |
| 597 |
+ // Cap negative sleep durations |
| 598 |
+ if (t < 0) t = 0; |
| 599 |
+ |
| 600 |
+ // Perform the sleep |
| 601 |
+ struct timespec req; |
| 602 |
+ req.tv_sec = (time_t) t; |
| 603 |
+ req.tv_nsec = (long) ((t - (time_t) t) * ONE_BILLION); |
| 604 |
+ |
| 605 |
+ if (req.tv_nsec >= ONE_BILLION) |
| 606 |
+ { |
| 607 |
+ req.tv_sec++; |
| 608 |
+ req.tv_nsec = 0; |
| 609 |
+ } |
| 610 |
+ else if (req.tv_nsec < 0) |
| 611 |
+ { |
| 612 |
+ req.tv_nsec = 0; |
| 613 |
+ } |
| 614 |
+ |
| 615 |
+ while (nanosleep(&req, &req) == -1 && errno == EINTR) |
| 616 |
+ ; |
| 617 |
+ |
| 618 |
return 0; |
| 619 |
} |
| 620 |
|
| 621 |
@@ -748,11 +850,8 @@ static int glfw_BroadcastCond( lua_State *L ) |
| 622 |
|
| 623 |
static int glfw_GetNumberOfProcessors( lua_State *L ) |
| 624 |
{ |
| 625 |
- int n; |
| 626 |
- n = glfwGetNumberOfProcessors(); |
| 627 |
- lua_settop( L,0 ); |
| 628 |
- lua_pushnumber( L, (lua_Number)n ); |
| 629 |
- return 1; |
| 630 |
+ unsupportedFunction( L, "GetNumberOfProcessors" ); |
| 631 |
+ return 0; |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
@@ -762,13 +861,9 @@ static int glfw_GetNumberOfProcessors( lua_State *L ) |
| 636 |
|
| 637 |
static int glfw_GetGLVersion( lua_State *L ) |
| 638 |
{ |
| 639 |
- int major, minor, rev; |
| 640 |
- glfwGetGLVersion( &major, &minor, &rev ); |
| 641 |
- lua_settop( L,0 ); |
| 642 |
- lua_pushnumber( L, (lua_Number)major ); |
| 643 |
- lua_pushnumber( L, (lua_Number)minor ); |
| 644 |
- lua_pushnumber( L, (lua_Number)rev ); |
| 645 |
- return 3; |
| 646 |
+ // GLFW3 requires an open window now, so most uses of this will fail anyway |
| 647 |
+ unsupportedFunction( L, "GetGLVersion" ); |
| 648 |
+ return 0; |
| 649 |
} |
| 650 |
|
| 651 |
static int glfw_ExtensionSupported( lua_State *L ) |
| 652 |
@@ -790,21 +885,13 @@ static int glfw_ExtensionSupported( lua_State *L ) |
| 653 |
|
| 654 |
static int glfw_Enable( lua_State *L ) |
| 655 |
{ |
| 656 |
- lua_Number param; |
| 657 |
- if ( badArgs( L, 1, "Enable" ) ) return 0; |
| 658 |
- param = lua_tonumber( L, 1 ); |
| 659 |
- lua_settop( L, 0 ); |
| 660 |
- glfwEnable( (int)param ); |
| 661 |
+ unsupportedFunction( L, "Enable" ); |
| 662 |
return 0; |
| 663 |
} |
| 664 |
|
| 665 |
static int glfw_Disable( lua_State *L ) |
| 666 |
{ |
| 667 |
- lua_Number param; |
| 668 |
- if ( badArgs( L, 1, "Disable" ) ) return 0; |
| 669 |
- param = lua_tonumber( L, 1 ); |
| 670 |
- lua_settop( L, 0 ); |
| 671 |
- glfwDisable( (int)param ); |
| 672 |
+ unsupportedFunction( L, "Disable" ); |
| 673 |
return 0; |
| 674 |
} |
| 675 |
|
| 676 |
@@ -827,16 +914,8 @@ static int glfw_FreeImage( lua_State *L ) |
| 677 |
|
| 678 |
static int glfw_LoadTexture2D( lua_State *L ) |
| 679 |
{ |
| 680 |
- const char *name; |
| 681 |
- lua_Number flags; |
| 682 |
- int res; |
| 683 |
- if( badArgs( L, 2, "LoadTexture2D" ) ) return 0; |
| 684 |
- name = lua_tostring( L, 1 ); |
| 685 |
- flags = lua_tonumber( L, 2 ); |
| 686 |
- lua_settop( L, 0 ); |
| 687 |
- res = glfwLoadTexture2D( name, (int)flags ); |
| 688 |
- lua_pushnumber( L, (lua_Number)res ); |
| 689 |
- return 1; |
| 690 |
+ unsupportedFunction( L, "LoadTexture2D" ); |
| 691 |
+ return 0; |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
@@ -846,97 +925,112 @@ static int glfw_LoadTexture2D( lua_State *L ) |
| 696 |
|
| 697 |
static int glfw_SetWindowSizeCallback( lua_State *L ) |
| 698 |
{ |
| 699 |
+ // Disallow if a window is not open |
| 700 |
+ if (window == NULL) return 0; |
| 701 |
+ |
| 702 |
GLFWwindowsizefun fun = NULL; |
| 703 |
if( lua_isstring( L, 1 ) ) |
| 704 |
{ |
| 705 |
windowsize_name = lua_tostring( L, 1 ); |
| 706 |
fun = luaglfw_windowsizefun; |
| 707 |
} |
| 708 |
- glfwSetWindowSizeCallback( fun ); |
| 709 |
+ glfwSetWindowSizeCallback( window, fun ); |
| 710 |
return 0; |
| 711 |
} |
| 712 |
|
| 713 |
static int glfw_SetWindowCloseCallback( lua_State *L ) |
| 714 |
{ |
| 715 |
+ // Disallow if a window is not open |
| 716 |
+ if (window == NULL) return 0; |
| 717 |
+ |
| 718 |
GLFWwindowclosefun fun = NULL; |
| 719 |
if( lua_isstring( L, 1 ) ) |
| 720 |
{ |
| 721 |
windowclose_name = lua_tostring( L, 1 ); |
| 722 |
fun = luaglfw_windowclosefun; |
| 723 |
} |
| 724 |
- glfwSetWindowCloseCallback( fun ); |
| 725 |
+ glfwSetWindowCloseCallback( window, fun ); |
| 726 |
return 0; |
| 727 |
} |
| 728 |
|
| 729 |
static int glfw_SetWindowRefreshCallback( lua_State *L ) |
| 730 |
{ |
| 731 |
+ // Disallow if a window is not open |
| 732 |
+ if (window == NULL) return 0; |
| 733 |
+ |
| 734 |
GLFWwindowrefreshfun fun = NULL; |
| 735 |
if( lua_isstring( L, 1 ) ) |
| 736 |
{ |
| 737 |
windowrefresh_name = lua_tostring( L, 1 ); |
| 738 |
fun = luaglfw_windowrefreshfun; |
| 739 |
} |
| 740 |
- glfwSetWindowRefreshCallback( fun ); |
| 741 |
+ glfwSetWindowRefreshCallback( window, fun ); |
| 742 |
return 0; |
| 743 |
} |
| 744 |
|
| 745 |
static int glfw_SetMouseButtonCallback( lua_State *L ) |
| 746 |
{ |
| 747 |
+ // Disallow if a window is not open |
| 748 |
+ if (window == NULL) return 0; |
| 749 |
+ |
| 750 |
GLFWmousebuttonfun fun = NULL; |
| 751 |
if( lua_isstring( L, 1 ) ) |
| 752 |
{ |
| 753 |
mousebutton_name = lua_tostring( L, 1 ); |
| 754 |
fun = luaglfw_mousebuttonfun; |
| 755 |
} |
| 756 |
- glfwSetMouseButtonCallback( fun ); |
| 757 |
+ glfwSetMouseButtonCallback( window, fun ); |
| 758 |
return 0; |
| 759 |
} |
| 760 |
|
| 761 |
static int glfw_SetMousePosCallback( lua_State *L ) |
| 762 |
{ |
| 763 |
- GLFWmouseposfun fun = NULL; |
| 764 |
+ // Disallow if a window is not open |
| 765 |
+ if (window == NULL) return 0; |
| 766 |
+ |
| 767 |
+ GLFWcursorposfun fun = NULL; |
| 768 |
if( lua_isstring( L, 1 ) ) |
| 769 |
{ |
| 770 |
mousepos_name = lua_tostring( L, 1 ); |
| 771 |
fun = luaglfw_mouseposfun; |
| 772 |
} |
| 773 |
- glfwSetMousePosCallback( fun ); |
| 774 |
+ glfwSetCursorPosCallback( window, fun ); |
| 775 |
return 0; |
| 776 |
} |
| 777 |
|
| 778 |
static int glfw_SetMouseWheelCallback( lua_State *L ) |
| 779 |
{ |
| 780 |
- GLFWmousewheelfun fun = NULL; |
| 781 |
- if( lua_isstring( L, 1 ) ) |
| 782 |
- { |
| 783 |
- mousewheel_name = lua_tostring( L, 1 ); |
| 784 |
- fun = luaglfw_mousewheelfun; |
| 785 |
- } |
| 786 |
- glfwSetMouseWheelCallback( fun ); |
| 787 |
+ unsupportedFunction( L, "SetMouseWheelCallback" ); |
| 788 |
return 0; |
| 789 |
} |
| 790 |
|
| 791 |
static int glfw_SetKeyCallback( lua_State *L ) |
| 792 |
{ |
| 793 |
+ // Disallow if a window is not open |
| 794 |
+ if (window == NULL) return 0; |
| 795 |
+ |
| 796 |
GLFWkeyfun fun = NULL; |
| 797 |
if( lua_isstring( L, 1 ) ) |
| 798 |
{ |
| 799 |
key_name = lua_tostring( L, 1 ); |
| 800 |
fun = luaglfw_keyfun; |
| 801 |
} |
| 802 |
- glfwSetKeyCallback( fun ); |
| 803 |
+ glfwSetKeyCallback( window, fun ); |
| 804 |
return 0; |
| 805 |
} |
| 806 |
|
| 807 |
static int glfw_SetCharCallback( lua_State *L ) |
| 808 |
{ |
| 809 |
+ // Disallow if a window is not open |
| 810 |
+ if (window == NULL) return 0; |
| 811 |
+ |
| 812 |
GLFWcharfun fun = NULL; |
| 813 |
if( lua_isstring( L, 1 ) ) |
| 814 |
{ |
| 815 |
char_name = lua_tostring( L, 1 ); |
| 816 |
fun = luaglfw_charfun; |
| 817 |
} |
| 818 |
- glfwSetCharCallback( fun ); |
| 819 |
+ glfwSetCharCallback( window, fun ); |
| 820 |
return 0; |
| 821 |
} |
| 822 |
|
| 823 |
@@ -964,8 +1058,7 @@ static struct lua_constant glfw_constants[] = |
| 824 |
|
| 825 |
// Keyboard key definitions |
| 826 |
{ "KEY_SPACE", GLFW_KEY_SPACE }, |
| 827 |
- { "KEY_SPECIAL", GLFW_KEY_SPECIAL }, |
| 828 |
- { "KEY_ESC", GLFW_KEY_ESC }, |
| 829 |
+ { "KEY_ESC", GLFW_KEY_ESCAPE }, |
| 830 |
{ "KEY_F1", GLFW_KEY_F1 }, |
| 831 |
{ "KEY_F2", GLFW_KEY_F2 }, |
| 832 |
{ "KEY_F3", GLFW_KEY_F3 }, |
| 833 |
@@ -995,19 +1088,19 @@ static struct lua_constant glfw_constants[] = |
| 834 |
{ "KEY_DOWN", GLFW_KEY_DOWN }, |
| 835 |
{ "KEY_LEFT", GLFW_KEY_LEFT }, |
| 836 |
{ "KEY_RIGHT", GLFW_KEY_RIGHT }, |
| 837 |
- { "KEY_LSHIFT", GLFW_KEY_LSHIFT }, |
| 838 |
- { "KEY_RSHIFT", GLFW_KEY_RSHIFT }, |
| 839 |
- { "KEY_LCTRL", GLFW_KEY_LCTRL }, |
| 840 |
- { "KEY_RCTRL", GLFW_KEY_RCTRL }, |
| 841 |
- { "KEY_LALT", GLFW_KEY_LALT }, |
| 842 |
- { "KEY_RALT", GLFW_KEY_RALT }, |
| 843 |
+ { "KEY_LSHIFT", GLFW_KEY_LEFT_SHIFT }, |
| 844 |
+ { "KEY_RSHIFT", GLFW_KEY_RIGHT_SHIFT }, |
| 845 |
+ { "KEY_LCTRL", GLFW_KEY_LEFT_CONTROL }, |
| 846 |
+ { "KEY_RCTRL", GLFW_KEY_RIGHT_CONTROL }, |
| 847 |
+ { "KEY_LALT", GLFW_KEY_LEFT_ALT }, |
| 848 |
+ { "KEY_RALT", GLFW_KEY_RIGHT_ALT }, |
| 849 |
{ "KEY_TAB", GLFW_KEY_TAB }, |
| 850 |
{ "KEY_ENTER", GLFW_KEY_ENTER }, |
| 851 |
{ "KEY_BACKSPACE", GLFW_KEY_BACKSPACE }, |
| 852 |
{ "KEY_INSERT", GLFW_KEY_INSERT }, |
| 853 |
- { "KEY_DEL", GLFW_KEY_DEL }, |
| 854 |
- { "KEY_PAGEUP", GLFW_KEY_PAGEUP }, |
| 855 |
- { "KEY_PAGEDOWN", GLFW_KEY_PAGEDOWN }, |
| 856 |
+ { "KEY_DEL", GLFW_KEY_DELETE }, |
| 857 |
+ { "KEY_PAGEUP", GLFW_KEY_PAGE_UP }, |
| 858 |
+ { "KEY_PAGEDOWN", GLFW_KEY_PAGE_DOWN }, |
| 859 |
{ "KEY_HOME", GLFW_KEY_HOME }, |
| 860 |
{ "KEY_END", GLFW_KEY_END }, |
| 861 |
{ "KEY_KP_0", GLFW_KEY_KP_0 }, |
| 862 |
@@ -1065,14 +1158,14 @@ static struct lua_constant glfw_constants[] = |
| 863 |
{ "JOYSTICK_LAST", GLFW_JOYSTICK_LAST }, |
| 864 |
|
| 865 |
// glfwOpenWindow modes |
| 866 |
- { "WINDOW", GLFW_WINDOW }, |
| 867 |
- { "FULLSCREEN", GLFW_FULLSCREEN }, |
| 868 |
+ { "WINDOW", GLFW2_WINDOW }, |
| 869 |
+ { "FULLSCREEN", GLFW2_FULLSCREEN }, |
| 870 |
|
| 871 |
// glfwGetWindowParam tokens |
| 872 |
- { "OPENED", GLFW_OPENED }, |
| 873 |
- { "ACTIVE", GLFW_ACTIVE }, |
| 874 |
+ { "OPENED", GLFW2_OPENED }, |
| 875 |
+ { "ACTIVE", GLFW_FOCUSED }, |
| 876 |
{ "ICONIFIED", GLFW_ICONIFIED }, |
| 877 |
- { "ACCELERATED", GLFW_ACCELERATED }, |
| 878 |
+ { "ACCELERATED", GLFW2_ACCELERATED }, |
| 879 |
{ "RED_BITS", GLFW_RED_BITS }, |
| 880 |
{ "GREEN_BITS", GLFW_GREEN_BITS }, |
| 881 |
{ "BLUE_BITS", GLFW_BLUE_BITS }, |
| 882 |
@@ -1089,31 +1182,10 @@ static struct lua_constant glfw_constants[] = |
| 883 |
{ "AUX_BUFFERS", GLFW_AUX_BUFFERS }, |
| 884 |
{ "STEREO", GLFW_STEREO }, |
| 885 |
|
| 886 |
- // glfwEnable/glfwDisable tokens |
| 887 |
- { "MOUSE_CURSOR", GLFW_MOUSE_CURSOR }, |
| 888 |
- { "STICKY_KEYS", GLFW_STICKY_KEYS }, |
| 889 |
- { "STICKY_MOUSE_BUTTONS", GLFW_STICKY_MOUSE_BUTTONS }, |
| 890 |
- { "SYSTEM_KEYS", GLFW_SYSTEM_KEYS }, |
| 891 |
- { "KEY_REPEAT", GLFW_KEY_REPEAT }, |
| 892 |
- { "AUTO_POLL_EVENTS", GLFW_AUTO_POLL_EVENTS }, |
| 893 |
- |
| 894 |
- // glfwWaitThread wait modes |
| 895 |
- { "WAIT", GLFW_WAIT }, |
| 896 |
- { "NOWAIT", GLFW_NOWAIT }, |
| 897 |
- |
| 898 |
// glfwGetJoystickParam tokens |
| 899 |
- { "PRESENT", GLFW_PRESENT }, |
| 900 |
- { "AXES", GLFW_AXES }, |
| 901 |
- { "BUTTONS", GLFW_BUTTONS }, |
| 902 |
- |
| 903 |
- // glfwReadImage/glfwLoadTexture2D flags |
| 904 |
- { "NO_RESCALE_BIT", GLFW_NO_RESCALE_BIT }, |
| 905 |
- { "ORIGIN_UL_BIT", GLFW_ORIGIN_UL_BIT }, |
| 906 |
- { "BUILD_MIPMAPS_BIT", GLFW_BUILD_MIPMAPS_BIT }, |
| 907 |
- { "ALPHA_MAP_BIT", GLFW_ALPHA_MAP_BIT }, |
| 908 |
- |
| 909 |
- // Time spans longer than this (seconds) are considered to be infinity |
| 910 |
- { "INFINITY", GLFW_INFINITY }, |
| 911 |
+ { "PRESENT", GLFW2_PRESENT }, |
| 912 |
+ { "AXES", GLFW2_AXES }, |
| 913 |
+ { "BUTTONS", GLFW2_BUTTONS }, |
| 914 |
|
| 915 |
{ NULL, 0 } |
| 916 |
}; |
| 917 |
diff --git a/dokidoki-support/mixer.c b/dokidoki-support/mixer.c |
| 918 |
index 895753c..015cd0a 100644 |
| 919 |
--- a/dokidoki-support/mixer.c |
| 920 |
+++ b/dokidoki-support/mixer.c |
| 921 |
@@ -455,33 +455,30 @@ do \ |
| 922 |
} while(0) |
| 923 |
|
| 924 |
#include <alsa/asoundlib.h> |
| 925 |
-#include <GL/glfw.h> |
| 926 |
+#include <stdatomic.h> |
| 927 |
+#include <pthread.h> |
| 928 |
|
| 929 |
static snd_pcm_t * sound_device = NULL; |
| 930 |
|
| 931 |
-static GLFWthread audio_thread = 0; |
| 932 |
-static volatile int running = 0; |
| 933 |
-static GLFWmutex running_mutex = NULL; |
| 934 |
+static pthread_t audio_thread; |
| 935 |
+static volatile atomic_flag running = ATOMIC_FLAG_INIT; |
| 936 |
|
| 937 |
-static GLFWCALL void audio_loop(void *data) |
| 938 |
+static void* audio_loop(void *data) |
| 939 |
{ |
| 940 |
sample_t buffer[BUFFER_SIZE*2]; |
| 941 |
|
| 942 |
- glfwLockMutex(running_mutex); |
| 943 |
- while(running) |
| 944 |
+ while(atomic_flag_test_and_set(&running)) |
| 945 |
{ |
| 946 |
- glfwUnlockMutex(running_mutex); |
| 947 |
- |
| 948 |
mix_into(buffer, BUFFER_SIZE); |
| 949 |
int err = snd_pcm_writei(sound_device, buffer, BUFFER_SIZE); |
| 950 |
if(err < 0) |
| 951 |
err = snd_pcm_recover(sound_device, err, 0); |
| 952 |
|
| 953 |
- glfwLockMutex(running_mutex); |
| 954 |
if(err < 0) |
| 955 |
break; |
| 956 |
} |
| 957 |
- glfwUnlockMutex(running_mutex); |
| 958 |
+ |
| 959 |
+ return NULL; |
| 960 |
} |
| 961 |
|
| 962 |
static int init() |
| 963 |
@@ -498,20 +495,16 @@ static int init() |
| 964 |
|
| 965 |
ALSA_CHECK(snd_pcm_prepare(sound_device)); |
| 966 |
|
| 967 |
- CHECK(running_mutex = glfwCreateMutex(), |
| 968 |
- "audio mutex creation failed"); |
| 969 |
- running = 1; |
| 970 |
- audio_thread = glfwCreateThread(audio_loop, NULL); |
| 971 |
+ atomic_flag_test_and_set(&running); |
| 972 |
+ CHECK(pthread_create(&audio_thread, NULL, audio_loop, NULL) == 0, "error creating alsa thread"); |
| 973 |
|
| 974 |
return 1; |
| 975 |
} |
| 976 |
|
| 977 |
static int uninit() |
| 978 |
{ |
| 979 |
- glfwLockMutex(running_mutex); |
| 980 |
- running = 0; |
| 981 |
- glfwUnlockMutex(running_mutex); |
| 982 |
- glfwWaitThread(audio_thread, GLFW_WAIT); |
| 983 |
+ atomic_flag_clear(&running); |
| 984 |
+ pthread_join(audio_thread, NULL); |
| 985 |
|
| 986 |
ALSA_CHECK(snd_pcm_close(sound_device)); |
| 987 |
sound_device = NULL; |