Browse Source

start implementing background

master
sauer2 2 years ago
parent
commit
bdcb3d2c35
6 changed files with 63 additions and 1 deletions
  1. 1
    1
      Makefile
  2. 26
    0
      background.cpp
  3. 22
    0
      background.hpp
  4. BIN
      img/bg_grid_48.png
  5. BIN
      img/bg_grid_72.png
  6. 14
    0
      main.cpp

+ 1
- 1
Makefile View File

@@ -8,7 +8,7 @@ LDFLAGS = -L "SFML21/lib" -lsfml-system -lsfml-window -lsfml-graphics
# for Windows using MinGW
#LDFLAGS = -L "SFML21/lib" -lmingw32 -lsfml-main -lsfml-system -lsfml-window -lsfml-graphics

OBJ = game.o mainmenu.o main.o
OBJ = game.o background.o mainmenu.o main.o

game: $(OBJ)
$(CC) $(CFLAGS) -o game.bin $(OBJ) $(LDFLAGS)

+ 26
- 0
background.cpp View File

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

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

namespace Proto4 {
void Background::init(sf::Texture bg0, sf::Texture bg1) {
bg0Tex = bg0;
bg1Tex = bg1;
bg0Sprite.setTexture(bg0Tex);
bg1Sprite.setTexture(bg1Tex);
bg0Sprite.setTextureRect(sf::IntRect(0, 0, xResolution, yResolution));
bg1Sprite.setTextureRect(sf::IntRect(0, 0, xResolution, yResolution));
bg0Tex.setRepeated(true);
bg1Tex.setRepeated(true);
bg0Tex.setSmooth(true);
bg1Tex.setSmooth(true);
}
void Background::update(float x, float y) {}
void Background::draw(sf::RenderTarget& target, sf::RenderStates states) const {
target.draw(bg0Sprite);
target.draw(bg1Sprite);
}
}

+ 22
- 0
background.hpp View File

@@ -0,0 +1,22 @@
#pragma once

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

#include "main.hpp"

namespace Proto4 {
class Background : public sf::Drawable {
public:
void init(sf::Texture bg0, sf::Texture bg1);
void update(float x, float y);

private:
sf::Sprite bg0Sprite;
sf::Sprite bg1Sprite;
sf::Texture bg0Tex;
sf::Texture bg1Tex;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
};
}

BIN
img/bg_grid_48.png View File


BIN
img/bg_grid_72.png View File


+ 14
- 0
main.cpp View File

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

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

@@ -17,6 +18,8 @@ namespace Proto4 {
AppState state = AppState::MainMenu;
MainMenu menu{};
Game game{};
Background background{};
sf::Texture bg0, bg1;
sf::Font font;

bool init() {
@@ -26,6 +29,14 @@ 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.";
return false;
}
background.init(bg0, bg1);

if (
!menu.init(font) ||
!game.init()
@@ -56,6 +67,8 @@ namespace Proto4 {

void update(sf::Time timestep) {

background.update(1.0f, 1.0f);

switch (state) {
case AppState::Game:
state = game.update(window, timestep);
@@ -80,6 +93,7 @@ namespace Proto4 {
void draw(sf::Time timestep) {

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

switch (state) {
case AppState::Game:

Loading…
Cancel
Save