能帮到你的话,就给个赞吧 😘
文章目录
scene.h
cpp
#pragma once
#include <graphics.h>
/*
场景
菜单
角色选择
游戏
*/
class Scene {
public:
virtual ~Scene() = 0;
public:
virtual void onEnter() = 0; //进入
virtual void onInput(const ExMessage& msg) = 0; //输入
virtual void onUpdate() = 0; //更新
virtual void onDraw() = 0; //渲染
virtual void onExit() = 0; //退出
};
menuScene.h
cpp
#pragma once
#include "scene.h"
class MenuScene :public Scene {
public:
virtual void onEnter(); //进入
virtual void onInput(const ExMessage& msg); //输入
virtual void onUpdate(); //更新
virtual void onDraw(); //渲染
virtual void onExit(); //退出
};
gameScene.h
cpp
#pragma once
#include "scene.h"
class gameScene :public Scene {
public:
virtual void onEnter(); //进入
virtual void onInput(const ExMessage& msg); //输入
virtual void onUpdate(); //更新
virtual void onDraw(); //渲染
virtual void onExit(); //退出
};
main
cpp
#include <Windows.h> //windows要放在graphic之前
#include <graphics.h>
#include "menuScene.h"
int main() {
ExMessage msg;
const int FPS = 1000 / 1000;
auto menuScene = new MenuScene;
initgraph(1280, 720, EW_SHOWCONSOLE);
BeginBatchDraw();
menuScene->onEnter();
while (1) {
auto startTime = GetTickCount64();
//读消息
peekmessage(&msg);
//处理消息
menuScene->onInput(msg);
//更新数据
menuScene->onUpdate();
//渲染
cleardevice();
menuScene->onDraw();
FlushBatchDraw();
//hertz
auto excutionTime = GetTickCount64() - startTime;
if (excutionTime < FPS)
Sleep(FPS - excutionTime);
}
EndBatchDraw();
delete menuScene;
}
scene.cpp
cpp
#include "scene.h"
Scene::~Scene(){
}
menuScene.cpp
cpp
#include "menuScene.h"
#include <iostream>
void MenuScene::onEnter(){
std::cout << "菜单场景进入" << std::endl;
}
void MenuScene::onInput(const ExMessage& msg){
}
void MenuScene::onUpdate(){
std::cout << "菜单场景正在运行..." << std::endl;
}
void MenuScene::onDraw(){
outtextxy(10, 10, _T("菜单渲染内容"));
}
void MenuScene::onExit(){
std::cout << "菜单场景退出" << std::endl;
}
gameScene.cpp
cpp
#include "gameScene.h"
#include <iostream>
void gameScene::onEnter(){
std::cout << "游戏场景进入" << std::endl;
}
void gameScene::onInput(const ExMessage& msg){
}
void gameScene::onUpdate(){
std::cout << "游戏场景正在运行" << std::endl;
}
void gameScene::onDraw(){
outtextxy(10, 10, _T("游戏绘制内容"));
}
void gameScene::onExit(){
std::cout << "游戏场景退出" << std::endl;
}