Jump to content
Tuts 4 You

[Any Language] GDI fun thread


simple

Recommended Posts

Get some ides from the spirit of this thread... - http://codegolf.stackexchange.com/questions/35569/tweetable-mathematical-art?page=1&tab=votes#tab-top... & make some cool GDI animations - demo style !Here's a code skeleton, core algo is the same as KyleMcCormick's - except for GDI animationsThis code will work on all Win C or C++ compilers - Goal is to edit PixelWrite() - the R, G & B values to make original, unique animationsedit a few lines & post ur graphics : )
 

RULES:- Post a binary - Post the source for your PixelWrite() function - No rules really, just try to make small code do big things & keep .exe's small - Post in whatever language u want- Using this skeleton is not required, I just thought it'd make it easiercredits: More than I can remember, thx to anyone who's ever posted a GDI code, Fudo, Koni, etc
#include <windows.h>#include <cmath>#define _sq(x) ((x)*(x))                           // square#define _cb(x) abs((x)*(x)*(x))                    // absolute value of cube#define _cr(x) (unsigned short)(pow((x),1.0/3.0))  // cube root#define Width  300#define Height 300HBITMAP BitmapDraw;HANDLE BackgroundThread;HWND hwnd;HDC hdcMem;float Camera = 0;struct RGB  {    union      {        struct          {            unsigned char r, g, b;        };        DWORD Padding;    };};void PixelWrite(RGB *pixels);DWORD WINAPI DrawBackground(HANDLE handle);  LRESULT CALLBACK WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);RGB *Buffer;int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){      WNDCLASSEX Wincl;      MSG Msg;      Wincl.cbClsExtra = 0;      Wincl.cbWndExtra = 0;      Wincl.cbSize = sizeof( WNDCLASSEX );      Wincl.hbrBackground = CreateSolidBrush( 0 );      Wincl.hCursor = LoadCursor( NULL, IDC_ARROW );      Wincl.hIcon = LoadIcon( NULL, IDI_APPLICATION );      Wincl.hIconSm = LoadIcon( NULL, IDI_APPLICATION );      Wincl.hInstance = hThisInstance;      Wincl.lpfnWndProc = WndProc;      Wincl.lpszClassName = "GDI";      Wincl.lpszMenuName = NULL;      Wincl.style = 0;      if (!RegisterClassEx(&Wincl))            return 0;      hwnd = CreateWindowEx (WS_EX_APPWINDOW, "GDI", "GDI Big Fun Thread", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, Width, Height, HWND_DESKTOP, NULL, hThisInstance, NULL );      while (GetMessage(&Msg, 0, 0, NULL) > 0)        {          TranslateMessage(&Msg);          DispatchMessage (&Msg);      }      return 0;}void CreateDIB(HWND hWnd)  {             BITMAPINFO bmi = { { sizeof(BITMAPINFOHEADER), Width, -Height, 1, 32, BI_RGB, 0, 0, 0, 0, 0 } };      HDC hdc = GetDC (hWnd);      BitmapDraw = CreateDIBSection (hdc, &bmi, DIB_RGB_COLORS, (void**)&Buffer, NULL, 0);      DeleteDC (hdc);      HANDLE BackgroundThread = CreateThread (NULL, NULL, &DrawBackground, NULL, NULL, NULL);}LRESULT CALLBACK WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam){      switch (Msg)        {      case WM_CREATE || WM_SHOWWINDOW:            CreateDIB (hWnd);            break;      case WM_PAINT:      {            PAINTSTRUCT Paint;            HDC hdc = BeginPaint (hWnd, &Paint);            BitBlt (hdc, 0, 0, Width, Height, hdcMem, 0, 0, SRCCOPY);            EndPaint (hWnd, &Paint);      }      break;      case WM_CLOSE:      {                DestroyWindow (hWnd);      }      break;      case WM_DESTROY:          TerminateThread (BackgroundThread, 0);          PostQuitMessage (0);      break;      default:          return DefWindowProc( hWnd, Msg, wParam, lParam );      }      return 0;}DWORD WINAPI DrawBackground(HANDLE handle)  {            ShowWindow (hwnd, SW_SHOW);      HDC hdc = GetDC (hwnd);      hdcMem = CreateCompatibleDC (hdc);      HBITMAP hbmOld = (HBITMAP)SelectObject( hdcMem, BitmapDraw );      while (1)        {          PixelWrite (Buffer);          BitBlt (hdc, 0, 0, Width, Height, hdcMem, 0, 0, SRCCOPY);          Sleep (20);      }      SelectObject (hdcMem, hbmOld);      DeleteDC (hdc);}void PixelWrite(RGB *pixels)  {      // this is the only function that needs to be editted to change graphics      // anything goes but u should be able to do everything u need in those 3 RGB lines below      RGB *Rgb;             for (int x = 0; x < Width; x++)        {              for (int y = 0; y < Height; y++)            {              Rgb = &pixels[y * Width + x];              Rgb->r = // YOUR CODE HERE              Rgb->g = // YOUR CODE HERE              Rgb->b = // YOUR CODE HERE          }      }      Camera += 0.009f;} 

 

  • Like 4
Link to comment

My first animation - Black vs. White - while black wants to fill the empty w/color, white wants everything to be empty...



void PixelWrite(RGB *pixels)
{
RGB *Rgb;
for (int x = 0; x < Width; x++)
{
for (int y = 0; y < Height; y++)
{
Rgb = &pixels[y * Width + x]; Rgb->r = (unsigned char)(((tan(_cr(float(x) / float(Width)) + Camera) / tan(_sq((float)y / (float)Height) + Camera) * cos(Camera * (float)y / (float)Height))* y + y));
Rgb->g = Rgb->r++;
Rgb->b = Rgb->g++;
}
}
Camera += 0.009f;
}

blackvswhite.rar

Link to comment

Great work thx for sharing! It's insane the art that math can create!
 
Dropping a camera into a wormhole:
 
34t3gi1.jpg
 
Endless tablecloth:
 
egu0pv.jpg

 

 

 

 // endless tablecloth - to speed/slow add/sub Camera void PixelWrite(RGB *pixels){      RGB *Rgb;            for (int x = 0; x < Width; x++)      {              for (int y = 0; y < Height; y++)          {                float s=3.0/(x+99);                Rgb = &pixels[y * Width + x];                Rgb->r = (int((y+Height * 2)*s+x*s + sin(Camera/ 10)) % 2 + int((Height*3-y)*s+x*s + Camera / 2 )%2)*127;                Rgb->b = 0x438;                Rgb->g = _cr(Rgb->b++);          }      }      Camera += 0.09f;} // wormhole unsigned short RD(int i,int j, float Camera){    double r = i * (sin((Camera))) /256.3-2,s=j/156.-2;    double q = _sq(r) + _sq(s) * (sin(Camera / 2)),n=hypot(r+(.266-r/4)/q * Camera,s+(r*.866+s/2)/q);    double d = .75 / log (n * 1.5); // end of tunnel    if(d<0 || d>1)        d=1;    return d*(log(n*10) + (Camera) *511+512);}unsigned short BL(int i,int j, float Camera){    double r = i / 256.6-2, s = i / 256.-2 , q = s * tan(r)/*+s*s*/; // tan(r) gives cool effect    return RD(i,j, Camera)/**sqrt(q/40)*/;}void PixelWrite(RGB *pixels){      RGB *Rgb;            for (int x = 0; x < Width; x++)      {              for (int y = 0; y < Height; y++)          {                float s=3.0/(x+99);              Rgb = &pixels[y * Width + x];              Rgb->r = RD(y, x, Camera);              Rgb->b = 0;              Rgb->g = BL(y, x, Camera);          }      }      Camera += .05f;}   

 

 

Wormhole.rar

tablecloth.rar

Link to comment

