By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,990 Members | 1,710 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,990 IT Pros & Developers. It's quick & easy.

my display so weird...

100+
P: 254
I have written 3 files, i dont know whether i do it correctly or wrongly but somehow it compiled well and can run.

My simple aim is to display the terrain.txt file into the terrain array, and then display terrain into screen.

I can't display the whitespace characters from terrain.txt file to screen.
and
It display "weird"( the red color part).



I tried searching one line by line, and i can't figure out which line has the problem.
here is my 3 file:
Expand|Select|Wrap|Line Numbers
  1. // screen.h
  2. #ifndef _SCREEN_H_
  3. #define _SCREEN_H_
  4. #include <string>
  5. #include <iostream>
  6. #include <fstream>
  7. #include <cstdlib>
  8. #include "array2d.h"
  9.  
  10. namespace Game{
  11.     using namespace std;
  12.  
  13.     class Screen{
  14.         public:
  15.  
  16.         Screen(unsigned xsize = 80, unsigned ysize = 23): Xsize(xsize), Ysize(ysize), myArray(Xsize, Ysize){}
  17.  
  18.         ~Screen()
  19.         {
  20.         }
  21.  
  22.         //Insert a character into the screen image at a specified row and column.
  23.         void Insert(unsigned x, unsigned y , char c){
  24.                     myArray.Put(x,y,c);
  25.         }
  26.  
  27.         void setStatus(string str){
  28.             status = str;
  29.         }
  30.  
  31.         /* clear the previous screen and display new screen image with Xsize Ysize */
  32.         void Paint(){
  33.  
  34.             /* clear the screen */
  35.             system("clear");
  36.  
  37.             // and then display the screen with width and height
  38.             for(unsigned y = 0; y < Ysize; y++){
  39.                 for(unsigned x = 0; x < Xsize; x++){
  40.                     cout << myArray.Get(x , y);
  41.                 }
  42.             }
  43.         }
  44.  
  45.         void Beep(){
  46.             cout << '\a';
  47.         }
  48.         private:
  49.         unsigned Xsize;
  50.         unsigned Ysize;
  51.         Array2D<char> myArray;
  52.         string status;
  53.     };
  54. }
  55. #endif
  56.  
Expand|Select|Wrap|Line Numbers
  1. // terrain.h
  2. #ifndef _TERRAIN_H_
  3. #define _TERRAIN_H_
  4. #include <iostream>
  5. #include <fstream>
  6. #include <string>
  7. #include "array2d.h"
  8. #include "screen.h"
  9.  
  10. namespace Game{
  11. using namespace std;
  12.  
  13.     class Terrain{
  14.  
  15.         public:
  16.  
  17.             // constructor that will input the terrain from a specified file and store it
  18.             // in dynamically allocated storage of an appropriate size.
  19.  
  20.                 Terrain(char* filename =""): width(25) , height(11), ter_array(width, height){
  21.  
  22.  
  23.                 ifstream in(filename);
  24.  
  25.                 if(!in){
  26.                     cout << "Cannot open terrain file." << endl;
  27.                 }
  28.  
  29.                 for(unsigned y = 0; y < height; y++){
  30.                     for(unsigned x = 0; x < width; x++){
  31.                         in >> piece;
  32.                         ter_array.Put(x,y,piece);
  33.                     }
  34.                 }
  35.                 in.close();
  36.             };
  37.  
  38.             ~Terrain(){  }
  39.  
  40.             // draw the terrain onto a specified Screen object.
  41.             void Draw(Screen& scr){
  42.                 for(unsigned y = 0; y < height; y++){
  43.                     for(unsigned x = 0; x < width; x++){
  44.                         scr.Insert(x, y, ter_array.Get(x,y));
  45.                     }
  46.                 }
  47.             }
  48.  
  49.         private:
  50.  
  51.             unsigned width;
  52.             unsigned height;
  53.  
  54.             Array2D<char> ter_array;
  55.             char piece;
  56.  
  57.     };
  58. }
  59. #endif
  60.  
Expand|Select|Wrap|Line Numbers
  1. // array2d.h - Simple template class for a sparse 2D array
  2. #ifndef _ARRAY2D_H_
  3. #define _ARRAY2D_H_
  4. #include "exception.h"
  5.  
  6. namespace Game {
  7.  
  8.     /*** Simple template class for a 2D Array ***/
  9.     template<class T>
  10.     class Array2D {
  11.     public:
  12.         Array2D(unsigned width, unsigned height) : xsize(width), ysize(height) {
  13.             array = new T[xsize * ysize];
  14.         }
  15.         ~Array2D() { delete array; }
  16.         void Put(unsigned x, unsigned y, T item) {
  17.             if (x >= xsize || y >= ysize) {
  18.                 throw GameException("Array out of bounds");
  19.             }
  20.             array[y * xsize + x] = item;
  21.         }
  22.         T Get(unsigned x, unsigned y) const {
  23.             if (x >= xsize || y >= ysize) {
  24.                 throw GameException("Array out of bounds");
  25.             }
  26.             return array[y * xsize + x];
  27.         }
  28.         void SetAll(T item) {
  29.             for (unsigned i = 0; i < xsize * ysize; i++) {
  30.                 array[i] = item;
  31.             }
  32.         }
  33.     private:
  34.         const unsigned xsize;
  35.         const unsigned ysize;
  36.         T* array;
  37.     };
  38. }
  39. #endif
  40.  
here is the main.cpp i used:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include "screen.h"
  4. #include "array2d.h"
  5. #include "terrain.h"
  6.  
  7. using namespace std;
  8. using namespace Game;
  9.  
  10. // get terrain.txt from command line
  11. int main(int argc, char* argv[]){
  12.         Screen s;
  13.         Terrain t(argv[1]);
  14.  
  15.         t.Draw(s);
  16.         s.Paint();
  17.  
  18.     return 0;
  19. }
  20.  
Dec 14 '06 #1
Share this Question
Share on Google+
8 Replies


100+
P: 254
Hi all,

i fix the whitespace problem, DeMan helped me out with that already.
get the whitespace using get().

So now leave the problem of that red color spot.
I think somehow the problem issued when i draw terrain to screen.

but still no idea about where's wrong. I'm looking at it now. Hope you guys can help me out too.
Dec 14 '06 #2

100+
P: 254
here is the code i been changing it for past 15 minutes.

Expand|Select|Wrap|Line Numbers
  1. // terrain.h
  2. #ifndef _TERRAIN_H_
  3. #define _TERRAIN_H_
  4. #include <iostream>
  5. #include <fstream>
  6. #include <string>
  7. #include "array2d.h"
  8. #include "screen.h"
  9.  
  10. namespace Game{
  11. using namespace std;
  12.  
  13.     class Terrain{
  14.  
  15.         public:
  16.  
  17.             // constructor that will input the terrain from a specified file and store it
  18.             // in dynamically allocated storage of an appropriate size.
  19.  
  20.                 Terrain(char* filename =""): width(25) , height(11), ter_array(width, height){
  21.  
  22.  
  23.                 ifstream in(filename);
  24.  
  25.                 if(!in){
  26.                     cout << "Cannot open terrain file." << endl;
  27.                 }
  28.  
  29.                 for(unsigned y = 0; y < height; y++){
  30.                     for(unsigned x = 0; x < width; x++){
  31.                                 piece = in.get();               // i get whitespace useing get()
  32.                         ter_array.Put(x,y,piece);
  33.                     }
  34.                 }
  35.                 in.close();
  36.             };
  37.  
  38.             ~Terrain(){  }
  39.  
  40.             // draw the terrain onto a specified Screen object.
  41.             void Draw(Screen& scr){
  42.                 for(unsigned y = 0; y < height; y++){
  43.                     for(unsigned x = 0; x < width; x++){
  44.                         scr.Insert(x, y, ter_array.Get(x,y));
  45.                     }
  46.                 }
  47.             }
  48.  
  49.         private:
  50.  
  51.             unsigned width;
  52.             unsigned height;
  53.  
  54.             Array2D<char> ter_array;
  55.             char piece;
  56.  
  57.     };
  58. }
  59. #endif
  60.  
Expand|Select|Wrap|Line Numbers
  1. // screen.h
  2. #ifndef _SCREEN_H_
  3. #define _SCREEN_H_
  4. #include <string>
  5. #include <iostream>
  6. #include <fstream>
  7. #include <cstdlib>
  8. #include "array2d.h"
  9.  
  10. namespace Game{
  11.     using namespace std;
  12.  
  13.     class Screen{
  14.         public:
  15.  
  16.         Screen(unsigned xsize = 79, unsigned ysize = 23): Xsize(xsize), Ysize(ysize), myArray(Xsize, Ysize){}
  17.  
  18.         ~Screen()
  19.         {
  20.         }
  21.  
  22.         //Insert a character into the screen image at a specified row and column.
  23.         void Insert(unsigned x, unsigned y , char c){
  24.                     myArray.Put(x,y,c);
  25.         }
  26.  
  27.         void setStatus(string str){
  28.             status = str;
  29.         }
  30.  
  31.         /* clear the previous screen and display new screen image with Xsize Ysize */
  32.         void Paint(){
  33.  
  34.             /* clear the screen */
  35.             system("clear");
  36.  
  37.             // and then display the screen with width and height
  38.             for(unsigned y = 0; y < Ysize; y++){
  39.                 for(unsigned x = 0; x < Xsize; x++){
  40.                     cout << myArray.Get(x , y);
  41.                 }
  42.             }
  43.         }
  44.  
  45.         void Beep(){
  46.             cout << '\a';
  47.         }
  48.         private:
  49.         unsigned Xsize;
  50.         unsigned Ysize;
  51.         Array2D<char> myArray;
  52.         string status;
  53.     };
  54. }
  55. #endif
  56.  
terrain.txt file:
Expand|Select|Wrap|Line Numbers
  1. +-----------------------+
  2. |                       |
  3. |              +-+--+ +-+
  4. |                |      |
  5. | +----+ +-------+      |
  6. | |      |              |
  7. | |  +---+    +         |
  8. | |           |         |
  9. | +-----+     +--+ +    |
  10. |                  |    |
  11. +------------------+----+
  12.  
here is the output:



it seems that the last line of terrain.txt file is missing some part.
where's the wrong code anyway...?

thanks in advance.
Nick.
Dec 14 '06 #3

Ganon11
Expert 2.5K+
P: 3,652
DId you make terrain.txt in a simple editor such as Notepad? There may be extra characters used for formatting if you used something 'fancier'...
Dec 14 '06 #4

100+
P: 254
DId you make terrain.txt in a simple editor such as Notepad? There may be extra characters used for formatting if you used something 'fancier'...
i used TextPad 4.7.3: 32-bit Edition to create the terrain.txt file.
I make sure there is no extra space in each end of line. The terrain should be 25 in columns and 11 in rows.
Dec 14 '06 #5

100+
P: 254
if the terrain.txt file is like this:
Expand|Select|Wrap|Line Numbers
  1. +++++++++++++++++++++++++
  2. +++++++++++++++++++++++++
  3. +++++++++++++++++++++++++
  4. +++++++++++++++++++++++++
  5. +++++++++++++++++++++++++
  6. +++++++++++++++++++++++++
  7. +++++++++++++++++++++++++
  8. +++++++++++++++++++++++++
  9. +++++++++++++++++++++++++
  10. +++++++++++++++++++++++++
  11. +++++++++++++++++++++++++
  12.  
it will display to screen like this:

Dec 14 '06 #6

montzter
P: 15
if the terrain.txt file is like this:
Expand|Select|Wrap|Line Numbers
  1. +++++++++++++++++++++++++
  2. +++++++++++++++++++++++++
  3. +++++++++++++++++++++++++
  4. +++++++++++++++++++++++++
  5. +++++++++++++++++++++++++
  6. +++++++++++++++++++++++++
  7. +++++++++++++++++++++++++
  8. +++++++++++++++++++++++++
  9. +++++++++++++++++++++++++
  10. +++++++++++++++++++++++++
  11. +++++++++++++++++++++++++
  12.  
it will display to screen like this:


Hi,

I believe this is a problem with the X and Y coordinates with your screen which doesn't match with the widtch and height of what you are drawing. try lowering down the value of Xsize to 23 then Ysize to 11 in screen.h. Or try organizing your array well to match with the how big your screen is.

That's all thanks. Hope this helps.
Dec 15 '06 #7

100+
P: 254
Hi,

I believe this is a problem with the X and Y coordinates with your screen which doesn't match with the widtch and height of what you are drawing. try lowering down the value of Xsize to 23 then Ysize to 11 in screen.h. Or try organizing your array well to match with the how big your screen is.

That's all thanks. Hope this helps.
since you mentioned me, i change the code in game.cpp(the main function located), where i obtain the width and height of terrain array, and use them to initialise screen array with screen constructor.

it display well, but seems that last line of the terrain.txt file cannot be display.



i guess this code is the problem where it can't get all the characters from terrain text file.
Expand|Select|Wrap|Line Numbers
  1.     for(unsigned y = 0; y < height; y++){
  2.                     for(unsigned x = 0; x < width; x++){
  3.  
  4.                         in.get(piece);
  5.                         ter_array.Put(x,y,piece);
  6.                     }
  7.                 }
  8.  
Dec 15 '06 #8

100+
P: 254
i fixed the reading all character from file problem by setting the terrain array with 27 in width and 11 in height. With this setting, i can display all characters from terrain text file.

now my problem is, i dont know why i can draw the terrain array into screen object without calling t.Draw(s) as following. i set 80x23 of the screen and Paint() is to display the screen into cout. So if i didnt' call t.Draw(s), it should display 80x23 screen into cout.

Anyone please explain to me.

Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char* argv[]){
  2.  
  3.         Terrain t(argv[1]);
  4.  
  5.         unsigned width = t.getWidth();
  6.         unsigned height = t.getHeight();
  7.  
  8.         Screen s(width, height);
  9.  
  10.         //t.Draw(s);
  11.         s.Paint();
  12.  
  13.     return 0;
  14. }
  15.  
Dec 15 '06 #9

Post your reply

Sign in to post your reply or Sign up for a free account.