diff --git a/Makefile b/Makefile index 6c07b90..94095e3 100644 --- a/Makefile +++ b/Makefile @@ -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/ diff --git a/includes/windowManager/WindowManager.hpp b/includes/windowManager/WindowManager.hpp index ab79756..7ce1d1c 100644 --- a/includes/windowManager/WindowManager.hpp +++ b/includes/windowManager/WindowManager.hpp @@ -3,6 +3,7 @@ #define NOISE_GENERATOR_WINDOWMANAGER_HPP #include +#include #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; diff --git a/srcs/main.cpp b/srcs/main.cpp index f2c4bcd..91d5311 100644 --- a/srcs/main.cpp +++ b/srcs/main.cpp @@ -1,4 +1,5 @@ +#include #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; } diff --git a/srcs/windowManager/WindowManager.cpp b/srcs/windowManager/WindowManager.cpp index 44bb210..5cb41a1 100644 --- a/srcs/windowManager/WindowManager.cpp +++ b/srcs/windowManager/WindowManager.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include 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(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); +}