SFML community forums
Help => Graphics => Topic started by: Bonafide on February 08, 2009, 05:12:45 am
-
Hey guys,
I'm having a bit of trouble with a bit of code:
//Scroll through level and display corresponding image of til
for(int y = 0; y < 10; y++)
{
for(int x = 0; x < 10; x++)
{
cout << level[y][x];
if(level[y][x] == 0)
{
blockTile.SetPosition(x*32, y*32);
App.Draw(blockTile);
}
else if(level[y][x] == 1)
{
groundTile.SetPosition(x*32, y*32);
App.Draw(groundTile);
}
The problem? It won't display any tiles. I have made a checker outside of the for loop to see if level[y]- was indeed outputing 0's and 1's (and it was), and if I check inside the posted for-loop, [y]
- does equal 0 or 1, so I don't have a clue why the argument "if level[y]
- == 0 or 1" is not working! if I have "num = level[y]
- ," cout << num outputs a 48 or 49.
If I were to comment the if/else arguments out (shown below), it works perfectly!
//Scroll through level and display corresponding image of til
for(int y = 0; y < 10; y++)
{
for(int x = 0; x < 10; x++)
{
cout << level[y][x];
//if(level[y][x] == 0)
//{
blockTile.SetPosition(x*32, y*32);
App.Draw(blockTile);
//}
//else if(level[y][x] == 1)
//{
groundTile.SetPosition(x*32, y*32);
App.Draw(groundTile);
//}
}
}
What could be causing the if arguments to not work?
Edit:
If I change the argument for block tiles to "if num equals 48," it works. If I change the argument for ground tiles to "if num equals 49," it works. What's going on here?
-
Yout are probably the one to blade :P.
I need more code to see what going on...
-
Here's the full code, sorry if it's a bit messy:
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <fstream>
using namespace sf;
using namespace std;
int main()
{
///////////////////////////////BEGIN LOAD FUNCTION///////////////////////////////////
//Read *filename
//Char array "LEVEL" that will store the integers of file
char level[10][10];
//Read textfle (2D char array made up of a series of integers/letters)
ifstream file("level.txt");
//Store the array into a "temp holder" called tileNum
std::string line;
int height = 0;
while(std::getline(file, line))
{
for(int width = 0; width < 10; width++)
{
level[height][width] = line.at(width);
}
height++;
}
//Close file stream
file.close();
for(int y = 0; y < 10; y++)
{
for(int x = 0; x < 10; x++)
{
cout << level[y][x];
}
cout << endl;
}
/////////////////////////////END LOAD FUNCTION//////////////////////////////////////
///*
/////////////////////////////BEGIN DISPLAY FUNCION//////////////////////////////////
//Display using SFML
//Render Window
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "The CodeWarrior");
//Create image
sf::Image blockImage;
sf::Image groundImage;
if(!blockImage.LoadFromFile("blocks.jpg"))
{
return EXIT_FAILURE;
}
if(!groundImage.LoadFromFile("ground.jpg"))
{
return EXIT_FAILURE;
}
//Create Sprite
sf::Sprite blockTile(blockImage);
sf::Sprite groundTile(groundImage);
//Game loop
while (App.IsOpened())
{
sf::Event Event;
while(App.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed)
{
App.Close();
}
}
//Clear window
App.Clear();
int num;
//Scroll through level and display corresponding image of tile
for(int y = 0; y < 10; y++)
{
for(int x = 0; x < 10; x++)
{
cout << level[y][x] << "LEVEL"; //Test to see what level[y][x] is outputting
cout << endl;
num = level[y][x];
cout << "LOL" << num << "LOL"; // Test to see what num is outputting
cout << endl;
if(num == 48)
{
blockTile.SetPosition(x*32, y*32);
App.Draw(blockTile);
}
else if(num == 49)
{
groundTile.SetPosition(x*32, y*32);
App.Draw(groundTile);
}
}
}
//Display sprites
App.Display();
}
return EXIT_SUCCESS;
//*/
}
-
48 is the ASCII code for 0, 49 is the ASCII code for 1. so you're probably using chars? '0' and '1'
-
Bah, never thought of setting it to "if equal '0' and '1'," which turned out to be the problem.
Thanks for the help!