Advertisement
Guest User

Untitled

a guest
Mar 21st, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1. //StateGame.h:
  2.  
  3. #ifndef STATEGAME_H
  4. #define STATEGAME_H
  5.  
  6. #include "State.h"
  7. #include "Entity.h"
  8. #include "Block.h"
  9.  
  10. #include <iostream>
  11. #include <sdl.h>
  12. #include <math.h>
  13. #include <list>
  14.  
  15. #include <cstdlib>
  16. #include <ctime>
  17.  
  18. using namespace std;
  19.  
  20. class Block;
  21.  
  22. class StateGame : public State
  23. {
  24.     public:
  25.  
  26.         int foo;
  27.  
  28.         static const int tilesize = 32;
  29.         int xsize,ysize;
  30.         Block** blockArray;
  31.        
  32.         StateGame();
  33.         virtual ~StateGame();
  34.  
  35.         void fillMap();
  36.         void setBlock(int x, int y, Block* b);
  37.         Block* getBlock(int x, int y);
  38.  
  39.         void render();
  40.     protected:
  41.     private:
  42. };
  43.  
  44. #endif // STATEGAME_H
  45.  
  46. //StateGame.cpp
  47.  
  48.  
  49. #include "StateGame.h"
  50. #include "EntityCB.h"
  51.  
  52. StateGame::StateGame()
  53. {
  54.     foo = 123;
  55.     cout << "create stategame" << endl;
  56.     xsize = 128;
  57.     ysize = 128;
  58.     fillMap();
  59. }
  60.  
  61. StateGame::~StateGame()
  62. {
  63.     //dtor
  64. }
  65.  
  66. void StateGame::fillMap() {
  67.     srand(time(NULL));
  68.     cout << "Filling map" <<endl;
  69.     for(int xx=0;xx<xsize;xx++)
  70.     for(int yy=0;yy<ysize;yy++) {
  71.         if (rand()%10==1) {
  72.         setBlock(xx,yy, new Block(this, xx,yy));
  73.         }
  74.     }
  75. }
  76.  
  77. Block* StateGame::getBlockWorld(float x, float y) {
  78.     x/=tilesize;
  79.     y/=tilesize;
  80.     return getBlock(x,y);
  81. }
  82.  
  83. void StateGame::setBlockWorld(float x, float y, Block* b) {
  84.     x/=tilesize;
  85.     y/=tilesize;
  86.     return setBlock(x,y,b);
  87. }
  88.  
  89. Block* StateGame::getBlock(int x, int y) {
  90.     return blockArray[x+(y*xsize)];
  91. }
  92.  
  93. void StateGame::setBlock(int x, int y, Block* b) {
  94.     blockArray[x+(y*xsize)] = b;
  95. }
  96.  
  97. void StateGame::render() {
  98.     cout << "Start render";
  99.  
  100.     for(int xx=0;xx<xsize;xx++)
  101.     for(int yy=0;yy<ysize;yy++) {
  102.         Block* b = getBlock(xx,yy);
  103.  
  104.         cout << "B("<<xx<<","<<yy<<") = "<<b<<endl;
  105.         if (b!=NULL){
  106.             cout << "Gonna render "<<b;
  107.             b->render();
  108.         }
  109.     }
  110.     cout << "Finish render";
  111. }
  112.  
  113. //Block.h
  114.  
  115.  
  116. #ifndef BLOCK_H
  117. #define BLOCK_H
  118.  
  119. #include <SDL.h>
  120. #include "StateGame.h"
  121. #include "foo.h"
  122.  
  123. class StateGame;
  124.  
  125. class Block
  126. {
  127.     public:
  128.         SDL_Surface* sprite;
  129.         SDL_Rect dstrect;
  130.         int x,y;
  131.         StateGame* state;
  132.         Block(StateGame* state, int x, int y);
  133.         void render();
  134.         virtual ~Block();
  135.     protected:
  136.     private:
  137. };
  138.  
  139. #endif // BLOCK_H
  140.  
  141. // Block.cpp
  142.  
  143. #include "Block.h"
  144. #include <iostream>
  145.  
  146. extern SDL_Surface* spr_block;
  147. extern SDL_Surface* screen;
  148.  
  149. Block::Block(StateGame* myState, int x, int y)
  150. {
  151.     sprite = spr_block;
  152.     state = myState;
  153.     this->x = x;
  154.     this->y = y;
  155.     cout << "blockInit";
  156.     cout << "state = "<< state;
  157.     cout << "state foo = "<<state->foo;
  158. }
  159.  
  160. Block::~Block()
  161. {
  162. }
  163.  
  164. void Block::render() {
  165.     cout << "blockRender";
  166.     cout << "state = "<< state;
  167.     cout << "state foo = "<<state->foo;
  168. }
  169.  
  170. // main.cpp
  171.  
  172. #include <cstdlib>
  173. #include <SDL.h>
  174. #endif
  175.  
  176. #include <iostream>
  177. #include "State.h"
  178. #include "StateGame.h"
  179.  
  180. using namespace std;
  181.  
  182. State* state;
  183.  
  184. int main ( int argc, char** argv )
  185. {
  186.     freopen( "CON", "wt", stdout );
  187.     freopen( "CON", "wt", stderr );
  188.  
  189.     state = new StateGame();
  190.  
  191.     // program main loop
  192.  
  193.     bool done = false;
  194.     printf("starting");
  195.  
  196.     while (!done)
  197.     {
  198.         state->render();
  199.     }
  200.  
  201.     return 0;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement