very early prototype of a spaceshooter in SFML
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cpp 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <iostream>
  2. #include <string>
  3. #include <SFML/System.hpp>
  4. #include <SFML/Window.hpp>
  5. #include <SFML/Graphics.hpp>
  6. #include "main.hpp"
  7. namespace Game {
  8. // because we want to create the objects in init(), we use the default constructor, so nothing can go wrong here
  9. sf::RenderWindow window{};
  10. GameState state = GameState::MainMenu;
  11. bool init() {
  12. sf::ContextSettings settings;
  13. // set antialising for shapes
  14. // this doesnt affect textures
  15. // (requires sf::Texture::setSmooth() )
  16. settings.antialiasingLevel = 4;
  17. window.create(sf::VideoMode(xResolution, yResolution, colorDepth), gameTitle, sf::Style::Default, settings);
  18. window.setPosition(sf::Vector2i(0, 0));
  19. // hide mouse cursor
  20. window.setMouseCursorVisible(false);
  21. if(!window.isOpen()) {
  22. std::cout << "Unable to open window object. Aborting execution.";
  23. return false;
  24. }
  25. return true;
  26. }
  27. void update() {
  28. switch(state) {
  29. case GameState::Game:
  30. // state = update game with updateTimestep.asSeconds()
  31. if(state != GameState::Game) {
  32. // game.uninit();
  33. }
  34. break;
  35. case GameState::MainMenu:
  36. // state = update menu
  37. if(state == GameState::Game) {
  38. // start a game
  39. }
  40. break;
  41. case GameState::Exit:
  42. window.close();
  43. break;
  44. }
  45. }
  46. void draw(float lagTime) {
  47. window.clear(sf::Color::Black);
  48. switch(state) {
  49. case GameState::Game:
  50. // draw game with lagTime
  51. break;
  52. case GameState::MainMenu:
  53. // draw main menu
  54. break;
  55. case GameState::Exit:
  56. break;
  57. }
  58. window.display();
  59. }
  60. void mainloop() {
  61. sf::Clock clock;
  62. sf::Time previousTime = sf::milliseconds(0);
  63. sf::Time lagTime = sf::milliseconds(0);
  64. while(window.isOpen()) {
  65. sf::Time currentTime = clock.getElapsedTime();
  66. sf::Time elapsedTime = currentTime - previousTime;
  67. previousTime = currentTime;
  68. lagTime += elapsedTime;
  69. while (lagTime >= updateTimestep) {
  70. update();
  71. lagTime -= updateTimestep;
  72. }
  73. draw(lagTime.asSeconds());
  74. }
  75. }
  76. int run() {
  77. if(!init()) return 1;
  78. mainloop();
  79. return 0;
  80. }
  81. }
  82. int main() {
  83. return Game::run();
  84. }