can anyone tell me why the timer doesn't work in my code? The game runs good but there is no timer?

//---------------- includes ----------------
#include "global.h"               
#include "SDL.h"
#include <string>
#include <cstdlib>
#include <windows.h>
#include <mmsystem.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <conio.h>


#pragma comment(lib, "winmm.lib")
using namespace std;


//---------------- global variables ----------------
//"engine"
Uint8            *keystates;
SDL_Surface        *screen;        //the screen (main sdl surface which is visible on the monitor)

//sprites
gfxSprite        spr_t[7];
gfxSprite        spr_player[2];
gfxSprite        spr_player2[2];

gfxSprite        spr_cross[1];
gfxSprite        spr_energy[2];
gfxSprite        spr_nuclear[3];



gfxSprite        spr_background;

gfxFont            font;

//game objects

CPlayer            player;
CPlayer            player2;

CMap            map;

CTile            tileset[9];


//scroll offset
int scroll_x = 0;
int scroll_y = 0;


void wait ( int sec)
{
clock_t end_wait;
end_wait = clock () + sec * CLK_TCK;
while (clock() <end_wait){}
}
//---------------- main ----------------


int main(int argc, char *argv[]){
       int hours, seconds, s;
      time_t t;
      struct tm *tm;
     
      tm = localtime(&t);
      printf("Enter the number of seconds: ");
      scanf("%d", &seconds);
      s = seconds;
    
      while(seconds > 0){
     
      tm->tm_sec = s;
      tm->tm_hour = s/3600;
      tm->tm_min = s/60;
     
      mktime(tm);

      printf("%02d:%02d:%02d\r", tm->tm_hour, tm->tm_min, tm->tm_sec);
      s--;
      seconds--;
      wait(1);
  
      }
   
   
    getch();


    PlaySound(L"C:\\Documents and Settings\\Louisa\\My Documents\\Visual Studio 2010\\Projects\\Ninja_Wars\\Ninja_Wars\\Music\\Game music.wav",NULL,SND_FILENAME|SND_ASYNC | SND_LOOP |SND_NODEFAULT);

  Sint32 delta;


    unsigned int    framestart;
    float            fps = 0, framefps = 0;
    int                divisor;
    bool            done;
    SDL_Event        event;


    //---------------- init the "engine" ----------------
    //initialize SDL
    gfx_init(640,480, false);
    //get keystate array
    keystates = SDL_GetKeyState(0);


    //---------------- load resources (graphics) ----------------
    spr_t[0].init("gfx/t1.bmp");                    //tile graphics
    spr_t[1].init("gfx/t2.bmp");
    spr_t[2].init("gfx/t3.bmp", 255, 0, 255);
    spr_t[3].init("gfx/tsloper.bmp", 255, 0, 255);
    spr_t[4].init("gfx/tslopel.bmp", 255, 0, 255);
    spr_t[5].init("gfx/t6.bmp", 255, 0, 255);
    spr_t[6].init("gfx/t7.bmp", 255, 0, 255);
   

    spr_player[0].init("gfx/left2.bmp", 255,0,255);    //player graphics
    spr_player[1].init("gfx/right2.bmp", 255,0,255);

    spr_player2[0].init("gfx/Rleft.bmp", 255,0,255);    //player graphics
    spr_player2[1].init("gfx/Rright.bmp", 255,0,255);

   

    spr_background.init("gfx/bg.bmp");                //background

    font.init("gfx/font0.bmp");                        //font





    //initialize the map, the player has already been initialized by its constructor
    map.loadMap("maps/map01.map");


   
    printf("\nhere comes the game loop...\n") ;
    done = false;

   



    //---------------- game loop ----------------
    while (!done){
        framestart = SDL_GetTicks();

        //handle messages
        while(SDL_PollEvent(&event)){
            switch(event.type){
                case SDL_QUIT:
                    done = true;
                break;

                default:
                break;
            }
        }

        if(keystates[SDLK_ESCAPE])        //quit?
            done = true;




        //---------------- update objects (game logic) ----------------
        player.think();
        player2.think2();


        //---------------- draw everything (render the scene) ----------------
       
        int sdx = (scroll_x%spr_background.getWidth());
        int sdy = (scroll_y%spr_background.getHeight());
        spr_background.draw(-sdx, - sdy);
        spr_background.draw(spr_background.getWidth() - sdx, -sdy);
        spr_background.draw(- sdx, spr_background.getHeight()-sdy);
        spr_background.draw(spr_background.getWidth() - sdx, spr_background.getHeight()-sdy);

       
        map.draw();

        player.draw();
        player2.draw();

       
        //the info text
        //font.drawf(0,0, "fps: frame/real/lock: %.1f/%.1f/%.1f", framefps, fps, (float)(1000 / WAITTIME));


        //---------------- that's it, now flip the buffers and keep the framerate constant ----------------

        divisor = SDL_GetTicks()-framestart;
        if(divisor != 0)
            framefps = (float)( 1000 / divisor );    //this is the framerate without vsync and the frame break
        else
            fps = 1111.11f;


        SDL_Flip(screen);    //double buffering -> flip buffers, also waits for vsync

        while((SDL_GetTicks()-framestart) < WAITTIME);    //framebreak - keep framerate constant at 1000/WAITTIME fps
       
        divisor = SDL_GetTicks()-framestart;
        if(divisor != 0)
            fps = (float)( 1000 / (SDL_GetTicks()-framestart) );       
        else
            fps = 1111.11f;
    }




    return 0;//end program
    }

Did you try a simpler program (still using SDL)? Try to get a < 20 line program where the timer doesn't work.

Dave

This article has been dead for over six months. Start a new discussion instead.