@@ -18,7 +18,10 @@ class Physics {
1818 Pair velocity;
1919public:
2020 // / CONSTRUCTORS
21- Physics () {}
21+ Physics () {
22+ position = {0 ,0 };
23+ velocity = {0 ,0 };
24+ }
2225 Physics (const Physics& state) {
2326 this ->position = state.position ;
2427 this ->velocity = state.velocity ;
@@ -36,7 +39,9 @@ class Physics {
3639 void UpdatePosition () {
3740 this ->position .x += velocity.x ;
3841 this ->position .y += velocity.y ;
39-
42+ }
43+ Pair GetPosition () {
44+ return position;
4045 }
4146 friend std::ostream& operator <<(std::ostream& out,const Physics& state);
4247};
@@ -48,26 +53,37 @@ std::ostream& operator<<(std::ostream& out, const Physics& state) {
4853// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
4954class SpaceShip {
5055private:
56+ sf::CircleShape triangle{10 .0f ,3 };
5157 Collider collider;
5258 Physics physics;
53- double direction;
5459 double fuel,energy,ore;
5560public:
61+ SpaceShip (Physics physics, double fuel, double energy, double ore) {
62+ this ->triangle .setOrigin ({this ->triangle .getRadius (), this ->triangle .getRadius ()});
63+ this ->physics = physics;
64+ this ->fuel = fuel;
65+ this ->energy = energy;
66+ this ->ore = ore;
67+ }
5668 SpaceShip (Physics physics,Collider collider,double direction, double fuel, double energy, double ore) {
5769 this ->collider = collider;
5870 this ->physics = physics;
59- this ->direction = direction;
6071 this ->fuel = fuel;
6172 this ->energy = energy;
6273 this ->ore = ore;
6374 }
75+ sf::CircleShape& GetShape () {
76+ return triangle;
77+ }
78+ Physics& GetPhysics () {
79+ return physics;
80+ }
6481 friend std::ostream& operator <<(std::ostream& out,SpaceShip ship);
6582};
6683std::ostream& operator <<(std::ostream& out,SpaceShip ship) {
6784 out<<" SHIP\n " ;
6885 out<<ship.physics <<' \n ' ;
6986 out<<" Stats:\n " ;
70- out<<" Direction:" <<ship.direction <<' \n ' ;
7187 out<<" Fuel:" <<ship.fuel <<" Energy:" <<ship.energy <<" ORE:" <<ship.ore <<' \n ' ;
7288 return out;
7389}
@@ -220,97 +236,86 @@ std::ostream& operator<<(std::ostream& out,Universe universe) {
220236}
221237// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
222238int main () {
223- // /
224- std::cout << " Hello, world!\n " ;
225- std::array<int , 100 > v{};
226- int nr;
227- std::cout << " Introduceți nr: " ;
228- // ///////////////////////////////////////////////////////////////////////
229- // / Observație: dacă aveți nevoie să citiți date de intrare de la tastatură,
230- // / dați exemple de date de intrare folosind fișierul tastatura.txt
231- // / Trebuie să aveți în fișierul tastatura.txt suficiente date de intrare
232- // / (în formatul impus de voi) astfel încât execuția programului să se încheie.
233- // / De asemenea, trebuie să adăugați în acest fișier date de intrare
234- // / pentru cât mai multe ramuri de execuție.
235- // / Dorim să facem acest lucru pentru a automatiza testarea codului, fără să
236- // / mai pierdem timp de fiecare dată să introducem de la zero aceleași date de intrare.
237- // /
238- // / Pe GitHub Actions (bife), fișierul tastatura.txt este folosit
239- // / pentru a simula date introduse de la tastatură.
240- // / Bifele verifică dacă programul are erori de compilare, erori de memorie și memory leaks.
241- // /
242- // / Dacă nu puneți în tastatura.txt suficiente date de intrare, îmi rezerv dreptul să vă
243- // / testez codul cu ce date de intrare am chef și să nu pun notă dacă găsesc vreun bug.
244- // / Impun această cerință ca să învățați să faceți un demo și să arătați părțile din
245- // / program care merg (și să le evitați pe cele care nu merg).
246- // /
247- // ///////////////////////////////////////////////////////////////////////
248- std::cin >> nr;
249- // ///////////////////////////////////////////////////////////////////////
250- for (int i = 0 ; i < nr; ++i) {
251- std::cout << " v[" << i << " ] = " ;
252- std::cin >> v[i];
253- }
254- std::cout << " \n\n " ;
255- std::cout << " Am citit de la tastatură " << nr << " elemente:\n " ;
256- for (int i = 0 ; i < nr; ++i) {
257- std::cout << " - " << v[i] << " \n " ;
258- }
259- // /////////////////////////////////////////////////////////////////////////
260- // / Pentru date citite din fișier, NU folosiți tastatura.txt. Creați-vă voi
261- // / alt fișier propriu cu ce alt nume doriți.
262- // / Exemplu:
263- // / std::ifstream fis("date.txt");
264- // / for(int i = 0; i < nr2; ++i)
265- // / fis >> v2[i];
266- // /
267- // /////////////////////////////////////////////////////////////////////////
268-
269239 sf::RenderWindow window;
270- // /////////////////////////////////////////////////////////////////////////
271240 // / NOTE: sync with env variable APP_WINDOW from .github/workflows/cmake.yml:31
272- window.create (sf::VideoMode ({800 , 700 }), " My Window" , sf::Style::Default);
273- // /////////////////////////////////////////////////////////////////////////
241+ window.create (sf::VideoMode ({800 , 800 }), " My Window" , sf::Style::Default);
274242 std::cout << " Fereastra a fost creată\n " ;
275- // /////////////////////////////////////////////////////////////////////////
276- // / NOTE: mandatory use one of vsync or FPS limit (not both) ///
277- // / This is needed so we do not burn the GPU ///
278- window.setVerticalSyncEnabled (true ); // /
279- // / window.setFramerateLimit(60); ///
280- // /////////////////////////////////////////////////////////////////////////
243+ window.setFramerateLimit (60 );// window.setVerticalSyncEnabled(true);
244+ sf::View view (sf::FloatRect ({0 , 0 }, {800 , 800 }));
245+ window.setView (view);
246+
247+ sf::Font font (" jetbrains.ttf" );
248+ sf::Text debugText (font);
249+ debugText.setFont (font);
250+ debugText.setCharacterSize (18 );
251+ debugText.setFillColor (sf::Color::White);
252+ debugText.setPosition ({10 , 10 });
253+
254+ Physics physics;
255+ SpaceShip player{physics,100 ,100 ,100 };
256+ player.GetShape ().setPosition ({400 .0f ,400 .0f });
281257
282258 while (window.isOpen ()) {
283259 bool shouldExit = false ;
284-
285260 while (const std::optional event = window.pollEvent ()) {
286- if (event->is <sf::Event::Closed>()) {
261+ if (event->is <sf::Event::Closed>())
287262 window.close ();
288- std::cout << " Fereastra a fost închisă\n " ;
289- }
290- else if (event->is <sf::Event::Resized>()) {
291- std::cout << " New width: " << window.getSize ().x << ' \n '
292- << " New height: " << window.getSize ().y << ' \n ' ;
263+ else
264+ if (event->is <sf::Event::Resized>()) {
265+ const auto * resize = event->getIf <sf::Event::Resized>();
266+ float newWidth = static_cast <float >(resize->size .x );
267+ float newHeight = static_cast <float >(resize->size .y );
268+ sf::FloatRect visibleArea ({0 .f , 0 .f }, {newWidth, newHeight});
269+ view = sf::View (visibleArea);
270+ window.setView (view);
271+
272+ player.GetShape ().setPosition ({newWidth / 2 .f , newHeight / 2 .f });
273+
274+ std::cout << " x nou: " << newWidth << ' \n '
275+ << " y nou: " << newHeight << ' \n ' ;
293276 }
294277 else if (event->is <sf::Event::KeyPressed>()) {
295278 const auto * keyPressed = event->getIf <sf::Event::KeyPressed>();
296279 std::cout << " Received key " << (keyPressed->scancode == sf::Keyboard::Scancode::X ? " X" : " (other)" ) << " \n " ;
297280 if (keyPressed->scancode == sf::Keyboard::Scancode::Escape) {
298281 shouldExit = true ;
299282 }
283+ else if (keyPressed->scancode == sf::Keyboard::Scancode::Up) {
284+ std::cout << " Up arrow pressed\n " ;
285+ }
286+ else if (keyPressed->scancode == sf::Keyboard::Scancode::Down) {
287+ std::cout << " Down arrow pressed\n " ;
288+ }
300289 }
301290 }
302291 if (shouldExit) {
303292 window.close ();
304293 std::cout << " Fereastra a fost închisă (shouldExit == true)\n " ;
305294 break ;
306295 }
307- using namespace std ::chrono_literals;
308- std::this_thread::sleep_for (300ms);
309-
296+ // using namespace std::chrono_literals;
297+ // std::this_thread::sleep_for(300ms);
310298 window.clear ();
299+
300+ if (sf::Keyboard::isKeyPressed (sf::Keyboard::Key::Left)){
301+ player.GetShape ().rotate (sf::degrees (-10 .f ));
302+ }
303+ if (sf::Keyboard::isKeyPressed (sf::Keyboard::Scan::Right)){
304+ player.GetShape ().rotate (sf::degrees (10 .f ));
305+ }
306+
307+ window.draw (player.GetShape ());
308+
309+ std::string posText =" x: " + std::to_string (player.GetPhysics ().GetPosition ().x ) +" y: " + std::to_string (player.GetPhysics ().GetPosition ().y );
310+ debugText.setString (posText);
311+ window.draw (debugText);
312+
311313 window.display ();
312314 }
313-
315+ // / TODO : solar systems can generate on eachother
316+ // / TODO : move spaceship
317+ // / TODO : show celestial bodies when they are in view
318+ // / TODO : move things from main into functions
314319 std::cout << " Programul a terminat execuția\n " ;
315320 return 0 ;
316321}
0 commit comments