Skip to main content
edited title
Link
user1430
user1430

Filtering Why does this code to filter events with SDL not function properly?

deleted 235 characters in body
Source Link
#include <SDL.h> #include <stdio.h> #include <memory> //Screen dimension constants const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; int isMouseEvent(SDL_Event* ev); int main(int argc, char* args[]) { SDL_Event ev; //The window we'll be rendering to SDL_Window* window = NULL; //The surface contained by the window SDL_Surface* screenSurface = NULL; //Initialize SDL if (SDL_Init(SDL_INIT_VIDEO) < 0) { printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); } else { //Create window window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if (window == NULL) { printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); } else { bool black = false; //Get window surface screenSurface = SDL_GetWindowSurface(window); SDL_SetEventFilter(isMouseEvent); bool isOn = true; while (isOn) { //Pump events SDL_PumpEvents(); SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF)); //Update the surface SDL_UpdateWindowSurface(window); } } } //Destroy window SDL_DestroyWindow(window); //Quit SDL subsystems SDL_Quit(); return 0; } int isMouseEvent(SDL_Event* ev) { if(ev!=NULL) {  if (ev->type == SDL_MOUSEMOTION)   return 0; } return 1; } 

However, I am recieving these errors:

Error 1 error C2660: 'SDL_SetEventFilter' : function does not take 1 arguments
2 IntelliSense: argument of type "int (*)(SDL_Event *ev)" is incompatible with parameter of type "SDL_EventFilter"

Which does not make any sense to me, because 1) SDL_EventFilter takeswhen the second argument as a void pointercode is ran no events are filtered, so it should be ableevery event passed to be omittedisMouseEvent(This comes from my understanding at looking at every example I could find on this) and 2) is null. The parameterSDL2 Wiki is the correct typevery bare, according toand the wikiexamples on using SDL_SetEventFilter are very basic and do not work properly.

Are there any suggestions or Any ideas on whatas to where I am doing incorrectlyshould go from here, seeing as the events are constantly null when checked from the isMouseEvent() function?

#include <SDL.h> #include <stdio.h> #include <memory> //Screen dimension constants const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; int isMouseEvent(SDL_Event* ev); int main(int argc, char* args[]) { SDL_Event ev; //The window we'll be rendering to SDL_Window* window = NULL; //The surface contained by the window SDL_Surface* screenSurface = NULL; //Initialize SDL if (SDL_Init(SDL_INIT_VIDEO) < 0) { printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); } else { //Create window window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if (window == NULL) { printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); } else { bool black = false; //Get window surface screenSurface = SDL_GetWindowSurface(window); SDL_SetEventFilter(isMouseEvent); bool isOn = true; while (isOn) { //Pump events SDL_PumpEvents(); SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF)); //Update the surface SDL_UpdateWindowSurface(window); } } } //Destroy window SDL_DestroyWindow(window); //Quit SDL subsystems SDL_Quit(); return 0; } int isMouseEvent(SDL_Event* ev) { if (ev->type == SDL_MOUSEMOTION) return 0; return 1; } 

However, I am recieving these errors:

Error 1 error C2660: 'SDL_SetEventFilter' : function does not take 1 arguments
2 IntelliSense: argument of type "int (*)(SDL_Event *ev)" is incompatible with parameter of type "SDL_EventFilter"

Which does not make any sense to me, because 1) SDL_EventFilter takes the second argument as a void pointer, so it should be able to be omitted(This comes from my understanding at looking at every example I could find on this) and 2) The parameter is the correct type, according to the wiki.

Are there any suggestions or ideas on what I am doing incorrectly?

#include <SDL.h> #include <stdio.h> #include <memory> //Screen dimension constants const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; int isMouseEvent(SDL_Event* ev); int main(int argc, char* args[]) { SDL_Event ev; //The window we'll be rendering to SDL_Window* window = NULL; //The surface contained by the window SDL_Surface* screenSurface = NULL; //Initialize SDL if (SDL_Init(SDL_INIT_VIDEO) < 0) { printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); } else { //Create window window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if (window == NULL) { printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); } else { bool black = false; //Get window surface screenSurface = SDL_GetWindowSurface(window); SDL_SetEventFilter(isMouseEvent); bool isOn = true; while (isOn) { //Pump events SDL_PumpEvents(); SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF)); //Update the surface SDL_UpdateWindowSurface(window); } } } //Destroy window SDL_DestroyWindow(window); //Quit SDL subsystems SDL_Quit(); return 0; } int isMouseEvent(SDL_Event* ev) { if(ev!=NULL) {  if (ev->type == SDL_MOUSEMOTION)   return 0; } return 1; } 

However, when the code is ran no events are filtered, every event passed to isMouseEvent() is null. The SDL2 Wiki is very bare, and the examples on using SDL_SetEventFilter are very basic and do not work properly. Any ideas as to where I should go from here, seeing as the events are constantly null when checked from the isMouseEvent() function?

Source Link

Filtering events with SDL

According to the SDL2 wiki,

SDL_SetEventFilter : Use this function to set up a filter to process all events before they change internal state and are posted to the internal event queue.

void SDL_SetEventFilter(SDL_EventFilter filter, void* userdata)

The function prototype for filter is: int YourEventFilter(void* userdata, SDL_Event* event)

And so, my code as follows is:

`

#include <SDL.h> #include <stdio.h> #include <memory> //Screen dimension constants const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; int isMouseEvent(SDL_Event* ev); int main(int argc, char* args[]) { SDL_Event ev; //The window we'll be rendering to SDL_Window* window = NULL; //The surface contained by the window SDL_Surface* screenSurface = NULL; //Initialize SDL if (SDL_Init(SDL_INIT_VIDEO) < 0) { printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); } else { //Create window window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if (window == NULL) { printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); } else { bool black = false; //Get window surface screenSurface = SDL_GetWindowSurface(window); SDL_SetEventFilter(isMouseEvent); bool isOn = true; while (isOn) { //Pump events SDL_PumpEvents(); SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF)); //Update the surface SDL_UpdateWindowSurface(window); } } } //Destroy window SDL_DestroyWindow(window); //Quit SDL subsystems SDL_Quit(); return 0; } int isMouseEvent(SDL_Event* ev) { if (ev->type == SDL_MOUSEMOTION) return 0; return 1; } 

However, I am recieving these errors:

Error 1 error C2660: 'SDL_SetEventFilter' : function does not take 1 arguments
2 IntelliSense: argument of type "int (*)(SDL_Event *ev)" is incompatible with parameter of type "SDL_EventFilter"

Which does not make any sense to me, because 1) SDL_EventFilter takes the second argument as a void pointer, so it should be able to be omitted(This comes from my understanding at looking at every example I could find on this) and 2) The parameter is the correct type, according to the wiki.

Are there any suggestions or ideas on what I am doing incorrectly?