setup: resize window feature

This commit is contained in:
Loic Rio 2024-12-14 22:03:38 +01:00
parent fdc24916cd
commit c655a31bb8
10 changed files with 64 additions and 65 deletions

View file

@ -4,13 +4,13 @@
#define WIDTH 720 #define WIDTH 720
#define HEIGHT 420 #define HEIGHT 420
int graph(uint32_t *img); int graph(uint32_t *img, int width, int height);
int graph2(uint32_t *img); int graph2(uint32_t *img, int width, int height);
int mountain(uint32_t *img); int mountain(uint32_t *img, int width, int height);
int cloud(uint32_t *img); int cloud(uint32_t *img, int width, int height);
int marble(uint32_t *img); int marble(uint32_t *img, int width, int height);
int random(uint32_t *img); int random(uint32_t *img, int width, int height);
int wood(uint32_t *img); int wood(uint32_t *img, int width, int height);
int test(uint32_t *img); int test(uint32_t *img, int width, int height);
#endif #endif

View file

@ -6,24 +6,25 @@
#include <mutex> #include <mutex>
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
#include "err.h"
typedef int (*renderFunction)(u_int32_t *img, int width, int height);
class WindowManager { class WindowManager {
public: public:
WindowManager(int width, int height, int (*)(u_int32_t *img)); WindowManager(int width, int height, renderFunction r);
~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)); void load_render(renderFunction r);
void loop(); void loop();
private: private:
void update_image(int (*)(u_int32_t *img)); void update_image(renderFunction);
void display_image(); void display_image();
void handle_events(XEvent &GeneralEvent); void handle_events(XEvent &GeneralEvent);
int (*render)(u_int32_t *img); renderFunction render;
std::vector<int (*)(u_int32_t *img)> renderFunctions; std::vector<renderFunction> renderFunctions;
uint8_t ptrTabIndex; uint8_t ptrTabIndex;
u_int32_t *img; u_int32_t *img;
int WindowX; int WindowX;

View file

@ -3,16 +3,16 @@
#include "window.hpp" #include "window.hpp"
#include "noise/perlin.hpp" #include "noise/perlin.hpp"
int cloud(uint32_t *img) { int cloud(uint32_t *img, int width, int height) {
static PerlinNoise PerlinNoise; static PerlinNoise PerlinNoise;
static float time = 0.0; static float time = 0.0;
for (int y = 0; y < HEIGHT; y++) { for (int y = 0; y < height; y++) {
for (int x = 0; x < WIDTH; x++) { for (int x = 0; x < width; x++) {
float n = PerlinNoise.noise(x / 100.0 + time, (y / 100.0) + time) * 0.5; 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; n += PerlinNoise.noise(x / 50.0 + time, (y / 50.0) + time) * 0.25;
int color = static_cast<int>((n + 1) * 127.5); int color = static_cast<int>((n + 1) * 127.5);
img[y * WIDTH + x] = (color << 16) | (color << 8) | color ; img[y * width + x] = (color << 16) | (color << 8) | color ;
} }
} }
time += 0.1; time += 0.1;

View file

@ -3,16 +3,15 @@
#include "window.hpp" #include "window.hpp"
#include "noise/perlin.hpp" #include "noise/perlin.hpp"
int test(uint32_t *img) { int test(uint32_t *img, int width, int height) {
static bool a = true; static bool a = true;
for (int y = 0; y < HEIGHT; y++) { for (int y = 0; y < height; y++) {
for (int x = 0; x < WIDTH; x++) { for (int x = 0; x < width; x++) {
if (a) if (a)
img[y * WIDTH + x] = 0x202020; img[y * width + x] = 0x202020;
else else
img[y * WIDTH + x] = 0x0; img[y * width + x] = 0x0;
} }
} }
a = !a;
return 0; return 0;
} }

View file

@ -3,38 +3,38 @@
#include "window.hpp" #include "window.hpp"
#include "noise/perlin.hpp" #include "noise/perlin.hpp"
int graph(uint32_t *img) { int graph(uint32_t *img, int width, int height) {
static PerlinNoise PerlinNoise; static PerlinNoise PerlinNoise;
static float time = 0.0; static float time = 0.0;
for (int x = 0; x < WIDTH; x++) { for (int x = 0; x < width; x++) {
float n = PerlinNoise.noise(x / 100.0 + time, 0.0) * 0.5; float n = PerlinNoise.noise(x / 100.0 + time, 0.0) * 0.5;
int y = static_cast<int>((n + 1) * (HEIGHT >> 1)); int y = static_cast<int>((n + 1) * ((int)height >> 1));
for (int i = 0; i < HEIGHT; ++i) { for (int i = 0; i < height; ++i) {
if (i == y) if (i == y)
img[i * WIDTH + x] = 0xFFFFFF; img[i * width + x] = 0xFFFFFF;
else else
img[i * WIDTH + x] = 0; img[i * width + x] = 0;
} }
} }
time += 0.001; time += 0.001;
return 0; return 0;
} }
int graph2(uint32_t *img) { int graph2(uint32_t *img, int width, int height) {
static PerlinNoise PerlinNoise; static PerlinNoise PerlinNoise;
static float time = 0.0; static float time = 0.0;
for (int x = 0; x < WIDTH; x++) { for (int x = 0; x < width; x++) {
float n = PerlinNoise.noise(x / 100.0, time) * 0.5; float n = PerlinNoise.noise(x / 100.0, time) * 0.5;
int y = static_cast<int>((n + 1) * (HEIGHT >> 1)); int y = static_cast<int>((n + 1) * (height >> 1));
for (int i = 0; i < HEIGHT; ++i) { for (int i = 0; i < height; ++i) {
if (i - y < 40 && i - y > -40){ if (i - y < 40 && i - y > -40){
int co = static_cast<int>(255 * (40 - abs(i - y)) / 40); int co = static_cast<int>(255 * (40 - abs(i - y)) / 40);
img[i * WIDTH + x] = 0 << 16| (co << 8) | co; img[i * width + x] = 0 << 16| (co << 8) | co;
} }
else else
img[i * WIDTH + x] = 0; img[i * width + x] = 0;
} }
} }
time += 0.001; time += 0.001;

View file

@ -3,15 +3,15 @@
#include "window.hpp" #include "window.hpp"
#include "noise/perlin.hpp" #include "noise/perlin.hpp"
int marble(uint32_t *img) { int marble(uint32_t *img, int width, int height) {
static PerlinNoise perlinNoise; static PerlinNoise perlinNoise;
static float time = 0.0; static float time = 0.0;
for (int y = 0; y < HEIGHT; y++) { for (int y = 0; y < height; y++) {
for (int x = 0; x < WIDTH; x++) { for (int x = 0; x < width; x++) {
float n = perlinNoise.noise(x / 100.0 + time, y / 100.0 + time); float n = perlinNoise.noise(x / 100.0 + time, y / 100.0 + time);
float marble = sin((x * 0.05 + n * 10.0)); float marble = sin((x * 0.05 + n * 10.0));
int color = static_cast<int>((marble + 1) * 127.5); int color = static_cast<int>((marble + 1) * 127.5);
img[y * WIDTH + x] = (color << 16) | (color << 8) | color; img[y * width + x] = (color << 16) | (color << 8) | color;
} }
} }
time += 0.01; time += 0.01;

View file

@ -3,7 +3,7 @@
#include "window.hpp" #include "window.hpp"
#include "noise/perlin.hpp" #include "noise/perlin.hpp"
int mountain(uint32_t *img) { int mountain(uint32_t *img, int width, int height) {
static PerlinNoise P(9); static PerlinNoise P(9);
static PerlinNoise P2(1000); static PerlinNoise P2(1000);
static PerlinNoise P3(500); static PerlinNoise P3(500);
@ -11,24 +11,24 @@ int mountain(uint32_t *img) {
/*basic color sunsine gradient no noise*/ /*basic color sunsine gradient no noise*/
for (int y = 0; y < HEIGHT; y++) { for (int y = 0; y < height; y++) {
for (int x = 0; x < WIDTH; x++) { for (int x = 0; x < width; x++) {
float n = P2.noise(x / 10 + (time * 0.5), y / 100.0 + (time * 0.5)) * 0.5 + 0.5; float n = P2.noise(x / 10 + (time * 0.5), y / 100.0 + (time * 0.5)) * 0.5 + 0.5;
int color = static_cast<int>(n * 255); int color = static_cast<int>(n * 255);
img[y * WIDTH + x] = (0 << 16) | (color << 8) | color; img[y * width + x] = (0 << 16) | (color << 8) | color;
} }
} }
/*mountain*/ /*mountain*/
for (int x = 0; x < WIDTH; x++) { for (int x = 0; x < width; x++) {
float n = P.noise(x / 100.0 + time, 0) * 0.5; float n = P.noise(x / 100.0 + time, 0) * 0.5;
float n2 = P2.noise(x / 1000.0 , 0) + P2.noise(x / 40.0 + (time * 0.5), 0) * 0.5; float n2 = P2.noise(x / 1000.0 , 0) + P2.noise(x / 40.0 + (time * 0.5), 0) * 0.5;
int y = static_cast<int>((n + 1) * (HEIGHT >> 1)); int y = static_cast<int>((n + 1) * (height >> 1));
int y2 = static_cast<int>((n2 + 1) * (HEIGHT >> 1) ); int y2 = static_cast<int>((n2 + 1) * (height >> 1) );
for (int i = y2; i < HEIGHT; ++i) { for (int i = y2; i < height; ++i) {
img[i * WIDTH + x] = 0xf0f0f0; img[i * width + x] = 0xf0f0f0;
} }
for (int i = y; i < HEIGHT; ++i) { for (int i = y; i < height; ++i) {
img[i * WIDTH + x] = 0x151515; img[i * width + x] = 0x151515;
} }
} }
time += 0.03; time += 0.03;

View file

@ -2,15 +2,14 @@
#include "window.hpp" #include "window.hpp"
#include "noise/perlin.hpp" #include "noise/perlin.hpp"
int random(uint32_t *img) { int random(uint32_t *img, int width, int height) {
static PerlinNoise PerlinNoise; static PerlinNoise PerlinNoise;
static float time = 0.0; static float time = 0.0;
for (int y = 0; y < HEIGHT; y++) { for (int y = 0; y < height; y++) {
for (int x = 0; x < WIDTH; x++) { for (int x = 0; x < width; x++) {
float r = (PerlinNoise.noise(x * 0.0001 + time, (y * 00.001) + time)) * 0.5; float r = (PerlinNoise.noise(x * 0.0001 + time, (y * 00.001) + time)) * 0.5;
int color = static_cast<int>((r + 1) * 127.5); int color = static_cast<int>((r + 1) * 127.5);
img[y * WIDTH + x] = (color << 16) >> 1| (color << 8) | 0; img[y * width + x] = (color << 16) >> 1| (color << 8) | 0;
} }
} }
time += 0.01; time += 0.01;

View file

@ -3,15 +3,15 @@
#include "window.hpp" #include "window.hpp"
#include "noise/perlin.hpp" #include "noise/perlin.hpp"
int wood(uint32_t *img) { int wood(uint32_t *img, int width, int height) {
static PerlinNoise perlinNoise; static PerlinNoise perlinNoise;
static float time = 0.0; static float time = 0.0;
for (int y = 0; y < HEIGHT; y++) { for (int y = 0; y < height; y++) {
for (int x = 0; x < WIDTH; x++) { for (int x = 0; x < width; x++) {
float n = perlinNoise.noise(x / 100.0 + time, y / 100.0 + time); float n = perlinNoise.noise(x / 100.0 + time, y / 100.0 + time);
float wood = sin((x * 0.1 + n * 5.0)); float wood = sin((x * 0.1 + n * 5.0));
int color = static_cast<int>((wood + 1) * 127.5); int color = static_cast<int>((wood + 1) * 127.5);
img[y * WIDTH + x] = (color << 16) | (color << 8) | color; img[y * width + x] = (color << 16) | (color << 8) | color;
} }
} }
time += 0.01; time += 0.01;

View file

@ -5,7 +5,7 @@
#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, renderFunction render) :
render(render), render(render),
ptrTabIndex(0), ptrTabIndex(0),
WindowX(0), WindowY(0), WindowX(0), WindowY(0),
@ -108,11 +108,11 @@ void WindowManager::display_image() {
XFlush(MainDisplay); XFlush(MainDisplay);
} }
void WindowManager::update_image(int (*func)(u_int32_t *img)) { void WindowManager::update_image(renderFunction r) {
if (func(this->img)) if (r(img, WindowWidth, WindowHeight) != 0)
this->isWindowOpen = false; this->isWindowOpen = false;
} }
void WindowManager::load_render(int (*func)(u_int32_t *img)) { void WindowManager::load_render(renderFunction r) {
this->renderFunctions.push_back(func); this->renderFunctions.push_back(r);
} }