示例#1
0
void run(SDL_Surface* screen, int fd){
  int currRoom, numSprites, i;
  SDL_Event event;

  Sprite components[NUMTYPES*NUMSPRITES];

  //Writing Variables
  struct Message message;
  int keyState[NUMKEYS];
  
  //Room Caption Variables
  char room_caption[7];
  strcpy(room_caption, "Room 0");


  //Local Chat Variables
  int typing = FALSE;  

  TTF_Font *font = TTF_OpenFont("src/Teen.ttf", 16);
  SDL_Color textColor = {0,0,0};

  char chatmsg[256];
  memset(chatmsg, '\0', 256); 

  SDL_Surface *chatsurface = NULL;
  SDL_Rect typePosition;
  typePosition.x = 30;
  typePosition.y = 550;


  //Server-wide chat log display positions
  SDL_Rect position[MAXCHATMESSAGES];

  for(i = 0; i < MAXCHATMESSAGES; i++){
    position[i].x = MSGLEFT;
    position[i].y = MSGTOP + ( MSGHEIGHT * (5 - i) );
  }


  currRoom = 0;
  
  long unsigned lastTick;
  lastTick = SDL_GetTicks();
  
  /*---------------------------------------

             END OF VARIABLE SEA

    ---------------------------------------*/



    while(1){
    //Considering how slow it's going, the FPS cap is probably useless now...
    lastTick = FPScap(lastTick);
    
    //Get the number of sprites to be read
    read(fd, &numSprites, sizeof(int));

    //The room has changed and the new room number has been read instead
    if(numSprites >= 40){
      
      currRoom = numSprites - 40;
      
      //Change room caption
      room_caption[5] = (char)(currRoom + 48);
      SDL_WM_SetCaption(room_caption, NULL);
      printf("Now in %s\n", room_caption);

      //Read number of sprites to be read, for real this time.
      read(fd, &numSprites, sizeof(int));
    
    }

    //Read all sprites
    for(i=0;i<numSprites;i++){
      read(fd, &(components[i]), sizeof(components[i]));
    }    

    //Draw components and blit the current chatsurface if player is typing
    draw(components, numSprites, screen);

    //Draw the chat message in progress
    if(chatsurface && strlen(chatmsg) > 0)
      SDL_BlitSurface(chatsurface, NULL, screen, &typePosition);

    //Draw the chat messages stored in text[]
    SDL_SemWait(textSem);

    for(i = 0; i<numMessages; i++){
      SDL_BlitSurface(text[i], NULL, (SDL_Surface*)screen, &position[i]); 
    }

    SDL_SemPost(textSem);
    
    //Flip the screen
    SDL_Flip(screen);    


    /*---------------
      EVENT HANDLING
      ---------------*/

    SDL_EnableUNICODE( SDL_ENABLE );
    SDL_PollEvent(&event);
    SDL_EnableUNICODE( SDL_DISABLE );


    if(event.type == SDL_QUIT)
      break;
    

    /*-----------------
      ENTER KEY PRESSED
      -----------------*/
    if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RETURN){
      
      //Started typing a message
      if(!typing){ 
	typing = TRUE;
	strcpy(chatmsg, clientname);
	chatsurface = TTF_RenderText_Solid(font, chatmsg, textColor);
      }
      
      //Finished typing a message
      else{
	typing = FALSE;
	
	if(strlen(chatmsg) > namelen){

	  //Write the chat message to the shared msgbuf
	  SDL_SemWait(msgbuf_sem);
	  strcpy(msgbuf, chatmsg);
	  SDL_SemPost(msgbuf_sem);
	  
	  //Reset the chat message
	  memset(chatmsg, '\0', 256);
	}
	
	//Free the chatsurface
	if(chatsurface != NULL){
	  SDL_FreeSurface(chatsurface);
	  chatsurface = NULL;
	}

      }
      
    }

  

    /*----------------
      CONTINUE TYPING
      ----------------*/
    else if(typing){

      //Handle chat if key pressed
      if(event.type == SDL_KEYDOWN)
	chatsurface = handleChat(event, namelen, chatmsg, chatsurface, font, textColor);      

    }
    

    /*--------------
      Message Setup
    ----------------*/
    message.clientid = getpid();
    message.roomid = currRoom;
	     
    for(i=0; i<NUMTYPES; i++)
      keyState[i] = FALSE;

    /*-------------------------
      Check event and keystates
    ---------------------------*/
    if(!typing)
      handleKeys(keyState);

    //Write the clientid and roomid
    write(fd, &message, sizeof(message));

    //Write the keystates
    for(i=0;i<NUMKEYS;i++){
      write(fd, &(keyState[i]), sizeof(int));
    }
   
  }

  if(chatsurface != NULL)
    SDL_FreeSurface(chatsurface);
}
DJGameUserContext::DJGameUserContext( DJGameController	*gameController, QWidget * parent, Qt::WFlags f )
	:QWidget(parent,f),m_gameController(gameController)
{
	djDebug() << "DJGameUserContext constructor";
	
	m_clickedUserId	= 0;
	
	QHBoxLayout* mainLayout	= new QHBoxLayout(this);
	mainLayout->setMargin( 10 );
	//mainLayout->setSpacing( 0 );
	//mainLayout->setSizeConstraint(QLayout::SetFixedSize);
	
	m_showView	= new DJShowView( 0 );
	QSize size	= m_showView->realSize();
	m_showView->setFixedSize( size );
	mainLayout->addWidget( m_showView );
	
	QVBoxLayout *rightLayout	= new QVBoxLayout;
	rightLayout->setMargin( 10 );
	rightLayout->setSpacing( 0 );
	//rightLayout->setSizeConstraint(QLayout::SetFixedSize);
    mainLayout->addLayout( rightLayout );
    
    
    m_userInfo		= new QLabel;
    rightLayout->addWidget( m_userInfo, 1 );
    
    //function menu
    m_actLookOn	= new QAction( tr("look on this table"), this );
	connect( m_actLookOn, SIGNAL(triggered()), SLOT(handleLookOn()) );
	m_actChat	= new QAction( tr("chat with this user"), this );
	connect( m_actChat, SIGNAL(triggered()), SLOT(handleChat()) );
	m_actInvite	= new QAction( tr("invite this user to join"), this );
	connect( m_actInvite, SIGNAL(triggered()), SLOT(handleInvite()) );
	m_actForceStandUp	= new QAction( tr("force this user stand up"), this );
	connect( m_actForceStandUp, SIGNAL(triggered()), SLOT(handleForceStandUp()) );
    m_menuFunction	= new QMenu( this );
	m_btnFunction	= new DJMenuButton;
    m_btnFunction->setText( tr("functions") );
    m_btnFunction->setMenu( m_menuFunction );
    rightLayout->addWidget( m_btnFunction, 1 );
    
    //contact menu
    m_actFriend	= new QAction( tr("add to friendlist"), this );
	m_actFriend->setCheckable( true );
	connect( m_actFriend, SIGNAL(triggered(bool)), SLOT(handleContactFriend(bool)) );
	m_actUnwelcome	= new QAction( tr("add to blacklist"), this );
	m_actUnwelcome->setCheckable( true );
	connect( m_actUnwelcome, SIGNAL(triggered(bool)), SLOT(handleContactUnwelcome(bool)) );
    m_menuContact	= new QMenu( this );
    m_menuContact->addAction( m_actFriend );
	m_menuContact->addAction( m_actUnwelcome );
    m_btnContact	= new DJMenuButton;
    m_btnContact->setText( tr("manage contacts") );
    m_btnContact->setMenu( m_menuContact );
    rightLayout->addWidget( m_btnContact, 1 );
    
    //admin menu
    m_actAdminDeltaScore = new QAction( tr("Delta ") + m_gameController->baseSectionName(DJGameController::Score), this );
	connect( m_actAdminDeltaScore, SIGNAL(triggered()), SLOT(handleAdminDeltaScore()) );
    //m_actAdminKick	= new QAction( tr("Kick out"), this );
	//connect( m_actAdminKick, SIGNAL(triggered()), SLOT(handleAdminKick()) );
	m_actAdminShutUp	= new QAction( tr("Shut up"), this );
	connect( m_actAdminShutUp, SIGNAL(triggered()), SLOT(handleAdminShutUp()) );
	m_actAdminDeltaMoney	= new QAction( tr("Delta ") + m_gameController->baseSectionName(DJGameController::Money), this );
	connect( m_actAdminDeltaMoney, SIGNAL(triggered()), SLOT(handleAdminDeltaMoney()) );
	m_actAdminDeltaMagic	= new QAction( tr("Delta ") + m_gameController->baseSectionName(DJGameController::Magic), this );
	connect( m_actAdminDeltaMagic, SIGNAL(triggered()), SLOT(handleAdminDeltaMagic()) );
	m_actAdminDeltaChips	= new QAction( tr("Delta ") + m_gameController->baseSectionName(DJGameController::Chips), this );
	connect( m_actAdminDeltaChips, SIGNAL(triggered()), SLOT(handleAdminDeltaChips()) );
	m_actAdminBanIp	= new QAction( tr("Ban Ip"), this );
	connect( m_actAdminBanIp, SIGNAL(triggered()), SLOT(handleAdminBanIp()) );
	m_actAdminBanAccount	= new QAction( tr("Ban Account"), this );
	connect( m_actAdminBanAccount, SIGNAL(triggered()), SLOT(handleAdminBanAccount()) );
	m_actAdminRobot	= new QAction( tr("Robot"), this );
	connect( m_actAdminRobot, SIGNAL(triggered()), SLOT(handleRobotChat()) );
	
	m_menuAdmin	= new QMenu( tr("Admin Command"), this );
    m_btnAdmin	= new DJMenuButton;
    m_btnAdmin->setText( tr("Admin command") );
    m_btnAdmin->setMenu( m_menuAdmin );
    rightLayout->addWidget( m_btnAdmin, 1 );
    
    rightLayout->addStretch();
	m_labelCards	= new QLabel( tr("cards") );
	rightLayout->addWidget( m_labelCards, 1 );
	m_cardsWidget	= new DJCardsWidget(m_gameController);
	rightLayout->addWidget( m_cardsWidget, 1 );
}