forked from rich/ReWalker
Update main.cpp
render random walker as dot and added hopefully helpful comments
This commit is contained in:
98
main.cpp
98
main.cpp
@@ -12,57 +12,137 @@
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
// Define window size and dot size (dot size should be odd)
|
||||
// Define window size and dot size (dot size should be odd for symmetry)
|
||||
#define WIDTH 800
|
||||
#define HEIGHT 600
|
||||
#define DOT_SIZE 5
|
||||
|
||||
#define DEBUG 0
|
||||
// Enable debug mode (set to 0 to disable debug output)
|
||||
#define DEBUG 1
|
||||
|
||||
/**
|
||||
* @brief Represents the walker that moves randomly on the screen.
|
||||
*/
|
||||
struct Walker {
|
||||
float x, y;
|
||||
float x, y; // 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) {}
|
||||
|
||||
/**
|
||||
* @brief Moves the walker one step in a random direction.
|
||||
*
|
||||
* Uses a random angle (0-360 degrees) to determine movement direction.
|
||||
* Ensures the walker stays within the screen bounds using std::clamp.
|
||||
*/
|
||||
void step() {
|
||||
float angle = static_cast<float>(rand()) / RAND_MAX * 360.0f;
|
||||
angle = angle * M_PI / 180.0f;
|
||||
float dx = std::cos(angle);
|
||||
float dy = std::sin(angle);
|
||||
angle = angle * M_PI / 180.0f; // Convert degrees to radians
|
||||
float dx = std::cos(angle); // Calculate movement in X direction
|
||||
float dy = std::sin(angle); // Calculate movement in Y direction
|
||||
|
||||
// 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));
|
||||
|
||||
// Debugging: Print walker's position if debug mode is enabled
|
||||
#if DEBUG
|
||||
std::cout << "Walker Position: (" << x << ", " << y << ")\n";
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Custom ReWindow window class that manages the random walker simulation.
|
||||
*/
|
||||
class RandomWalkerWindow : public ReWindow::RWindow {
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor initializes the window and walker.
|
||||
* @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), walker(std::make_unique<Walker>()) {}
|
||||
|
||||
/**
|
||||
* @brief Called once when the window is opened.
|
||||
*
|
||||
* Configures OpenGL to use a 2D orthographic projection
|
||||
* so that pixel coordinates match OpenGL coordinates.
|
||||
*/
|
||||
void OnOpen() override {
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
glOrtho(0, WIDTH, HEIGHT, 0, -1, 1); // Map OpenGL coordinates to screen pixels
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Called every frame to update and render the walker.
|
||||
*
|
||||
* Clears the screen, moves the walker, draws it, and swaps buffers.
|
||||
* @param elapsed Time elapsed since the last frame (not currently used).
|
||||
*/
|
||||
void OnRefresh(float elapsed) override {
|
||||
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Clear with white
|
||||
// Clear the screen to white each frame
|
||||
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
walker->step(); // Move walker in each frame
|
||||
// Move the walker in a random direction
|
||||
walker->step();
|
||||
|
||||
// Render the walker on the screen
|
||||
drawWalker();
|
||||
|
||||
// Swap buffers to display the frame
|
||||
GLSwapBuffers();
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<Walker> walker; // Use smart pointer for ownership
|
||||
std::unique_ptr<Walker> walker; // Unique pointer to manage walker instance
|
||||
|
||||
/**
|
||||
* @brief Draws the walker as a black square on the screen.
|
||||
*
|
||||
* Uses OpenGL `GL_QUADS` to draw a filled square at the walker’s position.
|
||||
*/
|
||||
void drawWalker() {
|
||||
float halfSize = DOT_SIZE / 2.0f;
|
||||
float left = walker->x - halfSize;
|
||||
float right = walker->x + halfSize;
|
||||
float top = walker->y - halfSize;
|
||||
float bottom = walker->y + halfSize;
|
||||
|
||||
glColor3f(0.0f, 0.0f, 0.0f); // Set color to black
|
||||
|
||||
// Draw a filled square (dot) at the walker's position
|
||||
glBegin(GL_QUADS);
|
||||
glVertex2f(left, top);
|
||||
glVertex2f(right, top);
|
||||
glVertex2f(right, bottom);
|
||||
glVertex2f(left, bottom);
|
||||
glEnd();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Main function that creates and runs the ReWindow application.
|
||||
*/
|
||||
int main() {
|
||||
// Create the window using a smart pointer
|
||||
auto window = std::make_unique<RandomWalkerWindow>("Random Walker with ReWindow", WIDTH, HEIGHT);
|
||||
|
||||
// Set the rendering API to OpenGL
|
||||
window->SetRenderer(RenderingAPI::OPENGL);
|
||||
|
||||
// Open the window (this triggers OnOpen())
|
||||
window->Open();
|
||||
|
||||
// Main event loop: Refreshes the window until it is closed
|
||||
while (!window->IsClosing()) {
|
||||
window->ManagedRefresh();
|
||||
}
|
||||
|
Reference in New Issue
Block a user