From: lukel495 Date: Wed, 8 Nov 2023 14:15:18 +0000 (+0100) Subject: Added code exampel from lecture X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=ff1b36f0540032910b0249f0df310c6583081a89;p=TDDC76_proj.git Added code exampel from lecture --- diff --git a/Makefile b/Makefile index 21421db..799827b 100644 --- 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 index 5557490..0000000 Binary files a/build/_main.o and /dev/null differ diff --git a/build/_test.o b/build/_test.o index bbeaa95..8b1f7d8 100644 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 index 97b21ed..0000000 Binary files a/build/exe/game.out and /dev/null differ diff --git a/build/exe/test.out b/build/exe/test.out index 94b0db0..12adb2a 100755 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 index 0000000..6a36a83 Binary files /dev/null and b/fighter.png differ diff --git a/font.ttf b/font.ttf new file mode 100644 index 0000000..f578602 Binary files /dev/null and b/font.ttf differ diff --git a/src/_main.cc b/src/_main.cc index 63056d6..18dde64 100644 --- a/src/_main.cc +++ b/src/_main.cc @@ -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 diff --git a/src/_test.cc b/src/_test.cc index 032e0f8..9c2e057 100644 --- a/src/_test.cc +++ b/src/_test.cc @@ -1,13 +1,151 @@ /** * Huvudfil för testning av spelets beståndsdelar. - * + * * All testkod bör köras från denna fil och denna fil enbart. -*/ + */ -#include +#include // 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 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 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 index b130bf0..0000000 --- a/src/sfml_start.cc +++ /dev/null @@ -1,36 +0,0 @@ -#include // 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 - } -}