Class 1:编程流程

目录

Class 1:编程流程

Start——main

Second——新建头文件InstanceMode.h

Third——新建源文件InstanceMode.c

Forth——修改main

END——main添加帧率


Class 1:编程流程

基于Linux下Class1 编程流程。

版本:Linux ubuntu 5.4.0-146-generic #163~18.04.1-Ubuntu

准备:提前安装好SDL2,教程参考:一、Ubuntu下SDL安装_GBEDC的博客-CSDN博客

Start——main

新建main.c 文件

touch main.c

搭好框架:

#include <stdio.h>
int main(int argc,char* argv[])
{
        return 0;
}

Second——新建头文件InstanceMode.h

新建头文件

touch InstanceMode.h

编写头文件

SDL中一个窗口的创建流程为:初始化-->更新-->渲染-->事件判断-->退出

按照这个流程,编写结构体

#ifndef INSTANCEMODE_H_
#define INSTANCEMODE_H
#include <SDL2/SDL.h>   //Linux下引用SDL2 头文件包含方式
#include <stdbool.h>    //使用布尔函数需要包含此文件夹
typedef struct InstanceMode
{
        SDL_Window* window; //SDL2窗口指针
        SDL_Renderer* renderer; //SDL2渲染指针
        SDL_Event events;   //SDL2事件处理
        bool isRunning; //布尔函数判断程序运行与否
        bool (*init)(const char* title,int w, int h);   //初始化
        void (*clean)();    //清除
        void (*update)();   //更新
        void (*render)();   //渲染
        void (*handleEvents)(); //事件处理
        bool (*running)();  //运行判断
        void (*quit)(); //退出
​
}InstanceMode;
​
InstanceMode* MainInstance;
​
#endif

Third——新建源文件InstanceMode.c

新建源文件

touch InstanceMode.c

根据头文件中结构体的内容,把对应的函数实现

编写源文件

#include <InstanceMode.h>
​
//函数声明
bool init(const char* title,int w,int h);
void clean();
void update();
void render();
void handleEvents();
bool running();
void quit();
​
static InstanceMode* pthis; //新建pthis方便程序编写
InstanceMode* MainInstance()
{
    static InstanceMode a;
    if(!pthis)
    {
        pthis = &a;
        //函数初始化
        pthis->init = init;
        pthis->clean = clean;
        pthis->update = update;
        pthis->render = render;
        pthis->handleEvents = handleEvents;
        pthis->running = running;
        pthis->quit = quit; 
    }
    return pthis;
}
​
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");
        }
        else
        {
            printf("SDL_CreateWindow Error:%s \n",SDL_GetError());
            return false;
        }
        pthis->render = SDL_CreateRenderer(pthis->window,-1,0);
        if(pthis->render)
        {
            SDL_Log("Render Created");
        }
        else
        {
            SDL_DestroyWindow(pthis->window);
            printf("SDL_CreateRender Error:%s \n",SDL_GetError());
            return false;
        }   
    pthis->isRunning = ture;
    }
    else
    {
    pthis->isRunning =false;
    }
    return pthis->isRunning;    
}
​
void clean()
{
    SDL_DestroyRenderer(pthis->renderer);
    SDL_DestroyWindow(pthis->window);
    SDL_Quit();
    SDL_Log("SDL Clean");
}
​
static int cnt;     //测试部分
void update()
{
    cnt++;      //测试部分
}
​
void render()
{
    SDL_Log("cnt:%d\n",cnt);
}
​
void handleEvents()
{
    if(SDL_PollEvent(&(pthis->events)))
    {
        if((pthis->events).type == SDL_QUIT)
        {
            quit();
        }
    }
}
bool running()
{
    return pthis->isRunning;
}
​
void quit()
{
    pthis->isRunning = false;
}

Forth——修改main

在mian使用单例

#include <stdio.h>
#include "InstanceMode.h"
int main(int argc,char* argv[])
{
        InstanceMode* instance = MainInstance();    //新建单例对象
        instance->init("SDL",640,480);  //初始化
        while(instance->isRunning)
        {
                instance->update(); //
                instance->render();
                instance->handleEvents();
        }
        instance->clean();
        return 0;
}

编译命令:

gcc main.c Instance.c -lSDL2 

生成a.out文件,执行:

./a.out

现象:通过在update中不断打印cnt的值显示在屏幕上

END——main添加帧率

上面写好后,程序不断打印的太快,通过添加帧率,控制打印日志的速度。

修改后的main函数如下:

#include <stdio.h>
#include "GameFuntion.h"
​
int main(int argc,char *argv[])
{
    float frameRate = 1000.0/24;    //帧率
    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;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值