Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
f18c8c3fe3 |
@@ -28,20 +28,39 @@ include(cmake/CPM.cmake)
|
|||||||
# You specify the release name to target against. See the link below for releases:
|
# You specify the release name to target against. See the link below for releases:
|
||||||
# https://git.redacted.cc/Redacted/ReWindow/releases
|
# https://git.redacted.cc/Redacted/ReWindow/releases
|
||||||
|
|
||||||
|
CPMAddPackage(
|
||||||
|
NAME mcolor
|
||||||
|
URL https://git.redacted.cc/maxine/mcolor/archive/Prerelease-5.zip
|
||||||
|
)
|
||||||
|
|
||||||
CPMAddPackage(
|
CPMAddPackage(
|
||||||
NAME ReWindow
|
NAME ReWindow
|
||||||
URL URL https://git.redacted.cc/Redacted/ReWindow/archive/Prerelease-31.zip
|
URL https://git.redacted.cc/Redacted/ReWindow/archive/Prerelease-32.zip
|
||||||
|
)
|
||||||
|
|
||||||
|
CPMAddPackage(
|
||||||
|
NAME JGL
|
||||||
|
URL https://git.redacted.cc/josh/JGL/archive/Prerelease-47.zip
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(ReWalkerApp main.cpp)
|
add_executable(ReWalkerApp main.cpp)
|
||||||
|
|
||||||
target_include_directories(ReWalkerApp PUBLIC ${ReWindow_SOURCE_DIR}/include ${J3ML_SOURCE_DIR}/include)
|
target_include_directories(ReWalkerApp PUBLIC
|
||||||
|
${ReWindow_SOURCE_DIR}/include
|
||||||
|
${J3ML_SOURCE_DIR}/include
|
||||||
|
${mcolor_SOURCE_DIR}/include
|
||||||
|
${JGL_SOURCE_DIR}/include
|
||||||
|
)
|
||||||
|
|
||||||
target_link_libraries(ReWalkerApp PUBLIC J3ML ReWindowLibrary)
|
target_link_libraries(ReWalkerApp PUBLIC J3ML ReWindow mcolor JGL)
|
||||||
|
|
||||||
# simple window test
|
# simple window test
|
||||||
add_executable(TestWindowApp test.cpp)
|
add_executable(TestWindowApp test.cpp)
|
||||||
|
|
||||||
target_include_directories(TestWindowApp PUBLIC ${ReWindow_SOURCE_DIR}/include ${J3ML_SOURCE_DIR}/include)
|
target_include_directories(TestWindowApp PUBLIC
|
||||||
|
${ReWindow_SOURCE_DIR}/include
|
||||||
|
${J3ML_SOURCE_DIR}/include
|
||||||
|
${mcolor_SOURCE_DIR}/include
|
||||||
|
)
|
||||||
|
|
||||||
target_link_libraries(TestWindowApp PUBLIC J3ML ReWindowLibrary)
|
target_link_libraries(TestWindowApp PUBLIC J3ML ReWindow)
|
121
main.cpp
121
main.cpp
@@ -5,9 +5,10 @@
|
|||||||
// Written by Rich
|
// Written by Rich
|
||||||
// With help from (and thanks to) from Maxine, Josh and Bill
|
// With help from (and thanks to) from Maxine, Josh and Bill
|
||||||
|
|
||||||
#include <rewindow/types/window.h>
|
#include <ReWindow/types/Window.h>
|
||||||
#include <rewindow/logger/logger.h>
|
#include <ReWindow/Logger.h>
|
||||||
#include <GL/gl.h>
|
#include <JGL/JGL.h>
|
||||||
|
#include <Color4.hpp>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
// Define window size and dot size (dot size should be odd for symmetry)
|
// Define window size and dot size (dot size should be odd for symmetry)
|
||||||
@@ -18,6 +19,8 @@
|
|||||||
// Define fading speed (higher = faster fading, range: 0.001 - 0.2)
|
// Define fading speed (higher = faster fading, range: 0.001 - 0.2)
|
||||||
#define FADE_SPEED 0.05f
|
#define FADE_SPEED 0.05f
|
||||||
|
|
||||||
|
constexpr unsigned int TRAIL_LENGTH = 20;
|
||||||
|
|
||||||
// Enable debug mode (set to 0 to disable debug output)
|
// Enable debug mode (set to 0 to disable debug output)
|
||||||
#define DEBUG 0
|
#define DEBUG 0
|
||||||
|
|
||||||
@@ -25,28 +28,28 @@
|
|||||||
* @brief Represents the walker that moves randomly on the screen.
|
* @brief Represents the walker that moves randomly on the screen.
|
||||||
*/
|
*/
|
||||||
struct Walker {
|
struct Walker {
|
||||||
float x, y; // Current position of the walker
|
Vector2 position{}; // Current position of the walker
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Constructor initializes the walker at the center of the screen.
|
* @brief Constructor initializes the walker at the center of the screen.
|
||||||
*/
|
*/
|
||||||
Walker() : x(WIDTH / 2.0f), y(HEIGHT / 2.0f) {}
|
Walker() : position(WIDTH / 2.0f, HEIGHT / 2.0f) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Moves the walker one step in a random direction.
|
* @brief Moves the walker one step in a random direction.
|
||||||
*/
|
*/
|
||||||
void step() {
|
void step() {
|
||||||
float angle = static_cast<float>(rand()) / RAND_MAX * 360.0f;
|
float angle = static_cast<float>(rand()) / RAND_MAX * 360.0f;
|
||||||
angle = angle * M_PI / 180.0f; // Convert degrees to radians
|
angle = Math::Radians(angle);
|
||||||
float dx = std::cos(angle);
|
float dx = std::cos(angle);
|
||||||
float dy = std::sin(angle);
|
float dy = std::sin(angle);
|
||||||
|
|
||||||
// Update position and clamp within screen boundaries
|
// Update position and clamp within screen boundaries
|
||||||
x = std::clamp(x + dx, 0.0f, static_cast<float>(WIDTH - 1));
|
position.x = std::clamp(position.x + dx, 0.0f, static_cast<float>(WIDTH - 1));
|
||||||
y = std::clamp(y + dy, 0.0f, static_cast<float>(HEIGHT - 1));
|
position.y = std::clamp(position.y + dy, 0.0f, static_cast<float>(HEIGHT - 1));
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
std::cout << "Walker Position: (" << x << ", " << y << ")\n";
|
std::cout << "Walker Position: (" << position.x << ", " << position.y << ")\n";
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,8 +62,8 @@ struct Walker {
|
|||||||
void pushAway(float mouseX, float mouseY, float force) {
|
void pushAway(float mouseX, float mouseY, float force) {
|
||||||
float adjustedMouseY = HEIGHT - mouseY; // Adjust for coordinate system
|
float adjustedMouseY = HEIGHT - mouseY; // Adjust for coordinate system
|
||||||
|
|
||||||
float deltaX = x - mouseX;
|
float deltaX = position.x - mouseX;
|
||||||
float deltaY = y - adjustedMouseY;
|
float deltaY = position.y - adjustedMouseY;
|
||||||
|
|
||||||
float distance = std::sqrt(deltaX * deltaX + deltaY * deltaY);
|
float distance = std::sqrt(deltaX * deltaX + deltaY * deltaY);
|
||||||
|
|
||||||
@@ -70,8 +73,8 @@ struct Walker {
|
|||||||
float unitY = deltaY / distance;
|
float unitY = deltaY / distance;
|
||||||
|
|
||||||
// Apply dynamic push force
|
// Apply dynamic push force
|
||||||
x = std::clamp(x + unitX * force, 0.0f, static_cast<float>(WIDTH - 1));
|
position.x = std::clamp(position.x + unitX * force, 0.0f, static_cast<float>(WIDTH - 1));
|
||||||
y = std::clamp(y + unitY * force, 0.0f, static_cast<float>(HEIGHT - 1));
|
position.y = std::clamp(position.y + unitY * force, 0.0f, static_cast<float>(HEIGHT - 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,15 +82,14 @@ struct Walker {
|
|||||||
* @brief Resets the walker to the center of the screen.
|
* @brief Resets the walker to the center of the screen.
|
||||||
*/
|
*/
|
||||||
void resetPosition() {
|
void resetPosition() {
|
||||||
x = WIDTH / 2.0f;
|
position = Vector2(WIDTH / 2.0f, HEIGHT / 2.0f);
|
||||||
y = HEIGHT / 2.0f;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Custom ReWindow window class that manages the random walker simulation.
|
* @brief Custom ReWindow window class that manages the random walker simulation.
|
||||||
*/
|
*/
|
||||||
class RandomWalkerWindow : public ReWindow::RWindow {
|
class RandomWalkerWindow : public ReWindow::OpenGLWindow {
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Constructor initializes the window, walker, and trail buffer.
|
* @brief Constructor initializes the window, walker, and trail buffer.
|
||||||
@@ -96,53 +98,54 @@ public:
|
|||||||
* @param height Window height
|
* @param height Window height
|
||||||
*/
|
*/
|
||||||
RandomWalkerWindow(const std::string& title, int width, int height)
|
RandomWalkerWindow(const std::string& title, int width, int height)
|
||||||
: ReWindow::RWindow(title, width, height),
|
: ReWindow::OpenGLWindow(title, width, height, 2, 1),
|
||||||
walker(std::make_unique<Walker>()),
|
walker(std::make_unique<Walker>()),
|
||||||
trailBuffer(WIDTH * HEIGHT, 1.0f), // Initialize trail buffer to white (1.0)
|
trailBuffer{}, // Initialize trail buffer to white (1.0)
|
||||||
pushForce(3.0f) // Default push force
|
pushForce(3.0f) // Default push force
|
||||||
|
|
||||||
{}
|
{}
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Called once when the window is opened.
|
|
||||||
* Configures OpenGL to use a 2D orthographic projection.
|
|
||||||
*/
|
|
||||||
void OnOpen() override {
|
void OnOpen() override {
|
||||||
glMatrixMode(GL_PROJECTION);
|
if (!JGL::Init({WIDTH, HEIGHT}, 0, 0))
|
||||||
glLoadIdentity();
|
exit(0);
|
||||||
glOrtho(0, WIDTH, HEIGHT, 0, -1, 1); // Map OpenGL coordinates to screen pixels
|
|
||||||
glMatrixMode(GL_MODELVIEW);
|
this->render_target = new RenderTarget({WIDTH, HEIGHT}, Colors::White);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Called every frame to update and render the walker with a fading trail.
|
* @brief Called every frame to update and render the walker with a fading trail.
|
||||||
*/
|
*/
|
||||||
void OnRefresh(float elapsed) override {
|
void OnRefresh(float elapsed) override {
|
||||||
// Clear the OpenGL buffer (not the trail buffer)
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
|
||||||
|
|
||||||
// Handle user inputs
|
// Handle user inputs
|
||||||
handleMouseInput();
|
handleMouseInput();
|
||||||
handleKeyboardInput();
|
handleKeyboardInput();
|
||||||
|
|
||||||
// Update trail (fade effect)
|
|
||||||
fadeTrail();
|
|
||||||
|
|
||||||
// Move the walker
|
// Move the walker
|
||||||
walker->step();
|
walker->step();
|
||||||
|
|
||||||
// Update the trail buffer with the walkers current position
|
JGL::Update({WIDTH, HEIGHT});
|
||||||
plotWalker();
|
J2D::Begin(render_target, true);
|
||||||
|
// Update trail (fade effect)
|
||||||
|
fadeTrail();
|
||||||
// Render the updated trail
|
// Render the updated trail
|
||||||
drawTrail();
|
drawTrail();
|
||||||
|
// Update the trail buffer with the walkers current position
|
||||||
|
plotWalker();
|
||||||
|
J2D::End();
|
||||||
|
|
||||||
|
J2D::Begin(nullptr, true);
|
||||||
|
J2D::DrawRenderTarget(render_target, {0, 0});
|
||||||
|
J2D::DrawString(Colors::Black, "Framerate: " + std::to_string((int) (1.f / delta_time)), 0, 0, 1, 16);
|
||||||
|
J2D::End();
|
||||||
// Swap buffers to display the frame
|
// Swap buffers to display the frame
|
||||||
GLSwapBuffers();
|
SwapBuffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Walker> walker;
|
std::unique_ptr<Walker> walker;
|
||||||
std::vector<float> trailBuffer; // Stores grayscale intensity of pixels (1.0 = white, 0.0 = black)
|
std::vector<std::pair<Vector2, float>> trailBuffer{}; // Stores grayscale intensity of pixels (1.0 = white, 0.0 = black)
|
||||||
|
RenderTarget* render_target = nullptr;
|
||||||
|
|
||||||
float pushForce; // Variable push force that can be adjusted in real time
|
float pushForce; // Variable push force that can be adjusted in real time
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -180,44 +183,34 @@ private:
|
|||||||
* @brief Applies the fade effect to the trail buffer.
|
* @brief Applies the fade effect to the trail buffer.
|
||||||
*/
|
*/
|
||||||
void fadeTrail() {
|
void fadeTrail() {
|
||||||
for (auto& pixel : trailBuffer) {
|
// Fade out existing trail points
|
||||||
pixel = std::min(pixel + FADE_SPEED, 1.0f); // Ensure value does not exceed 1.0 (white)
|
for (auto& trailPoint : trailBuffer)
|
||||||
}
|
trailPoint.second = std::max(trailPoint.second - FADE_SPEED, 0.0f);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
trailBuffer.emplace_back(walker->position, 1.0f);
|
||||||
* @brief Plots the walkers current position as a black dot in the trail buffer.
|
|
||||||
*/
|
|
||||||
void plotWalker() {
|
|
||||||
int centerX = static_cast<int>(walker->x);
|
|
||||||
int centerY = static_cast<int>(walker->y);
|
|
||||||
int halfSize = DOT_SIZE / 2;
|
|
||||||
|
|
||||||
for (int dy = -halfSize; dy <= halfSize; ++dy) {
|
// Limit trail length
|
||||||
for (int dx = -halfSize; dx <= halfSize; ++dx) {
|
if (trailBuffer.size() > TRAIL_LENGTH)
|
||||||
int px = centerX + dx;
|
trailBuffer.erase(trailBuffer.begin());
|
||||||
int py = centerY + dy;
|
|
||||||
|
|
||||||
if (px >= 0 && px < WIDTH && py >= 0 && py < HEIGHT) {
|
|
||||||
trailBuffer[py * WIDTH + px] = 0.0f;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawTrail() {
|
void drawTrail() {
|
||||||
std::vector<uint8_t> grayscale(WIDTH * HEIGHT);
|
for (const auto& trailPoint : trailBuffer)
|
||||||
for (size_t i = 0; i < trailBuffer.size(); ++i) {
|
J2D::DrawPoint({0, 0, 0, (uint8_t) (trailPoint.second * 255.0f)}, trailPoint.first, DOT_SIZE);
|
||||||
grayscale[i] = static_cast<uint8_t>(trailBuffer[i] * 255.0f);
|
|
||||||
}
|
}
|
||||||
glDrawPixels(WIDTH, HEIGHT, GL_LUMINANCE, GL_UNSIGNED_BYTE, grayscale.data());
|
|
||||||
|
void plotWalker() {
|
||||||
|
J2D::DrawPoint(Colors::Black, walker->position, DOT_SIZE);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
auto window = std::make_unique<RandomWalkerWindow>("Random Walker with ReWindow", WIDTH, HEIGHT);
|
auto window = std::make_unique<RandomWalkerWindow>("Random Walker with ReWindow", WIDTH, HEIGHT);
|
||||||
window->SetRenderer(RenderingAPI::OPENGL);
|
if (!window->Open())
|
||||||
window->Open();
|
return -1;
|
||||||
|
|
||||||
|
// Uncomment this to make things go super fast.
|
||||||
|
//window->SetVsyncEnabled(false);
|
||||||
|
|
||||||
while (!window->IsClosing()) {
|
while (!window->IsClosing()) {
|
||||||
window->ManagedRefresh();
|
window->ManagedRefresh();
|
||||||
|
16
test.cpp
16
test.cpp
@@ -1,23 +1,23 @@
|
|||||||
// simple test program to open a window with
|
// simple test program to open a window with
|
||||||
// a blue background.
|
// a blue background.
|
||||||
|
|
||||||
#include <rewindow/types/window.h>
|
#include <ReWindow/types/Window.h>
|
||||||
#include <rewindow/logger/logger.h>
|
#include <ReWindow/Logger.h>
|
||||||
#include <GL/gl.h>
|
#include <GL/gl.h>
|
||||||
|
|
||||||
#define WIDTH 800
|
#define WIDTH 800
|
||||||
#define HEIGHT 600
|
#define HEIGHT 600
|
||||||
|
|
||||||
|
|
||||||
class RandomWalkerWindow : public ReWindow::RWindow {
|
class RandomWalkerWindow : public ReWindow::OpenGLWindow {
|
||||||
public:
|
public:
|
||||||
RandomWalkerWindow(const std::string& title, int width, int height)
|
RandomWalkerWindow(const std::string& title, int width, int height)
|
||||||
: ReWindow::RWindow(title, width, height) {}
|
: ReWindow::OpenGLWindow(title, width, height, 2, 1) {}
|
||||||
|
|
||||||
void OnRefresh(float elapsed) override {
|
void OnRefresh(float elapsed) override {
|
||||||
glClearColor(0.0f, 0.0f, 1.0f, 1.0f); // Clear with blue
|
//glClearColor(0.0f, 0.0f, 1.0f, 1.0f); // Clear with blue
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
//glClear(GL_COLOR_BUFFER_BIT);
|
||||||
GLSwapBuffers();
|
SwapBuffers();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -25,8 +25,6 @@ int main() {
|
|||||||
|
|
||||||
std::unique_ptr<RandomWalkerWindow> window =
|
std::unique_ptr<RandomWalkerWindow> window =
|
||||||
std::make_unique<RandomWalkerWindow>("Random Walker with ReWindow", WIDTH, HEIGHT);
|
std::make_unique<RandomWalkerWindow>("Random Walker with ReWindow", WIDTH, HEIGHT);
|
||||||
|
|
||||||
window->SetRenderer(RenderingAPI::OPENGL);
|
|
||||||
window->Open();
|
window->Open();
|
||||||
|
|
||||||
while (!window->IsClosing()) {
|
while (!window->IsClosing()) {
|
||||||
|
Reference in New Issue
Block a user