目录
Class 4-1:实体组件——Component
原文件改动
相较于Class 3新增组件Component
新文件知识点
新增文件
程序构建文件——makefile
taget:Component.c Entity.c EntityManager.c GameFuntion.c main.c TransfromComponent.c
gcc -g -Wall Component.c Entity.c EntityManager.c GameFuntion.c main.c TransfromComponent.c -o taget -lSDL2
转换组件头文件——TransfromComponent.h
#ifndef TRANSFORMCOMPONENT_H_
#define TRANSFORMCOMPONENT_H_
#include "Component.h"
typedef struct TransfromComponent
{
Entity* enenty; //组件所属实体
bool (*init)();
void (*clean)();
void (*update)();
void (*render)();
int xPos;
int yPos;
}TransfromComponent;
TransfromComponent* creatTransfromComponent();
#endif
转换组件源文件——TransfromComponent.c
#include "TransfromComponent.h"
#include "GameFuntion.h"
static bool init();
static void clean();
static void update();
static void render();
static TransfromComponent* pthis = NULL;
TransfromComponent* creatTransfromComponent()
{
TransfromComponent* transform = SDL_calloc(1,sizeof(TransfromComponent));
SDL_assert(transform != NULL);
pthis = transform;
pthis->init = init;
pthis->clean = clean;
pthis->update = update;
pthis->render = render;
pthis->init();
return pthis;
}
bool init()
{
pthis->xPos = 0;
pthis->yPos = 0;
return true;
}
void clean()
{
SDL_Log("%s",__FUNCTION__);
}
void update()
{
SDL_Log("%s,%p",__FUNCTION__,pthis);
pthis->xPos++;
pthis->yPos++;
}
void render()
{
SDL_SetRenderDrawColor(GameInstance()->renderer,255,0,0,255);
SDL_Rect rect = {pthis->xPos,pthis->yPos,50,50};
SDL_RenderFillRect(GameInstance()->renderer,&rect);
// SDL_Log("%s",__FUNCTION__);
}
组件头文件——Component.h
#ifndef COMPONENT_H_
#define COMPONENT_H_
#include "Config.h"
typedef struct Entity Entity;
typedef struct Component
{
Entity* enenty; //组件所属实体
bool (*init)();
void (*clean)();
void (*update)();
void (*render)();
}Component;
#endif
组件源文件——Component.c
#include "Component.h"