Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
class Map { protected: int map_x, map_y; static int tile; int map[100][100]; public: Map() { map_x = 0; map_y = 0; }; ~Map(){}; void LoadMap(const char *filename) { int loadCounterX = 0, loadCounterY = 0; std::ifstream file(filename); if(file.is_open()) { file >> map_x >> map_y; while(!file.eof()) { file >> map[loadCounterX][loadCounterY]; loadCounterX++; if(loadCounterX >= map_x) { loadCounterX = 0; loadCounterY++; } } } } void DrawMap(sf::RenderTexture &rMap, sf::RenderWindow &Spiel) { sf::Image image; sf::Texture textur; image.loadFromFile("graphics/boden.png"); if(!image.loadFromFile("graphics/boden.png")) { std::cout << "boden.png konnte nicht geladen werden!\n"; } textur.loadFromImage(image); for(int i = 0; i < map_x; i++) { for(int j = 0; j < map_y; j++) { sf::Sprite sprite; sprite.setTexture(textur); if(map[i][j] >= 0) { sprite.setTextureRect(sf::IntRect(map[i][j]*32, 0, 32, 32)); sprite.move(i, j); } rMap.draw(sprite); } } sf::Sprite sMap(rMap.getTexture()); // Karte laden Spiel.draw(sMap); // Karte zeichnen } }; |
Und man möge mich steinigen, aber sollten die Streamoperatoren nicht eigentlich die anderen sein? "<<" statt ">>"? Da bin ich mir nicht sicher weil ich ewig kein C++ mehr benutzt hab, aber check mal ob in der Map wirklich Daten drin stehen nach dem laden.
Zitat
Der RCCSWU (RandomCamelCaseSomtimesWithUndersquare) Stil bricht auch mal mit den veraltet strukturierten Denkmustern und erlaubt dem Entwickler seine Kreativität zu entfalten.
Alter Hase
Beruf: Softwareentwickler (aktuell Web/Node); Freiberuflicher Google Proxy
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
#include <iostream> #include <fstream> #include <SFML/Graphics.hpp> using namespace std; class Map { protected: int map_x, map_y; static int tile; int map[100][100]; sf::Image image; sf::Texture textur; public: Map() { map_x = 0; map_y = 0; image.loadFromFile("graphics/boden.png"); if(!image.loadFromFile("graphics/boden.png")) { std::cout << "boden.png konnte nicht geladen werden!\n"; } textur.loadFromImage(image); }; ~Map(){}; static int getTile() { return 32; // 1 Tile = 32 Pixel } void LoadMap(const char *filename) { int loadCounterX = 0, loadCounterY = 0; std::ifstream file(filename); if(file.is_open()) { file >> map_x >> map_y; while(!file.eof()) { file >> map[loadCounterX][loadCounterY]; loadCounterX++; if(loadCounterX >= map_x) { loadCounterX = 0; loadCounterY++; } } } } void DrawMap(sf::RenderTexture &rMap, sf::RenderWindow &Spiel) { for(int i = 0; i < map_x; i++) { for(int j = 0; j < map_y; j++) { sf::Sprite sprite; sprite.setTexture(textur); if(map[i][j] >= 0) { sprite.setTextureRect(sf::IntRect(map[i][j]*32, 0, 32, 32)); sprite.move(i, j); } rMap.draw(sprite); } } sf::Sprite sMap(rMap.getTexture()); // Karte laden Spiel.draw(sMap); // Karte zeichnen } }; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <iostream> #include <fstream> #include <SFML/Graphics.hpp> #include <SFML/Window.hpp> #include <SFML/System.hpp> #include <vector> #include <sstream> #include "map.cpp" #include "player.cpp" int main(){ //Fenster erstellen, Titel: "Spiel", Auflösung 800x600, Farbtiefe 32 Bit sf::RenderWindow Spiel(sf::VideoMode(800, 608, 32), "Spiel", sf::Style::Close); //Synchronisation mit dem Monitor (60 FPS) - Anschalten Spiel.setVerticalSyncEnabled(true); Player Hero; sf::RenderTexture map; Map map1; map1.LoadMap("map1.txt"); // Inhalt löschen (Bildschirm schwarz füllen) Spiel.clear(); map1.DrawMap(map, Spiel); // Sprites, Kameras, Texte usw darstellen Spiel.draw(Hero.getSprite()); // Helden zeichnen // Bild aktualisieren Spiel.display(); } return EXIT_SUCCESS; } |
Werbeanzeige