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 draw(sf::Clock &Clock) { // Apply move direction if (dir == 0) glRotatef(Clock.GetElapsedTime() * 90, 1.f, 0.f, 0.f); else if (dir == 1) glRotatef(Clock.GetElapsedTime() * -90, 1.f, 0.f, 0.f); else if (dir == 2) glRotatef(Clock.GetElapsedTime() * 90, 0.f, 1.f, 0.f); else if (dir == 3) glRotatef(Clock.GetElapsedTime() * -90, 0.f, 1.f, 0.f); // Draw a cube glBegin(GL_QUADS); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-50.f, -50.f, -50.f); glVertex3f(-50.f, 50.f, -50.f); glVertex3f( 50.f, 50.f, -50.f); glVertex3f( 50.f, -50.f, -50.f); glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(-50.f, -50.f, 50.f); glVertex3f(-50.f, 50.f, 50.f); glVertex3f( 50.f, 50.f, 50.f); glVertex3f( 50.f, -50.f, 50.f); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-50.f, -50.f, -50.f); glVertex3f(-50.f, 50.f, -50.f); glVertex3f(-50.f, 50.f, 50.f); glVertex3f(-50.f, -50.f, 50.f); glColor3f(1.0f, 1.0f, 0.0f); glVertex3f(50.f, -50.f, -50.f); glVertex3f(50.f, 50.f, -50.f); glVertex3f(50.f, 50.f, 50.f); glVertex3f(50.f, -50.f, 50.f); glColor3f(0.0f, 1.0f, 1.0f); glVertex3f(-50.f, -50.f, 50.f); glVertex3f(-50.f, -50.f, -50.f); glVertex3f( 50.f, -50.f, -50.f); glVertex3f( 50.f, -50.f, 50.f); glColor3f(1.0f, 0.0f, 1.0f); glVertex3f(-50.f, 50.f, 50.f); glVertex3f(-50.f, 50.f, -50.f); glVertex3f( 50.f, 50.f, -50.f); glVertex3f( 50.f, 50.f, 50.f); glEnd(); }
void setShaderVariables(GLuint bubbleShaderProg) { GLfloat projMatrix[16]; GLfloat viewMatrix[16]; glGetFloatv(GL_PROJECTION_MATRIX, projMatrix); glGetFloatv(GL_MODELVIEW_MATRIX, viewMatrix); if(GL20Support) { glUniform1f(glGetUniformLocation(bubbleShaderProg, "elapsedTime"), motionClock.GetElapsedTime()); glUniform1f(glGetUniformLocation(bubbleShaderProg, "near"), _near); glUniform1f(glGetUniformLocation(bubbleShaderProg, "far"), _far); glUniform1f(glGetUniformLocation(bubbleShaderProg, "fov"), fov); glUniform1f(glGetUniformLocation(bubbleShaderProg, "cursorScrollAmount"), cursorScrollAmount); glUniform2f(glGetUniformLocation(bubbleShaderProg, "resolution"), RES_WIDTH, RES_HEIGHT); glUniform3f(glGetUniformLocation(bubbleShaderProg, "modelCenter"), center[0], center[1], center[2]); glUniform3f(glGetUniformLocation(bubbleShaderProg, "cameraPos"), cameraPos[0], cameraPos[1], cameraPos[2]); glUniform3f(glGetUniformLocation(bubbleShaderProg, "lookAtPos"), lookAtPos[0], lookAtPos[1], lookAtPos[2]); glUniform3f(glGetUniformLocation(bubbleShaderProg, "cameraUp"), cameraUp[0], cameraUp[1], cameraUp[2]); glUniformMatrix4fv(glGetUniformLocation(bubbleShaderProg, "projMatrix"), 1, false, projMatrix); glUniformMatrix4fv(glGetUniformLocation(bubbleShaderProg, "viewMatrix"), 1, false, viewMatrix); glUniform2f(glGetUniformLocation(bubbleShaderProg, "lightPos"), lightPos[0], lightPos[1]); glUniform2f(glGetUniformLocation(bubbleShaderProg, "cursorAbsolutePos"), cursorAbsolutePos[0], cursorAbsolutePos[1]); glUniform2f(glGetUniformLocation(bubbleShaderProg, "cursorDragAmount"), cursorDragAmount[0], cursorDragAmount[1]); glUniform2f(glGetUniformLocation(bubbleShaderProg, "lastClickPos"), lastClickPos[0], lastClickPos[1]); glUniform2f(glGetUniformLocation(bubbleShaderProg, "lastFrameDragPos"), lastFrameDragPos[0], lastFrameDragPos[1]); glUniform1i(glGetUniformLocation(bubbleShaderProg, "mouseButtonDown"), mouseButtonDown); } else { glUniform1fARB(glGetUniformLocationARB(bubbleShaderProg, "elapsedTime"), Clock.GetElapsedTime()); glUniform1fARB(glGetUniformLocationARB(bubbleShaderProg, "near"), _near); glUniform1fARB(glGetUniformLocationARB(bubbleShaderProg, "far"), _far); glUniform1fARB(glGetUniformLocationARB(bubbleShaderProg, "fov"), fov); glUniform1fARB(glGetUniformLocationARB(bubbleShaderProg, "cursorScrollAmount"), cursorScrollAmount); glUniform2fARB(glGetUniformLocationARB(bubbleShaderProg, "resolution"), RES_WIDTH, RES_HEIGHT); glUniform3fARB(glGetUniformLocationARB(bubbleShaderProg, "modelCenter"), center[0], center[1], center[2]); glUniform3fARB(glGetUniformLocationARB(bubbleShaderProg, "cameraPos"), cameraPos[0], cameraPos[1], cameraPos[2]); glUniform3fARB(glGetUniformLocationARB(bubbleShaderProg, "lookAtPos"), lookAtPos[0], lookAtPos[1], lookAtPos[2]); glUniform3fARB(glGetUniformLocationARB(bubbleShaderProg, "cameraUp"), cameraUp[0], cameraUp[1], cameraUp[2]); glUniformMatrix4fvARB(glGetUniformLocationARB(bubbleShaderProg, "projMatrix"), 1, false, projMatrix); glUniformMatrix4fvARB(glGetUniformLocationARB(bubbleShaderProg, "viewMatrix"), 1, false, viewMatrix); glUniform2fARB(glGetUniformLocationARB(bubbleShaderProg, "cursorAbsolutePos"), cursorAbsolutePos[0], cursorAbsolutePos[1]); glUniform2fARB(glGetUniformLocationARB(bubbleShaderProg, "cursorDragAmount"), cursorDragAmount[0], cursorDragAmount[1]); glUniform2fARB(glGetUniformLocationARB(bubbleShaderProg, "lastClickPos"), lastClickPos[0], lastClickPos[1]); glUniform2fARB(glGetUniformLocationARB(bubbleShaderProg, "lastFrameDragPos"), lastFrameDragPos[0], lastFrameDragPos[1]); glUniform1iARB(glGetUniformLocationARB(bubbleShaderProg, "mouseButtonDown"), mouseButtonDown); } }
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(); } }
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; }
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; }
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; }
// ag is the alpha goal between 1 and 0 void fadeTick(float ag, float speed) { float alphaGoal = 255 * ag; m_alpha -= speed * m_animTimer->GetElapsedTime(); if (m_alpha <= alphaGoal) { m_transitioning = NONE; return; } m_transition.SetColor(sf::Color(0, 0, 0, m_alpha)); }
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); }
/** * Run the game up to this moment. */ void runUntilNow() { game->runUntil(clock.GetElapsedTime()); }
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; }
int main() { // Create the main rendering window App.Create(sf::VideoMode(640,480,32), "SFML Graphics"); UIState uistate; uistate.renderer = &App; InitEntities(); PhysicsSub physSub(&App, &entitysystem); RenderingSystem renderSys(&App, &entitysystem); ControllerSystem controller(&App, &entitysystem); ProjectileSystem projSys(&App, &entitysystem); bool inventory = false; int invX; int invY; std::vector<Entity*> players; entitysystem.getEntities<CompPlayer>(players); Entity* player = players[0]; // Start game loop while (App.IsOpen()) { // Process events sf::Event Event; while (App.PollEvent(Event)) { uistate.uiEvents(Event); // Close window : exit if (Event.Type == sf::Event::Closed) App.Close(); if(Event.Type == sf::Event::MouseButtonPressed && Event.MouseButton.Button == sf::Mouse::Right) { inventory = !inventory; if(inventory) invX = sf::Mouse::GetPosition(App).x; invY = sf::Mouse::GetPosition(App).y; } if(Event.Type == sf::Event::MouseButtonPressed && Event.MouseButton.Button == sf::Mouse::Left) makeProjectile(); } controller.Tick(Clock.GetElapsedTime().AsSeconds()); physSub.Tick(Clock.GetElapsedTime().AsSeconds()); projSys.Tick(Clock.GetElapsedTime().AsSeconds()); // Clear the screen with red color App.Clear(sf::Color::Black); uistate.imgui_prepare(); if(widget::button(uistate, GEN_ID, sf::Vector2f(100, 30), sf::Vector2f(520, 10), "button", 24)) entitysystem.deleteEntity(player); if(inventory) if(widget::button(uistate, GEN_ID, sf::Vector2f(100, 30), sf::Vector2f(invX, invY), "inventory", 20)) player = makePlayer(); uistate.imgui_finish(); renderSys.Tick(Clock.GetElapsedTime().AsSeconds()); // Display window contents on screen App.Display(); Clock.Restart(); } return EXIT_SUCCESS; }