Browse Source

move gameView to Game, add Game:handleKey, cleanup

master
noerw 1 year ago
parent
commit
2ddef99993
6 changed files with 60 additions and 46 deletions
  1. 14
    19
      background.cpp
  2. 20
    1
      game.cpp
  3. 4
    0
      game.hpp
  4. 20
    23
      main.cpp
  5. 1
    2
      mainmenu.cpp
  6. 1
    1
      mainmenu.hpp

+ 14
- 19
background.cpp View File

@@ -3,11 +3,10 @@
#include <SFML/Window.hpp>

#include "background.hpp"
#include "main.hpp"

namespace Proto4 {
void Background::init(sf::Texture bg0, sf::Texture bg1,
sf::View &gameView) {
sf::View &rootView) {
bg0Tex = bg0;
bg1Tex = bg1;
bg0Tex.setRepeated(true);
@@ -17,31 +16,27 @@ namespace Proto4 {
bg0Sprite.setTexture(bg0Tex);
bg1Sprite.setTexture(bg1Tex);

bg0View = bg1View = gameView;
bg0View = bg1View = rootView;
bg0Size = bg0Tex.getSize().x;
bg1Size = bg1Tex.getSize().x;
bg0Sprite.setTextureRect(sf::IntRect(0, 0, xResolution + 2 * bg0Size,
yResolution + 2 * bg0Size));
bg1Sprite.setTextureRect(sf::IntRect(0, 0, xResolution + 2 * bg1Size,
yResolution + 2 * bg1Size));
// bg0Sprite.setPosition(window.mapPixelToCoords(sf::Vector2i(-bg0Size,
// -bg0Size), bg0View));
// bg1Sprite.setPosition(window.mapPixelToCoords(sf::Vector2i(-bg1Size,
// -bg1Size), bg1View));
bg0Sprite.setPosition(gameView.getCenter() - gameView.getSize() * 0.5f);
bg1Sprite.setPosition(gameView.getCenter() - gameView.getSize() * 0.5f);
sf::Vector2f rootViewSize = rootView.getSize();
bg0Sprite.setTextureRect(sf::IntRect(0, 0,
rootViewSize.x + 2 * bg0Size,
rootViewSize.y + 2 * bg0Size));
bg1Sprite.setTextureRect(sf::IntRect(0, 0,
rootViewSize.x + 2 * bg0Size,
rootViewSize.y + 2 * bg0Size));
bg0Sprite.setPosition(rootView.getCenter() - rootViewSize * 0.5f);
bg1Sprite.setPosition(rootView.getCenter() - rootViewSize * 0.5f);
}

void Background::update(sf::View &gameView, sf::RenderTarget &window) {
void Background::update(sf::View &rootView, sf::RenderTarget &window) {
// parallax
bg0View.setCenter(gameView.getCenter() / 3.5f);
bg1View.setCenter(gameView.getCenter() / 2.f);
bg0View.setCenter(rootView.getCenter() / 3.5f);
bg1View.setCenter(rootView.getCenter() / 2.f);

// move endless backgrounds if parallax has moved one -step, to offset
// parallax-movement
// sf::Vector2f bg0Pos = gameView.getCenter() - bg0View.getCenter(); //
// alternative solution without coord2pixel transformation? doesnt work
// currently
sf::Vector2i bg0Pos =
window.mapCoordsToPixel(bg0Sprite.getPosition(), bg0View);
if (bg0Pos.x >= 0)

+ 20
- 1
game.cpp View File

@@ -2,17 +2,29 @@
#include <SFML/System.hpp>
#include <SFML/Window.hpp>

#include "main.hpp"
#include "game.hpp"

namespace Proto4 {

bool Game::init() { return true; }
bool Game::init() {
mainView.setCenter(0, 0);
mainView.setSize(xResolution, yResolution);
return true;
}

AppState Game::handleKey(const sf::Event &event) {
if (event.key.code == sf::Keyboard::Q)
return AppState::MainMenu;
return AppState::Game;
}

AppState Game::update(sf::RenderWindow &window, sf::Time timestep) {
return AppState::Game;
}

void Game::draw(sf::RenderTarget &window, sf::RenderStates states) const {
window.setView(mainView);
window.draw(player);
}

@@ -23,4 +35,11 @@ namespace Proto4 {
bool Game::resume() { return true; }

bool Game::reset() { return true; }

void Game::resize(sf::Event &resizeEvent) {
mainView.setSize(resizeEvent.size.width, resizeEvent.size.height);
mainView.setCenter(resizeEvent.size.width / 2, resizeEvent.size.height / 2);
}

sf::View& Game::getMainView() { return mainView; }
}

+ 4
- 0
game.hpp View File

@@ -16,6 +16,9 @@ namespace Proto4 {
public:
virtual void draw(sf::RenderTarget &window, sf::RenderStates states) const;
AppState update(sf::RenderWindow &window, sf::Time timestep);
AppState handleKey(const sf::Event &event);
void resize(sf::Event& resizeEvent);
sf::View& getMainView();

bool init();

@@ -25,6 +28,7 @@ namespace Proto4 {
bool reset();

private:
sf::View mainView{};
GameState gamestate = GameState::Uninitialized;
Player player{};
uint32_t score = 0;

+ 20
- 23
main.cpp View File

@@ -6,9 +6,9 @@
#include <SFML/System.hpp>
#include <SFML/Window.hpp>

#include "main.hpp"
#include "background.hpp"
#include "game.hpp"
#include "main.hpp"
#include "mainmenu.hpp"

namespace Proto4 {
@@ -20,7 +20,6 @@ namespace Proto4 {
MainMenu menu{};
Game game{};
Background background{};
sf::View gameView;
sf::Texture bg0, bg1;
sf::Font font;

@@ -38,19 +37,14 @@ namespace Proto4 {
return false;
}

gameView.setCenter(middleX, middleY);
gameView.setSize(xResolution, yResolution);
gameView.setViewport(sf::FloatRect(0, 0, 1, 1));

background.init(bg0, bg1, gameView);
background.init(bg0, bg1, game.getMainView());

if (!menu.init(font) || !game.init()) {
return false;
}

// set antialising for shapes
// this doesnt affect textures
// (requires sf::Texture::setSmooth() )
// this doesnt affect textures (requires sf::Texture::setSmooth())
sf::ContextSettings settings;
settings.antialiasingLevel = 4;

@@ -79,13 +73,14 @@ namespace Proto4 {
background.resize(event);

switch (state) {

case AppState::MainMenu:
menu.resize(event);
break;

/* TODO: add a resize method to the game object, similar to
* the one in mainmenu */
case AppState::Game:
game.resize(event);
break;

default:
break;
}
@@ -94,9 +89,11 @@ namespace Proto4 {
case sf::Event::KeyPressed:
switch (state) {
case AppState::MainMenu:
state = menu.update(window, timestep, event);
state = menu.handleKey(event);
break;
case AppState::Game:
state = game.handleKey(event);
break;
/* TODO: add keyboard input queue for game */
default:
break;
}
@@ -106,35 +103,35 @@ namespace Proto4 {
}
}

// (temporary!) move camera to show off parallax background
gameView.setCenter(sf::Vector2f(
gameView.getCenter().x +
timestep.asSeconds() * 100 * std::sin(currentTime.asSeconds()),
gameView.getCenter().y + timestep.asSeconds() * 100));
background.update(gameView, window);

// Update everything that gets updated without events...
sf::View &gameView = game.getMainView();
switch (state) {
case AppState::MainMenu:
// move camera for background movement
gameView.setCenter(sf::Vector2f(
gameView.getCenter().x + timestep.asSeconds() * 100 * std::sin(currentTime.asSeconds()),
gameView.getCenter().y - timestep.asSeconds() * 100));
break;
case AppState::Game:
state = game.update(window, timestep);
if (state != AppState::Game) {
game.reset();
}
break;

case AppState::Exit:
window.close();
break;
default:
break;
}

background.update(gameView, window);
}

void draw() {

window.clear(sf::Color::Black);
window.draw(background);
window.setView(gameView);

switch (state) {
case AppState::Game:

+ 1
- 2
mainmenu.cpp View File

@@ -38,8 +38,7 @@ namespace Proto4 {
}

// Unlike in game, this gets only called on keypresses.
AppState MainMenu::update(sf::RenderWindow &window, sf::Time timestep,
sf::Event keyEvent) {
AppState MainMenu::handleKey(const sf::Event &keyEvent) {
if (keyEvent.key.code == sf::Keyboard::Q)
return AppState::Exit;
if (keyEvent.key.code == sf::Keyboard::S)

+ 1
- 1
mainmenu.hpp View File

@@ -11,7 +11,7 @@ namespace Proto4 {
class MainMenu : public sf::Drawable, public sf::Transformable {
public:
bool init(sf::Font &font);
AppState update(sf::RenderWindow &window, sf::Time timestep, sf::Event keyEvent);
AppState handleKey(const sf::Event &keyEvent);
virtual void draw(sf::RenderTarget &window, sf::RenderStates states) const;
void resize(sf::Event& resizeEvent);


Loading…
Cancel
Save