Browse Source

remove remaining onevent

master
sauer2 2 years ago
parent
commit
e210448f24
6 changed files with 109 additions and 111 deletions
  1. +47
    -24
      background.cpp
  2. +6
    -35
      game.cpp
  3. +0
    -1
      game.hpp
  4. +36
    -26
      main.cpp
  5. +18
    -23
      mainmenu.cpp
  6. +2
    -2
      mainmenu.hpp

+ 47
- 24
background.cpp View File

@@ -6,7 +6,8 @@
#include "main.hpp"

namespace Proto4 {
void Background::init(sf::Texture bg0, sf::Texture bg1, sf::View& gameView) {
void Background::init(sf::Texture bg0, sf::Texture bg1,
sf::View &gameView) {
bg0Tex = bg0;
bg1Tex = bg1;
bg0Tex.setRepeated(true);
@@ -19,45 +20,67 @@ namespace Proto4 {
bg0View = bg1View = gameView;
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.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);
}

void Background::update(sf::View& gameView, sf::RenderTarget& window) {
//parallax
void Background::update(sf::View &gameView, sf::RenderTarget &window) {
// parallax
bg0View.setCenter(gameView.getCenter() / 3.5f);
bg1View.setCenter(gameView.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) bg0Sprite.move(sf::Vector2f(-bg0Size, 0));
else if (bg0Pos.x <= -2 * bg0Size) bg0Sprite.move(sf::Vector2f( bg0Size, 0));
if (bg0Pos.y >= 0) bg0Sprite.move(sf::Vector2f(0,-bg0Size));
else if (bg0Pos.y <= -2 * bg0Size) bg0Sprite.move(sf::Vector2f(0, bg0Size));

sf::Vector2i bg1Pos = window.mapCoordsToPixel(bg1Sprite.getPosition(), bg1View);
if (bg1Pos.x >= 0) bg1Sprite.move(sf::Vector2f(-bg1Size, 0));
else if (bg1Pos.x <= -2 * bg1Size) bg1Sprite.move(sf::Vector2f( bg1Size, 0));
if (bg1Pos.y >= 0) bg1Sprite.move(sf::Vector2f(0,-bg1Size));
else if (bg1Pos.y <= -2 * bg1Size) bg1Sprite.move(sf::Vector2f(0, bg1Size));
// 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)
bg0Sprite.move(sf::Vector2f(-bg0Size, 0));
else if (bg0Pos.x <= -2 * bg0Size)
bg0Sprite.move(sf::Vector2f(bg0Size, 0));
if (bg0Pos.y >= 0)
bg0Sprite.move(sf::Vector2f(0, -bg0Size));
else if (bg0Pos.y <= -2 * bg0Size)
bg0Sprite.move(sf::Vector2f(0, bg0Size));

sf::Vector2i bg1Pos =
window.mapCoordsToPixel(bg1Sprite.getPosition(), bg1View);
if (bg1Pos.x >= 0)
bg1Sprite.move(sf::Vector2f(-bg1Size, 0));
else if (bg1Pos.x <= -2 * bg1Size)
bg1Sprite.move(sf::Vector2f(bg1Size, 0));
if (bg1Pos.y >= 0)
bg1Sprite.move(sf::Vector2f(0, -bg1Size));
else if (bg1Pos.y <= -2 * bg1Size)
bg1Sprite.move(sf::Vector2f(0, bg1Size));
}

void Background::draw(sf::RenderTarget& target, sf::RenderStates states) const {
void Background::draw(sf::RenderTarget &target,
sf::RenderStates states) const {
target.setView(bg0View);
target.draw(bg0Sprite);
target.setView(bg1View);
target.draw(bg1Sprite);
}

void Background::resize(sf::Event& resizeEvent) {
void Background::resize(sf::Event &resizeEvent) {
bg0View.setSize(resizeEvent.size.width, resizeEvent.size.height);
bg1View.setSize(resizeEvent.size.width, resizeEvent.size.height);
bg0Sprite.setTextureRect(sf::IntRect(0, 0, resizeEvent.size.width + 2 * bg0Size, resizeEvent.size.height + 2 * bg0Size));
bg1Sprite.setTextureRect(sf::IntRect(0, 0, resizeEvent.size.width + 2 * bg1Size, resizeEvent.size.height + 2 * bg1Size));
bg0Sprite.setTextureRect(
sf::IntRect(0, 0, resizeEvent.size.width + 2 * bg0Size,
resizeEvent.size.height + 2 * bg0Size));
bg1Sprite.setTextureRect(
sf::IntRect(0, 0, resizeEvent.size.width + 2 * bg1Size,
resizeEvent.size.height + 2 * bg1Size));
}
}

+ 6
- 35
game.cpp View File

@@ -6,48 +6,19 @@

namespace Proto4 {

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

AppState Game::onEvent(sf::Event& event) {
switch (event.type) {
case 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);
break;
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::Q) return AppState::MainMenu;
break;
default:
break;
}

return AppState::Game;
}
bool Game::init() { return true; }

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

void Game::draw(sf::RenderTarget &window, sf::RenderStates states) const {
}
void Game::draw(sf::RenderTarget &window, sf::RenderStates states) const {}

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

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

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

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

+ 0
- 1
game.hpp View File

@@ -14,7 +14,6 @@ namespace Proto4 {
public:
virtual void draw(sf::RenderTarget &window, sf::RenderStates states) const;
AppState update(sf::RenderWindow &window, sf::Time timestep);
AppState onEvent(sf::Event& event);

bool init();


+ 36
- 26
main.cpp View File

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

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

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

namespace Proto4 {

@@ -31,9 +31,10 @@ namespace Proto4 {
return false;
}


if (!bg0.loadFromFile("img/bg_grid_48.png") || !bg1.loadFromFile("img/bg_grid_72.png")) {
std::cout << "Unable to load background images. Aborting execution.";
if (!bg0.loadFromFile("img/bg_grid_48.png") ||
!bg1.loadFromFile("img/bg_grid_72.png")) {
std::cout
<< "Unable to load background images. Aborting execution.";
return false;
}

@@ -43,10 +44,7 @@ namespace Proto4 {

background.init(bg0, bg1, gameView);

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

@@ -76,22 +74,40 @@ namespace Proto4 {
// poll for events and pass them to components
sf::Event event;
while (window.pollEvent(event)) {
//switch (event.type) {
//case sf::Event::Resized:
if (event.type == sf::Event::Resized) {
switch (event.type) {
case sf::Event::Resized:
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 */
}

break;
case sf::Event::KeyPressed:
switch (state) {
case AppState::MainMenu:
state = menu.update(window, timestep, event);
break;
/* TODO: add keyboard input queue for game */
}
break;
}
state = menu.onEvent(event);
game.onEvent(event);
}

// (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
));
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...
switch (state) {
case AppState::Game:
state = game.update(window, timestep);
@@ -100,13 +116,6 @@ namespace Proto4 {
}
break;

case AppState::MainMenu:
state = menu.update(window, timestep);
if (state == AppState::Game) {
game.start();
}
break;

case AppState::Exit:
window.close();
break;
@@ -159,7 +168,8 @@ namespace Proto4 {
}

int main() {
if (!Proto4::init()) return 1;
if (!Proto4::init())
return 1;
Proto4::mainloop();
return 0;
}

+ 18
- 23
mainmenu.cpp View File

@@ -7,8 +7,10 @@

namespace Proto4 {

// IDEA: implement Menu superclass, which has a sf::Text title, vector<MenuAction> actions, and implements updating and drawing?
// eg. struct MenuAction { sf::Keyboard key, String text, AppState transition }
// IDEA: implement Menu superclass, which has a sf::Text title,
// vector<MenuAction> actions, and implements updating and drawing?
// eg. struct MenuAction { sf::Keyboard key, String text, AppState
// transition }

bool MainMenu::init(sf::Font &font) {
font = font;
@@ -30,32 +32,25 @@ namespace Proto4 {
return true;
}

AppState MainMenu::onEvent(sf::Event& event) {
switch (event.type) {
case 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);
break;
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::Q)
return AppState::Exit;
if (event.key.code == sf::Keyboard::S)
return AppState::Game;
break;
default:
break;
}

return AppState::MainMenu;
void MainMenu::resize(sf::Event &resizeEvent) {
view.setSize(resizeEvent.size.width, resizeEvent.size.height);
view.setCenter(resizeEvent.size.width / 2, resizeEvent.size.height / 2);
}

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

// to satisfy the analyzer
return AppState::MainMenu;
}

void MainMenu::draw(sf::RenderTarget &window, sf::RenderStates states) const {
void MainMenu::draw(sf::RenderTarget &window,
sf::RenderStates states) const {
window.setView(view);
window.draw(mainLine);
window.draw(entry1);

+ 2
- 2
mainmenu.hpp View File

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

private:
sf::View view{};

Loading…
Cancel
Save