Looks pretty awesome, people in the old days used to be able to simulate 3D effects just by using software, no 3D acceleration by any means. Must be really boring trying calibrate and figure out what values give the closest perspective effect, but they were able to do it still. ( ex: hxxps://www.youtube.com/watch?v=iIqeCyAzcb4 ). Also in the original code you seem to use a new thread to perform the animation, is there really any relevant difference between that and seting a timer every 33 ms?. (for 30 FPS).


Edited by xSRTsect
Link to comment

Looks pretty awesome, people in the old days used to be able to simulate 3D effects just by using software, no 3D acceleration by any means. Must be really boring trying calibrate and figure out what values give the closest perspective effect, but they were able to do it still. ( ex: hxxps://www.youtube.com/watch?v=iIqeCyAzcb4 ). Also in the original code you seem to use a new thread to perform the animation, is there really any relevant difference between that and seting a timer every 33 ms?. (for 30 FPS).

 

Maybe I'm wrong but I don't think any entries here use 3d acceleration libs, my wormhole has 3d effects. Briefly looking at old school amiga asm tuts I think the core RGB concepts are the same as GDI

 

I also like to keep low fps in Sleep() because in my skeleton there's a Camera variable, which is like the location of the pixels, and using that can give +/- fps effect w/out big resource consumption.

 

No, there's no difference in using SetTimer or CreateThread to have the code run, net result is the same

 

at0m0s - using VS2013 compiler your .exe idles consuming 99% cpu. Those stars should only be a few lines of C++, so I think your code is way too big (Fudo did a similar effect in much smaller code, check it out)

Link to comment

at0m0s - using VS2013 compiler your .exe idles consuming 99% cpu. Those stars should only be a few lines of C++, so I think your code is way too big (Fudo did a similar effect in much smaller code, check it out)

The code is written in C++ using a class, it is broken into multiple functions so it is going to look 'big' while the actual code is not much more then about 10 lines. The starfield also includes fading of stars which is another bit of code over a basic starfield.

As for CPU taking up 100%, I do not have that issue on my machine.

Link to comment

The code is written in C++ using a class, it is broken into multiple functions so it is going to look 'big' while the actual code is not much more then about 10 lines. The starfield also includes fading of stars which is another bit of code over a basic starfield.

As for CPU taking up 100%, I do not have that issue on my machine.

 

The problem was that ur exe doesn't run on machines w/out vs2015, and I don't have/want that. Installing the vs2015 redistro pkg won't fix it either.

 

I do have msbuild on vs2013 though, so I changed the project file from toolset v14 to 12 to compile/link. msbuild defaults to debug, which made it use 99% resources and I realized thats why.

 

Compiling in release works but linking doesn't, so I had to remove /NODEFAULTLIB linker option to get it to link, but that also removed ~30KB from your exe size too.

 

So I did get it to run properly using minimal resources, but since this is demoscene/codegolf style thread there still a lot ways to to reduce both ur code & .exe size, but thx for sharing.

Link to comment

The redist package will fix the issue, as long as you install the correct one. This code is compiled as 32bit, so you need the 32bit redist package.

And if you are on XP (for whatever reason..) you will need to set the compile options to use XP.

Edited by atom0s
Link to comment

The redist package will fix the issue, as long as you install the correct one. This code is compiled as 32bit, so you need the 32bit redist package.

And if you are on XP (for whatever reason..) you will need to set the compile options to use XP.

 

I did the vs2015 x86 redistro & it didn't work (on my very old Win7 w/no internet).

 

XP is the #2 most popular desktop OS in the world...

 

ur code is copy pasteable to g++

 

g++ - runs on XP - there's none of this redistro nonsense, it just works. U click it, it runs. It generates better ASM & overall code size much smaller than msvc.

 

when there's stuff like this happening often I cannot see the point in using propietary msvc when superior, free options exist.

 

https://www.humankode.com/security/how-a-bug-in-visual-studio-2015-exposed-my-source-code-on-github-and-cost-me-6500-in-a-few-hours

Link to comment

Saying it will run under g++ and not need redist has nothing to do with anything lol.. I coded it for Windows, for a Windows based game, so I use Windows based tools by Microsoft. I have no reason to personally touch g++ so my stuff is going to need a redist.

This code was also not built using VS2015 anyway. It is old code that was made a long while back.

I have no plans on switching the compiler/IDE I use because of a simple installation bothers some people though.

Link to comment

no it doesn't bother me. just saying because i've seen this question here many times


 


attached is ur exact code compiled w/default g++ settings, requires no dependencies runs on all windows including xp


stars.exe.rar

Edited by simple
Link to comment

Stars recompiled and CRT statically linked with XP support...


 


Also I made my first GDI sketch (very bad) but for fun!


 


wFlLG8q.gif



void PixelWrite(RGB *pixels)
{
RGB *Rgb;
for (int x = 0; x < Width; x++)
{
for (int y = 0; y < Height; y++)
{
Rgb = &pixels[y * Width + x];
Rgb->r = (unsigned char)(((tan(_cr(float(x) * float(Width)) - Camera) * tan(_sq((float)y * (float)Height) + Camera) * cos(Camera * (float)y * (float)Height))* y + y));
Rgb->g = 0;
Rgb->b = 0;
}
}
Camera += 0.09f;
}

stars_msvc2015.rar

bgr.rar

  • Like 1
Link to comment
  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...