SFML Tilemap不应该从.txt文件中呈现

时间:2016-10-06 14:48:35

标签: c++ drawing rendering render sfml

我试图制作吃豆子游戏。到目前为止,我只有鬼,我可以用箭头键移动,我想制作一个瓷砖地图,但我的程序绘制的方式与.txt文件不同,我不知道为什么。 Here's an image of it

这些是我的档案:

的main.cpp

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>

#include "tilemap.hpp"

int main()
{
    // WINDOW
    sf::RenderWindow appWindow( sf::VideoMode( 1280, 1024, 32 ), "Pacman" );
    appWindow.setVerticalSyncEnabled(true);

    // IMAGES
    sf::Texture spriteSheet;
    spriteSheet.loadFromFile( "spriteSheet.png" );
    sf::Image test;
    test.create(1280, 1024, sf::Color::Black);

    // SPRITES
    sf::Sprite ghostSprite;
    ghostSprite.setTexture( spriteSheet );
    ghostSprite.setTextureRect(sf::IntRect( 0, 0, 64, 64 ));
    ghostSprite.setOrigin( 32, 32 );
    ghostSprite.setPosition( 400, 300 );

    bool upPressed = false;
    bool leftPressed = false;
    bool rightPressed = false;
    bool downPressed = false;

    while( appWindow.isOpen() )
    {

        sf::Event event;
        while( appWindow.pollEvent( event ) )
        {
            if(( event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) || event.type == sf::Event::Closed )
            {
                appWindow.close();
            }
            if( event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Up )
            {
                upPressed = true;
                leftPressed = false;
                rightPressed = false;
                downPressed = false;
            }
            if( event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Left )
            {
                upPressed = false;
                leftPressed = true;
                rightPressed = false;
                downPressed = false;
            }
            if( event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Right )
            {
                upPressed = false;
                leftPressed = false;
                rightPressed = true;
                downPressed = false;
            }
            if( event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Down )
            {
                upPressed = false;
                leftPressed = false;
                rightPressed = false;
                downPressed = true;
            }

        }


        if( upPressed )
        {
            ghostSprite.move( 0, -6 );
            ghostSprite.setTextureRect(sf::IntRect( 132, 0, 64, 64 ));

        }
        if( downPressed )
        {
            ghostSprite.move( 0, 6 );
            ghostSprite.setTextureRect(sf::IntRect( 198, 0, 64, 64 ));
        }
        if( rightPressed )
        {
            ghostSprite.move( 6, 0 );
            ghostSprite.setTextureRect(sf::IntRect( 0, 0, 64, 64 ));
        }
        if( leftPressed )
        {
            ghostSprite.move( -6, 0 );
            ghostSprite.setTextureRect(sf::IntRect( 66, 0, 64, 64 ));
        }


        appWindow.clear( sf::Color::Black );
        appWindow.draw( ghostSprite );
        generateMap(appWindow);
        appWindow.display();

        sf::sleep(sf::microseconds(100));
    }
    return 0;
}

tilemap.cpp

#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
#include "tilemap.hpp"

void generateMap(sf::RenderWindow &window)
{
    std::ifstream openfile("Map2.txt");

    sf::Texture tileTexture;
    sf::Sprite tiles;

    sf::Vector2i map[100][100];
    sf::Vector2i loadCounter = sf::Vector2i(0,0);

    if(openfile.is_open())
    {
        std::string tileLocation;
        tileLocation = "tile.png";
        tileTexture.loadFromFile(tileLocation);
        tiles.setTexture(tileTexture);

        while(!openfile.eof())
        {
            std::string str;
            openfile >> str;
            char x = str[0];
            char y = str[2];
            if(!isdigit(x) || !isdigit(y))
            {
                map[loadCounter.x][loadCounter.y] = sf::Vector2i(-1,-1);
            }
            else
            {
                map[loadCounter.x][loadCounter.y] = sf::Vector2i(x - '0', y - '0');
            }

            if(openfile.peek() == '\n')
            {
                loadCounter.x = 0;
                loadCounter.y++;
            }
            else
            {
                loadCounter.x++;
            }


            for(int i = 0; i < loadCounter.x; i++)
            {
                for(int j = 0; j < loadCounter.y; j++)
                {
                    if(map[i][j].x != -1 && map[i][j].y != -1)
                    {
                        tiles.setPosition(i * 32, j * 32);

                    }
                }
            }
            window.draw(tiles);
        }
    }
}

和tilemap.hpp

#pragma once
#include <SFML/Graphics.hpp>

void generateMap(sf::RenderWindow &window);

我是制作游戏的新手,所以如果我错过了什么,我就不会感到惊讶。

1 个答案:

答案 0 :(得分:0)

我首先会给你一个提示,以避免这种事情:

            if( event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Up )
        {
            upPressed = true;
            leftPressed = false;
            rightPressed = false;
            downPressed = false;
        }

只需将您的pressedKey变量存储在一个枚举中,该枚举可以是LEFT,RIGHT,UP和DOWN,并且每按一次键就更改一次值。 现在,这里是如何阅读您的文件:

std::string line;
int lineCount = 0;
while (getline(openfile, line)) {
lineCount++
    for(int column = 0; mapLength; column++)
        if (line[column] = '1')
            map[lineCount][column] = 1;
        else
            map[lineCount][column] = -1; 
}

你的文件应该是什么样的:

0011001100
0011001100
0000001100
0000001111

此外,您应该将所有精灵放在矢量/数组中并逐个渲染