Main.cpp
C++ source for Fluid dynamics galaxy, 217 lines. Raw file
#include <windows.h>
#include <mmsystem.h>
#include "Pattern.h"
//--- DATA -------------------------------------------------------------------//
HINSTANCE hMainInstance;
HWND hMainWindow = NULL;
char szAppName[40] = "Lewpen's pattern generator";
//---- Startup ---------------------------------------------------------------//
void Startup()
{
Pattern_Startup();
}
//---- Display ---------------------------------------------------------------//
void Display(HDC dc, PATTERN_RGB *rgb)
{
BITMAPINFO bmi;
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = 256;
bmi.bmiHeader.biHeight = -256;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = 0;
bmi.bmiHeader.biXPelsPerMeter = 1000;
bmi.bmiHeader.biYPelsPerMeter = 1000;
bmi.bmiHeader.biClrUsed = 0;
bmi.bmiHeader.biClrImportant = 0;
int w = 1280, h = 1024, x, y;
for(y = 0; y < h; y += 256)
for(x = 0; x < w; x += 256)
StretchDIBits(
dc,
x, y, 256, 256,
0, 0, 256, 256,
(LPVOID)rgb->buf,
(LPBITMAPINFO)&bmi,
DIB_RGB_COLORS, SRCCOPY
);
}
//---- Redraw ----------------------------------------------------------------//
void Redraw(HDC dc)
{
Display( dc, Pattern_Generate() );
}
//---- WinProc ---------------------------------------------------------------//
int mouse_x=0, mouse_y=0, mouse_l=0;
LRESULT CALLBACK WinProc(HWND hwnd, UINT uiMsg, UINT wParam, LONG lParam)
{
HDC dc;
PAINTSTRUCT ps;
switch(uiMsg) {
case WM_CREATE:
Startup();
SetTimer(hwnd, 1, 10, NULL);
break;
case WM_TIMER:
dc = GetDC(hwnd);
Redraw(dc);
ReleaseDC(hwnd, dc);
return 0L;
case WM_PAINT:
dc = BeginPaint(hwnd, &ps);
Redraw(dc);
EndPaint(hwnd, &ps);
return 0L;
//- WM_MOUSEMOVE
case WM_MOUSEMOVE:
mouse_x = LOWORD(lParam);
mouse_y = HIWORD(lParam);
// TEMP: TODO: Make this a Pattern_MouseDrag function
if( mouse_l ) drag(mouse_x, mouse_y);
break;
//- WM_LBUTTONDOWN
case WM_LBUTTONDOWN: // todo: capture focus so that we know when the button is released
mouse_l = 1;
break;
//- WM_LBUTTONUP
case WM_LBUTTONUP:
mouse_l = 0;
break;
/*
// full-window drag
case WM_NCHITTEST:
return HTCAPTION;
*/
case WM_KEYDOWN:
if( wParam == VK_ESCAPE ) PostMessage( hwnd, WM_DESTROY, 0, 0 );
return 0L;
case WM_DESTROY:
KillTimer(hwnd, 1);
PostQuitMessage(0);
return 0L;
}
return DefWindowProc(hwnd, uiMsg, wParam, lParam);
}
//---- WinMain ---------------------------------------------------------------//
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wndclass;
char cSwitch;
MSG msg;
RECT rc;
hMainInstance = hInstance;
if(hPrevInstance) return 1;
//- Create window class
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WinProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = NULL;
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass)) return 1;
//- Create main window
hMainWindow = CreateWindowEx(
0,
szAppName,
szAppName,
WS_POPUP,
128,
128,
256,
256,
NULL,
NULL,
hInstance,
NULL
);
if(!hMainWindow) return 1;
ShowWindow(hMainWindow, SW_SHOW);
//- Message loop
while(TRUE) {
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) return TRUE;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
HDC dc = GetDC(hMainWindow);
Redraw(dc);
ReleaseDC(hMainWindow, dc);
}
return 0;
}
//---- END OF FILE -----------------------------------------------------------//