int main(int argc, char* argv[]) { char filename[] = "doom-e1m1.mid"; RtMidiOut *midiout = new RtMidiOut(); string portName; nPorts = midiout->getPortCount(); if(argc < 2) { if ( nPorts == 0 ) cout << "No ports available!\n"; for ( unsigned int i=0; i<nPorts; i++ ) { try { portName = midiout->getPortName(i); } catch (RtError &error) {error.printMessage(); } cout << " Output Port #" << i+1 << ": " << portName << '\n'; } }else{ int port = atoi(argv[1]); //midiout->openPort( port ); cout << "Opening midi" << endl; char *filef = argv[2]; if(argc == 3) parser.Open(filef); else parser.Open(filename); tickspersecond = ( (parser.bpm / 30.0) * ( parser.tpb)); cout << " Ticks per second: " << tickspersecond << endl; (void) signal(SIGINT, finish); tickcount = 0; Sequencer sequencers[parser.numtracks]; SequencerRunning = true; for(int i=0;i<parser.numtracks;i++) { sequencers[i].StartSequencer(i, &parser.mididata.tracks[i], port); sf::Sleep(0.01f); } sf::Sleep(1); timer.Reset(); while(run) { timecount = timer.GetElapsedTime(); tickcount += timecount * tickspersecond; timer.Reset(); //sf::Sleep(0.0001f); } } delete midiout; finish(0); return 0; }
void Train::Update(const sf::Input &input) { static sf::Clock clock; static bool waiting = false; acceleration = (1/mass) *(push_force - speed * brakes_factor); speed += acceleration; position.z += speed; if (position.z > 900 || position.z < -900) { position.z = -position.z; //comingSound.SetPosition(this->position); comingSound.Play(); StopGas(); } if (!user_controlled){ if (position.z < -250 && position.z > -300){ StopGas(); Brakes(); } if (IsStopped() && !waiting){ clock.Reset(); alarm=false; waiting = true; printf("\n\n\n waiting\n"); } if (waiting && clock.GetElapsedTime() > waiting_time) { Gas(); waiting = false; printf(" timed out\n"); } if (waiting && clock.GetElapsedTime() > waiting_time-4 && !alarm) { alarm=true; sound.SetBuffer(Buffer); //sound.SetPitch(0.6); //sound.SetAttenuation(0.4); sound.SetRelativeToListener(true); sound.SetPosition(this->position); sound.SetMinDistance(100); sound.Play(); //movmetnSound.SetPosition(this->position); movmetnSound.Play(); } light->position = sf::Vector3f(-40.900002,10,position.z + 5) ; if(camera !=NULL) camera->SetOrigin(position.x+8.3,position.y+15,position.z); }else { if (input.IsKeyDown(sf::Key::Z)) { push_force = 0.1f; }else if (input.IsKeyDown(sf::Key::X)){ brakes_factor = 0.05f; } } }
void updatePositions() { static float elapsed = 0.0f; elapsed += clck.GetElapsedTime(); clck.Reset(); float delta = elapsed - lastTime; lastTime = elapsed; float speed = 5.0; if (go_forward) { current_location += speed * delta * current_forward; } if (go_backward) { current_location -= speed * delta * current_forward; } if (go_left) { current_location -= speed * delta * current_right; } if (go_right) { current_location += speed * delta * current_right; } if (go_up) { current_location += speed * delta * Up; } if (go_down) { current_location -= speed * delta * Up; } }
// called every couple of lines. Use it to check for events void Turing_StdlibSFML_PeriodicCallback() { // check every quarter of a second. This does make input delayed. // It only stops the system from deeming us unresponsive if(WinMan && eventClock.GetElapsedTime() > 0.5) { WinMan->surface(); eventClock.Reset(); } }
int main(int argc, const char **argv) { init(argc, argv); loadAssets(); while (window.IsOpened()) { clockdiff = clck.GetElapsedTime(); elapsed += clockdiff; clck.Reset(); inputFn(); renderFn(); window.Display(); } return 0; }
sf::Vector2f cHero::getMovement(const sf::Input & theInput, sf::Clock & jumpClock) { sf::Vector2f movementVector; xSpeed = 0; ySpeed = 0; // horizontal movement if (theInput.IsKeyDown(sf::Key::Left)) { xSpeed += -5; xAccel += -.2; } else if (theInput.IsKeyDown(sf::Key::Right)) { xSpeed += 5; xAccel += .2; } // vertical movement if (theInput.IsKeyDown(sf::Key::Up)) { if (isOnGround()) { jumpClock.Reset(); //ySpeed = -8; } if (jumpClock.GetElapsedTime() < .3) { yAccel -= .12 / (jumpClock.GetElapsedTime()); ySpeed = 30 * (jumpClock.GetElapsedTime() - .3); } } if (isOnGround()) { yAccel = 0; if (xAccel > .2) xAccel -= .05; else if (xAccel < -.2) xAccel += .05; } else { yAccel += 1 + (jumpClock.GetElapsedTime() / 20); } movementVector.x = xSpeed + xAccel; movementVector.y = ySpeed + yAccel; return movementVector; }
void GameServer::update() { static sf::Clock clock; if (clock.GetElapsedTime() > 150) { while (client_->commandAvailable()) { client_->getCommand()->execute (this); } clock.Reset(); } std::vector<ActionArray::iterator> to_remove; ActionArray to_add; for (ActionArray::iterator it = actions_.begin(); it != actions_.end(); it ++) { if ((*it)->isDone()) { //if ((*it)->hasNextAction()) to_remove.push_back(it); } else { if ((*it)->isActive()) (*it)->update(); } client_->sendData ((*it)->getUnit()->getStatus()); } for (std::vector<ActionArray::iterator>::reverse_iterator it = to_remove.rbegin(); it != to_remove.rend(); it ++) { delete (*(*it)); actions_.erase(*it); } }
void World::update(float time) { static sf::Clock timer; static int flag = 0; if (timer.GetElapsedTime() > 1) { timer.Reset(); for ( vector<Dynamic*>::iterator i = dynamicList.begin(); i != dynamicList.end(); i++ ) { if ( !(*i)->echo() ) { if ( !Manager::Instance()->check( (*i)->getPosition() , (*i)->getSize() ) ) { Manager::Instance()->clearMatch(); Manager::Instance()->updatePath( (*i)->getPosition() , (*i)->getSize() ); for ( vector<Dynamic*>::iterator i = dynamicList.begin(); i != dynamicList.end(); i++ ) { if ( !(*i)->echo() ) continue; Manager::Instance()->collect( (*i)->getID() , (*i)->getPosition() ); } Manager::Instance()->solve(); } else { for ( vector<Dynamic*>::iterator i = dynamicList.begin(); i != dynamicList.end(); i++ ) { if ( !(*i)->echo() ) { int t = -1; Manager::Instance()->updatePos( t , (*i)->getPosition() ); } else Manager::Instance()->updatePos( (*i)->getID() , (*i)->getPosition() ); } Manager::Instance()->attack(); } break; } } } for (vector<Dynamic*>::iterator i = dynamicList.begin(); i != dynamicList.end(); ++ i) { (*i)->update(time); } Camera::Instance()->update(time); }
void init() { // Create the main window App = new sf::Window(sf::VideoMode(RESOLUTION_X, RESOLUTION_Y, 32), "Modeling Program"); // Create a clock for measuring time elapsed Clock = sf::Clock(); __glewInit(); currentRes[0] = RESOLUTION_X; currentRes[1] = RESOLUTION_Y; transM = TRANS_TRANSLATION; axisM = X_AXIS; selected = 0; idCounter = 1; //Initial light position lightPos[0] = 2.0f; lightPos[1] = 2.0f; lightPos[2] = 2.0f; //Initial camera position cameraPos[0] = 0.0f; cameraPos[1] = 0.0f; cameraPos[2] = 0.0f; //Perspective setup glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0,currentRes[0]/currentRes[1], 0.5, 50.0); glMatrixMode(GL_MODELVIEW); //Camera setup camera = Camera(); camera.translate(0.0,0.0,20.0); camera.rotate(60.0, 1.0, 0.0, 0.0); camera.rotate(30.0, 0.0, 0.0, 1.0); camera.apply_transformations(); //setup render target texture //this will eventually hald the rendered scene and be //rendered to a quad for post process effects /*int numTex = 1; glGenTextures(numTex, &textureTarget); setupTargetTexture();*/ //setup the shader programs //the first set is the vertex and fragment shaders for the 3D render //the second set is the shaders for the texture post process effects ShaderManager shaders; shaders.GL20Support = GL20Support; char const * drawVert = "Shaders/Render3dModel.vert"; char const * drawFrag = "Shaders/Render3dModel.frag"; shaderProg = shaders.buildShaderProgram(&drawVert, &drawFrag, 1, 1); /*char const * texVert = "Shaders/RenderTexture.vert"; char const * texFrag = "Shaders/RenderTexture.frag"; texProg = shaders.buildShaderProgram(&texVert, &texFrag, 1, 1);*/ char const * noLightVert = "Shaders/NoLighting.vert"; char const * noLightFrag = "Shaders/NoLighting.frag"; noLightProg = shaders.buildShaderProgram(&noLightVert, &noLightFrag, 1, 1); //this object helps draw textures that fill the viewport /*texRender = ShowTexture(texProg); texRender.GL20Support = GL20Support;*/ // Bind custom attributes glBindAttribLocation(shaderProg, 1, "ambient"); // Start render loop while (App->IsOpened()) { Clock.Reset(); // Process events handleEvents(); // Set the active window before using OpenGL commands // It's useless here because active window is always the same, // but don't forget it if you use multiple windows or controls App->SetActive(); //render 3D scene, then put 2D output in texture, render texture renderScene(); // Finally, display rendered frame on screen App->Display(); float diff = 1.0/FPS-Clock.GetElapsedTime(); if(diff > 0) sf::Sleep(diff); } }
int main() { // Open the serial port for reading: ardu.Open(PORT); ardu.SetBaudRate(SerialStreamBuf::BAUD_115200); ardu.SetCharSize(SerialStreamBuf::CHAR_SIZE_8); char str[8]; // ardu << out; ardu >> str; std::cout << str; //sscanf(str,"%d",&res); //sleep(1); //serial_send(fd, 'A'); sleep(1); //while(result != 'A') return 1; while(true) { ardu >> str; std::cout << str; //int test = read(fd, &byte, 1); //serial_send(fd, 'A'); //printf("%i %i %c\n", test, byte, byte); //sleep(1); } // Set the last signalled state of the system as sent by the opto signal over USB. // "t" is top-dead-center, and "b" is bottom-dead-center. char system_state = 'B'; int image_counter = image_counter_min; // Create the main rendering window sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics"); // Use v-sync (make it so that we sync with the monitor, and don't draw more than 60fps. App.UseVerticalSync(false); // App.SetFramerateLimit(60); // Limit to 60 frames per second // App.SetFramerateLimit(0); // No limit // Start game loop while (App.IsOpened()) { // Process events sf::Event Event; while (App.GetEvent(Event)) { // Close window : exit if (Event.Type == sf::Event::Closed) App.Close(); // A key has been pressed if (Event.Type == sf::Event::KeyPressed) { // Escape key : exit if (Event.Key.Code == sf::Key::Escape) App.Close(); } } // Read from the USB port. uint8_t input_state; //= serial_recv(fd); printf("%i ", input_state); if (input_state != system_state && (input_state == 'B' || input_state == 'T')) { // Start the appropriate image sequence. // Change the system state. system_state = input_state; //printf("%c\n", system_state); // Reset the image counter. image_counter = 0; } // Get the elapsed time. float elapsed = Clock.GetElapsedTime(); if (elapsed > refresh_rate) { // Update the image. printf("%i %f\n", image_counter, elapsed); Clock.Reset(); // Increment or decrement the image counter. if (system_state == 'B') image_counter++; else if (system_state == 'T') image_counter++; // Make sure the counter doesn't go out of bounds. if (image_counter > image_counter_max) image_counter = image_counter_max; if (image_counter < image_counter_min) image_counter = image_counter_min; // Draw the appropriate colour: switch( image_counter ) { case 0: App.Clear(sf::Color(0, 0, 0)); break; case 1: App.Clear(sf::Color(85, 85, 85)); break; case 2: App.Clear(sf::Color(170, 170, 170)); break; case 3: App.Clear(sf::Color(255, 255, 255)); break; } } // Clear the screen with red color // App.Clear(sf::Color(200, 0, 0)); // Display window contents on screen App.Display(); } return EXIT_SUCCESS; }