Updating Screen Question

Started by
4 comments, last by Decrius 15 years, 10 months ago

if ( mEvent.type == SDL_MOUSEMOTION )
			{
				SDL_GetMouseState( &x, &y );

				if ( ( x > imageRect[ 0 ].x ) && ( x < imageRect[ 0 ].x + imageRect[ 0 ].w ) 
				  && ( y > imageRect[ 0 ].y ) && ( y < imageRect[ 0 ].y + imageRect[ 0 ].h ) )
				{
					menu.BlitMouseOverButton( SBBLUE );
					menu.DisplayMenu();
				}

				else if ( ( x > imageRect[ 1 ].x ) && ( x < imageRect[ 1 ].x + imageRect[ 1 ].w ) 
				       && ( y > imageRect[ 1 ].y ) && ( y < imageRect[ 1 ].y + imageRect[ 1 ].h ) )
				{
					menu.BlitMouseOverButton( ABYELLOW );
				}
				
				else if ( ( x > imageRect[ 2 ].x ) && ( x < imageRect[ 2 ].x + imageRect[ 2 ].w ) 
				       && ( y > imageRect[ 2 ].y ) && ( y < imageRect[ 2 ].y + imageRect[ 2 ].h ) )
				{
					menu.BlitMouseOverButton( QBRED );
				}
				else
					menu.DisplayMenu();
			}





In the code above, when the mouse is continually over the button, it continually blits the button. When the mouse is off the button it continuously blits the menu. Is this bad? I'm guessing it is because in the background it's making function calls repeatedly non-stop. I thought I read somewhere that the screen should only be updated when something changes. Even though my mouse isn't moving, I know that it is continuously blitting the images. [Edited by - ICUP on June 10, 2008 4:19:30 AM]
Advertisement
SDL_MOUSEMOTION will only occur if the mouse moves. So everything is only blitted when the mouse is being moved.

You can limit the drawings like this:

When a mouse goes in or out (so not when it's just ON the button, it must make a transition between being ON and OFF the button), you update the button.
And I don't think you need to update the menu. Is the menu drawing the buttons too? And has the menu no changing things at all? Don't redraw it when a SDL_MOUSEMOTION occurs.

To make it more easy, I always update the screen whenever an events occurs. And in games it actually changes every frame, so checking if things have changed is overhead in that case, and it should update it just every frame. For GUI's you can limit it yes.

Games like Call of Duty 4 put a maximal FPS of 20 or 30 on menu's. They don't even check if things have changed. And HUD (GUI ingame) needs to be drawn on top every frame, so that has to be updated aswell.
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
See, here's the weird thing though. Those if statements are to be tested ONLY if the mouse moves. The thing is, the mouse ISN'T moving. I can have the mouse be still and I know that it is still testing those statements. I know because when I move my mouse over the button, it flashes the two different colors (one for mouse over, and one for when it isn't. To test this I placed the DisplayMenu function after it blits one of the buttons) even if the mouse isn't moving.

I wonder if this has anything to do with the mouse itself? Are optical mouses always moving? Those red lights searching for movement?
I think I got it. I switched from SDL_PollEvent to SDL_WaitEvent. I guess PollEvent is always looking for something to do.
Can someone give me a method in pseudocode or code where to test whether the mouse is ON and OFF the button?
SDL_Event event;    while (SDL_PollEvent(&event))    {        switch (event.type)        {            case SDL_MOUSEMOTION:            {                break; // mouse moves            }            case SDL_MOUSEBUTTONDOWN:            {                break; // mouse button clicked            }        }    }// and to see if something is inside a box:    struct Field    {        int x, y, w, h;        Field() : x(0), y(0), w(0), h(0) {}        Field(int x, int y, int w, int h) : x(x), y(y), w(w), h(h) {}    };    bool in_field(int x, int y)    {        if (x >= field.x && (!field.w || x < (field.x + field.w)) && y >= field.y && (!field.h || y < (field.y + field.h)))        {            return true;        }        return false;    }
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora

This topic is closed to new replies.

Advertisement