📅  最后修改于: 2023-12-03 15:27:42.089000             🧑  作者: Mango
背页风景是一款C++的开源游戏引擎,它采用了现代C++的许多新特性,如C++11、C++14、C++17等,同时还采用了各种现代化的技术,如OpenGL、OpenAL、SDL等。
#include <bgfx/bgfx.h>
#include <SDL2/SDL.h>
int main(int argc, char** argv) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow(
"My Game",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
640, 480, SDL_WINDOW_SHOWN);
SDL_Event event;
bool quit = false;
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
}
}
bgfx::renderFrame();
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
背页风景可以通过GitHub进行下载,也可以通过CMake进行构建和安装。
git clone https://github.com/Backpage-scenery/Backpage-scenery.git
创建build目录:
mkdir build && cd build
使用CMake进行构建:
cmake ..
构建可执行文件:
make
安装背页风景:
make install
使用背页风景来构建你的游戏非常容易,只需要包含必要的头文件和库文件,然后就可以开始编写你的游戏代码了。下面是背页风景的一些使用示例。
#include <SDL2/SDL.h>
int main(int argc, char** argv) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow(
"My Game",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
640, 480, SDL_WINDOW_SHOWN);
SDL_Event event;
bool quit = false;
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
#include <bgfx/bgfx.h>
#include <SDL2/SDL.h>
int main(int argc, char** argv) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow(
"My Game",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
640, 480, SDL_WINDOW_SHOWN);
bgfx::init();
bgfx::reset(640, 480, BGFX_RESET_VSYNC);
SDL_Event event;
bool quit = false;
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
}
}
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x000000ff, 1.0f, 0);
bgfx::touch(0);
bgfx::frame();
}
bgfx::shutdown();
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
#include <bgfx/bgfx.h>
#include <SDL2/SDL.h>
struct PosColorVertex {
float x, y, z;
uint32_t color;
static void init() {
ms_decl
.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
.end();
}
static bgfx::VertexDecl ms_decl;
};
bgfx::VertexDecl PosColorVertex::ms_decl;
PosColorVertex s_vertices[] = {
{ -1.0f, 1.0f, 0.0f, 0xff0000ff },
{ 1.0f, 1.0f, 0.0f, 0xff00ff00 },
{ 1.0f, -1.0f, 0.0f, 0xffff0000 },
{ -1.0f, -1.0f, 0.0f, 0xffffffff },
};
uint16_t s_indices[] = {
0, 1, 2,
2, 3, 0,
};
int main(int argc, char** argv) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow(
"My Game",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
640, 480, SDL_WINDOW_SHOWN);
PosColorVertex::init();
bgfx::init();
bgfx::reset(640, 480, BGFX_RESET_VSYNC);
bgfx::VertexBufferHandle vertexBuffer = bgfx::createVertexBuffer(
bgfx::makeRef(s_vertices, sizeof(s_vertices)),
PosColorVertex::ms_decl);
bgfx::IndexBufferHandle indexBuffer = bgfx::createIndexBuffer(
bgfx::makeRef(s_indices, sizeof(s_indices)));
SDL_Event event;
bool quit = false;
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
}
}
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x000000ff, 1.0f, 0);
bgfx::setVertexBuffer(0, vertexBuffer);
bgfx::setIndexBuffer(indexBuffer);
bgfx::setState(BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A |
BGFX_STATE_DEPTH_TEST_ALWAYS | BGFX_STATE_CULL_CW);
bgfx::submit(0, bgfx::getDefaultShader());
bgfx::frame();
}
bgfx::destroy(vertexBuffer);
bgfx::destroy(indexBuffer);
bgfx::shutdown();
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
背页风景是一个非常不错的C++游戏引擎,它采用了现代C++的许多新特性和现代化的技术,具有高度可定制和可扩展的架构,并且支持多平台运行。如果你正在寻找一款高质量的C++游戏引擎,那么背页风景是一个值得考虑的选择。