Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //StateGame.h:
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- class Block;
- class State
- {
- public:
- virtual void render();
- protected:
- private:
- };
- void State::render() {
- }
- class StateGame : public State
- {
- public:
- StateGame();
- int foo;
- int xsize,ysize;
- Block** blockArray;
- void fillMap();
- void setBlock(int x, int y, Block* b);
- Block* getBlock(int x, int y);
- void render();
- protected:
- private:
- };
- //StateGame.cpp:
- StateGame::StateGame()
- {
- foo = 123;
- cout << "create stategame" << endl;
- blockArray = new Block*[xsize*ysize];
- xsize = 128;
- ysize = 128;
- fillMap();
- }
- class Block
- {
- public:
- int x,y;
- StateGame* state;
- Block(StateGame* state, int x, int y);
- void render();
- protected:
- private:
- };
- Block::Block(StateGame* myState, int x, int y)
- {
- cout << "blockInit" << endl;
- state = myState;
- this->x = x;
- this->y = y;
- cout << "state = "<< state << endl;
- cout << "state foo = "<<state->foo << endl;
- cout << "done with blockInit" << endl;
- }
- void Block::render() {
- cout << "blockRender" << endl;
- cout << "state = "<< state << endl;
- cout << "state foo = "<<state->foo << endl ;
- }
- void StateGame::fillMap() {
- srand(time(NULL));
- for(int xx=0;xx<xsize;xx++)
- for(int yy=0;yy<ysize;yy++) {
- if (rand()%10==1) {
- setBlock(xx,yy, new Block(this, xx,yy));
- } else {
- }
- cout << "Next" << endl;
- }
- }
- Block* StateGame::getBlock(int x, int y) {
- return blockArray[x+(y*xsize)];
- }
- void StateGame::setBlock(int x, int y, Block* b) {
- blockArray[x+(y*xsize)] = b;
- }
- void StateGame::render() {
- cout << "Start render";
- for(int xx=0;xx<xsize;xx++)
- for(int yy=0;yy<ysize;yy++) {
- Block* b = getBlock(xx,yy);
- cout << "B("<<xx<<","<<yy<<") = "<<b<<endl;
- if (b!=NULL){
- cout << "Render "<< b << endl;
- b->render();
- }
- else {
- cout << "Null";
- }
- }
- cout << "Finish render";
- }
- // main.cpp
- State* state;
- int main ( int argc, char** argv )
- {
- state = new StateGame();
- bool done = true;
- while (!done)
- {
- state->render();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement