Создание приложений с использованием многопоточности при обработки запросов клиентов, страница 2

                                 key = event.key.keysym.sym;

                                 if (key == SDLK_UP||key==SDLK_DOWN||key==SDLK_LEFT||key==SDLK_RIGHT||key==SDLK_SPACE)

                                 {

                                       char* code = new char[5];

                                       sprintf(code, "%d", key);

                                       send(clientSocket, code, sizeof(code), 0);

                                       //Sleep(70);

                                 }

                }

                          break;

                    }

        }

             //

        SDL_Flip(screen);

    }

    return 0;

}

void ProcessString()

{

       WaitForSingleObject(semaphore,INFINITE);

       int iter=0;

       for(int i=0;i<7;i++)

             for(int j=0;j<13;j++)

                    for(int k=0;k<6;k++)

                    {

                          matrix[i][j][k] = (int) (str[iter]-'0');

                          iter++;

                    }

       ReleaseSemaphore(semaphore,1,NULL);

}

int* InitializePlayers(char* allPlayers)

{

       int length=(int) (allPlayers[0]-'0');

       mans=new SDL_Surface*[length];

       char* fName=new char[10];

       int* plArr=new int[length];

       for(int i=0;i<length;i++)

       {

             sprintf(fName,"man%d.gif",i+1);

             mans[i]=IMG_Load(fName);

             plArr[i]=i+1;

       }

       return plArr;

}

void RunClient()

{

       char message[2048];

       bool firstTime=true;

       bool secondTime=true;

       firstTime=true;

    while (true)

    {

             str=new char[1024];

             if(recv(clientSocket,str,1024, 0) == SOCKET_ERROR)

        {

                    MessageBox(0, (LPCSTR) "recvfrom error", (LPCSTR)"Error", MB_OK);

                    Sleep(INFINITE);

        }

             else

             {

                    if(firstTime)

                    {

                          InitializePlayers(str);

                          firstTime=false;

                    }

                    else

                    {

                          if(((int)str[0]-'0')==9)

                          {

                                 MessageBox(0, (LPCSTR) "YOU LOSE", (LPCSTR)"LOSER", MB_OK);

                                 exit(0);

                                 //Sleep(INFINITE);

                          }

                          else

                                 if(((int)str[0]-'0')==8)

                                 {

                                       MessageBox(0, (LPCSTR) "YOU WIN", (LPCSTR)"WINNER", MB_OK);

                                       exit(0);

                                       //Sleep(INFINITE);

                                 }

                          SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));

                          ProcessString();

                          DrawBomb(screen);

                    }

             }

    }

}

void DrawBomb(SDL_Surface* screen)

{

       SDL_BlitSurface(jpgMap, 0, screen, &mapPos);

       SDL_Rect bombCord;

       bombCord.x=-1;

       SDL_Rect dstrect;

       SDL_Surface* bomb;

       for(int i=0;i<7;i++)

             for(int j=0;j<13;j++)

                    {

                          if(matrix[i][j][bombCode]==bombCode)

                          {

                                 bomb=IMG_Load("bomb.gif");

                                 bombCord.x=j*60+3;

                                 bombCord.y=i*60;

                                 SDL_BlitSurface(bomb, 0, screen, &bombCord);

                          }

                          for(int k=1;k<5;k++)

                          {

                                 if(matrix[i][j][k]>0)

                                 {

                                       dstrect.x=j*60+3;

                                       dstrect.y=i*60;

                                       SDL_BlitSurface(mans[matrix[i][j][k]-1], 0, screen, &dstrect);