The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Difference between using event.type and event->type
blazed


Joined: 06 May 2015
Posts: 3
Hello everyone, I am very new to SDL and I started with SDL 2.0, but I am confused about the different ways to handle events. I learned two different ways, one by passing &event to the onEvent function and another by using the onEvent function without passing anything. The former requires the use of event->type while the latter uses event.type to handle events. Are both of them equivalent? If not, which one is better or more useful?
M-374 LX


Joined: 05 May 2015
Posts: 7
By using event.type, you refer to an event object directly. On the other hand, by using event->type, you have a pointer, which points to the location of an event object, which is not the same as the location of the pointer itself.

What is the scope of your event object? Can you show some code examples?
blazed


Joined: 06 May 2015
Posts: 3
Thanks for the reply, but which one should I be using and which one is more efficient? And I just started the tutorials so I'm afraid there is not much code to show. Although this is my header file and onEvent function which simply creates a window and draws an image to it.

class game
{
private:
bool running;
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Event event;
SDL_Texture *Track,*Car;
SDL_Rect rectTrack,rectCar;
public:
game();
int OnExecute();
bool OnInit();
bool LoadContent();
void OnEvent();
void OnLoop();
void OnRender();
void OnCleanup();
};

void game::OnEvent()
{
if (event.type == SDL_QUIT)
running = false;
if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_ESCAPE)
running = false;
}
}
M-374 LX


Joined: 05 May 2015
Posts: 7
In your case, the best idea is the OnEvent() method without the event as an argument. As the method has access to the event object, which is in the class scope, an argument is unnecessary.
blazed


Joined: 06 May 2015
Posts: 3
Thanks a lot again. Smile