Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <Windows.h>
- #include <string>
- #include <vector>
- #include <thread>
- #include <chrono>
- using namespace std;
- int filesInFolderCount(string fileType)
- {
- int fileCount = 0;
- string folderPath = "maps/" + fileType + "/";
- // Get the first file in the folder
- WIN32_FIND_DATAA findFileData;
- HANDLE hFind = FindFirstFileA((folderPath + "*").c_str(), &findFileData);
- // Iterate through the files in the folder
- if (hFind != INVALID_HANDLE_VALUE)
- {
- while (true)
- {
- // Check if the current file is a regular file
- if ( (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0 )
- {
- fileCount++;
- }
- if ( !(FindNextFileA(hFind, &findFileData)) )
- {
- break;
- }
- }
- // Clean up
- FindClose(hFind);
- }
- return fileCount;
- }
- int main()
- {
- int fileCount = filesInFolderCount("custom");
- string* menu = new string[fileCount]; // NOTE: To work with a dynamic array the second if statement in the while loop, that's located in a if statement is needed
- //vector<string> menu(fileCount); // NOTE: Works perfectly
- WIN32_FIND_DATA findData;
- HANDLE hFind;
- string folderPath = "maps/custom/";
- wstring wideFolderPath(folderPath.begin(), folderPath.end());
- wideFolderPath += L"*";
- hFind = FindFirstFile(wideFolderPath.c_str(), &findData);
- if (hFind != INVALID_HANDLE_VALUE)
- {
- char narrowStr[MAX_PATH];
- int counter = 0;
- while (FindNextFile(hFind, &findData) != 0)
- {
- WideCharToMultiByte(CP_ACP, 0, findData.cFileName, -1, narrowStr, MAX_PATH, NULL, NULL);
- if (strcmp(narrowStr, ".") != 0 && strcmp(narrowStr, "..") != 0)
- {
- if (counter == fileCount)
- {
- string* newData = new string[fileCount * 2];
- for (int i = 0; i < counter; i++)
- {
- newData[i] = menu[i];
- }
- delete[] menu;
- menu = newData;
- }
- menu[counter] = narrowStr;
- counter++;
- }
- }
- FindClose(hFind);
- }
- int pointer = 0;
- bool lastUpKeyState = false;
- bool lastDownKeyState = false;
- bool lastReturnKeyState = false;
- bool arrowVisible = true;
- while (true)
- {
- system("cls");
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
- cout << "There are " << fileCount << " files in the subfolder called \"custom\"." << "\n" << "\n";
- cout << "Select a Custom Map" << "\n" << "\n";
- for (int i = 0; i < fileCount; i++)
- {
- if (i == pointer)
- {
- if (arrowVisible)
- {
- cout << "-> ";
- arrowVisible = false;
- }
- else
- {
- cout << " "; // Prints 4 spaces to cover the previous "-> "
- arrowVisible = true;
- }
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
- cout << menu[i] << "\n";
- }
- else
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
- cout << menu[i] << "\n";
- }
- }
- bool upKeyState = GetAsyncKeyState(VK_UP);
- if ( (upKeyState) && !(lastUpKeyState) )
- {
- pointer -= 1;
- if (pointer == -1)
- {
- pointer = fileCount - 1;
- }
- }
- lastUpKeyState = upKeyState;
- bool downKeyState = GetAsyncKeyState(VK_DOWN);
- if ( (downKeyState) && !(lastDownKeyState) )
- {
- pointer += 1;
- if (pointer == fileCount)
- {
- pointer = 0;
- }
- }
- lastDownKeyState = downKeyState;
- bool returnKeyState = GetAsyncKeyState(VK_RETURN);
- if ( (returnKeyState) && !(lastReturnKeyState) )
- {
- // code to load the selected file goes here
- }
- lastReturnKeyState = returnKeyState;
- this_thread::sleep_for(chrono::milliseconds(200));
- }
- delete[] menu;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement