I am looking for a couple of pieces of advice.
First can some one show me how to split my code between headers and cpp files? I have managed to start splitting my code, but when I try to access a function from a secondary CPP from my main.cpp it says the function has not been defined.
here is a very basic example of what I have:
When running this it tells me that readMap is not defined in this scope. So I have tried map::readMap() and map.readMap().. . but i am missing something, can some one provide some simple examples and reading on how to get this working?
My next question is this: I have a map that is something like 250 X 250 tiles, and my bitmaps are 32x32, this means I am unable to fit the entire map onto the screen at one time. How can I tell my program to only show the 25 tiles surrounding my player at any given time? (Disclaimer: I have not actually tried to accomplish this yet, but I am having a hard time comprehending how or what I need to do.)
Thanks!
map.cpp should include map.h
And readMap has been spelled wrong in map.h.
Do you use header guards?
Header guards are constants that prevent the same header files from being included more than once. So for instance to use map.h you would typically have these lines before and after everything:
#ifndef map_h_included #define map_h_included ... #endif
For your second question, there's two things to it. 1, you have to read from a two dimensional array (or other data source representing your map) and draw the tiles relative to the main character.
2, you have to make sure not to draw tiles outside the map when near the edges.
Step 1 is done by doing a bit of conversions like this: draw_x = player_x - total_width/2 + tile_width*map_column
So what you need is to loop through the screen horizontally and vertically in a double for loop figuring out these coordinates and drawing the sub bitmaps at the spot.
Step 2 is simply continuing the for loop if the coordinate is outside
for( int r=player_y/32-25; r<player_y/32+25; ++r ) { for( int c=player_x/32-25; c<player_x/32+25; ++c ) { if(c<0 || r<0 || c>=255 || r>=255) continue; //draw map[r][c] at player_pos (x or y) - 255*32/2 +32*cell (c or r) } }
I did not test this...
SpectreNectar:
Yes I am using the header guards, code::blocks added them automagically and I had to go read up on what it was. I am not sure what I am doing wrong, but I cannot seem to call code from my main cpp that is contained in another cpp file. Is there any basic tutorial out there that can hold my hand through the ewxplanation?
As for the 2 dimensional array, I am actually using a vector filled with strings, from there I loop through each stirng character by character to display and or create items, so far this is workign well, but my code is quickly turning into a hot mess in my main.cpp. . .
Any way I will take a look at your ideas and see if I can't implement somehting similar to get the rendering to work for me.
Thanks~!
You are welcome.
This is where I learned about header files:
http://www.cplusplus.com/forum/articles/10627/
...and a vector will do fine in the case of drawing your tiles - just iterate through it
taron already told you the solution :
map.cpp should include map.h
And readMap has been spelled wrong in map.h.
Taron, and Edgar,
Thank you for pointing out my typo, however this is just a bit of sample code here if you must see is the real code:
Well it does appear there was a typo even in the real header.... but still getting the
'readMap' was not declared in this scope
and
'displayMap' was not declared in this scope
Thanks for the article SpectreNectar I will start reading up on headers there.
Main.cpp doesn't include map.h. It should.
EDIT: I am trying to get some more files split up and I am having some trouble.
I have a pointer to my player object and if I keep it in main, then my Mob.cpp file cannot find it. If i put it in a header called Globals.h it tells me I have multiple definitions of the pointer.
How can I have a variable like this that is available from all classes?
And another EDIT:
I am passing variables to the functions as needed, and this seems to work well. Still open to hearing suggestions on best practice.
Thanks bamccaig.
This is a very good article, a lot of this stuff I am already doing, header guards to not appear to be stopping the duplicate definition for me however.
I did change some to pass variables around and that works.
However I am hitting a very strange behavior with my header files.
I have a World class and a Mob class. The world class:
and the Mob class:
World needs to know what Mob is to keep track of them, and Mob needs to know what world is so it can accept a pointer object to it.
When Mob.H contains world.h and vice versa it will start telling me that Mob is not defined in World or World is not defined in Mob. .
Exact message is:
error: 'World' has not been declared
That is a circular inclusion. You'll need to forward declare one of the types and not include the other header file in one of them. As an extension to this, you must only use a pointer to the type within that header. E.g.,
Something like that anyway... Somebody should extend the Wiki article with the solution for circular inclusion if it doesn't already explain it. There are limitations, of course. You can't actually use World objects within the Mob header because in the Mob header we don't necessarily know what a World object actually is yet.
All that said, you might find that you don't want a list of Mob objects. For that to work reliably, your Mob type must follow the rules for a proper object, and even then it might be a good waste creating and destroying objects as the list is manipulated. I'm not really an expert on the STL or C++ in general. In any case, if you store pointers, it's inexpensive to manipulate the list, and you know it won't break anything. You do have to worry about managing pointers though. So you might prefer the best of both worlds; a list of smart pointers.
And as I've mentioned in another thread, you might opt to not have the Mob know about the World at all, which will eliminate this whole problem.
Just a random on the spot idea, it may have been suggested before (I'm being lazy and not reading the whole thing). Could you have another class that takes a Mob and a World and preforms the necessary work between them?
Could you have another class that takes a Mob and a World and preforms the necessary work between them?
Yes. That's one of the better ways to do it.