How do i incoporate this countdown timer into my main? ive given it a go but it comes up a black screen..


timer:

/* clock example: countdown */
#include <stdio.h>
#include <time.h>

void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

int main ()
{
  int n;
  printf ("Starting countdown...\n");
  for (n=10; n>0; n--)
  {
    printf ("%d\n",n);
    wait (1);
  }
  printf ("FIRE!!!\n");
  return 0;

main:

//---------------- includes ----------------
#include "global.h"				
#include "SDL.h"


//---------------- 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[];
gfxSprite		spr_energy[];
gfxSprite		spr_nuclear[];



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;

struct data //Data Abstraction, each variable holds this info. 
{char name[20]; //20 characters for a name 
int score;}; //score 
void score(data[],int,data); //pass array a, how many elements in a,array b into 
//score function 

//---------------- main ----------------


int main(int argc, char *argv[]){
	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



	//---------------- init the game variables ----------------
	tileset[0].type = t_nonsolid;		tileset[0].spr = NULL;
	tileset[1].type = t_solid;			tileset[1].spr = NULL;
	tileset[2].type = t_solid;			tileset[2].spr = &spr_t[0];
	tileset[3].type = t_solid;			tileset[3].spr = &spr_t[1];
	tileset[4].type = t_nonsolid;		tileset[4].spr = &spr_t[2];
	tileset[5].type = t_sloperight;		tileset[5].spr = &spr_t[3];
	tileset[6].type = t_slopeleft;		tileset[6].spr = &spr_t[4];
	tileset[7].type = t_solid;			tileset[7].spr = &spr_t[5];
	tileset[8].type = t_solid;			tileset[8].spr = &spr_t[6];




	//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) );		//this is the framerate with vsync and the framebreak (should be equal to 1000/WAITTIME)
		else
			fps = 1111.11f;
	}

	return 0;//end program
    }
    void score(data a[],int d,data b)
    {
    data temp; //need temp data for switching
    if (b.score>a[4].score) //if b.score is bigger than a[4].score
    {a[4]=b; //make the last score of the list equal to b(score just entered)
    for (int i=d-1;i>0;i--) //now that the score is on the list, just check and sort
    if (b.score>a[i-1].score)
    {temp=a[i]; //switch - if the 5th spot is bigger than
    a[i]=a[i-1]; //the 4th spot,switch. If 4th spot is bigger
    a[i-1]=temp;} //than 3rd spot, switch. etc...
    }
    }

Dani AI

Generated

Short diagnosis: the countdown you posted uses a busy-wait loop (it ties up the CPU) so when you drop that code into your SDL program the event loop and rendering stop — that explains the black screen and why you can’t quit. is right that the timer “works” standalone, but embedding a blocking wait inside an SDL app will make the app unresponsive unless you also pump events and yield CPU time.

Two safe ways to integrate a countdown:

  1. Non‑blocking, in the main loop — track elapsed time with SDL_GetTicks() and update the displayed seconds only when one second passes. This never blocks the event loop and works with your existing render code.
/* in-game, non-blocking countdown */
const int START = 10;
Uint32 startTicks = SDL_GetTicks();
int lastShown = START;

while (!done) {
  // poll events and game logic...
  int secondsLeft = START - (int)((SDL_GetTicks() - startTicks) / 1000);
  if (secondsLeft != lastShown) {
    lastShown = secondsLeft;
    // draw secondsLeft to screen (use your font sprite)
    if (secondsLeft <= 0) { /* trigger FIRE or next state */ }
  }
  // render / SDL_Flip(screen)
  SDL_Delay(1); // small yield
}
  1. Pre-game countdown (responsive) — if you want a short countdown before entering the main loop, still pump events inside that countdown loop and call SDL_Flip so the window stays responsive:
/* pre-game responsive countdown */
int sec = 5;
Uint32 last = SDL_GetTicks();
SDL_Event e;
while (sec > 0) {
  while (SDL_PollEvent(&e)) if (e.type == SDL_QUIT) { /* handle quit */ }
  if (SDL_GetTicks() - last >= 1000) { sec--; last += 1000; }
  // draw sec and SDL_Flip(screen)
  SDL_Delay(10);
}

Quick troubleshooting tips: run the program from a console (printf may be hidden in GUI builds), add small printf/log messages before and after your timer call to find the hang, and replace any busy-wait with SDL_Delay or an SDL_GetTicks-based approach.

Recommended Answers

All 2 Replies

I don't understand? It looks like you have working timer code - so just put it wherever you want the timer to happen in the real program, right?

Dave

It is a working code but when i try to put it into my main it either plays as a black screen or the game appears with no timer and i can't quit the game.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.