add: swap render function keybind

This commit is contained in:
loic 2024-12-14 00:47:43 +01:00
parent e4b900e721
commit e71702ae4c
11 changed files with 170 additions and 31 deletions

View file

@ -11,7 +11,7 @@ public:
private:
void initializeGradients();
Vector2 grad[256];
Vector2 grad[512];
Vector2 randoGradient(int ix, int iy);
float dotGridGradient(int ix, int iy, float x, float y);
float interpolate(float a, float a1, float w);

13
includes/window.hpp Normal file
View 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

View file

@ -5,22 +5,26 @@
#include <X11/Xlib.h>
#include <mutex>
#include <cstdint>
#include <vector>
#include "err.h"
class WindowManager {
public:
WindowManager(int width, int height, int (*)(u_int32_t *img));
WindowManager(int width, int height, int (*)(u_int32_t *img, bool &needUpdate));
~WindowManager();
u_int32_t *get_image_addr() { return img; }
void load_render(int (*)(u_int32_t *img, bool &needUpdate));
void loop();
private:
void update_image(int (*)(u_int32_t *img));
void update_image(int (*)(u_int32_t *img, bool &needUpdate));
void display_image();
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;
int WindowX;
int WindowY;
@ -38,6 +42,7 @@ private:
Atom wmDelete;
bool isWindowOpen;
bool isDisplayReady;
bool needUpdate;
};
#endif //NOISE_GENERATOR_WINDOWMANAGER_HPP

25
srcs/effects/cloud.cpp Normal file
View 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
View 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
View 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
View 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
View 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;
}

View file

@ -1,28 +1,16 @@
#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() {
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();
return 0;
}
}

View file

@ -11,10 +11,10 @@ PerlinNoise::PerlinNoise() {
}
void PerlinNoise::initializeGradients() {
std::mt19937 gen(0);
std::mt19937 gen(512);
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);
grad[i] = Vector2(cos(angle), sin(angle));
}

View file

@ -5,8 +5,9 @@
#include <X11/Xlib.h>
#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),
ptrTabIndex(0),
WindowX(0), WindowY(0),
WindowWidth(width), WindowHeight(height),
BorderWidth(0),
@ -14,7 +15,8 @@ WindowManager::WindowManager(int width, int height, int (*render)(u_int32_t *img
WindowClass(CopyFromParent),
WindowVisual(CopyFromParent),
AttributeValueMask(CWBackPixel | CWEventMask),
isDisplayReady(false)
isDisplayReady(false),
needUpdate(true)
{
img = new u_int32_t[width * height];
MainDisplay = XOpenDisplay(0);
@ -30,7 +32,7 @@ WindowManager::WindowManager(int width, int height, int (*render)(u_int32_t *img
XMapWindow(MainDisplay, MainWindow);
wmDelete = XInternAtom(MainDisplay, "WM_DELETE_WINDOW", false);
XSetWMProtocols(MainDisplay, MainWindow, &wmDelete, 1);
load_render(render);
isWindowOpen = true;
}
@ -44,11 +46,17 @@ WindowManager::~WindowManager() {
void WindowManager::handle_events(XEvent &GeneralEvent) {
switch(GeneralEvent.type) {
case KeyPress:
{
} break;
case KeyRelease:
{
XKeyPressedEvent *event = (XKeyPressedEvent *)&GeneralEvent;
if (event->keycode == XKeysymToKeycode(this->MainDisplay, XK_Escape)) {
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;
case ClientMessage: {
@ -76,6 +84,7 @@ void WindowManager::loop() {
if (isDisplayReady) {
update_image(this->render);
display_image();
XSync(this->MainDisplay, false);
}
}
}
@ -99,8 +108,11 @@ void WindowManager::display_image() {
XFlush(MainDisplay);
}
void WindowManager::update_image(int (*func)(u_int32_t *img)) {
if (func(this->img))
void WindowManager::update_image(int (*func)(u_int32_t *img, bool &needUpdate)) {
if (func(this->img, this->needUpdate))
this->isWindowOpen = false;
}
void WindowManager::load_render(int (*func)(u_int32_t *img, bool &needUpdate)) {
this->renderFunctions.push_back(func);
}