add: swap render function keybind
This commit is contained in:
parent
e4b900e721
commit
e71702ae4c
11 changed files with 170 additions and 31 deletions
|
|
@ -11,7 +11,7 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void initializeGradients();
|
void initializeGradients();
|
||||||
Vector2 grad[256];
|
Vector2 grad[512];
|
||||||
Vector2 randoGradient(int ix, int iy);
|
Vector2 randoGradient(int ix, int iy);
|
||||||
float dotGridGradient(int ix, int iy, float x, float y);
|
float dotGridGradient(int ix, int iy, float x, float y);
|
||||||
float interpolate(float a, float a1, float w);
|
float interpolate(float a, float a1, float w);
|
||||||
|
|
|
||||||
13
includes/window.hpp
Normal file
13
includes/window.hpp
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
#ifndef WINDOW_HPP
|
||||||
|
#define WINDOW_HPP
|
||||||
|
|
||||||
|
#define WIDTH 720
|
||||||
|
#define HEIGHT 420
|
||||||
|
|
||||||
|
int graph(uint32_t *img, bool &needUpdate);
|
||||||
|
int cloud(uint32_t *img, bool &needUpdate);
|
||||||
|
int marble(uint32_t *img, bool &needUpdate);
|
||||||
|
int random(uint32_t *img, bool &needUpdate);
|
||||||
|
int wood(uint32_t *img, bool &needUpdate);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -5,22 +5,26 @@
|
||||||
#include <X11/Xlib.h>
|
#include <X11/Xlib.h>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <vector>
|
||||||
#include "err.h"
|
#include "err.h"
|
||||||
|
|
||||||
class WindowManager {
|
class WindowManager {
|
||||||
public:
|
public:
|
||||||
WindowManager(int width, int height, int (*)(u_int32_t *img));
|
WindowManager(int width, int height, int (*)(u_int32_t *img, bool &needUpdate));
|
||||||
~WindowManager();
|
~WindowManager();
|
||||||
|
|
||||||
u_int32_t *get_image_addr() { return img; }
|
u_int32_t *get_image_addr() { return img; }
|
||||||
|
|
||||||
|
void load_render(int (*)(u_int32_t *img, bool &needUpdate));
|
||||||
void loop();
|
void loop();
|
||||||
private:
|
private:
|
||||||
void update_image(int (*)(u_int32_t *img));
|
void update_image(int (*)(u_int32_t *img, bool &needUpdate));
|
||||||
void display_image();
|
void display_image();
|
||||||
void handle_events(XEvent &GeneralEvent);
|
void handle_events(XEvent &GeneralEvent);
|
||||||
|
|
||||||
int (*render)(u_int32_t *img);
|
int (*render)(u_int32_t *img, bool &needUpdate);
|
||||||
|
std::vector<int (*)(u_int32_t *img, bool &needUpdate)> renderFunctions;
|
||||||
|
uint8_t ptrTabIndex;
|
||||||
u_int32_t *img;
|
u_int32_t *img;
|
||||||
int WindowX;
|
int WindowX;
|
||||||
int WindowY;
|
int WindowY;
|
||||||
|
|
@ -38,6 +42,7 @@ private:
|
||||||
Atom wmDelete;
|
Atom wmDelete;
|
||||||
bool isWindowOpen;
|
bool isWindowOpen;
|
||||||
bool isDisplayReady;
|
bool isDisplayReady;
|
||||||
|
bool needUpdate;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //NOISE_GENERATOR_WINDOWMANAGER_HPP
|
#endif //NOISE_GENERATOR_WINDOWMANAGER_HPP
|
||||||
|
|
|
||||||
25
srcs/effects/cloud.cpp
Normal file
25
srcs/effects/cloud.cpp
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include "window.hpp"
|
||||||
|
#include "noise/perlin.hpp"
|
||||||
|
|
||||||
|
int cloud(uint32_t *img, bool &needUpdate) {
|
||||||
|
if (!needUpdate)
|
||||||
|
return 0;
|
||||||
|
needUpdate = false;
|
||||||
|
static PerlinNoise PerlinNoise;
|
||||||
|
static float time = 0.0;
|
||||||
|
for (int y = 0; y < HEIGHT; y++) {
|
||||||
|
for (int x = 0; x < WIDTH; x++) {
|
||||||
|
float n = PerlinNoise.noise(x / 100.0 + time, (y / 100.0) + time) * 0.5;
|
||||||
|
n += PerlinNoise.noise(x / 50.0 + time, (y / 50.0) + time) * 0.25;
|
||||||
|
|
||||||
|
int color = static_cast<int>((n + 1) * 127.5);
|
||||||
|
img[y * WIDTH + x] = (color << 16) | (color << 8) | color ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
time += 0.1;
|
||||||
|
needUpdate = true;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
26
srcs/effects/graph.cpp
Normal file
26
srcs/effects/graph.cpp
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
#include <cstdint>
|
||||||
|
#include "window.hpp"
|
||||||
|
#include "noise/perlin.hpp"
|
||||||
|
|
||||||
|
int graph(uint32_t *img, bool &needUpdate) {
|
||||||
|
if (!needUpdate)
|
||||||
|
return 0;
|
||||||
|
needUpdate = false;
|
||||||
|
static PerlinNoise PerlinNoise;
|
||||||
|
static float time = 0.0;
|
||||||
|
for (int x = 0; x < WIDTH; x++) {
|
||||||
|
float n = PerlinNoise.noise(x / 100.0 + time, 0.0) * 0.5;
|
||||||
|
|
||||||
|
int y = static_cast<int>((n + 1) * (HEIGHT >> 1));
|
||||||
|
for (int i = 0; i < HEIGHT; ++i) {
|
||||||
|
if (i == y)
|
||||||
|
img[i * WIDTH + x] = 0xFFFFFF;
|
||||||
|
else
|
||||||
|
img[i * WIDTH + x] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
time += 0.001;
|
||||||
|
needUpdate = true;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
24
srcs/effects/marble.cpp
Normal file
24
srcs/effects/marble.cpp
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstdint>
|
||||||
|
#include "window.hpp"
|
||||||
|
#include "noise/perlin.hpp"
|
||||||
|
|
||||||
|
int marble(uint32_t *img, bool &needUpdate) {
|
||||||
|
if (!needUpdate)
|
||||||
|
return 0;
|
||||||
|
needUpdate = false;
|
||||||
|
static PerlinNoise perlinNoise;
|
||||||
|
static float time = 0.0;
|
||||||
|
for (int y = 0; y < HEIGHT; y++) {
|
||||||
|
for (int x = 0; x < WIDTH; x++) {
|
||||||
|
float n = perlinNoise.noise(x / 100.0 + time, y / 100.0 + time);
|
||||||
|
float marble = sin((x * 0.05 + n * 10.0));
|
||||||
|
int color = static_cast<int>((marble + 1) * 127.5);
|
||||||
|
img[y * WIDTH + x] = (color << 16) | (color << 8) | color;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
time += 0.01;
|
||||||
|
needUpdate = true;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
23
srcs/effects/random.cpp
Normal file
23
srcs/effects/random.cpp
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
#include <cstdint>
|
||||||
|
#include "window.hpp"
|
||||||
|
#include "noise/perlin.hpp"
|
||||||
|
|
||||||
|
int random(uint32_t *img, bool &needUpdate) {
|
||||||
|
if (!needUpdate)
|
||||||
|
return 0;
|
||||||
|
needUpdate = false;
|
||||||
|
static PerlinNoise PerlinNoise;
|
||||||
|
static float time = 0.0;
|
||||||
|
for (int y = 0; y < HEIGHT; y++) {
|
||||||
|
for (int x = 0; x < WIDTH; x++) {
|
||||||
|
float r = (PerlinNoise.noise(x * 0.0001 + time, (y * 00.001) + time)) * 0.5;
|
||||||
|
|
||||||
|
int color = static_cast<int>((r + 1) * 127.5);
|
||||||
|
img[y * WIDTH + x] = (color << 16) >> 1| (color << 8) | 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
time += 0.01;
|
||||||
|
needUpdate = true;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
23
srcs/effects/wood.cpp
Normal file
23
srcs/effects/wood.cpp
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstdint>
|
||||||
|
#include "window.hpp"
|
||||||
|
#include "noise/perlin.hpp"
|
||||||
|
|
||||||
|
int wood(uint32_t *img, bool &needUpdate) {
|
||||||
|
if (!needUpdate)
|
||||||
|
return 0;
|
||||||
|
needUpdate = false;
|
||||||
|
static PerlinNoise perlinNoise;
|
||||||
|
static float time = 0.0;
|
||||||
|
for (int y = 0; y < HEIGHT; y++) {
|
||||||
|
for (int x = 0; x < WIDTH; x++) {
|
||||||
|
float n = perlinNoise.noise(x / 100.0 + time, y / 100.0 + time);
|
||||||
|
float wood = sin((x * 0.1 + n * 5.0));
|
||||||
|
int color = static_cast<int>((wood + 1) * 127.5);
|
||||||
|
img[y * WIDTH + x] = (color << 16) | (color << 8) | color;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
time += 0.01;
|
||||||
|
needUpdate = true;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
@ -1,27 +1,15 @@
|
||||||
|
|
||||||
#include "windowManager/WindowManager.hpp"
|
#include "windowManager/WindowManager.hpp"
|
||||||
#include "noise/perlin.hpp"
|
#include "window.hpp"
|
||||||
|
|
||||||
#define WIDTH 720
|
|
||||||
#define HEIGHT 420
|
|
||||||
|
|
||||||
int render(uint32_t *img) {
|
|
||||||
static PerlinNoise PerlinNoise;
|
|
||||||
static float time = 0.0;
|
|
||||||
for (int y = 0; y < HEIGHT; y++) {
|
|
||||||
for (int x = 0; x < WIDTH; x++) {
|
|
||||||
float n = (PerlinNoise.noise(x / 100.0 + time, (y / 100.0) + time));
|
|
||||||
|
|
||||||
int color = static_cast<int>((n + 1) * 127.5);
|
|
||||||
img[y * WIDTH + x] = (color << 16) | (color << 8) | color ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
time += 0.1;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
WindowManager window(WIDTH, HEIGHT, render);
|
WindowManager window(WIDTH, HEIGHT, graph);
|
||||||
|
window.load_render(cloud);
|
||||||
|
window.load_render(marble);
|
||||||
|
window.load_render(random);
|
||||||
|
window.load_render(wood);
|
||||||
|
|
||||||
|
|
||||||
window.loop();
|
window.loop();
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
|
|
@ -11,10 +11,10 @@ PerlinNoise::PerlinNoise() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void PerlinNoise::initializeGradients() {
|
void PerlinNoise::initializeGradients() {
|
||||||
std::mt19937 gen(0);
|
std::mt19937 gen(512);
|
||||||
std::uniform_real_distribution<float> dis(0.0, 2 * M_PI);
|
std::uniform_real_distribution<float> dis(0.0, 2 * M_PI);
|
||||||
|
|
||||||
for (int i = 0; i < 256; ++i) {
|
for (int i = 0; i < 512; ++i) {
|
||||||
float angle = dis(gen);
|
float angle = dis(gen);
|
||||||
grad[i] = Vector2(cos(angle), sin(angle));
|
grad[i] = Vector2(cos(angle), sin(angle));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,9 @@
|
||||||
#include <X11/Xlib.h>
|
#include <X11/Xlib.h>
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
|
|
||||||
WindowManager::WindowManager(int width, int height, int (*render)(u_int32_t *img)) :
|
WindowManager::WindowManager(int width, int height, int (*render)(u_int32_t *img, bool &needUpdate)) :
|
||||||
render(render),
|
render(render),
|
||||||
|
ptrTabIndex(0),
|
||||||
WindowX(0), WindowY(0),
|
WindowX(0), WindowY(0),
|
||||||
WindowWidth(width), WindowHeight(height),
|
WindowWidth(width), WindowHeight(height),
|
||||||
BorderWidth(0),
|
BorderWidth(0),
|
||||||
|
|
@ -14,7 +15,8 @@ WindowManager::WindowManager(int width, int height, int (*render)(u_int32_t *img
|
||||||
WindowClass(CopyFromParent),
|
WindowClass(CopyFromParent),
|
||||||
WindowVisual(CopyFromParent),
|
WindowVisual(CopyFromParent),
|
||||||
AttributeValueMask(CWBackPixel | CWEventMask),
|
AttributeValueMask(CWBackPixel | CWEventMask),
|
||||||
isDisplayReady(false)
|
isDisplayReady(false),
|
||||||
|
needUpdate(true)
|
||||||
{
|
{
|
||||||
img = new u_int32_t[width * height];
|
img = new u_int32_t[width * height];
|
||||||
MainDisplay = XOpenDisplay(0);
|
MainDisplay = XOpenDisplay(0);
|
||||||
|
|
@ -30,7 +32,7 @@ WindowManager::WindowManager(int width, int height, int (*render)(u_int32_t *img
|
||||||
XMapWindow(MainDisplay, MainWindow);
|
XMapWindow(MainDisplay, MainWindow);
|
||||||
wmDelete = XInternAtom(MainDisplay, "WM_DELETE_WINDOW", false);
|
wmDelete = XInternAtom(MainDisplay, "WM_DELETE_WINDOW", false);
|
||||||
XSetWMProtocols(MainDisplay, MainWindow, &wmDelete, 1);
|
XSetWMProtocols(MainDisplay, MainWindow, &wmDelete, 1);
|
||||||
|
load_render(render);
|
||||||
isWindowOpen = true;
|
isWindowOpen = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -44,11 +46,17 @@ WindowManager::~WindowManager() {
|
||||||
void WindowManager::handle_events(XEvent &GeneralEvent) {
|
void WindowManager::handle_events(XEvent &GeneralEvent) {
|
||||||
switch(GeneralEvent.type) {
|
switch(GeneralEvent.type) {
|
||||||
case KeyPress:
|
case KeyPress:
|
||||||
|
{
|
||||||
|
|
||||||
|
} break;
|
||||||
case KeyRelease:
|
case KeyRelease:
|
||||||
{
|
{
|
||||||
XKeyPressedEvent *event = (XKeyPressedEvent *)&GeneralEvent;
|
XKeyPressedEvent *event = (XKeyPressedEvent *)&GeneralEvent;
|
||||||
if (event->keycode == XKeysymToKeycode(this->MainDisplay, XK_Escape)) {
|
if (event->keycode == XKeysymToKeycode(this->MainDisplay, XK_Escape)) {
|
||||||
this->isWindowOpen = false;
|
this->isWindowOpen = false;
|
||||||
|
} else if (event->keycode == XKeysymToKeycode(this->MainDisplay, XK_space)) {
|
||||||
|
this->ptrTabIndex = (this->ptrTabIndex + 1) % this->renderFunctions.size();
|
||||||
|
this->render = this->renderFunctions[this->ptrTabIndex];
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
case ClientMessage: {
|
case ClientMessage: {
|
||||||
|
|
@ -76,6 +84,7 @@ void WindowManager::loop() {
|
||||||
if (isDisplayReady) {
|
if (isDisplayReady) {
|
||||||
update_image(this->render);
|
update_image(this->render);
|
||||||
display_image();
|
display_image();
|
||||||
|
XSync(this->MainDisplay, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -99,8 +108,11 @@ void WindowManager::display_image() {
|
||||||
XFlush(MainDisplay);
|
XFlush(MainDisplay);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowManager::update_image(int (*func)(u_int32_t *img)) {
|
void WindowManager::update_image(int (*func)(u_int32_t *img, bool &needUpdate)) {
|
||||||
if (func(this->img))
|
if (func(this->img, this->needUpdate))
|
||||||
this->isWindowOpen = false;
|
this->isWindowOpen = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowManager::load_render(int (*func)(u_int32_t *img, bool &needUpdate)) {
|
||||||
|
this->renderFunctions.push_back(func);
|
||||||
}
|
}
|
||||||
Loading…
Add table
Reference in a new issue