• Advertisement

Archived

This topic is now archived and is closed to further replies.

SDL eats too much resources (problems with mouse solved) check the 3rd message.

This topic is 5750 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I've encountered a strange problem in SDL. I will post some simplified info on how my game works:
Uint8 buttons;
buttons = SDL_GetMouseState(NULL, NULL);

// This works, I've tested it, and it works perfectly
if (buttons == SDL_BUTTON_LEFT)
{
    PlaceABlockOnScreen();
}

// Later on, I wanted to add the feature to discard the block
// with right mouse button. I encounter problems here...
if (buttons == SDL_BUTTON_RIGHT)
{
    DiscardTheBlock();
}
   
The game wont recognize it, if I press the right mouse button. It just simply wont discard the block. I tried to change the buttons, so that the left button will discard and right will place. Okay, the left button discards the block. Good. But right mouse button wont place the damned block ! It just wont work. I tried with a different mouse, and it still wouldn't work. Does someone have a piece of code that does the thing that I want to archieve ? Please help me... this feature is for my game, and it would be good if this feature would be implemented (16 hours more to go...) [advertisement] Check out my game (it's my first game, so don't laugh at it): My homepage [/advertisement] [edited by - Pegu on May 29, 2002 4:01:55 PM] [edited by - Pegu on May 29, 2002 4:03:38 PM]

Share this post


Link to post
Share on other sites
Advertisement
This is how I handle the same thing


  
bool CInput::MouseButtonPressed(Sint16 Button)
{
if(SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(Button))
return(true); // Mouse button is down

else
return(false); // Mouse button is not down

}


with these as the mouse keys


  
#define MOUSELEFT 1
#define MOUSEMIDDLE 2
#define MOUSERIGHT 3
#define MOUSEROLLERBALLUP 4
#define MOUSEROLLERBALLDOWN 5




Ballistic Programs

Share this post


Link to post
Share on other sites
It works, it works !
Thanks man. Gamedev.net saves again.

Btw, do you have any idea, why my game (should be just a normal SDL application...) takes 100% of CPU speed ? My friend has somekind of a program that shows the current usage of system resources...

Some person also told me, that it actually slows down his other applications, which are running at the moment.

Is there any way to reduce the usage of CPU, or is it just Windows... ?

Share this post


Link to post
Share on other sites
SDL shouldn''t eat into your CPU like that, have you checked your main game loop, to make sure you arn''t flipping your buffer more tha once or somethig silly that that? Also did you compile the exe in visual c++? If so did you set it to release version?

Ballistic Programs

Share this post


Link to post
Share on other sites
quote:
Original post by Drazgal
SDL shouldn''t eat into your CPU like that, have you checked your main game loop, to make sure you arn''t flipping your buffer more tha once or somethig silly that that? Also did you compile the exe in visual c++? If so did you set it to release version?



I use Dev-C++, and it doesn''t generate debug info, or anything like that. I will check the flippings, thanks for the idea

Will post results soon.

Share this post


Link to post
Share on other sites
The "100% CPU Usage" thing is quite common (at least in Windows). Put a delay at the end of your game loop for the smallest amount of time that''s possible. SDL_Delay only goes down to milliseconds, so that''d be 1ms.

Share this post


Link to post
Share on other sites
quote:
Original post by Pegu
I use Dev-C++, and it doesn''t generate debug info, or anything like that.

It can, but it doesn''t by default. I don''t know if you get the debug tools with Dev C++ or if you need the entire Cygwin environment, but if you compile with -ggdb (in GCC, which is what Dev C++ uses) you can use gdb to debug the applications. There''s a graphical version of gdb called xxgdb for X11, but I don''t know if you can get a graphical version that runs in Windows.

Share this post


Link to post
Share on other sites
There is a graphical debugger for dev C++ in windows, I don''t know what it''s called but you can download it at the dev C++ site (or the resource site)

go here

Share this post


Link to post
Share on other sites
Hmm... it still uses too much CPU...
Basically using the SDL_Delay is limiting the FPS, right ? So I perhaps should make a thing, that limits the FPS to 30 or something...

This is a puzzlegame, so even FPS of 10 would be good

Share this post


Link to post
Share on other sites
quote:
Original post by Pegu
Basically using the SDL_Delay is limiting the FPS, right?

Yes and no. We''re only doing it so that other processes have more chances to get their turn on the CPU. You may want to do this instead:

#ifdef _WIN32
Sleep(0);
#endif

I''m very sure that should work (SDL_Delay may be using the performance timer, which wouldn''t help much).
quote:
Original post by Pegu
So I perhaps should make a thing, that limits the FPS to 30 or something...

