Jump to content
  • Advertisement
Sign in to follow this  
Methiazon

Please help me...I'm a noob

This topic is 5454 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I've got a problem with this piece of C++ code. //Calculate width unsigned int Width = 0; while( mRead.get() != '\n' ) { Width++; } //Calculate height unsigned int Height = 2; while( !mRead.eof() ) { if( mRead.get() == '\n' ) Height++; } RIGHT UP 'TIL HERE IT WORKS JUST FINE char* pLevel = new char[Width]; IS THIS POINTER CORRECT? mRead.seekg( 0, ios_base::beg ) mRead.getline( pLevel , Width ); I WANT TO STORE THE DATA (TEXT), FROM THE FILE, IN THE CHAR ARRAY FOR WHICH I ALLOCATED MEMORY WITH THE "NEW" COMMAND. THEN I WANT TO DRAW IT ON THE SCREEN. I KNOW THERE'S CODE MISSING AND SOME OF THE CODE I'VE WRITTEN IS PROBABLY ALL WRONG. PLEASE HELP...WHAT'S MISSING? WHAT'S WRONG? ANY SUGGESTIONS? GRATEFUL FOR ALL THE HELP I CAN GET.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
you seemed to have mistakenly hit your caps lock key about 3/4 the way through your post.

Share this post


Link to post
Share on other sites
Firstly, you can use the [sourse] and [/source] symbols on this forum so that you can write source like: (relace sourse with source)


int main(int argc, char **argv) {
blah;
}



Er, when you load the level into the char array you use getline. That will only load the first line of the level, won't it? You might want to do a loop for every row here, or just read width * height chars from the file.

Using eof is _IMHO_ a little messy. It's fine, but you could use an end-of-map delimiter character like ! or something, the way you use \n for the end-of-line delimiter. Your way is still perfectly ok though.

Do you know how to draw it on the screen? Are you planning to display it as text in a text-mode (like dos) display, or console, or do you want it to be displayed graphically?

Share this post


Link to post
Share on other sites
Thank's for the replies!

I want to draw the level, in text format, in the console window (DOS).

Something like this:

000000000000000
000000000000000
000000000##0000
000000000##0000
0000##000000000
0000##000000000
000000000000000
000000000000000

And yes you're right my goal is to multiply Width with Height and draw the entire file...I just don't know how to do it.

If you possibly could give me a suggestion with a code sample or something, I would greatly appreciate it.

Thanks!

Share this post


Link to post
Share on other sites
I haven't done text coding for years, and that was in pascal, but I think this method probably works fine.



drawing the level to the console

void drawLevel() {
clrscr(); // Clear the text screen and reset the cursor to 0-0.

for(int y = 0; y < Height; y++) { // Draw each row one at a time.
for(int x = 0; x < Width; x++) { // Draw each character one at a time.
printf("%c", pLevel[y * Width + x]; // Draw one charactr.
}
printf("\n");
}
}



I'm not sure if clrscr is the right function.


This should write your map to screen. If you update the level and call drawLevel, the map will change on screen.

Now if your level looks like the one below, then 0's and #'s are going to be drawn on the screen. That's not really very pretty. The next step might be to store each square of map as a number and use an array to convert the number into a character, and possibly a color. I made a game doing this back in form four.

That would be something like:


#define MAX_TILES 10
int tileSymbols[256];
char tileCharacters[MAX_TILES];
int tileColors[MAX_TILES];

void initialiseTileTypes() {
// empty space
tileSymbols['0'] = 0;
tileCharacters[0] = ' ';
tileColors[0] = 0;

// walls
tileSymbols['#'] = 1;
tileCharacters[1] = '#';
tileColors[1] = 255;
}

void readMap() {
for(int y = 0; y < Height; y++) { // Read each row one at a time.
for(int x = 0; x < Width; x++) { // Read each character
pLevel[y * Width + x] = tileSymbols[mRead.get()];
}
}
}




replace printf("%c", pLevel...

with


int tile = pLevel[y * width + x];
textcolor(tileColors[tile]);
printf("%c", tileCharacters[tile]);



I'm not sure if textcolor is the right function.

That should give you much more control of how your map looks without having to change the mapfiles.


Make sure you call the initialise before you try to load the level.

Share this post


Link to post
Share on other sites
Quote:
Original post by Krylloan

I'm not sure if textcolor is the right function.


I used SetConsoleTextAttribute in a game I did and it worked pretty well, except I couldn't get a good color for orange. You need to include <windows.h> to use it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!