////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#define STB_PERLIN_IMPLEMENTATION
#include "stb_perlin.h"
#include <SFML/Graphics.hpp>
#include <vector>
#include <deque>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <cmath>
namespace
{
// Width and height of the application window
const unsigned int windowWidth = 800;
const unsigned int windowHeight = 600;
// Resolution of the generated terrain
const unsigned int resolutionX = 800;
const unsigned int resolutionY = 600;
// Thread pool parameters
const unsigned int threadCount = 4;
const unsigned int blockCount = 32;
struct WorkItem
{
sf::Vertex* targetBuffer;
unsigned int index;
};
std::deque<WorkItem> workQueue;
std::vector<sf::Thread*> threads;
int pendingWorkCount = 0;
bool workPending = true;
bool bufferUploadPending = false;
sf::Mutex workQueueMutex;
struct Setting
{
const char* name;
float* value;
};
// Terrain noise parameters
const int perlinOctaves = 3;
float perlinFrequency = 7.0f;
float perlinFrequencyBase = 4.0f;
// Terrain generation parameters
float heightBase = 0.0f;
float edgeFactor = 0.9f;
float edgeDropoffExponent = 1.5f;
float snowcapHeight = 0.6f;
// Terrain lighting parameters
float heightFactor = windowHeight / 2.0f;
float heightFlatten = 3.0f;
float lightFactor = 0.7f;
}
// Forward declarations of the functions we define further down
void threadFunction();
void generateTerrain(sf::Vertex* vertexBuffer);
////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
// Create the window of the application
sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), "SFML Island",
sf::Style::Titlebar | sf::Style::Close);
window.setVerticalSyncEnabled(true);
sf::Font font;
if (!font.loadFromFile("resources/sansation.ttf"))
return EXIT_FAILURE;
// Create all of our graphics resources
sf::Text hudText;
sf::Text statusText;
sf::Shader terrainShader;
sf::RenderStates terrainStates(&terrainShader);
sf::VertexBuffer terrain(sf::Triangles, sf::VertexBuffer::Static);
// Set up our text drawables
statusText.setFont(font);
statusText.setCharacterSize(28);
statusText.setFillColor(sf::Color::White);
statusText.setOutlineColor(sf::Color::Black);
statusText.setOutlineThickness(2.0f);
hudText.setFont(font);
hudText.setCharacterSize(14);
hudText.setFillColor(sf::Color::White);
hudText.setOutlineColor(sf::Color::Black);
hudText.setOutlineThickness(2.0f);
hudText.setPosition(5.0f, 5.0f);
// Staging buffer for our terrain data that we will upload to our VertexBuffer
std::vector<sf::Vertex> terrainStagingBuffer;
// Check whether the prerequisites are suppprted
bool prerequisitesSupported = sf::VertexBuffer::isAvailable() && sf::Shader::isAvailable();
// Set up our graphics resources and set the status text accordingly
if (!prerequisitesSupported)
{
statusText.setString("Shaders and/or Vertex Buffers Unsupported");
}
else if (!terrainShader.loadFromFile("resources/terrain.vert", "resources/terrain.frag"))
{
prerequisitesSupported = false;
statusText.setString("Failed to load shader program");
}
else
{
// Start up our thread pool
for (unsigned int i = 0; i < threadCount; i++)
{
threads.push_back(new sf::Thread(threadFunction));
threads.back()->launch();
}
// Create our VertexBuffer with enough space to hold all the terrain geometry
terrain.create(resolutionX * resolutionY * 6);
// Resize the staging buffer to be able to hold all the terrain geometry
terrainStagingBuffer.resize(resolutionX * resolutionY * 6);
// Generate the initial terrain
generateTerrain(&terrainStagingBuffer[0]);
statusText.setString("Generating Terrain...");
}
// Center the status text
statusText.setPosition((windowWidth - statusText.getLocalBounds().width) / 2.f, (windowHeight - statusText.getLocalBounds().height) / 2.f);
// Set up an array of pointers to our settings for arrow navigation
Setting settings[] =
{
{"perlinFrequency", &perlinFrequency},
{"perlinFrequencyBase", &perlinFrequencyBase},
{"heightBase", &heightBase},
{"edgeFactor", &edgeFactor},
{"edgeDropoffExponent", &edgeDropoffExponent},
{"snowcapHeight", &snowcapHeight},
{"heightFactor", &heightFactor},
{"heightFlatten", &heightFlatten},
{"lightFactor", &lightFactor}
};
const int settingCount = 9;
int currentSetting = 0;
std::ostringstream osstr;
sf::Clock clock;
while (window.isOpen())
{
// Handle events
sf::Event event;
while (window.pollEvent(event))
{
// Window closed or escape key pressed: exit
if ((event.type == sf::Event::Closed) ||
((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape)))
{
window.close();
break;
}
// Arrow key pressed:
if (prerequisitesSupported && (event.type == sf::Event::KeyPressed))
{
switch (event.key.code)
{
case sf::Keyboard::Return: generateTerrain(&terrainStagingBuffer[0]); break;
case sf::Keyboard::Down: currentSetting = (currentSetting + 1) % settingCount; break;
case sf::Keyboard::Up: currentSetting = (currentSetting + settingCount - 1) % settingCount; break;
case sf::Keyboard::Left: *(settings[currentSetting].value) -= 0.1f; break;
case sf::Keyboard::Right: *(settings[currentSetting].value) += 0.1f; break;
default: break;
}
}
}
// Clear, draw graphics objects and display
window.clear();
window.draw(statusText);
if (prerequisitesSupported)
{
{
sf::Lock lock(workQueueMutex);
// Don't bother updating/drawing the VertexBuffer while terrain is being regenerated
if (!pendingWorkCount)
{
// If there is new data pending to be uploaded to the VertexBuffer, do it now
if (bufferUploadPending)
{
terrain.update(&terrainStagingBuffer[0]);
bufferUploadPending = false;
}
terrainShader.setUniform("lightFactor", lightFactor);
window.draw(terrain, terrainStates);
}
}
// Update and draw the HUD text
osstr.str("");
osstr << "Frame: " << clock.restart().asMilliseconds() << "ms\n"
<< "perlinOctaves: " << perlinOctaves << "\n\n"
<< "Use the arrow keys to change the values.\nUse the return key to regenerate the terrain.\n\n";
for (int i = 0; i < settingCount; ++i)
osstr << ((i == currentSetting) ? ">> " : " ") << settings[i].name << ": " << *(settings[i].value) << "\n";
hudText.setString(osstr.str());
window.draw(hudText);
}
// Display things on screen
window.display();
}
// Shut down our thread pool
{
sf::Lock lock(workQueueMutex);
workPending =
没有合适的资源?快使用搜索试试~ 我知道了~
C++游戏开发 codeblocks20专用 SFML-2.5.1-windows-gcc-7.3.0-mingw-64-bit
共698个文件
html:411个
hpp:99个
png:70个
需积分: 19 1 下载量 93 浏览量
2021-12-13
15:49:51
上传
评论
收藏 16.55MB ZIP 举报
温馨提示
C++游戏开发 codeblocks20专用 64位Windows SFML-2.5.1-windows-gcc-7.3.0-mingw-64-bit
资源详情
资源评论
资源推荐
收起资源包目录
C++游戏开发 codeblocks20专用 SFML-2.5.1-windows-gcc-7.3.0-mingw-64-bit (698个子文件)
libsfml-graphics-s-d.a 4.95MB
libsfml-window-s-d.a 2.95MB
libsfml-audio-s-d.a 2.72MB
libsfml-network-s-d.a 1.81MB
libsfml-system-s-d.a 1.02MB
libfreetype.a 811KB
libvorbisenc.a 696KB
libFLAC.a 649KB
libsfml-graphics-s.a 613KB
libsfml-graphics-d.a 359KB
libsfml-graphics.a 358KB
libvorbis.a 246KB
libsfml-window-s.a 232KB
libsfml-audio-s.a 202KB
libsfml-network-s.a 191KB
libsfml-audio-d.a 135KB
libsfml-network-d.a 135KB
libsfml-audio.a 135KB
libsfml-network.a 134KB
libopenal32.a 120KB
libsfml-system-d.a 95KB
libsfml-system.a 95KB
libsfml-window-d.a 75KB
libsfml-window.a 74KB
libsfml-system-s.a 71KB
libogg.a 66KB
libvorbisfile.a 37KB
libsfml-main-d.a 23KB
libsfml-main.a 1KB
SFMLConfig.cmake 6KB
SFMLStaticTargets.cmake 6KB
SFMLSharedTargets.cmake 5KB
SFMLConfigDependencies.cmake 4KB
SFMLSharedTargets-release.cmake 3KB
SFMLSharedTargets-debug.cmake 3KB
SFMLStaticTargets-release.cmake 3KB
SFMLStaticTargets-debug.cmake 3KB
SFMLConfigVersion.cmake 2KB
Island.cpp 20KB
Shader.cpp 14KB
Pong.cpp 9KB
OpenGL.cpp 9KB
Joystick.cpp 7KB
Ftp.cpp 7KB
Server.cpp 6KB
Window.cpp 5KB
Win32.cpp 4KB
Client.cpp 4KB
SoundCapture.cpp 3KB
Sound.cpp 3KB
TCP.cpp 3KB
UDP.cpp 2KB
Sockets.cpp 1KB
VoIP.cpp 1KB
doxygen.css 24KB
tabs.css 1KB
sfml-graphics-d-2.dll 4.26MB
sfml-audio-d-2.dll 3.38MB
sfml-window-d-2.dll 2.34MB
sfml-audio-2.dll 1.58MB
sfml-network-d-2.dll 1.33MB
sfml-graphics-2.dll 1.18MB
sfml-system-d-2.dll 840KB
openal32.dll 654KB
sfml-window-2.dll 234KB
sfml-network-2.dll 197KB
sfml-system-2.dll 111KB
pong.exe 2.75MB
sound-capture.exe 1.57MB
sound.exe 1.57MB
shader.exe 1.35MB
island.exe 1.33MB
joystick.exe 1.33MB
opengl.exe 1.31MB
win32.exe 653KB
window.exe 263KB
voip.exe 232KB
ftp.exe 221KB
sockets.exe 184KB
ding.flac 60KB
edge.frag 1KB
blur.frag 892B
terrain.frag 397B
pixelate.frag 247B
billboard.frag 175B
blink.frag 151B
billboard.geom 1KB
Shader.hpp 37KB
Utf.hpp 35KB
Texture.hpp 29KB
String.hpp 25KB
Ftp.hpp 25KB
Window.hpp 24KB
RenderTarget.hpp 20KB
Http.hpp 19KB
Text.hpp 19KB
Packet.hpp 18KB
Font.hpp 17KB
Transform.hpp 17KB
Transformable.hpp 16KB
共 698 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
一派青山
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0