Exemple #1
0
bool handleInput(int sock, struct addrinfo * p) {
    int charRead;
    while(true)  {
		if(timeForKeepAlive) {
			sendKeepAlivePacket(sock, p);
			timeForKeepAlive = false;
		}
		handleNetwork(sock, p);
        charRead = wgetch(inputWnd);        
        if(charRead == 127 && nchars > 0) {
            int cy, cx;
            getyx(inputWnd, cy, cx);
            mvwaddch(inputWnd, cy, cx-1, ' ');
			wmove(inputWnd, cy, cx-1);
            line[nchars] = '\0';
            --nchars;
            return true;
        } else if(charRead >= 32 && charRead <= 126 && nchars < MAXLINE) {
            wechochar(inputWnd, charRead);
            line[nchars] = charRead;
            refreshAll();
            ++nchars;
            return true;
        } else if(charRead == '\n') {
            if(strncmp(line, "/exit", std::max(nchars, MAXLINE+1)) == 0) {
                return false;
            } else if(strncmp(line, "/join ", 6) == 0) {
                sendJoinPacket(sock, p, &(line[6]));
				memset(curChannel, '\0', CHANNEL_MAX+1);
				strncpy(curChannel, &(line[6]), CHANNEL_MAX);
				channelsJoined.insert(curChannel);
			} else if(strncmp(line, "/switch ", 8) == 0) {
				char chanName[CHANNEL_MAX+1];
				memset(chanName, '\0', CHANNEL_MAX+1);
				strncpy(chanName, &(line[8]), CHANNEL_MAX);
				if(channelsJoined.count(chanName) > 0) {
					strncpy(curChannel, chanName, CHANNEL_MAX);
				} else {
					char err[256];
					snprintf(err, 256, "you have not subscribed to channel %.32s", chanName);
					printErrorMsg(err);
				}
            } else if(strncmp(line, "/leave ", 7) == 0) {
                sendLeavePacket(sock, p, &(line[7]));
				memset(curChannel, '\0', CHANNEL_MAX+1);
				strncpy(curChannel, &(line[7]), CHANNEL_MAX);
				channelsJoined.erase(curChannel);
				memset(curChannel, '\0', CHANNEL_MAX);
			} else if(strncmp(line, "/list", 5) == 0) {
				sendListPacket(sock, p);
			} else if(strncmp(line, "/who ", 5) == 0) {
				sendWhoPacket(sock, p, &(line[5]));
            } else if(line[0] == '/') {
				printErrorMsg("unrecognized command");
			} else if(curChannel[0] != '\0'){
				sendSayPacket(sock, p, curChannel, line);
			}
            //wprintw(wnd, "%s\n", line);
            memset(line, '\0', MAXLINE+1);
            clearInput();
            return true;
        }
    };
}
Exemple #2
0
int eventLoop(SDL_Surface * screen) {
    SDL_Event event;
    int animationCounter = 0;
    while(1) {
        while (SDL_PollEvent(&event)) {
            switch (event.type) {
            case SDL_KEYUP:
                if(handle_keyup(event.key.keysym.sym)==MENU) {
                    gameLoopCleanUp();
                    return MENU;
                }
                break;
            case SDL_KEYDOWN:
                handle_key(event.key.keysym.sym);
                break;
            }


        }

        handleNetwork();

        std::map<int, Hero*>::iterator it;
        for(it=heroGroup.begin(); it!=heroGroup.end(); ++it)
        {
            if (it->second->getVisible())
                it->second->update(blocks, colList, heroGroup, bombGroup, explosionGroup);
        }
        if(moved) {
            hero_pos newPos=heroGroup[myId]->getPos();
            heromessage msg;
            msg.LoadByte(newPos.x, newPos.y, newPos.id);
            udpclient->Send(msg,tcpclient->getIpAddress() ,-1);
            moved=false;
        }
        if(bombed)
        {
            int bombx, bomby, level;
            bombx=heroGroup[myId]->getBombx();
            bomby=heroGroup[myId]->getBomby();
            level=heroGroup[myId]->getBombLevel();
            bombmessage bmsg;
            bmsg.LoadByte(bombx, bomby, level);
            udpclient->Send(bmsg, tcpclient->getIpAddress(), -1);
            bombed=false;
        }
        for (int i = 0; i < blocks.size(); i++) {
            blocks[i]->update(colList, heroGroup, explosionGroup, upgradeGroup);
        }

        for (int i = 0; i < bombGroup.size(); i++) {
            bombGroup.at(i)->update(blocks, colList, heroGroup, bombGroup, explosionGroup);
        }
        for (int i = 0; i < explosionGroup.size(); i++) {
            explosionGroup.at(i)->update();
        }
        for (int i = 0; i < upgradeGroup.size(); i++) {
            upgradeGroup.at(i)->update();
        }

        for (int i = 0; i < colList.size(); i++) {
            CollisionPair * tmp = colList.at(i);
            if (tmp->isCollided()) {
                tmp->onCollision();
            }
        }

        background->blit(screen);

        if (Mix_PlayingMusic() == 0) {
            if (Mix_PlayMusic(mainMusic, -1) == -1)
                fprintf(stderr, "Unable to play WAV file: %s\n", Mix_GetError());
        }

        for (int j = 0; j < bombGroup.size(); j++) {
            bombGroup.at(j)->blit(screen);
        }
        for (int j = 0; j < explosionGroup.size(); j++) {
            SDL_Rect tmp = explosionGroup.at(j)->getShowPart();
            explosionGroup.at(j)->blit(screen, &tmp);
        }
        for (int i = 0; i < blocks.size(); i++) {
            blocks.at(i)->blit(screen);
        }

        for (int i = 0; i < upgradeGroup.size(); i++) {
            upgradeGroup.at(i)->blit(screen);
        }
        for(map<int, Hero* >::iterator it=heroGroup.begin(); it!=heroGroup.end(); ++it) {
            it->second->blit(screen);
        }

        for(map<int, Hero* >::iterator it=heroGroup.begin(); it!=heroGroup.end(); ++it) {
            sprintf(textbuf[it->first], "Player %d = %d", it->first+1, it->second->getLife());
            text_image[it->first] =  TTF_RenderText_Solid(text_font, textbuf[it->first], font_color);
            int offsetX = it->first<2 ? 0:1;
            int offsetY = it->first%2==0 ? 0:1;
            textDest[it->first].x = 50+750*offsetX;
            textDest[it->first].y = 10+50*offsetY;
            textDest[it->first].w = text_image[it->first]->w;
            textDest[it->first].h = text_image[it->first]->h;
            SDL_BlitSurface(text_image[it->first], NULL, screen, &(textDest[it->first]));
            SDL_FreeSurface(text_image[it->first]);
        }


        // game over
        bool iWin = false;
        if (heroGroup[myId]->getLife()!=0) {
            iWin = true;
            for(map<int, Hero* >::iterator it=heroGroup.begin(); it!=heroGroup.end(); ++it) {
                if (it->first != myId) {
                    if (it->second->getLife() != 0) {
                        iWin = false;
                        break;
                    }
                }
            }
        }
        bool iLose=false;
        if (heroGroup[myId]->getLife()==0) {
            iLose = true;
            int counter = 0;
            for(map<int, Hero* >::iterator it=heroGroup.begin(); it!=heroGroup.end(); ++it) {
                if (it->first != myId) {
                    if (it->second->getLife() != 0) {
                        counter++;
                        if (counter>1) {
                            iLose = false;
                            break;
                        }
                    }
                }
            }
        }

        if (iWin) {
            animationCounter++;
            if (animationCounter>20) {
                winScreen->blit(screen);
                SDL_Flip(screen);
                SDL_Delay(3000);
                gameLoopCleanUp();
                return MENU;
            }
        }
        else if (iLose) {
            animationCounter++;
            if (animationCounter>20) {
                loseScreen->blit(screen);
                SDL_Flip(screen);
                SDL_Delay(3000);
                gameLoopCleanUp();
                return MENU;
            }
        }

        /* since its double buffered, make
         the changes show up*/
        SDL_Flip(screen);

        SDL_Delay(50);
    }
    return 0;
}