Advertisement
gmfreaky

Untitled

Mar 21st, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. //StateGame.h:
  2.  
  3. #include <iostream>
  4.  
  5. #include <cstdlib>
  6. #include <ctime>
  7.  
  8. using namespace std;
  9.  
  10. class Block;
  11.  
  12. class State
  13. {
  14.     public:
  15.         virtual void render();
  16.  
  17.     protected:
  18.     private:
  19. };
  20.  
  21. void State::render() {
  22.  
  23. }
  24.  
  25. class StateGame : public State
  26. {
  27.     public:
  28.  
  29.         StateGame();
  30.  
  31.         int foo;
  32.  
  33.         int xsize,ysize;
  34.         Block** blockArray;
  35.  
  36.         void fillMap();
  37.         void setBlock(int x, int y, Block* b);
  38.         Block* getBlock(int x, int y);
  39.  
  40.         void render();
  41.     protected:
  42.     private:
  43. };
  44.  
  45. //StateGame.cpp:
  46.  
  47. StateGame::StateGame()
  48. {
  49.     foo = 123;
  50.     cout << "create stategame" << endl;
  51.     blockArray = new Block*[xsize*ysize];
  52.     xsize = 128;
  53.     ysize = 128;
  54.     fillMap();
  55. }
  56.  
  57. class Block
  58. {
  59.     public:
  60.         int x,y;
  61.         StateGame* state;
  62.         Block(StateGame* state, int x, int y);
  63.         void render();
  64.     protected:
  65.     private:
  66. };
  67.  
  68. Block::Block(StateGame* myState, int x, int y)
  69. {
  70.     cout << "blockInit" << endl;
  71.     state = myState;
  72.     this->x = x;
  73.     this->y = y;
  74.     cout << "state = "<< state << endl;
  75.     cout << "state foo = "<<state->foo << endl;
  76.     cout << "done with blockInit" << endl;
  77. }
  78.  
  79. void Block::render() {
  80.     cout << "blockRender" << endl;
  81.     cout << "state = "<< state << endl;
  82.     cout << "state foo = "<<state->foo << endl ;
  83. }
  84.  
  85. void StateGame::fillMap() {
  86.     srand(time(NULL));
  87.     for(int xx=0;xx<xsize;xx++)
  88.     for(int yy=0;yy<ysize;yy++) {
  89.         if (rand()%10==1) {
  90.             setBlock(xx,yy, new Block(this, xx,yy));
  91.         } else {
  92.         }
  93.         cout << "Next" << endl;
  94.     }
  95. }
  96.  
  97. Block* StateGame::getBlock(int x, int y) {
  98.     return blockArray[x+(y*xsize)];
  99. }
  100. void StateGame::setBlock(int x, int y, Block* b) {
  101.     blockArray[x+(y*xsize)] = b;
  102. }
  103.  
  104. void StateGame::render() {
  105.     cout << "Start render";
  106.  
  107.     for(int xx=0;xx<xsize;xx++)
  108.     for(int yy=0;yy<ysize;yy++) {
  109.         Block* b = getBlock(xx,yy);
  110.  
  111.         cout << "B("<<xx<<","<<yy<<") = "<<b<<endl;
  112.         if (b!=NULL){
  113.             cout << "Render "<< b << endl;
  114.             b->render();
  115.         }
  116.         else {
  117.             cout << "Null";
  118.         }
  119.     }
  120.     cout << "Finish render";
  121. }
  122.  
  123. // main.cpp
  124.  
  125. State* state;
  126.  
  127. int main ( int argc, char** argv )
  128. {
  129.     state = new StateGame();
  130.     bool done = true;
  131.     while (!done)
  132.     {
  133.         state->render();
  134.     }
  135.     return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement