Please help me...I'm a noob

Started by
6 comments, last by vinb 19 years, 6 months ago
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.
Say what you mean and mean what you say.
Advertisement
you seemed to have mistakenly hit your caps lock key about 3/4 the way through your post.
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?
To the AP: I think he was using CAPS to indicate non-code text.
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!
Say what you mean and mean what you say.
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 10int 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.
Thank's a bunch...I really appreciate your effort!

People like you make this forum great!

Say what you mean and mean what you say.
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.

This topic is closed to new replies.

Advertisement