Updated to use JGL
This commit is contained in:
175
main.cpp
175
main.cpp
@@ -1,22 +1,25 @@
|
||||
// ReWalker
|
||||
// A random walker that moves around the screen with a fading trail and mouse interaction.
|
||||
// Now with real-time adjustable push force using number keys 1-5!
|
||||
// Written using Redacted ReWindow https://git.redacted.cc/Redacted/ReWindow
|
||||
// A random walker that moves around the screen with a fading trail, mouse interaction, and a status bar.
|
||||
// Updated to use the latest ReWindow API and JGL rendering.
|
||||
// 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/logger/logger.h>
|
||||
#include <GL/gl.h>
|
||||
#include <ReWindow/types/Window.h>
|
||||
#include <ReWindow/Logger.h>
|
||||
#include <JGL/JGL.h>
|
||||
#include <Color4.hpp>
|
||||
#include <memory>
|
||||
|
||||
|
||||
// Define window size and dot size (dot size should be odd for symmetry)
|
||||
#define WIDTH 800
|
||||
#define HEIGHT 600
|
||||
#define DOT_SIZE 5
|
||||
#define STATUS_BAR_HEIGHT 30 // Height of the status bar at the bottom
|
||||
|
||||
// Define fading speed (higher = faster fading, range: 0.001 - 0.2)
|
||||
#define FADE_SPEED 0.05f
|
||||
constexpr unsigned int TRAIL_LENGTH = 20; // Number of past steps stored for fading effect
|
||||
|
||||
// Enable debug mode (set to 0 to disable debug output)
|
||||
#define DEBUG 0
|
||||
@@ -25,42 +28,40 @@
|
||||
* @brief Represents the walker that moves randomly on the screen.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
Walker() : x(WIDTH / 2.0f), y(HEIGHT / 2.0f) {}
|
||||
Walker() : position(WIDTH / 2.0f, (HEIGHT - STATUS_BAR_HEIGHT) / 2.0f) {}
|
||||
|
||||
/**
|
||||
* @brief Moves the walker one step in a random direction.
|
||||
*/
|
||||
void step() {
|
||||
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 dy = std::sin(angle);
|
||||
|
||||
// Update position and clamp within screen boundaries
|
||||
x = std::clamp(x + dx, 0.0f, static_cast<float>(WIDTH - 1));
|
||||
y = std::clamp(y + dy, 0.0f, static_cast<float>(HEIGHT - 1));
|
||||
// Update position and clamp within screen boundaries (excluding status bar)
|
||||
position.x = std::clamp(position.x + dx, 0.0f, static_cast<float>(WIDTH - 1));
|
||||
position.y = std::clamp(position.y + dy, 0.0f, static_cast<float>(HEIGHT - STATUS_BAR_HEIGHT - 1));
|
||||
|
||||
#if DEBUG
|
||||
std::cout << "Walker Position: (" << x << ", " << y << ")\n";
|
||||
std::cout << "Walker Position: (" << position.x << ", " << position.y << ")\n";
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Pushes the walker away from a given (mouse) position.
|
||||
* @param mouseX X coordinate of the mouse.
|
||||
* @param mouseY Y coordinate of the mouse.
|
||||
* @param force The force applied to push the walker away.
|
||||
*/
|
||||
* @brief Pushes the walker away from a given (mouse) position.
|
||||
* @param mouseX X coordinate of the mouse.
|
||||
* @param mouseY Y coordinate of the mouse.
|
||||
* @param force The force applied to push the walker away.
|
||||
*/
|
||||
void pushAway(float mouseX, float mouseY, float force) {
|
||||
float adjustedMouseY = HEIGHT - mouseY; // Adjust for coordinate system
|
||||
|
||||
float deltaX = x - mouseX;
|
||||
float deltaY = y - adjustedMouseY;
|
||||
float deltaX = position.x - mouseX;
|
||||
float deltaY = position.y - mouseY; // No need to invert Y
|
||||
|
||||
float distance = std::sqrt(deltaX * deltaX + deltaY * deltaY);
|
||||
|
||||
@@ -70,8 +71,8 @@ struct Walker {
|
||||
float unitY = deltaY / distance;
|
||||
|
||||
// Apply dynamic push force
|
||||
x = std::clamp(x + unitX * force, 0.0f, static_cast<float>(WIDTH - 1));
|
||||
y = std::clamp(y + unitY * force, 0.0f, static_cast<float>(HEIGHT - 1));
|
||||
position.x = std::clamp(position.x + unitX * force, 0.0f, static_cast<float>(WIDTH - 1));
|
||||
position.y = std::clamp(position.y + unitY * force, 0.0f, static_cast<float>(HEIGHT - STATUS_BAR_HEIGHT - 1));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,75 +80,62 @@ struct Walker {
|
||||
* @brief Resets the walker to the center of the screen.
|
||||
*/
|
||||
void resetPosition() {
|
||||
x = WIDTH / 2.0f;
|
||||
y = HEIGHT / 2.0f;
|
||||
position = Vector2(WIDTH / 2.0f, (HEIGHT - STATUS_BAR_HEIGHT) / 2.0f);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Custom ReWindow window class that manages the random walker simulation.
|
||||
*/
|
||||
class RandomWalkerWindow : public ReWindow::RWindow {
|
||||
class RandomWalkerWindow : public ReWindow::OpenGLWindow {
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor initializes the window, walker, and trail buffer.
|
||||
* @param title Window title
|
||||
* @param width Window width
|
||||
* @param height Window 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>()),
|
||||
trailBuffer(WIDTH * HEIGHT, 1.0f), // Initialize trail buffer to white (1.0)
|
||||
pushForce(3.0f) // Default push force
|
||||
trailBuffer{},
|
||||
pushForce(3.0f),
|
||||
health(100.0f)
|
||||
{}
|
||||
|
||||
/**
|
||||
* @brief Called once when the window is opened.
|
||||
* Configures OpenGL to use a 2D orthographic projection.
|
||||
*/
|
||||
void OnOpen() override {
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
glOrtho(0, WIDTH, HEIGHT, 0, -1, 1); // Map OpenGL coordinates to screen pixels
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
if (!JGL::Init({WIDTH, HEIGHT}, 0, 0))
|
||||
exit(0);
|
||||
|
||||
this->render_target = new RenderTarget({WIDTH, HEIGHT}, Colors::White);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Called every frame to update and render the walker with a fading trail.
|
||||
*/
|
||||
void OnRefresh(float elapsed) override {
|
||||
// Clear the OpenGL buffer (not the trail buffer)
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
// Handle user inputs
|
||||
handleMouseInput();
|
||||
handleKeyboardInput();
|
||||
|
||||
// Update trail (fade effect)
|
||||
fadeTrail();
|
||||
|
||||
|
||||
// Move the walker
|
||||
walker->step();
|
||||
|
||||
// Deplete health over time
|
||||
health = std::max(health - 0.1f, 0.0f);
|
||||
|
||||
// Update the trail buffer with the walkers current position
|
||||
plotWalker();
|
||||
JGL::Update({WIDTH, HEIGHT});
|
||||
J2D::Begin(render_target, true);
|
||||
fadeTrail();
|
||||
drawTrail();
|
||||
plotWalker();
|
||||
J2D::End();
|
||||
|
||||
// Render the updated trail
|
||||
drawTrail();
|
||||
J2D::Begin(nullptr, true);
|
||||
J2D::DrawRenderTarget(render_target, {0, 0});
|
||||
J2D::End();
|
||||
|
||||
// Swap buffers to display the frame
|
||||
GLSwapBuffers();
|
||||
SwapBuffers();
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<Walker> walker;
|
||||
std::vector<float> trailBuffer; // Stores grayscale intensity of pixels (1.0 = white, 0.0 = black)
|
||||
float pushForce; // Variable push force that can be adjusted in real time
|
||||
std::vector<std::pair<Vector2, float>> trailBuffer{};
|
||||
RenderTarget* render_target = nullptr;
|
||||
|
||||
float pushForce;
|
||||
float health;
|
||||
|
||||
/**
|
||||
* @brief Handles mouse input to push the walker away.
|
||||
*/
|
||||
void handleMouseInput() {
|
||||
auto mousePos = GetMouseCoordinates();
|
||||
|
||||
@@ -156,19 +144,16 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Handles keyboard input to reset the walker and adjust push force.
|
||||
*/
|
||||
void handleKeyboardInput() {
|
||||
if (IsKeyDown(Keys::Space)) {
|
||||
walker->resetPosition();
|
||||
health = 100.0f;
|
||||
}
|
||||
|
||||
if (IsKeyDown(Keys::Escape)) {
|
||||
Close();
|
||||
}
|
||||
|
||||
// Adjust push force using number keys 1-5
|
||||
if (IsKeyDown(Keys::One)) pushForce = 1.0f;
|
||||
if (IsKeyDown(Keys::Two)) pushForce = 2.0f;
|
||||
if (IsKeyDown(Keys::Three)) pushForce = 3.0f;
|
||||
@@ -176,48 +161,30 @@ private:
|
||||
if (IsKeyDown(Keys::Five)) pushForce = 5.0f;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Applies the fade effect to the trail buffer.
|
||||
*/
|
||||
void fadeTrail() {
|
||||
for (auto& pixel : trailBuffer) {
|
||||
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);
|
||||
|
||||
/**
|
||||
* @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) {
|
||||
for (int dx = -halfSize; dx <= halfSize; ++dx) {
|
||||
int px = centerX + dx;
|
||||
int py = centerY + dy;
|
||||
|
||||
if (px >= 0 && px < WIDTH && py >= 0 && py < HEIGHT) {
|
||||
trailBuffer[py * WIDTH + px] = 0.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
trailBuffer.emplace_back(walker->position, 1.0f);
|
||||
if (trailBuffer.size() > TRAIL_LENGTH)
|
||||
trailBuffer.erase(trailBuffer.begin());
|
||||
}
|
||||
|
||||
void drawTrail() {
|
||||
std::vector<uint8_t> grayscale(WIDTH * HEIGHT);
|
||||
for (size_t i = 0; i < trailBuffer.size(); ++i) {
|
||||
grayscale[i] = static_cast<uint8_t>(trailBuffer[i] * 255.0f);
|
||||
}
|
||||
glDrawPixels(WIDTH, HEIGHT, GL_LUMINANCE, GL_UNSIGNED_BYTE, grayscale.data());
|
||||
for (const auto& trailPoint : trailBuffer)
|
||||
J2D::DrawPoint({0, 0, 0, (uint8_t) (trailPoint.second * 255.0f)}, trailPoint.first, DOT_SIZE);
|
||||
}
|
||||
|
||||
void plotWalker() {
|
||||
J2D::DrawPoint(Colors::Black, walker->position, DOT_SIZE);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
int main() {
|
||||
auto window = std::make_unique<RandomWalkerWindow>("Random Walker with ReWindow", WIDTH, HEIGHT);
|
||||
window->SetRenderer(RenderingAPI::OPENGL);
|
||||
window->Open();
|
||||
auto window = std::make_unique<RandomWalkerWindow>("Random Walker with Status Bar", WIDTH, HEIGHT);
|
||||
if (!window->Open())
|
||||
return -1;
|
||||
|
||||
while (!window->IsClosing()) {
|
||||
window->ManagedRefresh();
|
||||
|
Reference in New Issue
Block a user