I have a few questions, that are concerning code I wrote using SDL as the base graphics framwork.

Which of these are better-written code? By better written, I mean they are more reliable, better when working with threads, etc.

Here is an excerpt from TaNF that displays the Dalan Polytechnic logo, and then fades it out. Here is the version I have now:


Code:
blackout(screen);
    SDL_BlitSurface(DalanTechLogo, rect(0, 0, 480, 48), screen, coord(144, 232));
    updatescreen();
    pausems(2500);
    blackout(screen);
    SDL_BlitSurface(DalanTechLogo, rect(0, 48, 480, 48), screen, coord(144, 232));
    updatescreen();
    pausems(500);
    blackout(screen);
    updatescreen();
    pausems(500);
    blackout(screen);


Here is what I'm thinking of making it to be something like this:


Code:
Uint32 timer = SDL_GetTicks();
while(SDL_GetTicks() - timer < 4000) {
  blackout(screen);
  if(SDL_GetTicks() - timer < 2500) {
    SDL_BlitSurface(DalanTechLogo, rect(0, 0, 480, 48), screen, coord(144, 232));
  } else if (SDL_GetTicks() - timer < 3000) {
    SDL_BlitSurface(DalanTechLogo, rect(0, 48, 480, 48), screen, coord(144, 232));
  }
  SDL_PollEvent(event);
  switch(event->type) {
    case EXIT:
      clean_exit();
    default:
      break;
  }
  updatescreen();
}


The second seems to be better because it polls events, and as benryves told me, only if applications poll events are they considered responsive by Windows (in most cases, at least). It also may reduce or completely get rid of my need for the pausems() function, and for Music, to change from a loop_start to loop_loop segment, instead of using threads to handle the automatic change, I can set up a reliable SDL_MusicFinishedHook compatible function that won't be stalled by waiting through something as long as a pausems() call. However, I'm not sure if this is a good coding practice, doing the second example above. Is it? Or was former idea better? What are the pros and cons of both? (I'm not completely sure which would have the more cons as compared to pros).



EDIT: after some testing, I found that the second is better; so, end of that questioning line, I guess. New question:

Would this modified pausems routine perhaps be a good alternative? Will it affect and SDL Music Hooks being activated during the waiting period?


Code:
void pausems(int ms) {
    SDL_Event*event = malloc(sizeof(SDL_Event));
    int timer = SDL_GetTicks();
    while(SDL_GetTicks() - timer < ms) {
        SDL_PollEvent(event);
        if(event->type == SDL_QUIT) {
            quit_quickly();
        }
    }

}
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement