Comments (3)
Is this an unfinished part of SDL2?
Not understanding something yet is not the same thing as it being incomplete.
I even tried looking through the headers and cannot find where SDL_EventFilter is defined, just where it was used.
It's defined here ... In case this is where the confusion is, this isn't a function, it's a function pointer. You set up a function of your own that matches that signature, and give it to SDL_SetEventFilter to call later.
userdata
is whatever you want to be passed to your function (and if you don't need it, you can pass NULL there and ignore it).
Here's a small example program that uses it. Basically every event that SDL intends to pass to the application will go through the my_event_filter function, which can veto them, or change them. It uses the userdata
arg to change a variable when the user presses the space bar, and that variable decides what color the window will be.
All of this said: the best documentation for event filters is probably "don't use them." They add confusion and complexity to things that often have cleaner solutions elsewhere, and have the ability to really break things when misused.
#include "SDL.h"
static int SDLCALL my_event_filter(void *userdata, SDL_Event * event)
{
if ((event->type == SDL_KEYDOWN) && (event->key.keysym.sym == SDLK_SPACE)) {
Uint8 *blue = (Uint8 *) userdata;
if (*blue == 0) {
*blue = 255;
} else {
*blue = 0;
}
}
return 1; // let all events be added to the queue since we always return 1.
}
int main(int argc, char **argv)
{
Uint8 blue = 0;
int quit = 0;
// just get a window on the screen and clear it to black.
// you _should_ check these for errors!
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow("Hello SDL", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
// Set up an event filter...
SDL_SetEventFilter(my_event_filter, &blue);
// Now run the event loop mostly forever. Each event goes through the
// my_event_filter function before it lands here. Each frame, we fill
// in the window with whatever color `blue` is set to, which might be
// changed by the filter function.
while (!quit) {
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
quit = 1;
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, blue, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
SDL_Quit();
return 0;
}
from sdlwiki.
Thank you, this actually does clear things up. Now I see that I really don't need to setup filters for what I want to do.
Yes, I am still new to programming and there are still things I don't understand, like function pointers. The straightforwardness and clarity of the SDL documentation is helping me to learn how to program, it just baffled me when I got to this page.
from sdlwiki.
(I added the code example to the wiki here, so it won't be lost.)
Yes, I am still new to programming and there are still things I don't understand, like function pointers.
Any day we learn something new is an awesome day. :)
from sdlwiki.
Related Issues (20)
- Include the header file in which a function is located in the documentation. HOT 5
- Report mouse wheel scroll state through a function
- SDL_IsEventEnabled / SDL_EventEnabled HOT 2
- [SDL3.1.1] - SDL_RenderGeometry Example failure HOT 6
- Quality of examples HOT 9
- Feedback on page SDL2/SDL_TextInputEvent HOT 1
- Feedback on page SDL2/SDL_TextInputEvent HOT 1
- `SDL_GetPath` still has a wiki page, despite being renamed to `SDL_GetUserFolder` HOT 2
- Add a wiki page about which C standard is used by SDL.
- Wiki: Add header Inclusion graph HOT 4
- Feedback on page SDL3/SDL_SetRenderVSync HOT 2
- Order of sections not mentioned on SDL3/README/documentation-rules
- Add examples to SDL3/SDL_CreateWindowWithProperties? HOT 2
- SDL_TextInputEvent requires SDL_StartTextInput to work HOT 5
- Feedback on pages SDL3/SDL_PROP_THREAD_CREATE_ENTRY_FUNCTION_POINTER & co.
- Feedback on page SDL2/SDL_GLattr
- Missing data in page SDL2/SDL_Keycode
- Feedback on page SDL3/Tutorials/AudioStream: interleaving? HOT 3
- Feedback on page SDL3/SDL_PeepEvents HOT 1
- Chore: add removal script to ci HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sdlwiki.