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

Author Topic: Problem with simple code.  (Read 2930 times)

0 Members and 1 Guest are viewing this topic.

Dmastermind

  • Newbie
  • *
  • Posts: 3
    • View Profile
Problem with simple code.
« on: February 03, 2013, 10:32:21 pm »
Hello i am new to SFML and i am using codingmadeeasy's tutorials on YouTube as well as the written tutorials for SFML to help me get started. Everything was going good till i got to one of his tutorials that deal with loading maps from a file where you can decide what tiles are using 1's and 0's. Well before i even try implementing code into my own games i copy and paste them and make sure the work at first. So when i tried to compile the code he had put on his site, the code compiled fine but the 1's on the map file wont appear which are orange color but the blue tiles which are 0's do appear, Can you figure out what is wrong with the code?

// C++ Sfml Made Easy Tutorial 29 - Loading Tile Maps [Easy]
// CodingMadeEasy

#include<SFML/Graphics.hpp>
#include<iostream>
#include<fstream>

#define ScreenWidth 800
#define ScreenHeight 600

#define BLOCKSIZE 40

int loadCounterX = 0, loadCounterY = 0;
int mapSizeX, mapSizeY;
int MapFile[100][100];

void LoadMap(const char *filename)
{
    std::ifstream openfile(filename);
    if(openfile.is_open())
    {
        openfile >> mapSizeX >> mapSizeY;
        while(!openfile.eof())
        {
            openfile >> MapFile[loadCounterX][loadCounterY];
            loadCounterX++;
            if(loadCounterX >= mapSizeX)
            {
                loadCounterX = 0;
                loadCounterY++;
            }
        }
    }
}

void DrawMap(sf::RenderWindow &Window)
{
    sf::Shape rect = sf::Shape::Rectangle(0, 0, BLOCKSIZE, BLOCKSIZE, sf::Color(255, 255, 255, 255));
    sf::Color rectCol;
    for(int i = 0; i < mapSizeX; i++)
    {
        for(int j = 0; j < mapSizeY; j++)
        {
            if(MapFile[i][j] == 0)
                rectCol = sf::Color(44, 117, 255);
            if (MapFile[i][j] == 1)
                rectCol = sf::Color(255, 100, 17);

            rect.SetPosition(i * BLOCKSIZE, j * BLOCKSIZE);
            rect.SetColor(rectCol);
            Window.Draw(rect);
        }
    }
}

int main()
{
    sf::RenderWindow Window(sf::VideoMode(ScreenWidth, ScreenHeight, 32), "SFML Made Easy");

    LoadMap("Map.txt");
    while(Window.IsOpened())
    {
        sf::Event Event;
        while(Window.GetEvent(Event))
        {
            if(Event.Type == sf::Event::Closed || Event.Key.Code == sf::Key::Escape)
                Window.Close();
        }

        Window.Clear();
        DrawMap(Window);
        Window.Display();
    }
    return 0;
}

 



By the way my Map.txt file looks like this
10 10
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
1111111111

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Problem with simple code.
« Reply #1 on: February 03, 2013, 11:18:36 pm »
Tried adding spaces between the numbers?
Back to C++ gamedev with SFML in May 2023

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Problem with simple code.
« Reply #2 on: February 03, 2013, 11:31:26 pm »
You have to be careful with this code, see here.

Additionally, it's SFML 1.6 of which the use is discouraged.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Problem with simple code.
« Reply #3 on: February 03, 2013, 11:44:37 pm »
I actually don't recommend them either, they're too slow, boring and contain some bad practice. Much better are official tutorials, documentation, googling specific problem for some library-less and language-less solution description and finally asking here on forum(in that order of execution). Searching sfml wiki is also good.
Back to C++ gamedev with SFML in May 2023

Dmastermind

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Problem with simple code.
« Reply #4 on: February 04, 2013, 12:36:20 am »
Thanks Everyone for your help, FRex's first answer helped me with the problem, i can now see the orange tiles perfectly :D. Also i do realize the YouTube tutorials contain some bad practice, but at first i was looking for a book but i could not find a book on sfml, If one exists could you please give me the name of it. Lastly the main reason why i have not decided to go with sfml 2.0 yet is that im not sure if the above code would be compatible with it and i have not seen any tutorials on it, I have yet to take a look at the wiki though.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Problem with simple code.
« Reply #5 on: February 04, 2013, 12:43:16 am »
There is no point in learning 1.6 anymore if you've just started with sfml.
Back to C++ gamedev with SFML in May 2023

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Problem with simple code.
« Reply #6 on: February 04, 2013, 02:52:41 am »
Moreover, the code above can be easily ported from 1.6 to 2.0.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!