Example #1
0
int main(int argc, char* argv[])
{
   // Initiate random numbers
   srand(time(NULL));

   int angleDeg = rand() % 360;
   float angleRad = (float) angleDeg * 2.0 * 3.14159 / 360.0;
   printf("Initial angle = %f\n", angleRad);

   XYPair pos = { (SCREEN_WIDTH - 100) / 2, (SCREEN_HEIGHT - 100) / 2 };
   XYPair vel = GetUnitVector(angleRad);
   vel = ScaleVector(vel, 200.0 / (float) FRAMERATE);
   double angle = 0;


   SDL_Window* window = NULL;
   SDL_Renderer* renderer = NULL;

   if (SDL_Init(SDL_INIT_VIDEO) < 0)
   {
      printf("SDL initialization error: %s\n", SDL_GetError());
      return -1;
   }

   SDL_LogSetAllPriority(SDL_LOG_PRIORITY_WARN);
   printf("Main\n");

   window = SDL_CreateWindow("Bouncing Ball", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
   if (window == NULL)
   {
      printf("Error creating window: %s\n", SDL_GetError());
      return -1;
   }

   renderer = SDL_CreateRenderer(window, -1, 0);
   if (renderer == NULL)
   {
      printf("Failed to create the renderer: %s\n", SDL_GetError());
      return -1;
   }

   SDL_SetRenderDrawColor(renderer, 255,255,255,255);

   // Load textures
   IMG_Init(IMG_INIT_PNG|IMG_INIT_JPG);
   SDL_Texture* ball = LoadTexture("bb.png", renderer);
   SDL_Texture* bg = LoadTexture("bg.jpg", renderer);
   IMG_Quit();

   if ( (ball == NULL) || (bg == NULL) )
   {
      printf("Error loading the textures\n");
      return -1;
   }

   printf("Images loaded OK\n");

   bool quit = false;
   while (!quit)
   {

      Uint32 startTick = SDL_GetTicks();

      SDL_Rect ballPos;
      ballPos.h = 100;
      ballPos.w = 100;
      ballPos.x = pos.x;
      ballPos.y = pos.y;

      SDL_RenderClear(renderer);
      SDL_RenderCopy(renderer, bg, NULL, NULL);
      SDL_RenderCopyEx(renderer, ball, NULL, &ballPos, angle, NULL, SDL_FLIP_NONE);
      SDL_RenderPresent(renderer);

      UpdatePosition(&pos, &vel, &angle, SCREEN_WIDTH - 100, SCREEN_HEIGHT - 100);

      Uint32 stopTick = SDL_GetTicks();
      Uint32 processingTime = (stopTick - startTick);

      if ( (stopTick - startTick) < (1000 / FRAMERATE) )
      {
           printf("Delaying %u ticks\n", 1000 / FRAMERATE - processingTime);
           SDL_Delay(1000 / FRAMERATE - processingTime);
      }

      SDL_Event e;
      while(SDL_PollEvent(&e) != 0)
      {
         if (e.type == SDL_QUIT)

         {
            printf("User quitting\n");
            quit = true;
         }
      }

   }


   SDL_DestroyTexture(ball);
   SDL_DestroyTexture(bg);

   SDL_DestroyRenderer(renderer);

   SDL_DestroyWindow(window);

   SDL_Quit();

   return 0;
}
Example #2
0
int
main( int argc, char *argv[ ] )
{
#ifndef _OPENMP
	fprintf( stderr, "OpenMP is not available\n" );
	return 1;
#endif

	omp_set_num_threads( NUMT );
	int numProcessors = omp_get_num_procs( );
	fprintf( stderr, "Have %d processors.\n", numProcessors );


	for ( int i = 0; i < NUMBODIES; i++ )
	{
		Bodies[i].mass = EARTH_MASS  * Ranf( 0.5f, 10.f );
		Bodies[i].x = EARTH_DIAMETER * Ranf( -100.f, 100.f );
		Bodies[i].y = EARTH_DIAMETER * Ranf( -100.f, 100.f );
		Bodies[i].z = EARTH_DIAMETER * Ranf( -100.f, 100.f );
		Bodies[i].vx = Ranf( -100.f, 100.f );;
		Bodies[i].vy = Ranf( -100.f, 100.f );;
		Bodies[i].vz = Ranf( -100.f, 100.f );;
	};

	double time0 = omp_get_wtime( );

	for ( int t = 0; t < NUMSTEPS; t++ )
	{
		// #pragma omp parallel for schedule(dynamic)
		for ( int i = 0; i < NUMBODIES; i++ )
		{
			float fx = 0.;
			float fy = 0.;
			float fz = 0.;
			Body *bi = &Bodies[i];
			#pragma omp parallel for reduction(+:fx,fy,fz) schedule(dynamic)
			for ( int j = 0; j < NUMBODIES; j++ )
			{
				if ( j == i )	continue;

				Body *bj = &Bodies[j];

				float rsqd = GetDistanceSquared( bi, bj );
				if ( rsqd > 0. )
				{
					float f = G * bi->mass * bj->mass / rsqd;
					float ux, uy, uz;
					GetUnitVector( bi, bj,   &ux, &uy, &uz );
					fx += f * ux;
					fy += f * uy;
					fz += f * uz;
				}
			}

			float ax = fx / Bodies[i].mass;
			float ay = fy / Bodies[i].mass;
			float az = fz / Bodies[i].mass;

			Bodies[i].xnew = Bodies[i].x + Bodies[i].vx * TIMESTEP + 0.5 * ax * TIMESTEP * TIMESTEP;
			Bodies[i].ynew = Bodies[i].y + Bodies[i].vy * TIMESTEP + 0.5 * ay * TIMESTEP * TIMESTEP;
			Bodies[i].znew = Bodies[i].z + Bodies[i].vz * TIMESTEP + 0.5 * az * TIMESTEP * TIMESTEP;

			Bodies[i].vxnew = Bodies[i].vx + ax * TIMESTEP;
			Bodies[i].vynew = Bodies[i].vy + ay * TIMESTEP;
			Bodies[i].vznew = Bodies[i].vz + az * TIMESTEP;
		}

		// setup the state for the next animation step:

		for ( int i = 0; i < NUMBODIES; i++ )
		{
			Bodies[i].x = Bodies[i].xnew;
			Bodies[i].y = Bodies[i].ynew;
			Bodies[i].z = Bodies[i].znew;
			Bodies[i].vx = Bodies[i].vxnew;
			Bodies[i].vy = Bodies[i].vynew;
			Bodies[i].vz = Bodies[i].vznew;
		}


	}  // t

	double time1 = omp_get_wtime( );

	// print performance here:::
	double mbodies = (float)(NUMBODIES * NUMBODIES * NUMSTEPS) / (time1 - time0) / 1000000.;
	printf("Performance = %8.2lf MegaBodies/Sec\n", mbodies);
	printf("Time = %8.2lf MegaSecs\n", (time1 - time0) * 1000000.);
	return 0;
}