add: image manager

This commit is contained in:
loic 2024-12-13 15:23:55 +01:00
parent 18c4896be4
commit e0d23c9a82
4 changed files with 46 additions and 3 deletions

View file

@ -9,7 +9,7 @@ NAME = window
CC = c++
CFLAGS = -Wall -Wextra -Werror -g3
CPPFLAGS = -std=c++98 -MMD -MP
CPPFLAGS = -MMD -MP
BUILDDIR = build/
INCLUDEDIR = includes/
DEPENDENCIESDIR = $(BUILDDIR)dependencies/

View file

@ -3,6 +3,7 @@
#define NOISE_GENERATOR_WINDOWMANAGER_HPP
#include <X11/Xlib.h>
#include <mutex>
#include "err.h"
class WindowManager {
@ -10,8 +11,12 @@ public:
WindowManager(int width, int height);
~WindowManager();
u_int32_t *get_image_addr() { return img; }
void display_image();
void loop();
private:
u_int32_t *img;
int WindowX;
int WindowY;
int WindowWidth;

View file

@ -1,4 +1,5 @@
#include <csignal>
#include "windowManager/WindowManager.hpp"
#define WIDTH 500
@ -6,7 +7,16 @@
int main() {
WindowManager window(WIDTH, HEIGHT);
window.loop();
u_int32_t *img = window.get_image_addr();
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
img[y * WIDTH + x] = (y * 255 / HEIGHT) * 0x100 + (x * 255 / WIDTH);
}
}
window.loop();
return 0;
}

View file

@ -3,6 +3,8 @@
#include <X11/Xutil.h>
#include <iostream>
#include <X11/Xlib.h>
#include <csignal>
#include <mutex>
WindowManager::WindowManager(int width, int height) :
WindowX(0), WindowY(0),
@ -13,10 +15,11 @@ WindowManager::WindowManager(int width, int height) :
WindowVisual(CopyFromParent),
AttributeValueMask(CWBackPixel | CWEventMask)
{
img = new u_int32_t[width * height];
MainDisplay = XOpenDisplay(0);
RootWindow = XDefaultRootWindow(MainDisplay);
bzero(&WindowAttributes, sizeof(XSetWindowAttributes));
WindowAttributes.background_pixel = 0xffafe9af;
WindowAttributes.background_pixel = 0x0;
WindowAttributes.event_mask = StructureNotifyMask | KeyPressMask | KeyReleaseMask | ExposureMask;
MainWindow = XCreateWindow(MainDisplay, RootWindow,
@ -34,6 +37,7 @@ WindowManager::~WindowManager() {
XUnmapWindow(this->MainDisplay, this->MainWindow);
XDestroyWindow(this->MainDisplay, this->MainWindow);
XCloseDisplay(this->MainDisplay);
delete[] img;
}
void WindowManager::loop() {
@ -55,6 +59,30 @@ void WindowManager::loop() {
this->isWindowOpen = false;
}
} break;
case Expose:
{
std::cout << "test" << std::endl;
display_image();
} break;
}
}
}
void WindowManager::display_image() {
XImage image;
image.width = WindowWidth;
image.height = WindowHeight;
image.format = ZPixmap;
image.data = reinterpret_cast<char *>(img);
image.byte_order = LSBFirst;
image.bitmap_unit = 32;
image.byte_order = LSBFirst;
image.bitmap_pad = 32;
image.depth = 24;
image.bytes_per_line = this->WindowWidth * 4;
image.bits_per_pixel = 32;
XPutImage(MainDisplay, MainWindow, DefaultGC(MainDisplay, 0), &image, 0, 0, 0, 0, WindowWidth, WindowHeight);
XFlush(MainDisplay);
}