具体的には、マインスイーパーの様なゲームを作るとき、
一マス一マスの状態を管理するクラスのメンバー関数を、描画クラスが使用する方法です。
継承とは少し違うと思いますし、
包含の場合、インスタンスを持つと、データクラスが持つデータ領域の大きさを変更出来ませんし。
ポインタだと、コントラクタの引数にデータオブジェクトのアドレスを入れる必要があり、
少し変な気がします。
一応、ポインタを包含する方法で、上手く動いているんですが、これでいいんでしょうか?
main.cpp
#include "DxLib.h"
#include "deta.h"
#include "byouga.h"
int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int){
ChangeWindowMode(TRUE),DxLib_Init(),SetDrawScreen( DX_SCREEN_BACK );
deta obj1(10,10);
byouga obu2(50,50,30,&obj1);
obu2.load();
while( ScreenFlip()==0 && ProcessMessage()==0 && ClearDrawScreen()==0 ){
obu2.paint();
}
DxLib_End();
return 0;
}
#ifndef DEF_DETA_H
#define DEF_DETA_H
class deta{
int *masufl;//0, 閉じている 1, 開けている 2, 旗を立てている 3, クエスチョンマーク
int x,y;
public:
deta(int,int);
int nyuusyu(int,int);
void henkou(int,int,int);
~deta();
};
#endif
#include "deta.h"
#include "DxLib.h"
deta::deta(int x1, int y1){
masufl=(int *)malloc(sizeof(int)*x1*y1);
if(masufl==NULL){return;}
for(int i=0;i<(x1*y1);i++)*(masufl+i)=0;
x=x1;y=y1;
}
int deta::nyuusyu(int x1,int y1){
return *(masufl+x*x1+y1);
}
void deta::henkou(int x1,int y1,int atai){
*(masufl+x*x1+y1)=atai;
}
deta::~deta(){
free(masufl);
}
#ifndef DEF_BYOUGA_H
#define DEF_BYOUGA_H
#include "deta.h"
class byouga{
int x,y,haba ;
int masu;
deta *po1;
public:
byouga(int,int,int,deta *);
void load();
void paint();
};
#endif
#include "byouga.h"
#include "DxLib.h"
#include "deta.h"
byouga::byouga(int x1,int y1,int tate,deta *po){
x=x1;
y=y1;
haba=tate;
po1=po;
}
void byouga::load(){
masu=LoadGraph("画像/無題2.jpg");
}
void byouga::paint(){
for(int i=0;i<10;i++){
for(int i1=0;i1<10;i1++){
if(po1->nyuusyu(i,i1)==0){
DrawExtendGraph((x+i*haba),(y+i1*haba), (x+i*haba+haba-1),(y+i1*haba+haba-1),masu,TRUE);
}
}
}
}