Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use struct info in src/library_sdl.js. NFC #22885

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 45 additions & 49 deletions src/library_sdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,13 @@ var LibrarySDL = {
translateRGBAToColor: (r, g, b, a) => r | g << 8 | b << 16 | a << 24,

makeSurface(width, height, flags, usePageCanvas, source, rmask, gmask, bmask, amask) {
var is_SDL_HWSURFACE = flags & 0x00000001;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like SDL_HWSURFACE constant was wrong here. It should have been 0x08000001. Luckily it was still matching because its a subset of the bits.

var is_SDL_HWPALETTE = flags & 0x00200000;
var is_SDL_OPENGL = flags & 0x04000000;
var is_SDL_HWSURFACE = flags & {{{ cDefs.SDL_HWSURFACE }}};
var is_SDL_HWPALETTE = flags & {{{ cDefs.SDL_HWPALETTE }}};
var is_SDL_OPENGL = flags & {{{ cDefs.SDL_OPENGL }}};

var surf = _malloc({{{ C_STRUCTS.SDL_Surface.__size__ }}});
var pixelFormat = _malloc({{{ C_STRUCTS.SDL_PixelFormat.__size__ }}});
//surface with SDL_HWPALETTE flag is 8bpp surface (1 byte)
// surface with SDL_HWPALETTE flag is 8bpp surface (1 byte)
var bpp = is_SDL_HWPALETTE ? 1 : 4;
var buffer = 0;

Expand Down Expand Up @@ -405,10 +405,10 @@ var LibrarySDL = {
}

var webGLContextAttributes = {
antialias: ((SDL.glAttributes[13 /*SDL_GL_MULTISAMPLEBUFFERS*/] != 0) && (SDL.glAttributes[14 /*SDL_GL_MULTISAMPLESAMPLES*/] > 1)),
depth: (SDL.glAttributes[6 /*SDL_GL_DEPTH_SIZE*/] > 0),
stencil: (SDL.glAttributes[7 /*SDL_GL_STENCIL_SIZE*/] > 0),
alpha: (SDL.glAttributes[3 /*SDL_GL_ALPHA_SIZE*/] > 0)
antialias: ((SDL.glAttributes[{{{ cDefs.SDL_GL_MULTISAMPLEBUFFERS }}}] != 0) && (SDL.glAttributes[{{{ cDefs.SDL_GL_MULTISAMPLESAMPLES }}}] > 1)),
depth: (SDL.glAttributes[{{{ cDefs.SDL_GL_DEPTH_SIZE }}}] > 0),
stencil: (SDL.glAttributes[{{{ cDefs.SDL_GL_STENCIL_SIZE }}}] > 0),
alpha: (SDL.glAttributes[{{{ cDefs.SDL_GL_ALPHA_SIZE }}}] > 0)
};

#if OFFSCREEN_FRAMEBUFFER
Expand Down Expand Up @@ -649,7 +649,7 @@ var LibrarySDL = {

// Simulate old-style SDL events representing mouse wheel input as buttons
// Subtract one since JS->C marshalling is defined to add one back.
var button = delta > 0 ? 3 /*SDL_BUTTON_WHEELUP-1*/ : 4 /*SDL_BUTTON_WHEELDOWN-1*/;
var button = (delta > 0 ? {{{ cDefs.SDL_BUTTON_WHEELUP }}} : {{{ cDefs.SDL_BUTTON_WHEELDOWN }}}) - 1;
SDL.events.push({ type: 'mousedown', button, pageX: event.pageX, pageY: event.pageY });
SDL.events.push({ type: 'mouseup', button, pageX: event.pageX, pageY: event.pageY });

Expand Down Expand Up @@ -859,12 +859,12 @@ var LibrarySDL = {
#endif
{{{ makeSetValue('SDL.keyboardState', 'code', 'down', 'i8') }}};
// TODO: lmeta, rmeta, numlock, capslock, KMOD_MODE, KMOD_RESERVED
SDL.modState = ({{{ makeGetValue('SDL.keyboardState', '1248', 'i8') }}} ? 0x0040 : 0) | // KMOD_LCTRL
({{{ makeGetValue('SDL.keyboardState', '1249', 'i8') }}} ? 0x0001 : 0) | // KMOD_LSHIFT
({{{ makeGetValue('SDL.keyboardState', '1250', 'i8') }}} ? 0x0100 : 0) | // KMOD_LALT
({{{ makeGetValue('SDL.keyboardState', '1252', 'i8') }}} ? 0x0080 : 0) | // KMOD_RCTRL
({{{ makeGetValue('SDL.keyboardState', '1253', 'i8') }}} ? 0x0002 : 0) | // KMOD_RSHIFT
({{{ makeGetValue('SDL.keyboardState', '1254', 'i8') }}} ? 0x0200 : 0); // KMOD_RALT
SDL.modState = ({{{ makeGetValue('SDL.keyboardState', '1248', 'i8') }}} ? {{{ cDefs.KMOD_LCTRL }}} : 0) |
({{{ makeGetValue('SDL.keyboardState', cDefs.SDLK_LSHIFT, 'i8') }}} ? {{{ cDefs.KMOD_LSHIFT }}} : 0) |
({{{ makeGetValue('SDL.keyboardState', cDefs.SDLK_LALT, 'i8') }}} ? {{{ cDefs.KMOD_LALT }}} : 0) |
({{{ makeGetValue('SDL.keyboardState', cDefs.SDLK_RCTRL, 'i8') }}} ? {{{ cDefs.KMOD_RCTRL }}} : 0) |
({{{ makeGetValue('SDL.keyboardState', cDefs.SDLK_RSHIFT, 'i8') }}} ? {{{ cDefs.KMOD_RSHIFT }}} : 0) |
({{{ makeGetValue('SDL.keyboardState', cDefs.SDLK_RALT, 'i8') }}} ? {{{ cDefs.KMOD_RALT }}} : 0);
if (down) {
SDL.keyboardMap[code] = event.keyCode; // save the DOM input, which we can use to unpress it during blur
} else {
Expand Down Expand Up @@ -900,7 +900,7 @@ var LibrarySDL = {
},

pollEvent(ptr) {
if (SDL.initFlags & 0x200 && SDL.joystickEventState) {
if (SDL.initFlags & {{{ cDefs.SDL_INIT_JOYSTICK }}} && SDL.joystickEventState) {
// If SDL_INIT_JOYSTICK was supplied AND the joystick system is configured
// to automatically query for events, query for joystick events.
SDL.queryJoysticks();
Expand Down Expand Up @@ -1043,23 +1043,19 @@ var LibrarySDL = {
break;
}
case 'focus': {
var SDL_WINDOWEVENT_FOCUS_GAINED = 12 /* SDL_WINDOWEVENT_FOCUS_GAINED */;
{{{ makeSetValue('ptr', C_STRUCTS.SDL_WindowEvent.type, 'SDL.DOMEventToSDLEvent[event.type]', 'i32') }}};
{{{ makeSetValue('ptr', C_STRUCTS.SDL_WindowEvent.windowID, '0', 'i32') }}};
{{{ makeSetValue('ptr', C_STRUCTS.SDL_WindowEvent.event, 'SDL_WINDOWEVENT_FOCUS_GAINED', 'i8') }}};
{{{ makeSetValue('ptr', C_STRUCTS.SDL_WindowEvent.event, cDefs.SDL_WINDOWEVENT_FOCUS_GAINED, 'i8') }}};
break;
}
case 'blur': {
var SDL_WINDOWEVENT_FOCUS_LOST = 13 /* SDL_WINDOWEVENT_FOCUS_LOST */;
{{{ makeSetValue('ptr', C_STRUCTS.SDL_WindowEvent.type, 'SDL.DOMEventToSDLEvent[event.type]', 'i32') }}};
{{{ makeSetValue('ptr', C_STRUCTS.SDL_WindowEvent.windowID, '0', 'i32') }}};
{{{ makeSetValue('ptr', C_STRUCTS.SDL_WindowEvent.event, 'SDL_WINDOWEVENT_FOCUS_LOST', 'i8') }}};
{{{ makeSetValue('ptr', C_STRUCTS.SDL_WindowEvent.event, cDefs.SDL_WINDOWEVENT_FOCUS_LOST, 'i8') }}};
break;
}
case 'visibilitychange': {
var SDL_WINDOWEVENT_SHOWN = 1 /* SDL_WINDOWEVENT_SHOWN */;
var SDL_WINDOWEVENT_HIDDEN = 2 /* SDL_WINDOWEVENT_HIDDEN */;
var visibilityEventID = event.visible ? SDL_WINDOWEVENT_SHOWN : SDL_WINDOWEVENT_HIDDEN;
var visibilityEventID = event.visible ? {{{ cDefs.SDL_WINDOWEVENT_SHOWN }}} : {{{ cDefs.SDL_WINDOWEVENT_HIDDEN }}};
{{{ makeSetValue('ptr', C_STRUCTS.SDL_WindowEvent.type, 'SDL.DOMEventToSDLEvent[event.type]', 'i32') }}};
{{{ makeSetValue('ptr', C_STRUCTS.SDL_WindowEvent.windowID, 0, 'i32') }}};
{{{ makeSetValue('ptr', C_STRUCTS.SDL_WindowEvent.event, 'visibilityEventID' , 'i8') }}};
Expand Down Expand Up @@ -1390,28 +1386,28 @@ var LibrarySDL = {
window.addEventListener("unload", SDL.receiveEvent);
SDL.keyboardState = _calloc(0x10000, 1); // Our SDL needs 512, but 64K is safe for older SDLs
// Initialize this structure carefully for closure
SDL.DOMEventToSDLEvent['keydown'] = 0x300 /* SDL_KEYDOWN */;
SDL.DOMEventToSDLEvent['keyup'] = 0x301 /* SDL_KEYUP */;
SDL.DOMEventToSDLEvent['keypress'] = 0x303 /* SDL_TEXTINPUT */;
SDL.DOMEventToSDLEvent['mousedown'] = 0x401 /* SDL_MOUSEBUTTONDOWN */;
SDL.DOMEventToSDLEvent['mouseup'] = 0x402 /* SDL_MOUSEBUTTONUP */;
SDL.DOMEventToSDLEvent['mousemove'] = 0x400 /* SDL_MOUSEMOTION */;
SDL.DOMEventToSDLEvent['wheel'] = 0x403 /* SDL_MOUSEWHEEL */;
SDL.DOMEventToSDLEvent['touchstart'] = 0x700 /* SDL_FINGERDOWN */;
SDL.DOMEventToSDLEvent['touchend'] = 0x701 /* SDL_FINGERUP */;
SDL.DOMEventToSDLEvent['touchmove'] = 0x702 /* SDL_FINGERMOTION */;
SDL.DOMEventToSDLEvent['unload'] = 0x100 /* SDL_QUIT */;
SDL.DOMEventToSDLEvent['resize'] = 0x7001 /* SDL_VIDEORESIZE/SDL_EVENT_COMPAT2 */;
SDL.DOMEventToSDLEvent['visibilitychange'] = 0x200 /* SDL_WINDOWEVENT */;
SDL.DOMEventToSDLEvent['focus'] = 0x200 /* SDL_WINDOWEVENT */;
SDL.DOMEventToSDLEvent['blur'] = 0x200 /* SDL_WINDOWEVENT */;
SDL.DOMEventToSDLEvent['keydown'] = {{{ cDefs.SDL_KEYDOWN }}};
SDL.DOMEventToSDLEvent['keyup'] = {{{ cDefs.SDL_KEYUP }}};
SDL.DOMEventToSDLEvent['keypress'] = {{{ cDefs.SDL_TEXTINPUT }}};
SDL.DOMEventToSDLEvent['mousedown'] = {{{ cDefs.SDL_MOUSEBUTTONDOWN }}};
SDL.DOMEventToSDLEvent['mouseup'] = {{{ cDefs.SDL_MOUSEBUTTONUP }}};
SDL.DOMEventToSDLEvent['mousemove'] = {{{ cDefs.SDL_MOUSEMOTION }}};
SDL.DOMEventToSDLEvent['wheel'] = {{{ cDefs.SDL_MOUSEWHEEL }}};
SDL.DOMEventToSDLEvent['touchstart'] = {{{ cDefs.SDL_FINGERDOWN }}};
SDL.DOMEventToSDLEvent['touchend'] = {{{ cDefs.SDL_FINGERUP }}};
SDL.DOMEventToSDLEvent['touchmove'] = {{{ cDefs.SDL_FINGERMOTION }}};
SDL.DOMEventToSDLEvent['unload'] = {{{ cDefs.SDL_QUIT }}};
SDL.DOMEventToSDLEvent['resize'] = {{{ cDefs.SDL_VIDEORESIZE }}};
SDL.DOMEventToSDLEvent['visibilitychange'] = {{{ cDefs.SDL_WINDOWEVENT }}};
SDL.DOMEventToSDLEvent['focus'] = {{{ cDefs.SDL_WINDOWEVENT }}};
SDL.DOMEventToSDLEvent['blur'] = {{{ cDefs.SDL_WINDOWEVENT }}};

// These are not technically DOM events; the HTML gamepad API is poll-based.
// However, we define them here, as the rest of the SDL code assumes that
// all SDL events originate as DOM events.
SDL.DOMEventToSDLEvent['joystick_axis_motion'] = 0x600 /* SDL_JOYAXISMOTION */;
SDL.DOMEventToSDLEvent['joystick_button_down'] = 0x603 /* SDL_JOYBUTTONDOWN */;
SDL.DOMEventToSDLEvent['joystick_button_up'] = 0x604 /* SDL_JOYBUTTONUP */;
SDL.DOMEventToSDLEvent['joystick_axis_motion'] = {{{ cDefs.SDL_JOYAXISMOTION }}};
SDL.DOMEventToSDLEvent['joystick_button_down'] = {{{ cDefs.SDL_JOYBUTTONDOWN }}};
SDL.DOMEventToSDLEvent['joystick_button_up'] = {{{ cDefs.SDL_JOYBUTTONUP }}};
return 0; // success
},

Expand Down Expand Up @@ -1504,7 +1500,7 @@ var LibrarySDL = {
assert(!SDL.screen);
}

if (SDL.GL) flags = flags | 0x04000000; // SDL_OPENGL - if we are using GL, then later calls to SetVideoMode may not mention GL, but we do need it. Once in GL mode, we never leave it.
if (SDL.GL) flags = flags | {{{ cDefs.SDL_OPENGL }}}; // if we are using GL, then later calls to SetVideoMode may not mention GL, but we do need it. Once in GL mode, we never leave it.

SDL.screen = SDL.makeSurface(width, height, flags, true, 'screen');

Expand Down Expand Up @@ -1580,7 +1576,7 @@ var LibrarySDL = {

if (SDL.defaults.copyOnLock && !SDL.defaults.discardOnLock) {
// Copy pixel data to somewhere accessible to 'C/C++'
if (surfData.isFlagSet(0x00200000 /* SDL_HWPALETTE */)) {
if (surfData.isFlagSet({{{ cDefs.SDL_HWPALETTE }}})) {
// If this is needed then
// we should compact the data from 32bpp to 8bpp index.
// I think best way to implement this is use
Expand Down Expand Up @@ -1619,7 +1615,7 @@ var LibrarySDL = {
}

// Copy pixel data to image
if (surfData.isFlagSet(0x00200000 /* SDL_HWPALETTE */)) {
if (surfData.isFlagSet({{{ cDefs.SDL_HWPALETTE }}})) {
SDL.copyIndexedColorData(surfData);
} else if (!surfData.colors) {
var data = surfData.image.data;
Expand Down Expand Up @@ -1917,7 +1913,7 @@ var LibrarySDL = {
var surfData = SDL.surfaces[surf];
assert(!surfData.locked); // but we could unlock and re-lock if we must..

if (surfData.isFlagSet(0x00200000 /* SDL_HWPALETTE */)) {
if (surfData.isFlagSet({{{ cDefs.SDL_HWPALETTE }}})) {
//in SDL_HWPALETTE color is index (0..255)
//so we should translate 1 byte value to
//32 bit canvas
Expand Down Expand Up @@ -1981,7 +1977,7 @@ var LibrarySDL = {
var surfData = SDL.surfaces[surf];
surfData.alpha = alpha;

if (!(flag & 0x00010000)) { // !SDL_SRCALPHA
if (!(flag & {{{ cDefs.SDL_SRCALPHA }}})) { // !SDL_SRCALPHA
surfData.alpha = 255;
}
},
Expand Down Expand Up @@ -2134,12 +2130,12 @@ var LibrarySDL = {
var state = 0;

if (Browser.pointerLock) {
state |= 0x01; // SDL_APPMOUSEFOCUS
state |= {{{ cDefs.SDL_APPMOUSEFOCUS }}};
}
if (document.hasFocus()) {
state |= 0x02; // SDL_APPINPUTFOCUS
state |= {{{ cDefs.SDL_APPINPUTFOCUS }}};
}
state |= 0x04; // SDL_APPACTIVE
state |= {{{ cDefs.SDL_APPACTIVE }}};

return state;
},
Expand Down
70 changes: 68 additions & 2 deletions src/struct_info.json
Original file line number Diff line number Diff line change
Expand Up @@ -537,14 +537,32 @@
// ===========================================
// SDL
// ===========================================
{
"file": "SDL/SDL.h",
"defines": [
"SDL_INIT_JOYSTICK"
]
},
{
"file": "SDL/SDL_compat.h",
"structs": {
"SDL_VideoInfo": [
"current_w",
"current_h"
]
}
},
"defines": [
"SDL_APPMOUSEFOCUS",
"SDL_APPINPUTFOCUS",
"SDL_APPACTIVE",
"SDL_VIDEORESIZE",
"SDL_SRCALPHA",
"SDL_HWPALETTE",
"SDL_HWSURFACE",
"SDL_OPENGL",
"SDL_BUTTON_WHEELUP",
"SDL_BUTTON_WHEELDOWN"
]
},
{
"file": "SDL/SDL_rect.h",
Expand Down Expand Up @@ -670,7 +688,25 @@
"w",
"h"
]
}
},
"defines": [
"SDL_VIDEORESIZE",
"SDL_WINDOWEVENT",
"SDL_MOUSEBUTTONDOWN",
"SDL_MOUSEBUTTONUP",
"SDL_MOUSEMOTION",
"SDL_MOUSEWHEEL",
"SDL_FINGERDOWN",
"SDL_FINGERUP",
"SDL_FINGERMOTION",
"SDL_QUIT",
"SDL_JOYBUTTONUP",
"SDL_JOYBUTTONDOWN",
"SDL_JOYAXISMOTION",
"SDL_TEXTINPUT",
"SDL_KEYUP",
"SDL_KEYDOWN"
]
},
{
"file": "SDL/SDL_touch.h",
Expand Down Expand Up @@ -728,6 +764,36 @@
]
}
},
{
"file": "SDL/SDL_keycode.h",
"defines": [
"KMOD_LCTRL",
"KMOD_RCTRL",
"KMOD_LALT",
"KMOD_RALT",
"KMOD_LSHIFT",
"KMOD_RSHIFT",
"SDLK_LSHIFT",
"SDLK_LALT",
"SDLK_RCTRL",
"SDLK_RSHIFT",
"SDLK_RALT"
]
},
{
"file": "SDL/SDL_video.h",
"defines": [
"SDL_GL_DEPTH_SIZE",
"SDL_GL_STENCIL_SIZE",
"SDL_GL_ALPHA_SIZE",
"SDL_GL_MULTISAMPLEBUFFERS",
"SDL_GL_MULTISAMPLESAMPLES",
"SDL_WINDOWEVENT_HIDDEN",
"SDL_WINDOWEVENT_SHOWN",
"SDL_WINDOWEVENT_FOCUS_LOST",
"SDL_WINDOWEVENT_FOCUS_GAINED"
]
},
{
"file": "uuid/uuid.h",
"defines": [
Expand Down
46 changes: 46 additions & 0 deletions src/struct_info_generated.json
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,12 @@
"ISIG": 1,
"IUTF8": 16384,
"IXON": 1024,
"KMOD_LALT": 256,
"KMOD_LCTRL": 64,
"KMOD_LSHIFT": 1,
"KMOD_RALT": 512,
"KMOD_RCTRL": 128,
"KMOD_RSHIFT": 2,
"MAP_ANONYMOUS": 32,
"MAP_FIXED": 16,
"MAP_PRIVATE": 2,
Expand Down Expand Up @@ -364,8 +370,48 @@
"RTLD_NODELETE": 4096,
"RTLD_NOW": 2,
"R_OK": 4,
"SDLK_LALT": 1250,
"SDLK_LSHIFT": 1249,
"SDLK_RALT": 1254,
"SDLK_RCTRL": 1252,
"SDLK_RSHIFT": 1253,
"SDL_APPACTIVE": 4,
"SDL_APPINPUTFOCUS": 2,
"SDL_APPMOUSEFOCUS": 1,
"SDL_BUTTON_WHEELDOWN": 5,
"SDL_BUTTON_WHEELUP": 4,
"SDL_FINGERDOWN": 1792,
"SDL_FINGERMOTION": 1794,
"SDL_FINGERUP": 1793,
"SDL_GL_ALPHA_SIZE": 3,
"SDL_GL_DEPTH_SIZE": 6,
"SDL_GL_MULTISAMPLEBUFFERS": 13,
"SDL_GL_MULTISAMPLESAMPLES": 14,
"SDL_GL_STENCIL_SIZE": 7,
"SDL_HWPALETTE": 2097152,
"SDL_HWSURFACE": 134217729,
"SDL_INIT_JOYSTICK": 512,
"SDL_JOYAXISMOTION": 1536,
"SDL_JOYBUTTONDOWN": 1539,
"SDL_JOYBUTTONUP": 1540,
"SDL_KEYDOWN": 768,
"SDL_KEYUP": 769,
"SDL_MOUSEBUTTONDOWN": 1025,
"SDL_MOUSEBUTTONUP": 1026,
"SDL_MOUSEMOTION": 1024,
"SDL_MOUSEWHEEL": 1027,
"SDL_OPENGL": 67108864,
"SDL_PIXELFORMAT_RGBA8888": -2042224636,
"SDL_QUIT": 256,
"SDL_SRCALPHA": 65536,
"SDL_TEXTINPUT": 771,
"SDL_TOUCH_MOUSEID": -1,
"SDL_VIDEORESIZE": 28673,
"SDL_WINDOWEVENT": 512,
"SDL_WINDOWEVENT_FOCUS_GAINED": 12,
"SDL_WINDOWEVENT_FOCUS_LOST": 13,
"SDL_WINDOWEVENT_HIDDEN": 2,
"SDL_WINDOWEVENT_SHOWN": 1,
"SEEK_CUR": 1,
"SEEK_END": 2,
"SEEK_SET": 0,
Expand Down
Loading
Loading