bool PlayerSelection(void) { PlayerSelectionData data; memset(&data, 0, sizeof data); data.IsOK = true; GetDataFilePath(data.prefixes, "data/prefixes.txt"); GetDataFilePath(data.suffixes, "data/suffixes.txt"); GetDataFilePath(data.suffixnames, "data/suffixnames.txt"); NameGenInit(&data.g, data.prefixes, data.suffixes, data.suffixnames); // Create selection menus for each local player for (int i = 0, idx = 0; i < (int)gPlayerDatas.size; i++, idx++) { PlayerData *p = CArrayGet(&gPlayerDatas, i); if (!p->IsLocal) { idx--; continue; } PlayerSelectMenusCreate( &data.menus[idx], GetNumPlayers(false, false, true), idx, i, &gEventHandlers, &gGraphicsDevice, &data.g); } if (!gCampaign.IsClient) { NetServerOpen(&gNetServer); } GameLoopData gData = GameLoopDataNew( &data, PlayerSelectionUpdate, &data, PlayerSelectionDraw); GameLoop(&gData); if (data.IsOK) { for (int i = 0, idx = 0; i < (int)gPlayerDatas.size; i++, idx++) { PlayerData *p = CArrayGet(&gPlayerDatas, i); if (!p->IsLocal) { idx--; continue; } // For any player slots not picked, turn them into AIs if (p->inputDevice == INPUT_DEVICE_UNSET) { PlayerSetInputDevice(p, INPUT_DEVICE_AI, 0); } } } for (int i = 0; i < GetNumPlayers(false, false, true); i++) { MenuSystemTerminate(&data.menus[i].ms); } NameGenTerminate(&data.g); return data.IsOK; }
int PlayerSelection(int numPlayers, GraphicsDevice *graphics) { int i; int hasInputDevice[MAX_PLAYERS]; PlayerSelectMenu menus[MAX_PLAYERS]; for (i = 0; i < numPlayers; i++) { PlayerSelectMenusCreate( &menus[i], numPlayers, i, &gCampaign.Setting.characters.players[i], &gPlayerDatas[i], &gInputDevices, graphics, &gConfig.Input); hasInputDevice[i] = 0; } KeyInit(&gInputDevices.keyboard); for (;;) { int cmds[MAX_PLAYERS]; int isDone = 1; InputPoll(&gInputDevices, SDL_GetTicks()); if (KeyIsPressed(&gInputDevices.keyboard, SDLK_ESCAPE)) { // TODO: destroy menus return 0; // hack to allow exit } GetPlayerCmds(&cmds, gPlayerDatas); for (i = 0; i < numPlayers; i++) { if (hasInputDevice[i] && !MenuIsExit(&menus[i].ms)) { MenuProcessCmd(&menus[i].ms, cmds[i]); } } for (i = 0; i < numPlayers; i++) { if (!MenuIsExit(&menus[i].ms)) { isDone = 0; } } if (isDone) { break; } AssignPlayerInputDevices( hasInputDevice, numPlayers, gPlayerDatas, &gInputDevices, &gConfig.Input); GraphicsBlitBkg(graphics); for (i = 0; i < numPlayers; i++) { if (hasInputDevice[i]) { MenuDisplay(&menus[i].ms); } else { Vec2i center; const char *prompt = "Press Fire to join..."; Vec2i offset = Vec2iScaleDiv(TextGetSize(prompt), -2); int w = graphics->cachedConfig.ResolutionWidth; int h = graphics->cachedConfig.ResolutionHeight; switch (numPlayers) { case 1: // Center of screen center = Vec2iNew(w / 2, h / 2); break; case 2: // Side by side center = Vec2iNew(i * w / 2 + w / 4, h / 2); break; case 3: case 4: // Four corners center = Vec2iNew( (i & 1) * w / 2 + w / 4, (i / 2) * h / 2 + h / 4); break; default: assert(0 && "not implemented"); break; } DrawTextString(prompt, graphics, Vec2iAdd(center, offset)); } } BlitFlip(graphics, &gConfig.Graphics); SDL_Delay(10); } for (i = 0; i < numPlayers; i++) { MenuSystemTerminate(&menus[i].ms); } return 1; }