Added code exampel from lecture
authorlukel495 <lukel495@tlvm-4-2-4.ad.liu.se>
Wed, 8 Nov 2023 14:15:18 +0000 (15:15 +0100)
committerlukel495 <lukel495@tlvm-4-2-4.ad.liu.se>
Wed, 8 Nov 2023 14:15:18 +0000 (15:15 +0100)
12 files changed:
Makefile
build/_main.o [deleted file]
build/_test.o
build/exe/game.out [deleted file]
build/exe/test.out
fighter.png [new file with mode: 0644]
font.ttf [new file with mode: 0644]
src/_main.cc
src/_test.cc
src/fighter.png [deleted file]
src/font.ttf [deleted file]
src/sfml_start.cc [deleted file]

index 21421dbc3c1245e502c0fd89b963abbf803db849..799827bf0b04c4b9ca5c3b65da545b24bdfcd8b1 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 CC := g++
 CCFLAGS := -std=c++17 -Wall -Wextra -pedantic -Weffc++ -Wold-style-cast -I src
-LDFLAGS :=
+LDFLAGS := -L${SFML_ROOT}/lib -I${SFML_ROOT}/include -lsfml-window -lsfml-graphics -lsfml-system
 
 OBJDIR := build
 EXEDIR := build/exe
@@ -27,10 +27,10 @@ testing: dir
        @$(MAKE) --no-print-directory test-target
 
 game-target: $(OBJDIR)/_main.o $(OBJECTS)
-       $(CC) $(LDFLAGS) $^ -o $(EXE)
+       $(CC) $(LDFLAGS) $^ -o $(EXE) $(LDFLAGS)
 
 test-target: $(OBJDIR)/_test.o $(OBJECTS)
-       $(CC) $(LDFLAGS) $^ -o $(TEST)
+       $(CC) $(LDFLAGS) $^ -o $(TEST) $(LDFLAGS)
 
 $(OBJDIR)/%.o: %.cc
        $(CC) $(CCFLAGS) -c -o $@ $<
diff --git a/build/_main.o b/build/_main.o
deleted file mode 100644 (file)
index 5557490..0000000
Binary files a/build/_main.o and /dev/null differ
index bbeaa95b1b62f29f635e14fb2a3265ff9c3d71f3..8b1f7d89b789cb503f0072caa27f4c45ed6ac077 100644 (file)
Binary files a/build/_test.o and b/build/_test.o differ
diff --git a/build/exe/game.out b/build/exe/game.out
deleted file mode 100755 (executable)
index 97b21ed..0000000
Binary files a/build/exe/game.out and /dev/null differ
index 94b0db0b7bf48df778e89f3a18acfe783144460d..12adb2a5e9b518d76e5c088e5e457ddf08abbcf1 100755 (executable)
Binary files a/build/exe/test.out and b/build/exe/test.out differ
diff --git a/fighter.png b/fighter.png
new file mode 100644 (file)
index 0000000..6a36a83
Binary files /dev/null and b/fighter.png differ
diff --git a/font.ttf b/font.ttf
new file mode 100644 (file)
index 0000000..f578602
Binary files /dev/null and b/font.ttf differ
index 63056d6de459eb84f51fa9c68008a6d8b2d5dd8e..18dde64c24d5d18d76c535edf0beed6d0be1cf2a 100644 (file)
@@ -3,14 +3,10 @@
  * 
  * All spelkod bör köras från denna fil och denna fil enbart.
 */
-
+/*
 #include "Game.h"
 #include "constants.h"
 
-/*
- * If you want to study the code, it is recommended that
- * you start looking in Game.h.
- */
 
 int main ()
 {
@@ -18,3 +14,4 @@ int main ()
     Game g { "Example Program", screen_width, screen_height };
     g.start ();
 }
+*/
\ No newline at end of file
index 032e0f83b0bcf87113b25ab796db31be14bc27bf..9c2e057acba2e3996a8a0da97de2d5617c1b1876 100644 (file)
 /**
  * Huvudfil för testning av spelets beståndsdelar.
- * 
+ *
  * All testkod bör köras från denna fil och denna fil enbart.
-*/
+ */
 
-#include <iostream>
+#include <SFML/Graphics.hpp> // includes most things in SFML
 
+using namespace sf;
 
