static bool FormKeyDown(unsigned key_code) { switch (key_code) { #ifdef GNAV case KEY_APP2: MoveTarget(0); return true; case KEY_APP3: MoveTarget(180); return true; case '6': MoveTarget(270); return true; case '7': MoveTarget(90); return true; #endif case KEY_LEFT: OnPrevClicked(); return true; case KEY_RIGHT: OnNextClicked(); return true; } return false; }
void InitGame() { px = size / 2; py = size / 2; dx = 1; dy = 0; maxLength = 5; length = 1; index = 0; MoveTarget(); segments = new int[maxLength](); segments[index++] = py * size + px; map = new bool[size * size](); map[py * size + px] = true; }
void MoveSnake(int dx, int dy) { int x = px; int y = py; px += dx; Clamp(&px, 0, size-1); py += dy; Clamp(&py, 0, size-1); if (x != px || y != py) { int i = py * size + px; if (i == target) { maxLength = ResizeArray<int>(&segments, maxLength, maxLength + 1); MoveTarget(); } int old = segments[index]; if (map[i]) { map[old] = false; InitGame(); return; } map[i] = true; segments[index++] = i; ++length; if (index >= maxLength) index = 0; if (length > maxLength) length = maxLength; if (length == maxLength) map[old] = false; } }
int main (void) { const double YS_PI=3.1415927; FsOpenWindow(16, 16, 800, 600, 1); int ObstaclesState[5]; int x1[5], y1[5], x2[5], y2[5]; int Point[10000];//trajectory int p[10000],q[10000];//trajectory srand((unsigned)time(NULL)); // for(int i=0;i<10000;i++) // { // Point[i]=1; // }//draw points for(int i=0;i<5;++i) { x1[i]=rand()%720; y1[i]=rand()%520; x2[i]=x1[i]+80+rand()%70; y2[i]=y1[i]+80+rand()%70; ObstaclesState[i]=1; } int terminate=0; int y=0; int x=30; int a=55; int b=545; double m=1.0; double dt=0.025; int CannonballState=0; int angle=30; double vx; double vy; int Hit=0; int TargetState=1; while(0==terminate) { FsPollDevice(); angle++; if(angle>=90) angle=0; int key=FsInkey(); if(FSKEY_ESC==key) { terminate=1; } else if(FSKEY_SPACE==key) { if(0==CannonballState) { CannonballState=1; vx=cos(angle*YS_PI/180)*400; vy=-sin(angle*YS_PI/180)*400; } } //draw screen glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); if(0!=ObstaclesState) { for(int i=0; i<5; ++i) { if(0!=ObstaclesState[i]) { DrawObstacles(x1[i],y1[i],x2[i],y2[i]); if(0!=CannonballState && 0!=CheckCollision(a, b, x1[i], y1[i], x2[i], y2[i])) { printf("Hit An Obstacle!\n"); ObstaclesState[i]=0; CannonballState=0; Hit=1; a=55; b=545; } } if(0!=Hit) { DrawObstacles2(x1[i],y1[i],x2[i],y2[i]); } } } if(0!=TargetState) { if(0!=CannonballState && 0!=CheckHit(a, b, y)) { printf("HitTarget!\n"); TargetState=0; CannonballState=0; break; } } DrawCannonLine(x); RotateCannonline(x, 90); DrawCannon(); DrawTarget(y); MoveTarget(y, 600); if(0!=CannonballState) { DrawCannonball(a, b); MoveCannonball(a, b, vx, vy, m, dt); // for(int i=0;i<10000;i++) // { // Point[i]=1; // }//draw points // if(0!=Point)//not working // { for(int i=0; i<10000; ++i) { // if(0!=Point[i]) // { p[i]=a; // printf("%d\n",a); q[i]=b; // DrawTrajectory(p[i], q[i]); /// } // } } if(a>800||b>600) { CannonballState=0; a=55; b=545; } } FsSwapBuffers(); FsSleep(25); } return 0; }
bool Editor::OnEvent(const SEvent& event) { if (event.EventType == irr::EET_MOUSE_INPUT_EVENT && event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP ){ mouse_down=false; // drop scale drag point if (point_on != -1) point_on = -1; }else if (event.EventType == irr::EET_MOUSE_INPUT_EVENT && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN ){ mouse_down=true; }else if (event.EventType == irr::EET_MOUSE_INPUT_EVENT && event.MouseInput.Event == EMIE_MOUSE_MOVED ){ mouse_position.X=event.MouseInput.X; mouse_position.Y=event.MouseInput.Y; }else if (event.EventType == EET_KEY_INPUT_EVENT){ printf("key down %i \n",event.KeyInput.Key); if (event.KeyInput.Key== KEY_DOWN || event.KeyInput.Key== KEY_KEY_S){ pivot->setRotation(vector3df(pivot->getRotation().X-1,pivot->getRotation().Y,pivot->getRotation().Z)); }else if (event.KeyInput.Key== KEY_UP || event.KeyInput.Key== KEY_KEY_W){ pivot->setRotation(vector3df(pivot->getRotation().X+1,pivot->getRotation().Y,pivot->getRotation().Z)); }else if (event.KeyInput.Key== KEY_LEFT || event.KeyInput.Key== KEY_KEY_A){ pivot->setRotation(vector3df(pivot->getRotation().X,pivot->getRotation().Y+1,pivot->getRotation().Z)); }else if (event.KeyInput.Key== KEY_RIGHT || event.KeyInput.Key== KEY_KEY_D){ pivot->setRotation(vector3df(pivot->getRotation().X,pivot->getRotation().Y-1,pivot->getRotation().Z)); }else if (event.KeyInput.Key == 162){ printf("cntrl toggle\n"); KEY_SCALE_DOWN = event.KeyInput.PressedDown; }else if (event.KeyInput.Key == 160){ printf("shift toggle\n"); KEY_MOVE_DOWN = event.KeyInput.PressedDown; } }else if (event.EventType == EET_GUI_EVENT){ irr::s32 id = event.GUIEvent.Caller->getID(); irr::gui::IGUIEnvironment* env = device->getGUIEnvironment(); switch(event.GUIEvent.EventType) { case EGET_MENU_ITEM_SELECTED: OnMenuItemSelected( (IGUIContextMenu*)event.GUIEvent.Caller ); break; case EGET_ELEMENT_LEFT: if (id > 299 && id < 324){ if (mouse_down == true && point_on == -1){ printf("Selected scaler id %i\n",id); point_on = id - 300; } } break; case EGET_MESSAGEBOX_OK: if (event.GUIEvent.Caller->getID()==GUI_ID_NEW){ printf("New project...\n"); delete project; project = new Project(); project->nodes[0] = new Node(device,data); MoveTarget(project->nodes[0]->getPosition()); }else if (event.GUIEvent.Caller->getID()==GUI_ID_EXIT){ printf("Exiting...\n"); device->closeDevice(); } break; } for(list<IEventReceiver*>::Iterator Iterator = menus->begin(); Iterator != menus->end(); ++Iterator) { IEventReceiver* m = *Iterator; if (m && m->OnEvent(event)==true) return true; } } return false; }
bool Editor::run(IrrlichtDevice* irr_device){ // Irrlicht Device device=irr_device; driver = device->getVideoDriver(); smgr = device->getSceneManager(); guienv = device->getGUIEnvironment(); coli=smgr->getSceneCollisionManager(); device->setEventReceiver(this); // Editor Settings data=new ed_data(); data->snapping = true; data->limiting = true; data->type = TOOL_NODEB; data->menu = NULL; // Load editor Load_Scene(); Load_UI(); // Create project project = new Project(); project->nodes[0] = new Node(device,data); MoveTarget(project->nodes[0]->getPosition()); // Set up window device->setWindowCaption(convert((project->mod + stringc(" - The NodeBox Editor")).c_str())); device->setResizable(true); // Step data unsigned int counter=0; dimension2d<irr::u32> last_render_size = driver->getCurrentRenderTargetSize(); while (device->run()){ counter++; driver->beginScene(true, true, irr::video::SColor(255,100,101,140)); int ResX = driver->getScreenSize().Width; int ResY = driver->getScreenSize().Height; if (isSplitScreen==true) { // Draw Camera 0 if (camera[0]){ smgr->setActiveCamera(camera[0]); driver->setViewPort(rect<s32>(0,0,ResX/2,ResY/2)); smgr->drawAll(); } // Draw Camera 1 if (camera[1]){ smgr->setActiveCamera(camera[1]); driver->setViewPort(rect<s32>(ResX/2,0,ResX,ResY/2)); smgr->drawAll(); updatePoint(0,5); } // Draw Camera 2 if (camera[2]){ smgr->setActiveCamera(camera[2]); driver->setViewPort(rect<s32>(0,ResY/2,ResX/2,ResY)); smgr->drawAll(); updatePoint(5,10); } // Draw Camera 3 if (camera[3]){ smgr->setActiveCamera(camera[3]); driver->setViewPort(rect<s32>(ResX/2,ResY/2,ResX,ResY)); smgr->drawAll(); updatePoint(10,15); } // Draw GUI driver->setViewPort(rect<s32>(0,0,ResX,ResY)); driver->draw2DLine(vector2d<irr::s32>(0,ResY/2),vector2d<irr::s32>(ResX,ResY/2),SColor(175,255,255,255)); driver->draw2DLine(vector2d<irr::s32>(0,ResY/2-1),vector2d<irr::s32>(ResX,ResY/2-1),SColor(175,255,255,255)); driver->draw2DLine(vector2d<irr::s32>(ResX/2,0),vector2d<irr::s32>(ResX/2,ResY),SColor(175,255,255,255)); driver->draw2DLine(vector2d<irr::s32>(ResX/2+1,0),vector2d<irr::s32>(ResX/2+1,ResY),SColor(175,255,255,255)); }else if (camera[currentWindow]){ smgr->setActiveCamera(camera[currentWindow]); driver->setViewPort(rect<s32>(0,0,ResX,ResY)); smgr->drawAll(); updatePoint(0,15); } guienv->drawAll(); driver->endScene(); if ( driver->getCurrentRenderTargetSize() != last_render_size){ printf("Adjusting FOV to new screen size...\n"); last_render_size = driver->getCurrentRenderTargetSize(); camera[0]->setAspectRatio((float)driver->getScreenSize().Width/(float)driver->getScreenSize().Height); // reset matrix matrix4 projMat; irr::f32 orth_w = (float)driver->getScreenSize().Width / (float)driver->getScreenSize().Height; orth_w = 3 * orth_w; projMat.buildProjectionMatrixOrthoLH(orth_w,3,1,100); for (int i=1;i<4;i++){ if (camera[i]){ camera[i]->remove(); camera[i]=NULL; } } // Remake cameras camera[1]=smgr->addCameraSceneNode(target,vector3df(0,2,-0.01),vector3df(0,0,0)); camera[1]->setProjectionMatrix(projMat,true); camera[2]=smgr->addCameraSceneNode(target,vector3df(0,0,-5),vector3df(0,0,0)); camera[2]->setProjectionMatrix(projMat,true); camera[3]=smgr->addCameraSceneNode(target,vector3df(-5,0,0),vector3df(0,0,0)); camera[3]->setProjectionMatrix(projMat,true); } if (counter>500){ counter=0; if (project->GetCurNode()) project->GetCurNode()->update(); } } return true; }