Class 4-1:实体组件——Component

目录

Class 4-1:实体组件——Component

原文件改动

新文件知识点

新增文件

程序构建文件——makefile

转换组件头文件——TransfromComponent.h

转换组件源文件——TransfromComponent.c

组件头文件——Component.h

组件源文件——Component.c


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"
​

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值