Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //StateGame.h:
- #ifndef STATEGAME_H
- #define STATEGAME_H
- #include "State.h"
- #include "Entity.h"
- #include "Block.h"
- #include <iostream>
- #include <sdl.h>
- #include <math.h>
- #include <list>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- class Block;
- class StateGame : public State
- {
- public:
- int foo;
- static const int tilesize = 32;
- int xsize,ysize;
- Block** blockArray;
- StateGame();
- virtual ~StateGame();
- void fillMap();
- void setBlock(int x, int y, Block* b);
- Block* getBlock(int x, int y);
- void render();
- protected:
- private:
- };
- #endif // STATEGAME_H
- //StateGame.cpp
- #include "StateGame.h"
- #include "EntityCB.h"
- StateGame::StateGame()
- {
- foo = 123;
- cout << "create stategame" << endl;
- xsize = 128;
- ysize = 128;
- fillMap();
- }
- StateGame::~StateGame()
- {
- //dtor
- }
- void StateGame::fillMap() {
- srand(time(NULL));
- cout << "Filling map" <<endl;
- 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));
- }
- }
- }
- Block* StateGame::getBlockWorld(float x, float y) {
- x/=tilesize;
- y/=tilesize;
- return getBlock(x,y);
- }
- void StateGame::setBlockWorld(float x, float y, Block* b) {
- x/=tilesize;
- y/=tilesize;
- return setBlock(x,y,b);
- }
- 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 << "Gonna render "<<b;
- b->render();
- }
- }
- cout << "Finish render";
- }
- //Block.h
- #ifndef BLOCK_H
- #define BLOCK_H
- #include <SDL.h>
- #include "StateGame.h"
- #include "foo.h"
- class StateGame;
- class Block
- {
- public:
- SDL_Surface* sprite;
- SDL_Rect dstrect;
- int x,y;
- StateGame* state;
- Block(StateGame* state, int x, int y);
- void render();
- virtual ~Block();
- protected:
- private:
- };
- #endif // BLOCK_H
- // Block.cpp
- #include "Block.h"
- #include <iostream>
- extern SDL_Surface* spr_block;
- extern SDL_Surface* screen;
- Block::Block(StateGame* myState, int x, int y)
- {
- sprite = spr_block;
- state = myState;
- this->x = x;
- this->y = y;
- cout << "blockInit";
- cout << "state = "<< state;
- cout << "state foo = "<<state->foo;
- }
- Block::~Block()
- {
- }
- void Block::render() {
- cout << "blockRender";
- cout << "state = "<< state;
- cout << "state foo = "<<state->foo;
- }
- // main.cpp
- #include <cstdlib>
- #include <SDL.h>
- #endif
- #include <iostream>
- #include "State.h"
- #include "StateGame.h"
- using namespace std;
- State* state;
- int main ( int argc, char** argv )
- {
- freopen( "CON", "wt", stdout );
- freopen( "CON", "wt", stderr );
- state = new StateGame();
- // program main loop
- bool done = false;
- printf("starting");
- while (!done)
- {
- state->render();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement