Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Help] Creating a Map  (Read 2007 times)

0 Members and 4 Guests are viewing this topic.

JohnB

  • Newbie
  • *
  • Posts: 9
    • View Profile
[Help] Creating a Map
« on: March 25, 2012, 04:57:11 am »
I have a class called Block and a bunch of other classes extend this class. I have a problem however... I want to store all the blocks into an array. However, I am having a problem. The plan is to have the max number of blocks in the game 256 width 512 height. I want to efficiently store these into an array or any method to hold them.


Should I make a list of addresses pointing to them or what?

DeanOfLeicester

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: [Help] Creating a Map
« Reply #1 on: March 25, 2012, 07:04:15 am »
I have a class called Block and a bunch of other classes extend this class. I have a problem however... I want to store all the blocks into an array. However, I am having a problem. The plan is to have the max number of blocks in the game 256 width 512 height. I want to efficiently store these into an array or any method to hold them.


Should I make a list of addresses pointing to them or what?


Erm...probably like this :-
 
#define width 256;
#define height 512;

cBlock Blocks[width][height]; //An instance of your block class

// render your blocks
For(int i=0; I<width; I++)
{
   For(int j=0; j<height; j++)
     {
        App.Draw(Blocks[i][j]);
      }
}
 
« Last Edit: March 25, 2012, 08:56:39 am by Laurent »

JohnB

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: [Help] Creating a Map
« Reply #2 on: March 25, 2012, 05:17:08 pm »
Thanks, didn't know if there was a better way.

JohnB

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: [Help] Creating a Map
« Reply #3 on: March 25, 2012, 07:54:01 pm »
Erm... I am still having a problem....
Here is my Block.h and GrassBlock.h

I define Draw as a virtual function... But when I go through the Block[][] and call Draw, it calls it on the block side. Even though the value stored in teh array is a GrassBlock, or StoneBlock, or even WoodBlock it all calls the Draw of the Block class.

Code: [Select]
#pragma once
#include "SFML/Graphics.hpp"

class Block
{
public:
void SetBoundingBox(int offSetX, int offSetY, int width, int height);
void SetPosition(int x, int y);

Block();

virtual void Initialize(sf::Image &image);
virtual void Update(sf::RenderWindow &window);
virtual void Draw(sf::RenderWindow &window);

virtual bool ContainsPoint(int x, int y); //doesn't check if the edges are hit
virtual bool CollidesWithPoint(int x, int y); //checks edges and inside
void output();

bool active;
protected:
sf::Sprite blockSprite;
int hardness, objId, health, maxHealth;
int offsetX, offsetY, width, height; //bounding box
int positionX, positionY;
};

Code: [Select]
#pragma once
#include "Block.h"

class GrassBlock : public Block
{
public:
void Initialize(sf::Image &image);
void Draw(sf::RenderWindow &window);
void Update(sf::RenderWindow &window);
};