There''s _no_ reason to do that.

Share this post


Link to post
Share on other sites
Damn... I replaced my FPS limiter with the code you gave, and it took 100% that time too. When I use the FPS limiter, I can make the program eat less CPU time...

Grr... I think that there is something horribly wrong with my code... Can you tell me the most potential things, that can use too much CPU (with SDL (events, flipping buffers, and other)).

In the game I just use SDL to grab input and draw graphics...

Share this post


Link to post
Share on other sites
I''ve had some trouble with input in SDL too:

if I hold a button, then moving the mouse seems to go much slower! This means, circle-strafing is quite hard, and that''s NOT what a hardcore gamer wants. This has been tested in a raytracing engine I made, NOT in a fast 3D engine, but it''s still weird that I can look around with the mouse very fast when I''m not holding a key, and when I hold ANY key, even a key I don''t use, mouse movement goes slower.

Share this post


Link to post
Share on other sites
Firstly, regarding your input problem, you already have the answer but you should check the SDL docs, as they give plenty of good examples on how to handle things. Use the online docs where possible as they are more up to date and helpful than the ones distributed with the development kit itself.

Regarding CPU time, there is a common misconception that a program will not use all your CPU time if it is ''simple''. The fact is, unless your program explicitly returns control to the OS, it will chew up every CPU cycle it is given, whether it is Quake 3 or just int main { while (true) ; } . Furthermore, most full-screen applications (ie. games) are expected to be played to the exclusion of nearly all other tasks anyway, so it''s not a big deal. So long as they give the Windows message queue a chance to operate, they''ll be fine.

Now, if you still think it''s a problem, you probably need to do one of two things:

1) Move to an event based model. This means your game sits totally idle until it receives a message, such as a mouse movement, a timer, a keypress, or whatever. Standard Windows apps tend to work this way. This is well suited to puzzle games and the like, but will probably require a change to your game''s design. (And I doubt you''d have time for that.)

2) Explicitly relinquish control every so often in case it is needed. This is what the Sleep(0) suggestion was for below. It doesn''t necessarily reduce the CPU usage below 100%, but it does give Windows an opportunity to execute anything else that needs to run. And if Windows doesn''t have anything else it needs to run, then it doesn''t matter if you''re using 100% CPU. If you search the SDL mailing list archives there was a discussion about Sleep(0) and the alternatives for other platforms a month or two back. You may be able to do it with SDL_Delay(0) but I dunno if they made the required alterations for that to work across all platforms in the same way.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Lode: I had a similar problem, and instead of the way these posts have handled input, I was doing it like windows and used an event loop, my problem was that an event was generated anytime anything happened, including moving the mouse, so I changed it so that only mouse button events were recognized and mouse movements were ignored, hope it helps,

Share this post


Link to post
Share on other sites
quote:
Original post by Kylotan
1) Move to an event based model. This means your game sits totally idle until it receives a message, such as a mouse movement, a timer, a keypress, or whatever. Standard Windows apps tend to work this way. This is well suited to puzzle games and the like, but will probably require a change to your game''s design. (And I doubt you''d have time for that.)


So basicially I should make a while-loop, which will then proceed onwards when a mousebutton is pressed ? So it would look something like this:

while(done == 0)
{
if(MouseWasClicked())
{
PlaceANewBlock();
SDL_Flip(screen);
}
if(EscWasPressed())
{
CleanThignsFromMemory();
done = 1;
}
}


Just want to make sure if this is any good, because I may have the time to change the whole thing, but I am not able to do so many times.

*Goes and looks for SDL docs*

Share this post


Link to post
Share on other sites
Weehaa !
Would SDL_WaitEvent(&mainevent); be good ?
Because it drops the CPU usage, I have decided, that it is a good thingie.

Share this post


Link to post
Share on other sites
One thing I don''t think was covered, is that the "buttons" is a bitmask field, meaning it can have multiple values inside it. So instead of doing:

if (buttons == SDL_BUTTON_LEFT)

you would do:

if (buttons & SDL_BUTTON_LEFT)


This way if BOTH buttons are held down, you would process both of them.

Also, I use SDL_Delay(1) inside my programs, and that works well to allow Windows to do what it needs to (especially since my current game is in a window, not fullscreen)

Share this post


Link to post
Share on other sites
Yes Pegu, the SDL_WaitEvent is exactly the sort of thing I meant. That tells the program to sleep until it gets a new event to process, which you can then act upon.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]

Share this post


Link to post
Share on other sites

  • Advertisement