From e82315a3eb18590aeba40f29f4892fc5e71a31c3 Mon Sep 17 00:00:00 2001 From: Pavel V Date: Thu, 29 Jun 2023 19:20:24 +0300 Subject: [PATCH] acidwarp works --- CMakeLists.txt | 12 +- acidwarp.cpp | 128 +++++++++++++++ acidwarp.h | 24 +++ acidwarp/acidwarp.c | 387 +------------------------------------------- acidwarp/acidwarp.h | 11 +- acidwarp/handy.h | 9 +- acidwarp/rolnfade.c | 6 +- artfactory.cpp | 2 + imgui_elements.cpp | 60 +++++++ main.cpp | 2 + 10 files changed, 237 insertions(+), 404 deletions(-) create mode 100644 acidwarp.cpp create mode 100644 acidwarp.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b778302..acbe075 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,7 +58,17 @@ add_library(clouds OBJECT vermiculate.cpp discrete.cpp thornbird.cpp - rdbomb.cpp) + rdbomb.cpp + acidwarp/acidwarp.c + acidwarp/lut.c + acidwarp/palinit.c + acidwarp/bit_map.c + acidwarp/rolnfade.c + acidwarp/warp_text.c + acidwarp.cpp + ) + +target_include_directories(clouds PRIVATE .) target_link_libraries(clouds IMGUI glfw GL colormap) #set_property(TARGET clouds PROPERTY CXX_STANDARD 17) diff --git a/acidwarp.cpp b/acidwarp.cpp new file mode 100644 index 0000000..f4a9b67 --- /dev/null +++ b/acidwarp.cpp @@ -0,0 +1,128 @@ +/* acidwarp by Noah Spurrier https://noah.org/acidwarp/ + * Dear ImGui port by Pavel Vasilyev , Jun 2023 + */ + +#include + +#include "random.h" +#include "acidwarp.h" + +#include "imgui_elements.h" + +extern "C" { +#include "acidwarp/handy.h" +#include "acidwarp/acidwarp.h" +#include "acidwarp/rolnfade.h" +#include "acidwarp/palinit.h" +} + +int RES = 3; +int XMax = 1023, YMax = 767; + +int GO = TRUE; +int SKIP = FALSE; +int NP = FALSE; /* flag indicates new palette */ +int LOCK = FALSE; /* flag indicates don't change to next image */ + +std::vector buf_graf; + +UCHAR MainPalArray [256 * 3]; +UCHAR TargetPalArray [256 * 3]; + + +#include + +uint32_t pal_rgb(int i) { + if (i > 256) + printf("%d ", i); + return 0xff000000 | + (MainPalArray[i*3+0]*4) << 0 | + (MainPalArray[i*3+1]*4) << 8 | + (MainPalArray[i*3+2]*4) << 16; +} + +bool AcidWarp::render(uint32_t *p) { + + ++frame; + + switch(acid_state) { + case IMAGE: + generate_image(image_func, buf_graf.data(), XMax/2, YMax/2, XMax, YMax, 255); + //pal_num = xoshiro256plus() % NUM_PALETTE_TYPES; + initPalArray(TargetPalArray, pal_num); + acid_state = FADE_IN; + //break; + case FADE_IN: + rolNFadeMainPalAryToTargNLodDAC(MainPalArray, TargetPalArray); + if (frame > frame_max) + frame = 0, acid_state = ROTATE; + break; + case ROTATE: + rollMainPalArrayAndLoadDACRegs(MainPalArray); + if (frame > frame_max) + frame = 0, acid_state = FADE_OUT; + break; + case FADE_OUT: + if (fade_dir) + rolNFadeBlkMainPalArrayNLoadDAC(MainPalArray); + else + rolNFadeWhtMainPalArrayNLoadDAC(MainPalArray); + if (frame > frame_max) + frame = 0, acid_state = IMAGE; + break; + } + + for (int x = 0; x < XMax; ++x) + for(int y = 0; y < YMax; ++y) + { + auto c = pal_rgb(buf_graf.at(x + y*XMax)); + //printf("%x ", c); + drawdot(p, x, y, c); + } + + return true; +} + +bool AcidWarp::render_gui() { + bool up = false, re = false; + + re |= ScrollableSliderInt("image func", &image_func, 0, NUM_IMAGE_FUNCTIONS+1, "%d", 1); + re |= ScrollableSliderInt("pal num", &pal_num, 0, NUM_PALETTE_TYPES, "%d", 1); + ScrollableSliderInt("frames each state", &frame_max, 0, 60*300, "%d", 60); + //up |= ScrollableSliderInt("resolution", &RES, 0, 3, "%d", 0); + ImGui::Checkbox("fade_dir", &fade_dir); + + if (re) { + frame = 0; + acid_state = IMAGE; + initPalArray(TargetPalArray, pal_num); + } + + pal.RenderGui(); + + ImGui::Text("state:%d frame:%d", acid_state, frame); + + + return up; +} + +void AcidWarp::resize(int _w, int _h) { + XMax = _w - 1; + YMax = _h - 1; + //tex_w = XMax+1; + //tex_h = YMax+1; + + initPalArray(MainPalArray, pal_num); + initPalArray(TargetPalArray, pal_num); + + buf_graf.resize(tex_w*tex_h); + fill0(buf_graf); + + //default_resize(width, height); + //pal.rescale(ncolors); + + + frame = 0; + acid_state = IMAGE; + +} diff --git a/acidwarp.h b/acidwarp.h new file mode 100644 index 0000000..45a8bc0 --- /dev/null +++ b/acidwarp.h @@ -0,0 +1,24 @@ +#include "art.hpp" +#include "imgui.h" + +#include "settings.hpp" + +class AcidWarp : public Art { +public: + AcidWarp() + : Art("AcidWarp") {} +private: + virtual bool render_gui() override; + virtual void resize(int _w, int _h) override; + virtual bool render(uint32_t *p) override; + + PaletteSetting pal; + + enum { IMAGE, FADE_IN, ROTATE, FADE_OUT } acid_state = IMAGE; + + int image_func = 0, pal_num = 0; + int frame = 0, frame_max = 60*10; + bool fade_dir = true; + +}; + diff --git a/acidwarp/acidwarp.c b/acidwarp/acidwarp.c index 9cc3a61..cc0b8ef 100644 --- a/acidwarp/acidwarp.c +++ b/acidwarp/acidwarp.c @@ -7,12 +7,9 @@ #include #include #include -#include -#include -#include #include -#include "warp_text.c" +//#include "warp_text.c" #include "handy.h" #include "acidwarp.h" #include "lut.h" @@ -20,392 +17,10 @@ #include "palinit.h" #include "rolnfade.h" -#define NUM_IMAGE_FUNCTIONS 40 #define NOAHS_FACE 0 /* there are WAY too many global things here... */ -extern int RedRollDirection, GrnRollDirection, BluRollDirection; -extern UINT FadeCompleteFlag; -int VGAMODE; -int VIRTUAL; -int RES = 0; -int ROTATION_DELAY = 30000; -GraphicsContext *physicalscreen; -int logo_time = 30, image_time = 20; -int XMax = 0, YMax = 0; -UCHAR *buf_graf; -int GO = TRUE; -int SKIP = FALSE; -int NP = FALSE; /* flag indicates new palette */ -int LOCK = FALSE; /* flag indicates don't change to next image */ -UCHAR MainPalArray [256 * 3]; -UCHAR TargetPalArray [256 * 3]; -void main (int argc, char *argv[]) -{ - int imageFuncList[NUM_IMAGE_FUNCTIONS], userOptionImageFuncNum; - int paletteTypeNum = 0, userPaletteTypeNumOptionFlag = FALSE; - int argNum, imageFuncListIndex=0, fade_dir = TRUE; - time_t ltime, mtime; - - RANDOMIZE(); - - /* Default options */ - userPaletteTypeNumOptionFlag = 0; /* User Palette option is OFF */ - userOptionImageFuncNum = -1; /* No particular functions goes first. */ - - commandline(argc, argv); - - printf ("\nPlease wait...\n" - "\n\n*** Press Control-C to exit the program at any time. ***\n"); - printf ("\n\n%s\n", VERSION); - - graphicsinit(); - - physicalscreen = gl_allocatecontext(); - gl_getcontext(physicalscreen); - - initPalArray(MainPalArray, RGBW_LIGHTNING_PAL); - initPalArray(TargetPalArray, RGBW_LIGHTNING_PAL); - gl_setpalettecolors(0, 256, MainPalArray); - - if (logo_time != 0) { - /* show the logo for a while */ - writeBitmapImageToArray(buf_graf, NOAHS_FACE, XMax, YMax); - gl_putbox(1,1,XMax,YMax,buf_graf); - ltime=time(NULL); - mtime=ltime + logo_time; - for(;;) { - processinput(); - if(GO) - rollMainPalArrayAndLoadDACRegs(MainPalArray); - if(SKIP) - break; - if(ltime>mtime) - break; - ltime=time(NULL); - usleep(ROTATION_DELAY); - } - while(!FadeCompleteFlag) { - processinput(); - if(GO) - rolNFadeBlkMainPalArrayNLoadDAC(MainPalArray); - if(SKIP) - break; - usleep(ROTATION_DELAY); - } - FadeCompleteFlag=!FadeCompleteFlag; - } - - SKIP = FALSE; - makeShuffledList(imageFuncList, NUM_IMAGE_FUNCTIONS); - - for(;;) { - /* move to the next image */ - if (++imageFuncListIndex >= NUM_IMAGE_FUNCTIONS) - { - imageFuncListIndex = 0; - makeShuffledList(imageFuncList, NUM_IMAGE_FUNCTIONS); - } - - /* install a new image */ - generate_image( - (userOptionImageFuncNum < 0) ? - imageFuncList[imageFuncListIndex] : - userOptionImageFuncNum, - buf_graf, XMax/2, YMax/2, XMax, YMax, 255); - gl_putbox(1,1,XMax,YMax,buf_graf); - - /* create new palette */ - paletteTypeNum = RANDOM(NUM_PALETTE_TYPES +1); - initPalArray(TargetPalArray, paletteTypeNum); - - /* this is the fade in */ - while(!FadeCompleteFlag) { - processinput(); - if(GO) - rolNFadeMainPalAryToTargNLodDAC(MainPalArray,TargetPalArray); - if(SKIP) - break; - usleep(ROTATION_DELAY); - gl_setpalettecolors(0, 256, MainPalArray); - } - - FadeCompleteFlag=!FadeCompleteFlag; - ltime = time(NULL); - mtime = ltime + image_time; - - /* rotate the palette for a while */ - for(;;) { - processinput(); - if(GO) - rollMainPalArrayAndLoadDACRegs(MainPalArray); - if(SKIP) - break; - if(NP) { - newpal(); - NP = FALSE; - } - ltime=time(NULL); - if((ltime>mtime) && !LOCK) - break; - usleep(ROTATION_DELAY); - } - - /* fade out */ - while(!FadeCompleteFlag) { - if(SKIP) - break; - processinput(); - if(GO) - if (fade_dir) - rolNFadeBlkMainPalArrayNLoadDAC(MainPalArray); - else - rolNFadeWhtMainPalArrayNLoadDAC(MainPalArray); - usleep(ROTATION_DELAY); - } - FadeCompleteFlag=!FadeCompleteFlag; - SKIP = FALSE; - } - /* exit */ - printStrArray(Command_summary_string); - printf("%s\n", VERSION); -} - -/* ------------------------END MAIN----------------------------------------- */ - -void newpal() -{ - int paletteTypeNum; - - paletteTypeNum = RANDOM(NUM_PALETTE_TYPES +1); - initPalArray(MainPalArray, paletteTypeNum); - gl_setpalettecolors(0, 256, TargetPalArray); -} - -int checkinput() -{ - keyboard_update(); - if(keyboard_keypressed(SCANCODE_P)) { - while(keyboard_keypressed(SCANCODE_P)) - keyboard_update(); - return 1; - } - if(keyboard_keypressed(SCANCODE_N)) { - while(keyboard_keypressed(SCANCODE_N)) - keyboard_update(); - return 2; - } - if(keyboard_keypressed(SCANCODE_Q)) { - while(keyboard_keypressed(SCANCODE_Q)) - keyboard_update(); - return 3; - } - if(keyboard_keypressed(SCANCODE_K)) { - while(keyboard_keypressed(SCANCODE_K)) - keyboard_update(); - return 4; - } - if(keyboard_keypressed(SCANCODE_L)) { - while(keyboard_keypressed(SCANCODE_L)) - keyboard_update(); - return 5; - } - if(keyboard_keypressed(SCANCODE_CURSORBLOCKUP)) { - while(keyboard_keypressed(SCANCODE_CURSORBLOCKUP)) - keyboard_update(); - return 6; - } - if(keyboard_keypressed(SCANCODE_CURSORBLOCKDOWN)) { - while(keyboard_keypressed(SCANCODE_CURSORBLOCKDOWN)) - keyboard_update(); - return 7; - } - - /* default case */ - return 0; -} - -void processinput() -{ - switch(checkinput()) - { - case 1: - if(GO) - GO = FALSE; - else - GO = TRUE; - break; - case 2: - SKIP = TRUE; - break; - case 3: - exit(0); - break; - case 4: - NP = TRUE; - break; - case 5: - if(LOCK) - LOCK = FALSE; - else - LOCK = TRUE; - break; - case 6: - ROTATION_DELAY = ROTATION_DELAY - 5000; - if (ROTATION_DELAY < 0) - ROTATION_DELAY = 0; - break; - case 7: - ROTATION_DELAY = ROTATION_DELAY + 5000; - break; - } -} - -void commandline(int argc, char *argv[]) -{ - int argNum; - - /* Parse the command line */ - if (argc >= 2) { - for (argNum = 1; argNum < argc; ++argNum) { - if (!strcmp("-w",argv[argNum])) { - printStrArray(The_warper_string); - exit (0); - } - if (!strcmp("-h",argv[argNum])) { - printStrArray(Help_string); - printf("\n%s\n", VERSION); - exit (0); - } - if(!strcmp("-r",argv[argNum])) { - if((argc-1) > argNum) { - argNum++; - RES=atoi(argv[argNum]); - if ((RES != 1) && (RES != 2) && (RES !=3)) - RES = 0; - } - } - if(!strcmp("-n",argv[argNum])) { - logo_time = 0; - } - if(!strcmp("-d",argv[argNum])) { - if((argc-1) > argNum) { - argNum++; - image_time = atoi(argv[argNum]); - } - } - if(!strcmp("-s", argv[argNum])) { - if((argc-1) > argNum) { - argNum++; - ROTATION_DELAY = atoi(argv[argNum]); - } - } - } - } -} - -void graphicsinit() -{ - /* setup the screen */ - switch (RES) - { - case 0: - XMax = 319; - YMax = 199; - buf_graf = alloca((XMax+1)*(YMax+1)); - memset(buf_graf, 0x00, (size_t)(320*200)); - break; - case 1: - XMax = 639; - YMax = 479; - buf_graf = alloca((XMax+1)*(YMax+1)); - memset(buf_graf, 0x00, (size_t)(640*480)); - break; - case 2: - XMax = 799; - YMax = 599; - buf_graf = alloca((XMax+1)*(YMax+1)); - memset(buf_graf,0x00, (size_t)(800*600)); - break; - case 3: - XMax = 1023; - YMax = 767; - buf_graf = alloca((XMax+1)*(YMax+1)); - memset(buf_graf,0x00, (size_t)(1024*768)); - break; - } - vga_init(); - switch (RES) - { - case 0: - VGAMODE=G320x200x256; - break; - case 1: - VGAMODE=G640x480x256; - break; - case 2: - VGAMODE=G800x600x256; - break; - case 3: - VGAMODE=G1024x768x256; - break; - } - VIRTUAL=0; - vga_setmode(VGAMODE); - - if (keyboard_init()) { - printf("Could not initialize keyboard.\n"); - exit(1); - } - gl_setcontextvga(VGAMODE); /* Physical screen context. */ -} - -void printStrArray(char *strArray[]) -{ - /* Prints an array of strings. The array is terminated with a null string. */ - char **strPtr; - - for (strPtr = strArray; **strPtr; ++strPtr) - printf ("%s", *strPtr); -} - -void setNewVideoMode (void) -{ - vga_setmode(G320x200x256); -} - -void restoreOldVideoMode (void) -{ - vga_setmode(TEXT); -} - -void writePixel(int x, int y, int color) -{ - int temp; - - /* temp = vga_getcolors(); */ - vga_setcolor(color); - vga_drawpixel(x,y); - /* vga_setcolor(temp); */ -} - -void makeShuffledList(int *list, int listSize) -{ - int entryNum, r; - - for (entryNum = 0; entryNum < listSize; ++entryNum) - list[entryNum] = -1; - - for (entryNum = 0; entryNum < listSize; ++entryNum) - { - do - r = RANDOM(listSize); - while (list[r] != -1); - - list[r] = entryNum; - } -} int generate_image(int imageFuncNum, UCHAR *buf_graf, int xcenter, int ycenter, int xmax, int ymax, int colormax) { diff --git a/acidwarp/acidwarp.h b/acidwarp/acidwarp.h index d985c45..6f4a0ae 100644 --- a/acidwarp/acidwarp.h +++ b/acidwarp/acidwarp.h @@ -16,18 +16,9 @@ #define GREEN 1 #define BLUE 2 #define NUM_PALETTE_TYPES 8 +#define NUM_IMAGE_FUNCTIONS 40 -void printStrArray(char *strArray[]); -void rotatePalette(void); -void rotateforward(int color, UCHAR *Pal); -void rotatebackward(int color, UCHAR *Pal); -void roll_rgb_palArray(UCHAR *Pal); -void makeShuffledList(int *list, int listSize); -void commandline(int argc, char *argv[]); -void graphicsinit(); int generate_image(int imageFuncNum, UCHAR *buf_graf, int xcenter, int ycenter, int xmax, int ymax, int colormax); -void processinput(); -void newpal(); diff --git a/acidwarp/handy.h b/acidwarp/handy.h index 4cf9f8f..8ceafbe 100644 --- a/acidwarp/handy.h +++ b/acidwarp/handy.h @@ -18,11 +18,14 @@ typedef enum {FALSE, TRUE} BOOL; #include #include /* Needed for NULL * */ #include -#define RANDOMIZE() (srand((UINT)time( (time_t *)NULL ))) -#define RANDOM(a) (rand()%(a)) +//#define RANDOMIZE() (srand((UINT)time( (time_t *)NULL ))) +//#define RANDOM(a) (rand()%(a)) +#include +uint64_t xoshiro256plus(void); +#define RANDOM(a) (xoshiro256plus()%(a)) /* Mini-benchmarking tools. Only one second accuracy */ -time_t __HANDY_BENCH; +//time_t __HANDY_BENCH; #define START() (__HANDY_BENCH = time (& __HANDY_BENCH)) #define MARK() ((long)time ((time_t *)0) - __HANDY_BENCH) diff --git a/acidwarp/rolnfade.c b/acidwarp/rolnfade.c index e688288..36c013a 100644 --- a/acidwarp/rolnfade.c +++ b/acidwarp/rolnfade.c @@ -5,8 +5,6 @@ #include "acidwarp.h" #include "rolnfade.h" #include "palinit.h" -#include -#include int RedRollDirection = 0, GrnRollDirection = 0, BluRollDirection = 0; UINT FadeCompleteFlag = 0; @@ -46,7 +44,7 @@ void rollMainPalArrayAndLoadDACRegs(UCHAR *MainPalArray) { maybeInvertSubPalRollDirection(); roll_rgb_palArray(MainPalArray); - gl_setpalettecolors(0, 255, MainPalArray); + //gl_setpalettecolors(0, 255, MainPalArray); } @@ -102,7 +100,7 @@ void rolNFadMainPalAry2RndTargNLdDAC(UCHAR *MainPalArray, UCHAR *TargetPalArray) maybeInvertSubPalRollDirection(); roll_rgb_palArray ( MainPalArray); roll_rgb_palArray (TargetPalArray); - gl_setpalettecolors(0, 256, MainPalArray); + //gl_setpalettecolors(0, 256, MainPalArray); } /**********************************************************************************/ diff --git a/artfactory.cpp b/artfactory.cpp index eb5f261..c4c55b6 100644 --- a/artfactory.cpp +++ b/artfactory.cpp @@ -8,6 +8,7 @@ #include "thornbird.h" #include "onepixel.h" #include "rdbomb.h" +#include "acidwarp.h" ArtFactory::ArtFactory() { @@ -19,6 +20,7 @@ ArtFactory::ArtFactory() { add_art("Thornbird"); add_art("OnePixel"); add_art("RDbomb"); + add_art("AcidWarp"); vc = VectorCombo("Art", art_items); vc.set_index(1); diff --git a/imgui_elements.cpp b/imgui_elements.cpp index 8b7f621..db400c7 100644 --- a/imgui_elements.cpp +++ b/imgui_elements.cpp @@ -185,3 +185,63 @@ void cpu_load_gui() { ImGui::Text(cpu_load_text()); } + +using namespace ImGui; + +// https://github.com/ocornut/imgui/issues/694#issuecomment-1004190040 + +static bool palettePanel(const char *title, const ImVec4* colors, int nColors, const ImVec2 &colorButtonSize, int *selectedPalIdx, int highlightPalIdx = -1, ImGuiWindowFlags flags = 0u) { + if (ImGui::Begin(title, nullptr, flags)) { + const ImVec2 &pos = ImGui::GetCursorScreenPos(); + bool colorHovered = false; + ImDrawList* drawList = ImGui::GetWindowDrawList(); + const ImDrawListFlags backupFlags = drawList->Flags; + drawList->Flags &= ~ImDrawListFlags_AntiAliasedLines; + + const ImU32 redColor = ImGui::GetColorU32(core::Color::Red); + const ImU32 yellowColor = ImGui::GetColorU32(core::Color::Yellow); + const ImU32 darkRedColor = ImGui::GetColorU32(core::Color::DarkRed); + + const float windowWidth = ImGui::GetWindowContentRegionMax().x; + ImVec2 globalCursorPos = ImGui::GetCursorScreenPos(); + for (int palIdx = 0; palIdx < nColors; ++palIdx) { + const float borderWidth = 1.0f; + const ImVec2 v1(globalCursorPos.x + borderWidth, globalCursorPos.y + borderWidth); + const ImVec2 v2(globalCursorPos.x + colorButtonSize.x, globalCursorPos.y + colorButtonSize.y); + + drawList->AddRectFilled(v1, v2, ImGui::GetColorU32(colors[palIdx])); + + const core::String &id = core::string::format("##palitem-%i", palIdx); + if (ImGui::InvisibleButton(id.c_str(), colorButtonSize)) { + *selectedPalIdx = palIdx; + } +#if 0 + const core::String &contextMenuId = core::string::format("Actions##context-palitem-%i", palIdx); + if (ImGui::BeginPopupContextItem(contextMenuId.c_str())) { + ImGui::EndPopup(); + } +#endif + if (!colorHovered && ImGui::IsItemHovered()) { + colorHovered = true; + drawList->AddRect(v1, v2, redColor); + } else if (palIdx == highlightPalIdx) { + drawList->AddRect(v1, v2, yellowColor); + } else if (palIdx == *selectedPalIdx) { + drawList->AddRect(v1, v2, darkRedColor); + } + globalCursorPos.x += colorButtonSize.x; + if (globalCursorPos.x > windowWidth - colorButtonSize.x) { + globalCursorPos.x = pos.x; + globalCursorPos.y += colorButtonSize.y; + } + ImGui::SetCursorScreenPos(globalCursorPos); + } + const ImVec2 cursorPos(pos.x, globalCursorPos.y + colorButtonSize.y); + ImGui::SetCursorScreenPos(cursorPos); + + // restore the draw list flags from above + drawList->Flags = backupFlags; + return true; + } + return false; +} diff --git a/main.cpp b/main.cpp index 4973de9..c9cf1c6 100644 --- a/main.cpp +++ b/main.cpp @@ -45,6 +45,8 @@ GLuint image_texture; GLuint pboIds[2]; int pbo_index = 0; +// GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER, GL_MIRRORED_REPEAT, GL_REPEAT, GL_MIRROR_CLAMP_TO_EDGE + void make_pbos() { image_data_vector.resize(texture_size); image_data = image_data_vector.data();