Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Generate a game map from a text file  (Read 2054 times)

0 Members and 2 Guests are viewing this topic.

koyuki

  • Guest
Generate a game map from a text file
« on: October 23, 2019, 11:44:16 pm »
hello everyone :D
i want to start telling you that i am new with programmation, c++ and sfml, i'm doing this kind of things sinche a month for university, so sorry if may appear noob, it's just because i am very noob. ::)



#include <SFML/Window/Event.hpp>
#include <fstream>
#include "Game.h"
#include "Tile.h"

Game::Game() {
    renderWindow.create(sf::VideoMode(1024, 768), "Citadel", sf::Style::Default);
    renderWindow.setFramerateLimit(60);

    sf::Event event{};

    generateMap();
    while(renderWindow.isOpen()) {
        while (renderWindow.pollEvent(event)){
            if(event.type==sf::Event::Closed)
                renderWindow.close();
        }
        renderWindow.clear();
        renderWindow.display();
    }


}



void Game::generateMap() {
    std::ifstream mapFile("map.txt");
    char character;
    float axis=0.;
    float ord=0.;
    Tile* tile;
    while(!mapFile.eof()){
        mapFile.get(character);
        switch (character){
            case 0:
                tile = new Terrain(Plain, axis+512, ord, renderWindow);

            case 1:
                tile = new Terrain(Forest, axis+512, ord, renderWindow);

            case 2:
                tile = new Terrain(Water, axis+512, ord, renderWindow);

            case 3:
                tile = new Building(Cityhall,1, axis+512, ord, renderWindow);

            case '\n':
                continue;

        }

        axis=(axis - ord) * 72;
        ord= (axis + ord) * 32;
    }



}

 


this is the game.cpp file, wich create the sf::RenderWindow and it should also generate the map.
The method too draw a tile is written in the tile class, wich is activated every time the animation method changes frame.

in the map.txt file i just wrote:

2
22


just to make some test.

if i don't launch the generateMap method the program generate the black render window, if i launch it the renderwindow is generate but it is not cleared of black and it doesn't handle the Closed event.

oh, of course the map is not generated ...
same situation if at the place of launching generateMap(); i run this
Terrain test(Water, 512, 0, renderWindow);

someone could tell me where do i mistake?

this is the whole project, i know it's not okay to post all the project code, but believe me it is a very short code right now, so if you want to check more.
https://drive.google.com/open?id=1N_JoqfPfTAjKuRcFF0v_Vr5PxUOi3bRM
« Last Edit: October 24, 2019, 12:05:01 am by koyuki »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Generate a game map from a text file
« Reply #1 on: October 24, 2019, 01:53:34 pm »
all the variables you create in the function generateMap() are destroyed at the end of the function, so
std::ifstream mapFile("map.txt");
char character;
float axis=0.;
float ord=0.;
Tile* tile;
are actually doing nothing, mainly because "Tile* tile" dies before you can use it.
you probably want to makeTile* tile a variable inside your class (I dont really know if you are using classes, to be honest), so you can use it across all the functions inside that class.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

koyuki

  • Guest
Re: Generate a game map from a text file
« Reply #2 on: October 24, 2019, 09:42:49 pm »
all the variables you create in the function generateMap() are destroyed at the end of the function, so
std::ifstream mapFile("map.txt");
char character;
float axis=0.;
float ord=0.;
Tile* tile;
are actually doing nothing, mainly because "Tile* tile" dies before you can use it.
you probably want to makeTile* tile a variable inside your class (I dont really know if you are using classes, to be honest), so you can use it across all the functions inside that class.

you're right, this is true, i really appreciate this advice since nobody teached me to code, but even if i do as you said the problem still persist.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Generate a game map from a text file
« Reply #3 on: October 24, 2019, 11:29:13 pm »
ok, so think with me:
1- you'll need your Tile* all the time while the map is being drawn, so don't put it inside a simple function. move it to inside the Game class, so you can access it later (and when things get bigger, you'll probably want a GameMap class or something like that).

2- inside your TextureManager.h, you could use a enum with tile types, like
enum TileNames{Plain, Water, Etc};
instead of passing string to the TextureManager constructor. then, in TextureManager.cpp, you could use a switch/case instead of many if/else cases.

3- on top of everything, this is your game loop inside Game.cpp (I added comments):
while(renderWindow.isOpen()) { //while window is open
        while (renderWindow.pollEvent(event)){ //and while there are events to run
            if(event.type==sf::Event::Closed)  //if this event is "window.close"
                renderWindow.close();    //close the game window
        }
        renderWindow.clear(); //clear the game window (if its no closed, of course)
        renderWindow.display(); // display what have been drawn on game window
    }
the problem is, you haven't drawn anything to the renderWindow! you should call something like
renderWindow.draw(tile)
before renderWindow.display() and after renderWindow.clear(); more info on how that works.

4- and finally, have a look on the game class:
class Game {
public:

    sf::RenderWindow renderWindow;

    Game();
    void generateMap();


};
there is no map inside it. just a renderWindow. so you really can't draw anything...

move the Tile* inside the Game class, make the adjustments I said, and try again. probably will not work yet, but lets get going. ah, a good idea would be using
std::vector<Tile> tiles
instead of
Tile* tile
. vectors are like arrays, but much better to handle.

to be honest, i think you should start from zero. don't try to make a game from scratch; and it seems to me that you are using the SFML book to creating a game, which is very confusing to newbies.
instead, try creating pieces of game. use a project to create a TileMap, another to create pathfinding, another to create some kind of procedural terrain generator... this way you can keep things simple.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

koyuki

  • Guest
Re: Generate a game map from a text file
« Reply #4 on: October 25, 2019, 12:08:46 am »
this helps me a lot, i will try what you told me tomorrow since here in italy is actually pretty late ;D

just one more question in the time.
i thought i could generate a map in the render window right with a method since i tried to make a draw method in tile class, so that the generateMap method would have created a series of tiles in the place corresponding to the coordinates it gives, and so that with the animation method of the tile the sprite would have updated its texture (i just move the Rect) and then drawn itself in the place it is.

void Tile::animation(int frameMilliseconds) {

    this->sprite.setTexture(this->tex->TextureAtlas[this->tileLevel - 1]);

    if (frameMilliseconds) {                                                             //tempo uguale a 0 per disattivare l' animazione e disegnare e basta
        sf::IntRect intRect(0, 0, 72, 36);
        this->sprite.setTextureRect(intRect);
        bool loop = true;
        while (loop) {
            if (this->clock.getElapsedTime().asMilliseconds() % frameMilliseconds == 0) {
                if (intRect.left == (72 * (this->tex->size - 1)))
                    intRect.left = 0;
                else
                    intRect.left += 72;

                sprite.setTextureRect(intRect);
                this->draw();
            }

        }
    }
    else
        this->draw();
}

void Tile::draw() {
    this->sprite.setPosition(this->axis, this->ord);
    this->rw->draw(this->sprite);

}

this is the idea of it right now before i modify what you said.


if i create a Map object, how does it work when i draw it? i mean, for what i know i can draw sprites, so what would be the map? This question drove me to make a map with a method, but again, i am still a noob but i want to learn a lot since i have to study those kind of things by myself :-[