void Framework::loop()
{
	/* main loop variable */
	bool done = false;
	/* used to collect events */
	SDL_Event event;

	/* wait for events */
	while (!done)
	{
		/* handle the events in the queue */
		while (SDL_PollEvent(&event))
		{
			switch(event.type)
			{
			case SDL_MOUSEMOTION:
				/* give away mouse movement with buttons pressed */
				handleMouseMove(event.motion.xrel, event.motion.yrel, event.motion.state);
				break;
			case SDL_MOUSEBUTTONUP:
				/* handle mouse button release for serving */
				if (event.button.button == SDL_BUTTON_LEFT)
					if (!paused) serveBall();
			case SDL_KEYDOWN:
				/* handle key presses */
				handleKeyPress(&event.key.keysym);
				break;
			case SDL_QUIT:
				/* handle quit requests */
				done = true;
				break;
			case SDL_USEREVENT:
				switch (event.user.code)
				{
				case NET2_EXTERNAL:
					if (((TimerData*)event.user.data1)->timer == NULL)
					{
						/* this means our timer has gone inactive and we are pleased to stop our work! */
					} else {
						((TimerData*)event.user.data1)->receiver->action(((TimerData*)event.user.data1)->event);
					}
					break;
				case NET2_ERROREVENT:
					printf("Error: %s(%d)\n", NET2_GetEventError(&event), NET2_GetSocket(&event));
					break;
				case NET2_UDPRECEIVEEVENT:
					UDPpacket *p = NULL;
					p = NET2_UDPRead(NET2_GetSocket(&event));
					while (p != NULL) // if we get NULL we are done
					{
						Peer* peer = getCreatePeer(p->address.host);
						receivePacket(peer, (char*)p->data, p->len);
						NET2_UDPFreePacket(p);
						p = NET2_UDPRead(NET2_GetSocket(&event));
					}
					break;
				}
			}
		}

		int tdiff = SDL_GetTicks() - lasttime;
		if (tdiff > timeunit) {
			frames++;
			xdiff += tdiff; // always greater 0 because we decided to let tdiff be greater than timeunit
			if ((xdiff >= 100)&&(xdiff >= timeunit * 20)) {
				output.updateFPS(frames * 1000.0 / xdiff); // There are 1000 ticks / second
				frames = 0;
				xdiff = 0;
			}
			lasttime += tdiff;

			// Game status code
			updateGame(tdiff);

			// Rendering code
			drawScene();
		}
	}
}
Beispiel #2
0
int main(int argc, char **argv)
{
  SDL_Event ev;
  char welcome[] = "Test de connexion PerudOnLan";
  char buf[1024];
  int len = 0;
  int count = 0;
  int s = -1;
  int next[0xffff];
  int n = -1;
  IPaddress *monIP = NULL;

  memset(next, 0, sizeof(next));

  mySDLInitOrQuit(SDL_INIT_EVENTTHREAD |
                  SDL_INIT_VIDEO |
                  SDL_INIT_NOPARACHUTE);

  if((NET2_ResolveHost(monIP,"localhost",6666))!=0 || monIP==NULL)
    {
      fprintf(stderr,"errreur à la récupération de l'IP");
      return EXIT_FAILURE;
    }

  NET2_TCPAcceptOn(monIP->port);
  while (FE_WaitEvent(&ev))
  {
    switch (ev.type)
    {
    case SDL_USEREVENT:
      switch (NET2_GetEventType(&ev))
      {
      case NET2_TCPACCEPTEVENT:
        count++;
        printf("accept(%d)\n", NET2_GetSocket(&ev));
        s = NET2_GetSocket(&ev);
        if (-1 == NET2_TCPSend(s, welcome, sizeof(welcome) - 1))
        {
          printf("WELCOME SEND FAILED\n");
        }
        next[s] = 0;
        break;

      case NET2_TCPRECEIVEEVENT:

        s = NET2_GetSocket(&ev);
        while (0 != (len = NET2_TCPRead(s, buf, sizeof(buf))))
        {
          int i;

          n = next[s];
          for (i = 0; i < len; i++)
          {
            if (welcome[n] != buf[i])
            {
              printf("\nout of sync %c != %c\n", welcome[n], buf[i]);
              exit(0);
            }
            n = (n + 1) % (sizeof(welcome) - 1);
          }
          next[s] = n;

          if (-1 == NET2_TCPSend(s, buf, len))
          {
            printf("RECIEVE SEND FAILED\n");
          }
        }
        break;

      case NET2_TCPCLOSEEVENT:
        printf("close(%d)\n", NET2_GetSocket(&ev));
	NET2_TCPClose(NET2_GetSocket(&ev));
        count--;
        /*if (0 >= count)
        {
          exit(0);
        }
	*/
        break;
      case NET2_ERROREVENT:
        printf("Error: %s(%d)\n", NET2_GetEventError(&ev), NET2_GetSocket(&ev));
        exit(0);
        break;
      }
      break;

    case SDL_QUIT:
      mySDL_Quit();
      exit(0);
      break;
    }
  }

  mySDL_Quit();
}
Beispiel #3
0
int main(int argc, char **argv)
{
  SDL_Event ev;                              // an SDL event
  char buf[1024];                            // a message buffer
  int len = 0;                               // some counters
  int count = 0;
  int socks = 0;                             // the number of
                                             // connected sockets,
                                             // which is also the
                                             // number of connected
                                             // clients.

  mySDLInitOrQuit(SDL_INIT_EVENTTHREAD |     // initialize SDL. We need
                  SDL_INIT_VIDEO |           // events and it doesn't
                  SDL_INIT_NOPARACHUTE);     // work without video

// This is where we tell NET2 to start accepting TCP/IP connections on
// port 6666. This call does not wait for a connection to come in. It
// tells NET2 to do the waiting and it returns immediately. After this
// call returns your program will get an event any time a computer
// trys to connect to the port given in this call.

  NET2_TCPAcceptOn(6666);

  while (FE_WaitEvent(&ev))                  // wait for events
  {
    //printSDLEvent(&ev);

    switch (ev.type)
    {

// all NE2 events are SDL_USEREVENTs but not all user events are NET2
// events. If you use user events in your code you need to be careful
// to make sure you are not using the same event codes that NET2 uses.

    case SDL_USEREVENT:
      switch(NET2_GetEventType(&ev))
      {

// This next piece of code handles an accept event. This event tells
// us that an connection has been accepted. Here, all we do is count
// it. You would normally take some other action.

      case NET2_TCPACCEPTEVENT:
        printf("accept(%d)\n", NET2_GetSocket(&ev));
        printNET2Event(&ev);
        socks++;
        break;

        // This next piece of code is for handling receive
        // events. This kind of event tells us we have input waiting
        // on a socket. You need to grab all of it. No that we get the
        // socket from the event. You can use the socket to tell you
        // which user sent the information to you.

      case NET2_TCPRECEIVEEVENT:
        while (0 != (len = NET2_TCPRead(NET2_GetSocket(&ev), buf, sizeof(buf))))
        {
          count += len;
        }
        break;

        // If an error occurs on a socket or the other computer closes
        // the connection you will get a close event. When you get a
        // close event you must close the socket. Use the socket in
        // the event to tell you which connection went away.

      case NET2_TCPCLOSEEVENT:
        printf("close(%d)\n", NET2_GetSocket(&ev));
        printNET2Event(&ev);
        NET2_TCPClose(NET2_GetSocket(&ev));  // close the socket

        printf("count=%d\n", count); fflush(NULL);
        socks--;
        if (0 >= socks)
        {
          //exit(0);
        }
        //count = 0;
        break;

        // Sometimes you will get errors. It is best to keep track of
        // them and try to figure out what is causing them.

      case NET2_ERROREVENT:
        printf("Error: %s(%d)\n", NET2_GetEventError(&ev), NET2_GetSocket(&ev));
        printNET2Event(&ev);
        break;
      }
      break;

    case SDL_QUIT:                           // time to quit
      mySDL_Quit();                          // clean up and exit
      exit(0);
      break;
    }
  }

  mySDL_Quit();
}