Page 2 of 2
Re: Loading tiles from a file
Posted: Sun Aug 08, 2010 11:29 pm
by PaperDuckyFTW
Thoughout this issue, I have printed the array into the console to see if it was working correctly. Even though the file is completely filled with '2's, the output to the console just reads '0'. I can post a screenshot if its needed. At the moment, these are my loading and drawing functions. The loading function works perfectly fine, its just the drawing thats not working :\
Code: Select all
bool LoadLevel( const std::string& filename )
{
std::ifstream file;
file.open( filename.c_str(), ios::in );
char temp;
if(!file.is_open())
{
return false;
}
for( int x = 0; x < 40; x++ )
{
for( int y = 0; y < 30; y++ )
{
file >> map[y][x];
//map[y][x] = temp;
cout<<map[y][x];
}
}
file.close();
return true;
}
void draw_level()
{
for( int x = 0; x < 40; x++ )
{
for( int y = 0; y < 30; y++ )
{
if( map[y][x] == 0 )
{
draw_surface( x * 32 - camera.x, y * 32 - camera.y, tiles, screen, &clip_tiles[0] ); //draw the first tile in the sprite sheet
}
if( map[y][x] == 2 )
{
draw_surface( x * 32 - camera.x, y * 32 - camera.y, tiles, screen, &clip_tiles[2] ); //draw the third sprite in the tilesheet
}
}
}
}
Sorry this is a bit off topic, but how is your text based rpg coming along, dragoon? I remember ages ago watching a youtube vid of its development, and it helped motivate me as I was planning on making one myself when I was starting with c++. Haha i guess i owe you a thanks on that part, and best of luck with it. When I have the laod funciton read the file, it reads '50' for some reason...idk. When i have it read to the array like file<<map[y][x] instead of reading '50' it reads '0' as mentioned.
Re: Loading tiles from a file
Posted: Mon Aug 09, 2010 4:40 am
by ultimatedragoon69
I'm unsure why you have a char temp and your not using it. if it was me and i wanted to use the buffer variable you have i would do something like this->
Code: Select all
file >> temp;
map[y][x] = temp;
cout<<map[y][x];
Another thing i wanted to note on in your code is where your drawing the tile why you dont just use map[y][x] as &clip_tiles[map[y][x]]. If your text file is only integers you could also just load int's instead of char's. Would probably make things alot easier.
Code: Select all
bool LoadLevel( const std::string& filename )
{
std::ifstream file;
file.open( filename.c_str(), ios::in );
char temp;
if(!file.is_open())
{
return false;
}
for( int x = 0; x < 40; x++ )
{
for( int y = 0; y < 30; y++ )
{
file >> temp;
map[y][x] = temp;
cout<<map[y][x];
}
}
file.close();
return true;
}
void draw_level()
{
for( int x = 0; x < 40; x++ )
{
for( int y = 0; y < 30; y++ )
{
/*
if( map[y][x] == 0 )
{
draw_surface( x * 32 - camera.x, y * 32 - camera.y, tiles, screen, &clip_tiles[0] ); //draw the first tile in the sprite sheet
}
if( map[y][x] == 2 )
{
draw_surface( x * 32 - camera.x, y * 32 - camera.y, tiles, screen, &clip_tiles[2] ); //draw the third sprite in the tilesheet
}
*/
draw_surface(x*32-camera.x, y*32-camera.y, tiles, screen, &clip_tiles[map[y][x]] );
}
}
}
I'm not 100% sure why your reading only 0's could you show me the text file you are using?
Oh as far as Pangea's Quest i had to once again put in on the back burner because of real life stuff, I'm still working on it (even though its near stand still).
Re: Loading tiles from a file
Posted: Mon Aug 09, 2010 5:25 am
by PaperDuckyFTW
i really have no ideae whats going on.
Here is part of my level file:
It doesnt matter if I have it filled with 0's, 2's or 1's, its just not reading anything.
Code: Select all
200000000000000000000000000002
222222000000000001122222222222
222222222222222222222222222222
222222222222222222222222222222
222222222222222222222222222222
222222222222222222222222222222
222222222222222222222222222222
222222222222222222222222222222
222222222222222222222222222222
222222222222222222222222222222
222222222222222222222222222222
Re: Loading tiles from a file
Posted: Mon Aug 09, 2010 6:17 am
by K-Bal
Put spaces between the numbers.
Re: Loading tiles from a file
Posted: Mon Aug 09, 2010 11:24 am
by wearymemory
K-Bal wrote:Put spaces between the numbers.
That shouldn't matter if he's reading in chars.
OP, is your map variable a 2-dimensional array of chars? If so, then you should use character literals when comparing the values in your draw function.
If not, then the simplest solution would be to make your map variable a 2-dimensional array of chars, and make sure that all comparisons are done using character literals.
Re: Loading tiles from a file
Posted: Mon Aug 09, 2010 12:36 pm
by xiphirx
K-Bal wrote:Put spaces between the numbers.
Do this.
Then
Code: Select all
bool LoadLevel( const std::string& filename )
{
std::ifstream file;
file.open( filename.c_str(), ios::in );
if(!file.is_open())
{
return false;
}
for( int x = 0; x < 40; x++ )
{
for( int y = 0; y < 30; y++ )
{
file >> map[x][y];
cout<<map[x][y];
}
}
file.close();
return true;
}
void draw_level()
{
for( int x = 0; x < 40; x++ )
{
for( int y = 0; y < 30; y++ )
{
if( map[x][y] == 0 )
{
draw_surface( x * 32 - camera.x, y * 32 - camera.y, tiles, screen, &clip_tiles[0] ); //draw the first tile in the sprite sheet
}
if( map[x][y] == 2 )
{
draw_surface( x * 32 - camera.x, y * 32 - camera.y, tiles, screen, &clip_tiles[2] ); //draw the third sprite in the tilesheet
}
}
}
}
Re: Loading tiles from a file
Posted: Mon Aug 09, 2010 1:00 pm
by K-Bal
wearymemory wrote:K-Bal wrote:Put spaces between the numbers.
That shouldn't matter if he's reading in chars.
Yeah, but the white space is neccessary when using >>.
Re: Loading tiles from a file
Posted: Mon Aug 09, 2010 5:36 pm
by wearymemory
K-Bal wrote:wearymemory wrote:K-Bal wrote:Put spaces between the numbers.
That shouldn't matter if he's reading in chars.
Yeah, but the white space is neccessary when using >>.
This is true when reading strings, but I've tested the following code and this is its resulting output when using chars (as stated in my post you quoted):
main.cpp:
Code: Select all
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char** argv) {
ifstream fin("data.txt");
char c;
while (fin >> c) {
cout << "Read from file: " << c << endl;
}
return 0;
}
data.txt:
output:
Code: Select all
Read from file: 1
Read from file: 2
Read from file: 3
Read from file: 4
Read from file: 5
Read from file: 6
Read from file: 7
Read from file: 8
Read from file: 9
Press [Enter] to close the terminal ...
It shows that adding spaces between the numbers is unnecessary, even when using the >> operator on an ifstream instance. I believe that the OP's answer is dependent on the question I asked in my previous post.
Re: Loading tiles from a file
Posted: Tue Aug 10, 2010 2:49 am
by K-Bal
You are right.
Re: Loading tiles from a file
Posted: Tue Aug 10, 2010 4:32 am
by PaperDuckyFTW
YESSSSSS!!!!!!!!!!!!!!!!!!!!
Okay i am estatic but also feel like smashing my face agsint a wall. Im soo terribly sorry for wasting all of your time. The problem wasnt loading or drawing the tiles. The problem was HOW the tiles were loaded. It was reading the file, but there wasnt any breaks in it:
for example, instead of it reading somethig like this:
Code: Select all
0 2 2 2 0
0 0 0 0 0
0 0 2 0 0
0 2 1 2 0
It was reading the file like this:
So it wasnt drawing as a map, it was drawing as a single line.
Thankyou to everyone who helped, it has helped me learn more about c++ which i never knew before, plus it now means I can move onto a game entity or state machine.
Now to jsut figure out how to make it start reading another line, but im armed with lazyfoo's tutorial so i have an idea. Thanks again for all your help, and haah sorry it took so damn long. Take care yall
EDIT: Dont worry, I now know EXACTLY what was wrong, I was reading the file backwards, then drawing the tiles, reading the array backwards. Kinda confusing but it works perfectly fine now =]