c++游戏框架

游戏类 

class Sprite {
public:
    Sprite(int x, int y, int w, int h, const char* imagePath);
    ~Sprite();
    void render(SDL_Renderer* renderer);
    void move(int x, int y);
private:
    SDL_Texture* texture_;
    SDL_Rect rect_;
};

物理引擎类

class PhysicsEngine {
public:
    PhysicsEngine();
    ~PhysicsEngine();
    void applyForce(Sprite* sprite, float x, float y);
    void update(Sprite* sprite, float deltaTime);
};

游戏对象类

class GameObject {
public:
    virtual void update(float deltaTime) = 0;
    virtual void render(SDL_Renderer* renderer) = 0;
};

游戏函数

游戏初始化

bool init() {
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        return false;
    }
    window_ = SDL_CreateWindow("Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screenWidth_, screenHeight_, SDL_WINDOW_SHOWN);
    renderer_ = SDL_CreateRenderer(window_, -1, SDL_RENDERER_ACCELERATED);

    return true;
}

游戏主循环

void run() {
    bool quit = false;
    SDL_Event event;
    while (!quit) {
        float deltaTime = getDeltaTime();
        while (SDL_PollEvent(&event) != 0) {
            if (event.type == SDL_QUIT) {
                quit = true;
            }
        }

        update(deltaTime);
        render();
    }
}

游戏更新

void update(float deltaTime) {
    for (auto& gameObject : gameObjects_) {
        gameObject->update(deltaTime);
    }
}

游戏渲染
 

void render() {
    SDL_SetRenderDrawColor(renderer_, 0xFF, 0xFF, 0xFF, 0xFF);
    SDL_RenderClear(renderer_);
    for (auto& gameObject : gameObjects_) {
        gameObject->render(renderer_);
    }

    SDL_RenderPresent(renderer_);
}

你可能感兴趣的:(C++更多语法,游戏,ffmpeg,c++,游戏,框架)