void tick(int value) { // Calls the display() function glutPostRedisplay(); // FPS calculus // Increase frame count fpsFrameCount++; // Get the number of milliseconds since glutInit called (or first call to glutGet(GLUT ELAPSED TIME)) fpsCurrentTime = glutGet(GLUT_ELAPSED_TIME); // Calculate time passed int timeInterval = fpsCurrentTime - fpsPreviousTime; if (timeInterval > 1000) { // Calculate the number of frames per second fps = fpsFrameCount / (float)timeInterval * 1000; // Set time fpsPreviousTime = fpsCurrentTime; // Reset frame count fpsFrameCount = 0; // Change the window's title char title[50]; if (mot_GetState(MOT_IS_PAUSED)) { sprintf(title, "Epic Game Paused | FPS: %f", fps); } else { sprintf(title, "Epic Game | FPS: %f", fps); } glutSetWindowTitle(title); } #ifdef _WIN32 // Save the map if Ctrl-S is pressed if (GetKeyState(VK_LCONTROL) < 0 && GetKeyState('S') < 0) { if (!saveBtnWasPressed) { saveBtnWasPressed = 1; if (saveMapToFile(fp, Map)) { printf("Error while saving file :(\n"); } else { printf("Saved map to file.\n"); } } } else { saveBtnWasPressed = 0; } // Toggle OP mode if (GetKeyState('C') < 0) { mot_SetState(MOT_IS_OP, 1); } if (GetKeyState('R') < 0) { mot_SetState(MOT_IS_OP, 0); } #endif // _WIN32 // Waits 10 ms glutTimerFunc(10, tick, value + 1); }
bool MapMakerScene::init() { if (!Layer::init()) { return false; } m_lastSelectGroup = nullptr; Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto s = Director::getInstance()->getWinSize(); //input map name auto pTextField = TextFieldTTF::textFieldWithPlaceHolder(std::string(LocalizedCStringByKey("input_map_name")), "fonts/arial.ttf", 48); addChild(pTextField,0,"tbMapName"); pTextField->setPosition(Vec2(s.width / 2,s.height - 250)); //输入事件 auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = [pTextField](Touch* touch, Event*) { auto beginPos = touch->getLocation(); Rect rect; rect.size = pTextField->getContentSize(); auto clicked = isScreenPointInRect(beginPos, Camera::getVisitingCamera(), pTextField->getWorldToNodeTransform(), rect, nullptr); if (clicked) { return true; } pTextField->detachWithIME(); return false; }; listener->onTouchEnded = [pTextField](Touch* touch, Event* event) { auto endPos = touch->getLocation(); Rect rect; rect.size = pTextField->getContentSize(); auto clicked = isScreenPointInRect(endPos, Camera::getVisitingCamera(), pTextField->getWorldToNodeTransform(), rect, nullptr); if (clicked) { pTextField->attachWithIME(); } }; _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); //返回主菜单 auto returnToMainMenuItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(MapMakerScene::returnToMainMenuCallback, this) ); returnToMainMenuItem->setPosition(Vec2(origin.x + visibleSize.width - returnToMainMenuItem->getContentSize().width / 2, origin.y + returnToMainMenuItem->getContentSize().height / 2)); auto menu = Menu::create(returnToMainMenuItem, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); //baseplate auto baseplateLayer = SquareBaseplateLayer::create(); baseplateLayer->createEmptyMap(BaseSize(15,15)); baseplateLayer->drawBasesplate(Vec2(32,32)); baseplateLayer->setPosition(Vec2(100,200)); addChild(baseplateLayer); //菜单,创建group auto menuItemCreateGroup = MenuItemFont::create(LocalizedCStringByKey("create_group")); menuItemCreateGroup->setCallback( [=](Ref*) { auto squareGroup = SquareGroupMapMaker::create(); squareGroup->setPosition(Vec2(100,100)); squareGroup->SetSquareGroup(Vec2(32,32),SquareGroup::SQUAREGROUP_TYPE::ST_Z,Square::SQUARE_COLOR::SC_GREEN); squareGroup->DrawGroup(); squareGroup->setSelectedListener( [=](SquareGroup* sg) { m_lastSelectGroup = sg; }); this->addChild(squareGroup); } ); //菜单,删除group auto menuItemDeleteSelectedGroup = MenuItemFont::create(LocalizedCStringByKey("delete_group")); menuItemDeleteSelectedGroup->setCallback( [=](Ref*) { for(Node* node:this->getChildren()) { SquareGroupMapMaker* sg = dynamic_cast<SquareGroupMapMaker*>(node); if(sg != nullptr) { if(sg->getGroupState() == SGS_SELECTED) { assert(m_lastSelectGroup == sg); m_lastSelectGroup = nullptr; this->removeChild(node); } } } } ); //菜单,保存地图 auto menuItemSaveMap = MenuItemFont::create(LocalizedCStringByKey("save_map")); menuItemSaveMap->setCallback( [=](Ref*) { saveMapToFile(); } ); //创建菜单组 auto operationMenu = Menu::create(menuItemCreateGroup,menuItemDeleteSelectedGroup,menuItemSaveMap, NULL); operationMenu->alignItemsVerticallyWithPadding(20); addChild(operationMenu); operationMenu->setPosition(Vec2(s.width / 2, s.height - 100)); //产生本地图的guid m_guid = std::string(GameUilityTools::CreateGuidString()); //添加选择颜色的层 auto colorLayer = SelectColorLayer::create(); colorLayer->setPosition(Vec2(0, 0)); colorLayer->setColorChangeListener( [=](Square::SQUARE_COLOR color){ //todo xuhua if (m_lastSelectGroup) { m_lastSelectGroup->setSquareGroupColor(color); m_lastSelectGroup->DrawGroup(); } } ); addChild(colorLayer, 0); return true; }
// Prompts for a file to open the map // If anything fails the function returns 1, else 0 is returned int openMapFile(void) { printf("Enter file name: %s", mapFolder); // Input entered char * fileN = (char*)malloc(32 * sizeof(char)); if (scanf("%s", fileN) == 1) { // Lets extract the extension // Pointer to the last dot char * p = NULL; char * p1 = strchr(fileN, '.'); while (p1 != NULL) { p = p1; p1 = strchr(p1 + 1, '.'); } if (p != NULL) { if (strstr(p + 1, "map") != NULL && strlen(p + 1) == 3) { // File path is folder path + file name filePath = (char *)malloc((strlen(mapFolder) + strlen(fileN) + 1) * sizeof(char)); strcpy(filePath, mapFolder); strcat(filePath, fileN); // File name fileName = (char *)malloc((strlen(fileN) + 1) * sizeof(char)); strcpy(fileName, fileN); // Try to open file in read-binay mode fp = fopen(filePath, "rb"); int exists = 0; if (fp != NULL) { // File already exists, lets try to read its content if (loadMapFromFile(fp, &Map)) { // Failed printf("Error loading map from file \"%s\". Invalid or corrupted .map file\n", filePath); } else { // Succeeded exists = 1; printf("Successfully loaded map from file\n"); } } else { // No file found printf("No such vaid file found, creating one: \"%s\"\n", filePath); } // Open the file in write-binary mode (this also creates the file if it doesn't exist) if (exists) { fp = freopen(filePath, "wb", fp); } else { fp = fopen(filePath, "wb"); // Init our Map int size[3] = {50, 50, 50}; initMap(&Map, size); } if (fp != NULL) { // Save the map in case the user forgets to save it saveMapToFile(fp, Map); // Everything went well printf("Done.\n"); return 0; } // Something bad happened printf("Could not open file \"%s\"\n", filePath); return 1; } // Extension found but it's not .map printf("Invalid file extension: \"%s\"\n", p); return 1; } // No dot found so there is no extension printf("No file extension in given name\n"); return 1; } // Input failed? return 1; }