-int main() {
-    std::cout << "_test.cc" << std::endl;
-    return 0;
+int const width{640};
+int const height{480};
+
+int main()
+{
+    RenderWindow window{
+        VideoMode{width, height},
+        "SFML DEMO"};
+
+    /* skapa en cirkel */
+    float const r{16.0};
+    CircleShape circle{r};
+
+    // placera cirkeln på skärmen
+    circle.setPosition(width / 2, height / 2);
+
+    // sätt vilken punkt som är origo
+    // i cirkeln (det är denna punkt som
+    // cirkelns position är specificerad ifrån
+    circle.setOrigin(r, r);
+
+    // sätt en bakgrundsfärg på cirkeln
+    circle.setFillColor(Color::Green);
+
+    /* ladda in en texture (bild) */
+    Texture texture;
+    if (!texture.loadFromFile("fighter.png"))
+    {
+        // gick inte att ladda bilden
+        return 1;
+    }
+
+    // skapa sprite
+    Sprite sprite{texture};
+    sprite.setPosition(width / 2, height / 2);
+
+    // sätt origin på sprite till mitten
+    auto size{texture.getSize()};
+    sprite.setOrigin(size.x / 2, size.y / 2);
+
+    float const speed{4.0};
+
+    // skapa en klocka
+    Clock clock;
+
+    double const fps{60.0};
+
+    // beräkna hur lång tid vi har per frame
+    auto const target{milliseconds(1000.0 / fps)};
+
+    /* skapa text */
+    Font font;
+    if (!font.loadFromFile("font.ttf"))
+    {
+        // kunde inte ladda typsnitt
+        return 2;
+    }
+
+    // skapa text objekt
+    Text text{"Demo", font};
+
+    auto bounds{text.getGlobalBounds()};
+    text.setPosition((width - bounds.width) / 2, 0);
+
+    // loopa sålänge fönstret finns
+    while (window.isOpen())
+    {
+        // börja tidtagning
+        clock.restart();
+
+        /* hantera events */
+        Event event;
+        // hämta ett event i taget
+        while (window.pollEvent(event))
+        {
+            if (event.type == Event::Closed)
+            {
+                window.close();
+            }
+            // har en mus-knapp tryckts?
+            else if (event.type == Event::MouseButtonPressed)
+            {
+                auto mouse{event.mouseButton};
+                // är det vänster musknapp?
+                if (mouse.button == Mouse::Button::Left)
+                {
+                    // flytta cirkeln
+                    circle.setPosition(mouse.x, mouse.y);
+                }
+            }
+        }
+
+        /* hantera logiken */
+        auto old_position{sprite.getPosition()};
+
+        if (Keyboard::isKeyPressed(Keyboard::Key::W))
+        {
+            sprite.setRotation(0);
+            sprite.move(0, -speed);
+        }
+        else if (Keyboard::isKeyPressed(Keyboard::Key::S))
+        {
+            sprite.setRotation(180);
+            sprite.move(0, speed);
+        }
+        else if (Keyboard::isKeyPressed(Keyboard::Key::A))
+        {
+            sprite.setRotation(-90);
+            sprite.move(-speed, 0);
+        }
+        else if (Keyboard::isKeyPressed(Keyboard::Key::D))
+        {
+            sprite.setRotation(90);
+            sprite.move(speed, 0);
+        }
+
+        auto sprite_box{sprite.getGlobalBounds()};
+        auto circle_box{circle.getGlobalBounds()};
+        if (sprite_box.intersects(circle_box))
+        {
+            // om kollision, flytta tillbaks sprite
+            sprite.setPosition(old_position);
+        }
+
+        /* rita ut */
+        window.clear(); // rensa skärmen
+
+        window.draw(text);   // rita ut texten
+        window.draw(circle); // rita ut cirkeln
+        window.draw(sprite); // rita ut bilden
+
+        window.display(); // visa ändringarna
+
+        // räkna ut hur lång tid vi har kvar tills nästa
+        // frame ska börja
+        auto wait_time{target - clock.getElapsedTime()};
+
+        // vänta tiden som blev över
+        sleep(wait_time);
+    }
 }
\ No newline at end of file
diff --git a/src/fighter.png b/src/fighter.png
deleted file mode 100644 (file)
index 6a36a83..0000000
Binary files a/src/fighter.png and /dev/null differ
diff --git a/src/font.ttf b/src/font.ttf
deleted file mode 100644 (file)
index f578602..0000000
Binary files a/src/font.ttf and /dev/null differ
diff --git a/src/sfml_start.cc b/src/sfml_start.cc
deleted file mode 100644 (file)
index b130bf0..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-#include <SFML/Graphics.hpp> // includes most things in SFML
-
-using namespace sf;
-
-int const width  { 640 };
-int const height { 480 };
-
-int main ()
-{
-    RenderWindow window {
-        VideoMode { width, height },
-        "SFML DEMO"
-    };
-
-    // loopa sålänge fönstret finns
-    while ( window.isOpen () )
-    {
-        /* hantera events */
-        Event event;
-        // hämta ett event i taget
-        while ( window.pollEvent (event) )
-        {
-            if ( event.type == Event::Closed )
-            {
-                window.close ();
-            }
-        }
-
-        /* hantera logiken */
-
-        /* rita ut */
-        window.clear (); // rensa skärmen
-
-        window.display (); // visa ändringarna
-    }
-}