目录
Class 2:游戏实体——Entity
原文件改动:
相较于Class 1,新增了Entity实体。改动部分
-
因原GameFunction.c中包含的头文件和新增Entity.c源文件共同头文件部分提取,新建为Config.h头文件
-
原GameFunction.c中新增Entity.h头文件
-
GameFunction.c构建两个Entity实体player1、player2
-
GameFunction.c中init()函数内新增
player1 = GetEntity();player2 = GetEntity();
-
GameFunction.c中clean()函数内新增
Cast(player1)->clean();Cast(player2)->clean();
-
GameFunction.c中update()函数内新增
Cast(player1)->update();Cast(player2)->update();
-
GameFunction.c中render()函数内新增
Cast(player1)->render();Cast(player2)->render();
新文件知识点
-
Entity.c中
Entity* entity_cast(Entity* e)
函数用作对象的切换,如果不做这个切换pthis对象不改变。 -
Entity.h中使用#define简化函数
Entity* entity_cast(Entity* e)
-
calloc和malloc动态内存分配的区别
-
Assert断言的作用
-
__FUNCTION__:ANSI C 定义了许多宏。在编程中可以使用这些宏,但是不能直接修改这些预定义的宏。
DATE 当前日期,一个以 “MMM DD YYYY” 格式表示的字符串常量。
TIME 当前时间,一个以 “HH:MM:SS” 格式表示的字符串常量。
FILE 这会包含当前文件名,一个字符串常量。
LINE 这会包含当前行号,一个十进制常量。
STDC 当编译器以 ANSI 标准编译时,则定义为 1;判断该文件是不是标准 C 程序。
FUNCTION 程序预编译时预编译器将用所在的函数名,返回值是字符串;
程序构建
主程序——main.c
#include <stdio.h>
#include "GameFuntion.h"
int main(int argc,char *argv[])
{
float frameRate = 1000.0/60; //帧率
uint32_t startTime = 0;
uint32_t frameTime = 0;
GameFuntion* game = GameInstance();
game->init("SDL",640,480);
while(game->running())
{
startTime = SDL_GetTicks();
game->update();
game->render();
game->handleEvents();
frameTime = SDL_GetTicks() - startTime;
if(frameRate - frameTime > 0)
{
SDL_Delay(frameRate - frameTime);
}
}
game->clean();
return 0;
}
单例头文件——GameFunction.h
#ifndef GAMEFUNTION_H_
#define GAMEFUNTION_H_
#include "Config.h" //修改
typedef struct GameFuntion
{
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Event events;
bool isRunning;
//声明函数指针
//返回值类型 (*指针变量名) (形参列表)
bool (*init)(const char* title,int w,int h);
void (*clean)();
void (*update)();
void (*render)();
void (*handleEvents)();
bool (*running)();
void (*quit)();
}GameFuntion;
GameFuntion* GameInstance();
#endif
单例源文件——GameFunction.c
#include "GameFuntion.h"
#include "Entity.h" //新增
static bool init(const char* title,int w,int h);
static void clean();
static void update();
static void render();
static void handleEvents();
static bool running();
static void quit();
static GameFuntion* pthis = NULL;
GameFuntion* GameInstance()
{
static GameFuntion game;
if (!pthis)
{
pthis = &game;
pthis->init = init;
pthis->clean = clean;
pthis->update = update;
pthis->render = render;
pthis->handleEvents = handleEvents;
pthis->running = running;
pthis->quit = quit;
}
return pthis;
}
Entity* player1; //新增
Entity* player2;
bool init(const char* title,int w,int h)
{
if(SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
SDL_Log("SDL Init");
pthis->window = SDL_CreateWindow(title,SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,w,h,SDL_WINDOW_SHOWN);
if (pthis->window)
{
SDL_Log("Window Created");
}
pthis->renderer = SDL_CreateRenderer(pthis->window,-1,0);
if(pthis->renderer)
{
SDL_Log("Renderer Created");
}
pthis->isRunning = true;
}
else
{
pthis->isRunning = false;
}
player1 = GetEntity(); //新增
player2 = GetEntity();
return pthis->isRunning;
}
void clean()
{
Cast(player1)->clean(); //新增
Cast(player2)->clean();
SDL_DestroyRenderer(pthis->renderer);
SDL_DestroyWindow(pthis->window);
SDL_Quit();
SDL_Log("SDL Clean");
}
int cnt = 0;
void update()
{
cnt++;
Cast(player1)->update(); //新增
Cast(player2)->update();
}
void render()
{
//SDL_Log("cnt:%d",cnt);
Cast(player1)->render(); //新增
Cast(player2)->render();
}
void handleEvents()
{
if(SDL_PollEvent(&pthis->events))
{
if(pthis->events.type == SDL_QUIT)
{
quit();
}
}
}
bool running(){return pthis->isRunning;}
static void quit(){pthis->isRunning = false;}
实体头文件——Entity.h
#ifndef ENTITY_H_
#define ENTITY_H_
#include "Config.h"
typedef struct Entity
{
bool (*init)();
void (*clean)();
void (*update)();
void (*render)();
}Entity;
Entity* GetEntity();
Entity* entity_cast(Entity* e); //对象切换
#define Cast(e) entity_cast(e)
#endif
实体源文件——Entity.c
#include "Config.h"
#include "Entity.h"
static bool init();
static void clean();
static void update();
static void render();
static Entity* pthis = NULL;
Entity* GetEntity()
{
Entity* ins = SDL_calloc(1,sizeof(Entity)); //动态分配内存
SDL_assert(ins != NULL); //断言
pthis = ins;
pthis->init = init;
pthis->clean = clean;
pthis->update = update;
pthis->render = render;
return pthis;
}
Entity* entity_cast(Entity* e)
{
pthis = e;
return e;
}
bool init()
{
return true;
}
void clean()
{
SDL_Log("%s",__FUNCTION__);
}
void update()
{
SDL_Log("%s,%p",__FUNCTION__,pthis);
}
void render()
{
// SDL_Log("%s",__FUNCTION__);
}
共用配置头文件——Config.h
#ifndef CONFIGER_H_
#define CONFIGER_H_
#include <stdbool.h>
#include "SDL.h"
#pragma comment(lib,"SDL2.lib")
#pragma comment(lib,"SDLmain.lib")
#endif