Block Pushing - MVC

<< < (2/2)

Richard Marks:
So now it is time to integrate the World with the rest of our Demo.

We add some member variables to the Demo struct:

WorldModel* worldmodel_;
WorldView* worldview_;
BITMAP* graphics_;

Initialize them in the Demo() constructor:

graphics_ = load_bitmap("graphics.bmp", 0);
worldmodel_ = new WorldModel(BOARD_COLUMNS, BOARD_ROWS);
worldview_ = new WorldView(worldmodel_, grid_, graphics_);

Cleanup in the ~Demo() destructor:

delete worldview_;
delete worldmodel_;

and call the View's render method in the Demo::render() method:


That was simple right?
If you run your program will crash.
Why? Because you don't have the graphics.bmp file...duh.
You can download the file by right-clicking and saving the following image:

The tiles are in order of left to right:

Floor (unused because we currently use the Grid's display for the floor)

All tiles are 32x32 (remember we set up the Grid to be 32x32? well this is why...)

So once you have that and you run the program, it should do the exact same thing as before...
because the tile map is all zero right now, and we don't render any tiles with zeros...
so it doesn't display anything!
Next I'll show you the next structure in this project, for world generation.
Until then...

Richard Marks:
I'm back! And now lets get to the world generation already! ;D

The struct name is rather obvious... WorldGenerator.
All that this struct needs to do is modify a world model for us, so we just need a pointer to one.
Our struct looks like this thus far:

struct WorldGenerator
WorldModel* model_;
WorldGenerator(WorldModel* model) :


I've decided to implement this as individual methods for each type of generation.
Right now, all that exists is a very simple "room" generator.
It clears the world tile map, and adds walls around the edge.


void basic_room()

int width = model_->get_width();
int height = model_->get_height();

for (int y = 0; y < height; y++)
model_->set(0, y, 1);
model_->set(width - 1, y, 1);

for (int x = 0; x < width; x++)
model_->set(x, 0, 1);
model_->set(x, height - 1, 1);

Now lets see how we use this new struct.
In our Demo() constructor, right after we init the worldmodel_ variable, we call our generator:

WorldGenerator gen(worldmodel_); gen.basic_room();

That was simple. :D

Now, when you run the code, you should get the basic room displayed.
Feel free to add more generators for different things.

Oh right, before I forget..I added one more thing to the WorldGenerator struct.
A token spawn method.
(Or in other words, a method that randomly places a specific value on the map, but never overwrites existing values.
So only on a zero will the "token" be placed. Useful for creating random rooms.

void spawn_token(int token)
int width = model_->get_width();
int height = model_->get_height();
bool ok = false;
int spawnx = 0;
int spawny = 0;
if (!ok)
spawnx = Random::in_range(2, width - 2);
spawny = Random::in_range(2, height - 2);
int value = (*model_)(spawnx, spawny);
if (!value)
ok = true;
model_->set(spawnx, spawny, token);

Before I go, let me show you where to use the basic_room() method!
The last thing in the method should be:


That spawns 1 box and 1 goal in the room. 8)

Next up I'll talk about the player.


[0] Message Index

[*] Previous page