Browse Source

create style file

master
sauer2 2 years ago
parent
commit
b44f1cb545
6 changed files with 106 additions and 110 deletions
  1. 4
    0
      .clang-format
  2. BIN
      game
  3. 55
    59
      main.cpp
  4. 2
    3
      main.hpp
  5. 32
    34
      mainmenu.cpp
  6. 13
    14
      mainmenu.hpp

+ 4
- 0
.clang-format View File

@@ -0,0 +1,4 @@
---
Language: Cpp
IndentWidth: 4
NamespaceIndentation: All

BIN
game View File


+ 55
- 59
main.cpp View File

@@ -1,15 +1,16 @@
#include <iostream>
#include <string>

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

#include "main.hpp"
#include "mainmenu.hpp"

namespace Game {
// because we want to create the objects in init(), we use the default constructor, so nothing can go wrong here
// because we want to create the objects in init(), we use the default
// constructor, so nothing can go wrong here
sf::RenderWindow window{};

GameState state = GameState::MainMenu;
@@ -17,111 +18,106 @@ namespace Game {
sf::Font font;

bool init() {
sf::ContextSettings settings;
// set antialising for shapes
// this doesnt affect textures
// (requires sf::Texture::setSmooth() )
settings.antialiasingLevel = 4;
window.create(sf::VideoMode(xResolution, yResolution, colorDepth), gameTitle, sf::Style::Default, settings);

window.create(sf::VideoMode(xResolution, yResolution, colorDepth),
gameTitle, sf::Style::Default, settings);
window.setPosition(sf::Vector2i(0, 0));
// hide mouse cursor
window.setMouseCursorVisible(false);
if(!window.isOpen()) {
if (!window.isOpen()) {
std::cout << "Unable to open window object. Aborting execution.";
return false;
}

if (!font.loadFromFile("Audiowide-Regular.ttf")) {
if (!font.loadFromFile("Audiowide-Regular.ttf")) {
std::cout << "Unable to load font. Aborting execution.";
return false;
}

menu.init(font);
return true;
}

void update() {
switch(state) {
case GameState::Game:
// state = update game with updateTimestep.asSeconds()
if(state != GameState::Game) {
// game.uninit();
}
break;
case GameState::MainMenu:
state = menu.update(window);
if(state == GameState::Game) {
// start game
}
break;
case GameState::Exit:
window.close();
break;
switch (state) {
case GameState::Game:
// state = update game with updateTimestep.asSeconds()
if (state != GameState::Game) {
// game.uninit();
}
break;
case GameState::MainMenu:
state = menu.update(window);
if (state == GameState::Game) {
// start game
}
break;
case GameState::Exit:
window.close();
break;
}
}

void draw(float lagTime) {
window.clear(sf::Color::Black);
switch(state) {
case GameState::Game:
// draw game with lagTime
break;
case GameState::MainMenu:
menu.draw(window);
break;
case GameState::Exit:
break;
switch (state) {
case GameState::Game:
// draw game with lagTime
break;
case GameState::MainMenu:
menu.draw(window);
break;
case GameState::Exit:
break;
}
window.display();
}

void mainloop() {
sf::Clock clock;
sf::Time previousTime = sf::milliseconds(0);
sf::Time lagTime = sf::milliseconds(0);
while(window.isOpen()) {
while (window.isOpen()) {
sf::Time currentTime = clock.getElapsedTime();
sf::Time elapsedTime = currentTime - previousTime;
previousTime = currentTime;
lagTime += elapsedTime;
while (lagTime >= updateTimestep) {
update();
lagTime -= updateTimestep;
}
draw(lagTime.asSeconds());
}
}

int run() {
if(!init()) return 1;
if (!init())
return 1;

mainloop();
return 0;
}

}



int main() {
return Game::run();
}
int main() { return Game::run(); }

+ 2
- 3
main.hpp View File

@@ -5,11 +5,10 @@

#pragma once


namespace Game {

enum class GameState { Game, MainMenu, Exit };
// configuration
const int xResolution = 1150;
const int yResolution = 750;
@@ -17,7 +16,7 @@ namespace Game {
const int colorDepth = 32;
const std::string gameTitle = "Proto4";
const sf::Time updateTimestep = sf::seconds(0.0004);
const float middleX = xResolution / 2;
const float middleY = yResolution / 2;
}

+ 32
- 34
mainmenu.cpp View File

@@ -1,59 +1,57 @@
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

#include "mainmenu.hpp"
#include "main.hpp"
#include "mainmenu.hpp"

namespace Game {

void MainMenu::draw(sf::RenderWindow &window) {
window.setView(view);
window.draw(mainLine);
window.draw(entry1);
window.draw(entry2);
window.draw(mainLine);
window.draw(entry1);
window.draw(entry2);
}

GameState MainMenu::update(sf::RenderWindow &window) {
sf::Event event;
while( window.pollEvent(event) ) {
if( event.type == sf::Event::Closed ) {
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
else if(event.type == sf::Event::Resized) {
//resize the views, so everything is still displayed proportionally
} else if (event.type == sf::Event::Resized) {
// resize the views, so everything is still displayed
// proportionally
view.setSize(event.size.width, event.size.height);
view.setCenter(event.size.width/2, event.size.height/2);
view.setCenter(event.size.width / 2, event.size.height / 2);
} else if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Q)
return GameState::Exit;
if (event.key.code == sf::Keyboard::S)
return GameState::Game;
}
else if(event.type == sf::Event::KeyPressed) {
if(event.key.code == sf::Keyboard::Q) return GameState::Exit;
if(event.key.code == sf::Keyboard::S) return GameState::Game;
}
}

return GameState::MainMenu;
}
void MainMenu::init(sf::Font &font) {
font = font;
font = font;
view.setCenter(middleX, middleY);
view.setSize(xResolution, yResolution);
view.setViewport(sf::FloatRect(0, 0, 1, 1));
mainLine.setString("Proto 4");
entry1.setString("[S] Start New Game");
entry2.setString("[Q] Quit Game");

mainLine.setFont(font);
entry1.setFont(font);
entry2.setFont(font);

mainLine.setPosition(32.f, 32.f);
entry1.setPosition(32.f, 96.f);
entry2.setPosition(32.f, 160.f);
}

}
mainLine.setString("Proto 4");
entry1.setString("[S] Start New Game");
entry2.setString("[Q] Quit Game");

mainLine.setFont(font);
entry1.setFont(font);
entry2.setFont(font);

mainLine.setPosition(32.f, 32.f);
entry1.setPosition(32.f, 96.f);
entry2.setPosition(32.f, 160.f);
}
}

+ 13
- 14
mainmenu.hpp View File

@@ -1,26 +1,25 @@
#pragma once

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

#include "main.hpp"

namespace Game {
class MainMenu {
public:
void draw(sf::RenderWindow &window);
GameState update(sf::RenderWindow &window);
void init(sf::Font &font);
private:
sf::View view{};
sf::Font *font = nullptr;
public:
void draw(sf::RenderWindow &window);
GameState update(sf::RenderWindow &window);
void init(sf::Font &font);

sf::Text mainLine;
sf::Text entry1;
sf::Text entry2;
};
private:
sf::View view{};
sf::Font *font = nullptr;

sf::Text mainLine;
sf::Text entry1;
sf::Text entry2;
};
}

Loading…
Cancel
Save