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;
- // Перемещение на среднюю величину 2ух кадров
- //
- class ClockTme
- {
- LARGE_INTEGER ticksBase, freq;
- public:
- Int64 getTicks()
- {
- QueryPerformanceCounter(&ticksBase);
- return ticksBase.QuadPart;
- }
- Int64 getFrequency()
- {
- return freq.QuadPart;
- }
- ClockTme()
- {
- QueryPerformanceFrequency(&freq);
- }
- };
- class FrameTme
- {
- Int64 ticks, ticksPrev, frequency, elapsedTicks, elTmeMicrosec;
- ClockTme cl;
- void setElapsedTicks()
- {
- if(ticks > ticksPrev)
- elapsedTicks = ticks - ticksPrev;
- // произошло обнуление
- // произойдет примерно через 50 лет
- else if(ticks < ticksPrev)
- elapsedTicks = _I64_MAX - ticksPrev + ticks;
- }
- void setElTmeMicrosec()
- {
- elTmeMicrosec = elapsedTicks * 1000000 / frequency;
- }
- public:
- void updateTme()
- {
- ticksPrev = ticks;
- ticks = cl.getTicks();
- setElapsedTicks();
- setElTmeMicrosec();
- }
- // вызывать после обновление времени updateTme()
- Int64 getElapsedTicks()
- {
- return elapsedTicks;
- }
- // вызывать после обновление времени updateTme()
- Int64 getElTmeMicrosec()
- {
- return elTmeMicrosec;
- }
- void restartTme()
- {
- ticks = cl.getTicks();
- }
- FrameTme()
- {
- frequency = cl.getFrequency();
- ticks = cl.getTicks();
- }
- };
- struct State
- {
- float x, y;
- };
- class midStateSprite
- {
- State prevState;
- State currState;
- float midX, midY, velX, velY;
- public:
- Texture tx;
- Sprite sp;
- midStateSprite(char* txFile, float velocityX, float velocityY)
- {
- tx.loadFromFile(txFile);
- sp.setTexture(tx);
- currState.x = sp.getPosition().x;
- currState.y = sp.getPosition().y;
- prevState = currState;
- velX = velocityX;
- velY = velocityY;
- }
- void setTexture(char* txFile)
- {
- tx.loadFromFile(txFile);
- sp.setTexture(tx);
- }
- void updateStatesAndMove(Int64 &elTmeMicros)
- {
- prevState = currState;
- currState.x += velX * elTmeMicros;
- currState.y += velY * elTmeMicros;
- midX = (prevState.x + currState.x) / 2;
- midY = (prevState.y + currState.y) / 2;
- sp.setPosition(midX, midY);
- }
- };
- void main()
- {
- int wWid = 600, wHei = 500;
- RenderWindow wIn(VideoMode(wWid, wHei), "My12345", Style::Default);
- FrameTme frTm;
- Int64 elTmeMicros;
- midStateSprite mStSp("tree.png", 0.0001f, 0);
- frTm.restartTme();
- Event ev;
- while (wIn.isOpen())
- {
- 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();
- frTm.updateTme();
- elTmeMicros = frTm.getElTmeMicrosec();
- if(elTmeMicros > 25000)
- elTmeMicros = 25000;
- mStSp.updateStatesAndMove(elTmeMicros);
- wIn.draw(mStSp.sp);
- wIn.display();
- // убираю нагрузку с процессора
- sleep(microseconds(1000));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement