Jump to content
  • Advertisement
Sign in to follow this  
napacan

Sleep() problem

This topic is 4215 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, in my application I have several meshes that i show them one after another to get an animation. When I do this using keyboard, (by pressing '.' several times) it shows the animation but when I try to automate this by using such a code: for(int i = 0; i < 10; i ++) { Sleep( 500 ); key('.'); post_redraw(); } it waits freezed for like 5 secs and then shows the last mesh. What can I do to correct this?

Share this post


Link to post
Share on other sites
Advertisement
5 seconds? Double check your code to make certain that you're calling Sleep(500) and not Sleep(5000).

Share this post


Link to post
Share on other sites
Does post_redraw( ) actually cause all the drawing code to be called, or just alert the drawing manager that it could use a redraw? I suspect the latter. A fix for this is to use a proper timer, rather than relying on Sleep. This way, your update loop can keep updating, and the mesh will change/update when it needs to (when the value of current_time >= next_mesh_time).

Share this post


Link to post
Share on other sites
I tried checking current time in a while() loop but it loops so many times that the program gets into not responding state. Just a call to key('.'); updates the mesh successfully but i need to do it after doing nothing for a while, like 0.5s. The Sleep function waits but it doesn't update mesh when I call key('.'); after sleep. Btw thanks for the answers.

Share this post


Link to post
Share on other sites
Quote:
Original post by napacan
I tried checking current time in a while() loop but it loops so many times that the program gets into not responding state. Just a call to key('.'); updates the mesh successfully but i need to do it after doing nothing for a while, like 0.5s. The Sleep function waits but it doesn't update mesh when I call key('.'); after sleep. Btw thanks for the answers.

Simply check the time whenever you draw your mesh. If the correct time hasn't passed, don't do anything; if it has, update your mesh before you draw it. It shouldn't block anything else from happening.

If you still can't get it, toss some code up and I can point to where it should go.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by napacan
I tried checking current time in a while() loop but it loops so many times that the program gets into not responding state.



Sounds like you need to use other input methods than 'key', which sounds like it sits and waits for a keyboard character....

look into Polling and Interrupts if you don't know what they are...

more details needed on what you mean by 'not responding'

Share this post


Link to post
Share on other sites
here's the code for play:


void Pane::play() {
_ftime( &timebuffer );

double initialTime = (double)timebuffer.time + (double)timebuffer.millitm*0.001;
for(int i = 1; i <= 10; i ++)
{
Sleep(500);
currentTime = (double)timebuffer.time + (double)timebuffer.millitm*0.001;
if (currentTime > initialTime + (0.5*i))
{
key('.');
}

}
}



key('.'); does the following:


if (currentFrame == (numFrames - 1))
{
currentFrame = 0;
}
else
{currentFrame++;}
switchToFrame(currentFrame);



switchToFrame just changes the pointers to the current model to next model

i used sleep to not to do anything but it's not showing the model. the class Pane inherits from CFrameWnd and has some functions like:

private:
HGLRC gl_context;
int pixel_format;
CDC *active_dc;

bool set_pixel_format(HDC);
bool create_glcontext(HDC);

void handle_mouse(int kind, UINT flags, CPoint point);
void do_redraw(CDC& dc);

protected: // MFC virtual function overrides
BOOL PreCreateWindow(CREATESTRUCT& cs);

protected: // MFC event handlers
afx_msg void OnPaint();
afx_msg BOOL OnEraseBkgnd(CDC *);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnDestroy();
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnLButtonDown(UINT flags, CPoint point);
afx_msg void OnLButtonUp(UINT flags, CPoint point);
afx_msg void OnRButtonDown(UINT flags, CPoint point);
afx_msg void OnRButtonUp(UINT flags, CPoint point);
afx_msg void OnMButtonDown(UINT flags, CPoint point);
afx_msg void OnMButtonUp(UINT flags, CPoint point);
afx_msg void OnMouseMove(UINT flags, CPoint point);
afx_msg void OnChar(UINT ch, UINT repcount, UINT flags);
afx_msg void OnTimer(UINT timer_id);
DECLARE_MESSAGE_MAP ()

public:
void create(char *name);
inline void make_current(HDC dc) { wglMakeCurrent(dc, gl_context); }
inline void finish(HDC dc) { SwapBuffers(dc); }
void get_canvas_geometry(int *x, int *y, int *w, int *h);
void draw_string(int x, int y, const char *str);

void snapshot_ppm(const char *name=NULL);
void snapshot_tiff(const char *name=NULL);

virtual void gl_init();
virtual void begin_redraw();
virtual void end_redraw();

void post_redraw();
void require_redraw();
virtual void redraw();
virtual void illustrate();
virtual void key(char);
virtual void button(int kind, int which, int x, int y);
virtual void size(int width, int height);

unsigned int add_timer(unsigned int id, unsigned int millisecs);
bool remove_timer(unsigned int id);
virtual void timer(unsigned int);

Share this post


Link to post
Share on other sites
Actually key('.') is called when . key is pressed. Any character is sent to key()and if the character is . then it draws the next mesh. It's listening for input all the time and does something if the input has some corresponding code in the
key(char c)
function.

By not responding I mean it crashes, i don't know it stops responding.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!