植物明星大乱斗2
能帮到你的话,就给个赞吧 😘
文章目录
scene.h
#pragma once
#include <graphics.h>
/*
场景
菜单
角色选择
游戏
*/
class Scene {
public:
virtual ~Scene();
public:
virtual void onEnter();//进入
virtual void onExit();//退出
virtual void onInput(const ExMessage& msg);//输入
virtual void onUpdate();//更新
virtual void onDraw();//渲染
};
menuScene.h
#pragma once
#include "scene.h"
#include "sceneManager.h"
extern SceneManager sceneManager;
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
#pragma once
#include "scene.h"
#include "sceneManager.h"
extern SceneManager sceneManager;
class GameScene :public Scene {
public:
virtual void onEnter();//进入
virtual void onInput(const ExMessage& msg);//输入
virtual void onUpdate();//更新
virtual void onDraw();//渲染
virtual void onExit();//退出
};
selectorScene.h
#pragma once
#include "scene.h"
class SelectorScene :public Scene {
};
sceneManager.h
#pragma once
#include "scene.h"
extern Scene* menuScene;
extern Scene* gameScene;
extern Scene* selectorScene;
class SceneManager {
private:
Scene* currentScene;
public:
enum SceneType { Menu, Game, Selector };
public:
void switchTo(SceneType type);//场景切换由场景来判断——场景内不要有其他场景的引用
public:
void setCurrentScene(Scene* scene);//初始化设置
void onInput(const ExMessage& msg);//输入
void onUpdate();//更新
void onDraw();//渲染
};
atlas.h
#pragma once
#include <graphics.h>
#include <vector>
class Atlas {
private:
std::vector<IMAGE> imgs;
public:
void loadImage(LPCTSTR path, int num);
void addImage(const IMAGE& img) { imgs.push_back(img); }
public:
int getSize() const { return imgs.size(); }
IMAGE* getImage(int i) { return &imgs[i]; }
public:
void clear() { imgs.clear(); }
};
main
#include <Windows.h>//windows要放在graphics之前
#include <graphics.h>
#include "menuScene.h"
#include "gameScene.h"
#include "selectorScene.h"
#include "sceneManager.h"
#include <thread>
#include <chrono>
//场景与场景管理器互相引用
Scene* menuScene = new MenuScene;
Scene* gameScene = new GameScene;
Scene* selectorScene = new SelectorScene;
SceneManager sceneManager;
int main() {
sceneManager.setCurrentScene(menuScene);//初始化管理器
ExMessage msg;
const int FPS = 1000 / 1;
initgraph(700, 600, EW_SHOWCONSOLE);
BeginBatchDraw();
while (1) {
//auto startTime = GetTickCount64();
//读消息
peekmessage(&msg);
//处理消息
sceneManager.onInput(msg);
//更新数据
sceneManager.onUpdate();
//渲染
cleardevice();
sceneManager.onDraw();
FlushBatchDraw();
//hertz
/*auto excutionTime = GetTickCount64() - startTime;
if (excutionTime < FPS)
Sleep(FPS - excutionTime);*/
//Sleep(1000);
std::this_thread::sleep_for(std::chrono::microseconds(100));
}
EndBatchDraw();
//释放资源
delete menuScene;
delete gameScene;
}
scene.cpp
#include "scene.h"
Scene::~Scene(){
}
void Scene::onEnter(){
}
void Scene::onExit(){
}
void Scene::onInput(const ExMessage& msg){
}
void Scene::onUpdate(){
}
void Scene::onDraw(){
}
menuScene.cpp
#include "menuScene.h"
#include <iostream>
void MenuScene::onEnter(){
std::cout << "菜单场景进入" << std::endl;
}
void MenuScene::onInput(const ExMessage& msg){
if (msg.message == WM_KEYDOWN)
sceneManager.switchTo(SceneManager::Game);
}
void MenuScene::onUpdate(){
std::cout << "菜单场景正在运行..." << std::endl;
}
void MenuScene::onDraw(){
outtextxy(10, 10, _T("菜单渲染内容"));
}
void MenuScene::onExit(){
std::cout << "菜单场景退出" << std::endl;
}
gameScene.cpp
#include "gameScene.h"
#include <iostream>
#include "sceneManager.h"
void GameScene::onEnter(){
std::cout << "游戏场景进入" << std::endl;
}
void GameScene::onInput(const ExMessage& msg){
if (msg.message == WM_KEYDOWN)
sceneManager.switchTo(SceneManager::Menu);
}
void GameScene::onUpdate(){
std::cout << "游戏场景正在运行" << std::endl;
}
void GameScene::onDraw(){
outtextxy(10, 10, _T("游戏绘制内容"));
}
void GameScene::onExit(){
std::cout << "游戏场景退出" << std::endl;
}
sceneManager.cpp
#include "sceneManager.h"
void SceneManager::setCurrentScene(Scene* scene){
currentScene = scene;
currentScene->onEnter();
}
void SceneManager::switchTo(SceneType type){
currentScene->onExit();
switch (type){
case SceneManager::Menu:
currentScene = menuScene;
break;
case SceneManager::Game:
currentScene = gameScene;
break;
case SceneManager::Selector:
currentScene = selectorScene;
break;
default:
break;
}
currentScene->onEnter();
}
void SceneManager::onInput(const ExMessage& msg){
currentScene->onInput(msg);
}
void SceneManager::onUpdate(){
currentScene->onUpdate();
}
void SceneManager::onDraw(){
currentScene->onDraw();
}
atlas.cpp
#include "atlas.h"
void Atlas::loadImage(LPCTSTR path, int num){
imgs.clear();
imgs.resize(num);
TCHAR tPath[128];
for (int i = 0; i < num; i++) {
_stprintf_s(tPath, path, i + 1);
loadimage(&imgs[i], tPath);
}
}
原文地址:https://blog.csdn.net/qq_42863961/article/details/143659655
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!