Browse Source

start implementing background

master
sauer2 1 year 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
8 8
 # for Windows using MinGW
9 9
 #LDFLAGS = -L "SFML21/lib" -lmingw32 -lsfml-main -lsfml-system -lsfml-window -lsfml-graphics
10 10
 
11
-OBJ = game.o mainmenu.o main.o 
11
+OBJ = game.o background.o mainmenu.o main.o 
12 12
 
13 13
 game: $(OBJ)
14 14
 	$(CC) $(CFLAGS) -o game.bin $(OBJ) $(LDFLAGS)

+ 26
- 0
background.cpp View File

@@ -0,0 +1,26 @@
1
+#include <SFML/Graphics.hpp>
2
+#include <SFML/System.hpp>
3
+#include <SFML/Window.hpp>
4
+
5
+#include "background.hpp"
6
+#include "main.hpp"
7
+
8
+namespace Proto4 {
9
+    void Background::init(sf::Texture bg0, sf::Texture bg1) {
10
+        bg0Tex = bg0;
11
+        bg1Tex = bg1;
12
+        bg0Sprite.setTexture(bg0Tex);
13
+        bg1Sprite.setTexture(bg1Tex);
14
+		bg0Sprite.setTextureRect(sf::IntRect(0, 0, xResolution, yResolution));
15
+		bg1Sprite.setTextureRect(sf::IntRect(0, 0, xResolution, yResolution));
16
+		bg0Tex.setRepeated(true);
17
+		bg1Tex.setRepeated(true);
18
+		bg0Tex.setSmooth(true);
19
+		bg1Tex.setSmooth(true);
20
+    }
21
+    void Background::update(float x, float y) {}
22
+    void Background::draw(sf::RenderTarget& target, sf::RenderStates states) const {
23
+        target.draw(bg0Sprite);
24
+        target.draw(bg1Sprite);
25
+    }
26
+}

+ 22
- 0
background.hpp View File

@@ -0,0 +1,22 @@
1
+#pragma once
2
+
3
+#include <SFML/Graphics.hpp>
4
+#include <SFML/System.hpp>
5
+#include <SFML/Window.hpp>
6
+
7
+#include "main.hpp"
8
+
9
+namespace Proto4 {
10
+    class Background : public sf::Drawable {
11
+      public:
12
+        void init(sf::Texture bg0, sf::Texture bg1);
13
+        void update(float x, float y);
14
+
15
+      private:
16
+        sf::Sprite bg0Sprite;
17
+        sf::Sprite bg1Sprite;
18
+        sf::Texture bg0Tex;
19
+        sf::Texture bg1Tex;
20
+        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
21
+    };
22
+}

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 @@
6 6
 #include <SFML/Window.hpp>
7 7
 
8 8
 #include "main.hpp"
9
+#include "background.hpp"
9 10
 #include "mainmenu.hpp"
10 11
 #include "game.hpp"
11 12
 
@@ -17,6 +18,8 @@ namespace Proto4 {
17 18
     AppState state = AppState::MainMenu;
18 19
     MainMenu menu{};
19 20
     Game game{};
21
+    Background background{};
22
+    sf::Texture bg0, bg1;
20 23
     sf::Font font;
21 24
 
22 25
     bool init() {
@@ -26,6 +29,14 @@ namespace Proto4 {
26 29
             return false;
27 30
         }
28 31
 
32
+
33
+        if (!bg0.loadFromFile("img/bg_grid_48.png") || !bg1.loadFromFile("img/bg_grid_72.png")) {
34
+            std::cout << "Unable to load background images. Aborting execution.";
35
+            return false;
36
+        }
37
+        
38
+        background.init(bg0, bg1);
39
+
29 40
         if (
30 41
             !menu.init(font) ||
31 42
             !game.init()
@@ -56,6 +67,8 @@ namespace Proto4 {
56 67
 
57 68
     void update(sf::Time timestep) {
58 69
 
70
+        background.update(1.0f, 1.0f);
71
+
59 72
         switch (state) {
60 73
         case AppState::Game:
61 74
             state = game.update(window, timestep);
@@ -80,6 +93,7 @@ namespace Proto4 {
80 93
     void draw(sf::Time timestep) {
81 94
 
82 95
         window.clear(sf::Color::Black);
96
+        window.draw(background);
83 97
 
84 98
         switch (state) {
85 99
         case AppState::Game:

Loading…
Cancel
Save