Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ы
- //#include <windows.h>
- //#include <iostream>
- //using namespace std;
- #include "SFML/Graphics.hpp"
- using namespace sf;
- // Interpolation
- // trying to do like here >> http://gafferongames.com/game-physics/fix-your-timestep/
- //
- // Problem - motion a bit laggy with flickering 1px sprite border
- // also a bit slowed and(or) accelerated
- class timeLogic
- {
- Clock cl;
- bool start;
- Int64 prevTme, currTme, accum;
- public:
- const Int64 tmeStep;
- Int64 frTme;
- float alpha;
- unsigned short stepCount;
- // 100 fps fixed step
- timeLogic() : tmeStep(10000)
- {
- start = true;
- currTme = 0;
- accum = 0;
- }
- void startTme()
- {
- if(start)
- {
- cl.restart();
- start = false;
- }
- }
- void newFrame()
- {
- prevTme = currTme;
- currTme = cl.getElapsedTime().asMicroseconds();
- frTme = currTme - prevTme;
- if(frTme > 40000)
- frTme = 40000;
- accum += frTme;
- // wipe value, interpolated in previous frame if setted
- stepCount = 0;
- while(accum >= tmeStep)
- {
- // set count to sprite interpolation logic
- stepCount++;
- accum -= tmeStep;
- }
- // interpolation alpha
- alpha = (float)accum / tmeStep;
- }
- }
- tmeL;
- class moveSpriteLogic
- {
- Vector2f prevPos;
- Vector2f currPos;
- Vector2f interpPos;
- Vector2f realPos;
- float velX, velY;
- public:
- Texture tx;
- Sprite sp;
- moveSpriteLogic(char* file, float velocityX, float velocityY)
- {
- tx.loadFromFile(file);
- sp.setTexture(tx);
- // velocity for 1 microsecond
- velX = velocityX;
- velY = velocityY;
- currPos.x = sp.getPosition().x;
- currPos.y = sp.getPosition().y;
- prevPos = currPos;
- }
- // main Interpolation method
- void move()
- {
- if(tmeL.stepCount)
- {
- for(unsigned short n = 0; n < tmeL.stepCount; n++)
- {
- prevPos = currPos;
- currPos.x += velX * tmeL.tmeStep;
- currPos.y += velY * tmeL.tmeStep;
- }
- }
- // Interpolation
- interpPos.x = currPos.x * tmeL.alpha + prevPos.x * (1 - tmeL.alpha);
- interpPos.y = currPos.y * tmeL.alpha + prevPos.y * (1 - tmeL.alpha);
- realPos.x = floor(interpPos.x + 0.5f);
- realPos.y = floor(interpPos.y + 0.5f);
- sp.setPosition(realPos.x, realPos.y);
- }
- void respawn()
- {
- if(realPos.x > 540)
- {
- sp.setPosition(0, 0);
- currPos.x = 0;
- currPos.y = 0;
- prevPos = currPos;
- }
- }
- };
- void main()
- {
- RenderWindow wIn;
- wIn.create(VideoMode(600, 500), "My12345", Style::Default);
- moveSpriteLogic mspL("tree.png", 0.00015f, 0);
- Sprite &sp = mspL.sp;
- Event ev;
- tmeL.startTme();
- while (wIn.isOpen())
- {
- tmeL.newFrame();
- while (wIn.pollEvent(ev))
- {
- // Close window : exit
- if (ev.type == Event::Closed)
- wIn.close();
- if (ev.type == Event::KeyPressed)
- {
- if (ev.key.code == Keyboard::Escape)
- wIn.close();
- else if (ev.key.control && ev.key.code == Keyboard::C)
- wIn.close();
- }
- }
- wIn.clear();
- // interpolation move sprite
- mspL.move();
- mspL.respawn();
- wIn.draw(sp);
- wIn.display();
- // reduce CPU charge
- // value must be in milliseconds
- // 1 millsec = 1000 microsec
- sleep(milliseconds(1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement