Jump to content

FreeBasic Graphical Examples build 2019-05-08


UEZ
 Share

Recommended Posts

I was curious why the executables are that big and whether free version of Visual Studio can be set to produce smaller exes. So I rewrote your code in C(++) and compiled it. I ended up with 5.5kb executables after removing CRT dependencies (by using Taylor series to approximate sin and cos) Pythagoras.zip

I hate default settings ...shit.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

19 hours ago, wakillon said:

Very nice effects !

You play with graphics with a disconcerting skilfulness.

It's a pleasure for the eyes ! ;)

Thank you. :)

 

53 minutes ago, trancexx said:

I was curious why the executables are that big and whether free version of Visual Studio can be set to produce smaller exes. So I rewrote your code in C(++) and compiled it. I ended up with 5.5kb executables after removing CRT dependencies (by using Taylor series to approximate sin and cos) Pythagoras.zip

I hate default settings ...shit.

Well done. Can you please post your C++ code? I want to see how you did that in C++.

Apropos dependencies: why didn't you use the inline ASM functions for Sin/Cos instead of approx. using Taylor series?

\sin x=\sum _{n=0}^{\infty }{\frac {(-1)^{n}}{(2n+1)!}}x^{2n+1}=x-{\frac {x^{3}}{3!}}+{\frac {x^{5}}{5!}}-\cdots \quad {\text{ for all }}x\!

\cos x=\sum _{n=0}^{\infty }{\frac {(-1)^{n}}{(2n)!}}x^{2n}=1-{\frac {x^{2}}{2!}}+{\frac {x^{4}}{4!}}-\cdots \quad {\text{ for all }}x\!

 

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

7 hours ago, UEZ said:

Well done. Can you please post your C++ code? I want to see how you did that in C++.

Apropos dependencies: why didn't you use the inline ASM functions for Sin/Cos instead of approx. using Taylor series?

\sin x=\sum _{n=0}^{\infty }{\frac {(-1)^{n}}{(2n+1)!}}x^{2n+1}=x-{\frac {x^{3}}{3!}}+{\frac {x^{5}}{5!}}-\cdots \quad {\text{ for all }}x\!

\cos x=\sum _{n=0}^{\infty }{\frac {(-1)^{n}}{(2n)!}}x^{2n}=1-{\frac {x^{2}}{2!}}+{\frac {x^{4}}{4!}}-\cdots \quad {\text{ for all }}x\!

 

Inline ASM when I have real compiler to work with? No thanks.

As for sin and cos approximations, this could be the code (in AutoIt):

Func cos_($x)
    If $x < 0 Then $x = -$x
    While $x >= 3.14159265359
        $x -= 6.28318530718
    WEnd
    Return 1.0 - ($x * $x / 2) * (1 - ($x * $x / 12) * (1 - ($x * $x / 30) * (1 - $x * $x / 56)))
EndFunc

Func sin_($x)
    Return cos_($x - 1.570796326795)
EndFunc

Source of the exe is pretty much your AutoIt code. I just copy/passed it and cleaned a bit:

#include <windows.h>
#pragma comment( linker, "/entry:MwWinMain" )



__forceinline float cos_(float x) {
	if (x < 0.0f)
		x = -x;
	while (3.14159265359f < x)
		x -= 6.28318530718f;
	return 1.0f - (x*x / 2.0f)*(1.0f - (x*x / 12.0f) * (1.0f - (x*x / 30.0f) * (1.0f - x*x / 56.0f)));
}

__forceinline float sin_(float x) { return cos_(x - 1.570796326795f); }


__forceinline WCHAR* int_to_wstr(int val)
{
	static WCHAR buf[32] = { 0 };
	int i = 30;
	for (; val && i; --i, val /= 10)
		buf[i] = L"0123456789"[val % 10];
	
	return &buf[i + 1];
}


// Global Variables:
HWND hWnd;
WCHAR szTitle[50] = L"GDI Animated Pythagoras Tree v2.5 / FPS:   ";                  // The title bar text
WCHAR* szWindowClass = L"UEZ_thingy";            // the main window class name
UINT_PTR IDT_TIMER1 = 100;
UINT_PTR IDT_TIMER2 = 1000;
HDC $hDC;
HDC $hGfxDC;
int $iW = 1200, $iH = 700;
HBITMAP $hBitmapGDI;
HGDIOBJ $hObjOld, $hPen, $hBrush, $hPen_Old, $hBrush_Old;
int $iFPS = 0;

// Forward declarations of functions included in this code module:
__forceinline ATOM                MyRegisterClass();
__forceinline bool                InitInstance();
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);


int APIENTRY MwWinMain()
{
	MyRegisterClass();
	if (!InitInstance()) return FALSE;

	MSG msg;
	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	ExitProcess((int)msg.wParam);
}


__forceinline ATOM MyRegisterClass()
{
	WNDCLASSEXW wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style = CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc = WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = nullptr;
	wcex.hIcon = nullptr;
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);// (HBRUSH)(GetStockObject(WHITE_BRUSH));
	wcex.lpszMenuName = nullptr;
	wcex.lpszClassName = szWindowClass;
	wcex.hIconSm = nullptr;

	return RegisterClassExW(&wcex);
}

__forceinline bool InitInstance()
{
	hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, $iW, $iH, nullptr, nullptr, nullptr, nullptr);

	if (!hWnd) return false;

	$hDC = GetDC(hWnd);
	$hBitmapGDI = CreateCompatibleBitmap($hDC, $iW, $iH);
	$hGfxDC = CreateCompatibleDC($hDC);
	$hObjOld = SelectObject($hGfxDC, $hBitmapGDI);
	$hPen = GetStockObject(DC_PEN);
	$hBrush = GetStockObject(DC_BRUSH);
	$hPen_Old = SelectObject($hGfxDC, $hPen);
	$hBrush_Old = SelectObject($hGfxDC, $hBrush);
	SetStretchBltMode($hDC, WHITEONBLACK);

	ShowWindow(hWnd, 1);
	UpdateWindow(hWnd);

	SetTimer(hWnd,             // handle to main window 
		IDT_TIMER1,           // timer identifier 
		10,                  //interval 
		nullptr);           // no timer callback

	SetTimer(hWnd,             // handle to main window 
		IDT_TIMER2,           // timer identifier 
		1000,                //interval 
		nullptr);           // no timer callback

	return true;
}


void PythagorasRec(HDC $hDC, int $iX1, int $iY1, int $iX2, int $iY2, int $iRecDepth)
{
	auto $iBGR1 = ((255 - ($iRecDepth + 10) * 10) << 8) + $iRecDepth * 20;

	static float $i = 0;
	int $dx = $iX2 - $iX1;
	int $dy = $iY1 - $iY2;
	int  $iX3 = $iX2 - $dy;
	int $iY3 = $iY2 - $dx;
	int  $iX4 = $iX1 - $dy;
	int $iY4 = $iY1 - $dx;

	int  $iX5 = $iX4 + ($dx - $dy) / (2.5f - cos_(($iX4 + $i) / 400) / 1.5f);

	int  $iY5 = $iY4 - ($dx + $dy) / (2.25f + sin_(($iX5 + $iY4 - $i) / 500));

	$i += 0.06666f;

	BeginPath($hDC);
	MoveToEx($hDC, $iX1, $iY1, nullptr);
	LineTo($hDC, $iX2, $iY2);
	LineTo($hDC, $iX3, $iY3);
	LineTo($hDC, $iX4, $iY4);
	LineTo($hDC, $iX1, $iY1);
	EndPath($hDC);

	// DllCall($hDLL_gdi32, "dword", "SetDCPenColor", "handle", $hDC, "dword", $iBGR1)
	SetDCBrushColor($hDC, $iBGR1 | 0x305090);
	FillPath($hDC);
	//; DllCall($hDLL_gdi32, "bool", "StrokeAndFillPath", "handle", $hDC)
	
	if ($iRecDepth > 0)
	{
		PythagorasRec($hDC, $iX4, $iY4, $iX5, $iY5, $iRecDepth - 1);
		PythagorasRec($hDC, $iX5, $iY5, $iX3, $iY3, $iRecDepth - 1);
	}
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_TIMER:
		if (wParam == IDT_TIMER1)
		{
			BitBlt($hGfxDC, 0, 0, $iW, $iH, $hGfxDC, 0, 0, WHITENESS);
			PythagorasRec($hGfxDC, 550, 700, 650, 700, 8);
			BitBlt($hDC, 0, 0, $iW, $iH, $hGfxDC, 0, 0, SRCCOPY);
			++$iFPS;
			return 0;
		}
		else if (wParam == IDT_TIMER2)
		{
			WCHAR* sFPS = int_to_wstr($iFPS);
			$iFPS = 0;
			szTitle[41] = *sFPS;
			szTitle[42] = *++sFPS;
			SetWindowText(hWnd, szTitle);
			return 0;
		}
		break;

	case WM_DESTROY:
		KillTimer(hWnd, IDT_TIMER1);
		KillTimer(hWnd, IDT_TIMER2);
		SelectObject($hGfxDC, $hPen_Old);
		DeleteObject($hPen);
		SelectObject($hGfxDC, $hBrush_Old);
		DeleteObject($hBrush);
		SelectObject($hGfxDC, $hObjOld);
		ReleaseDC(hWnd, $hDC);
		DeleteDC($hGfxDC);
		DeleteObject($hBitmapGDI);
		PostQuitMessage(0);
		return 0;

	case WM_PAINT:
	{
		PAINTSTRUCT ps;
		HDC hdc = BeginPaint(hWnd, &ps);
		//// TODO: Add any drawing code that uses hdc here...
		EndPaint(hWnd, &ps);
	}
	break;

	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

 

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

@trancexx thanks. At the first look C++ code looks more difficult but it isn't at least compared with Pythagoras code.

The C++ code made me curious and I will install the monster Visual Studio once again.

Thanks for sharing.

Btw, I add your Sin_/Cos_ function to FB just to compare the speed. I expected that the ASM inline is faster but isn't. Might be because the API calls amortize the speed difference.

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

18 hours ago, UEZ said:

I expected that the ASM inline is faster but isn't. Might be because the API calls amortize the speed difference.

ASM (handcoded) is (a little bit) faster, but what you meant are the "native" FPU-Code FSIN(), FCOS() and FSINCOS(), where are way slower than an (as mentioned and showed by trancexx) approximation.

Calling the/an API-function is once more slower...

One of the reasons, why trancexx used 

__forceinline

which causes the compiler to inline this function into code. That means, no jumps/calls into a function! Makes the code some bytes bigger, but faster:thumbsup:

19 hours ago, UEZ said:

The C++ code made me curious and I will install the monster Visual Studio once again.

I deinstalled VS yesterday....i am waiting for a compiler which is able to use "real" SIMD when switch on the SSE-Flag. I examined the EXE with IDApro. Yes, the compiler produces short and (in most cases) fast(?!) code using (half-hearted) processor-features which were introduced about 20 years ago...

The only code i saw from a compiler using SIMD (as intended) was from the newest INTEL C++/FORTRAN-Compiler XE (v17.0). And to get this performance it is necessary to program in a "vectorizing" style ( and/or excessive use of _intrinsics_(), which force the compiler to fast(?!) code) .

Or use OpenMP, CILK and other features/libraries. But all this is fu*** sh** to push their (INTEL´s) CPU´s. If the program/algorithm is good to parallelize, OpenCL/CUDA on a price-adjusted GPU is up to 50 to 100 times faster than the code running on a CPU.

Enough whining, we have AutoIt after all...:P

Link to comment
Share on other sites

@trancexx: I installed VS 2015 Pro. How can I get run your source code?

I opened a Win32Project and pasted your code into the editor but when I try to run it I get ems.

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

1 hour ago, UEZ said:

@trancexx: I installed VS 2015 Pro. How can I get run your source code?

I opened a Win32Project and pasted your code into the editor but when I try to run it I get ems.

All default?

If yes then add #include <stdafx.h> at the top of that code, switch to "Release" "x86" then click "Debug" and select "Start Without Debugging"

VS.png

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Aha, working. Now I can play with code. :thumbsup::thumbsup::thumbsup:

THANKS.

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

@trancexx: what have you done that the output (exe) is that small? My output is approx. 100kb.

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

1 minute ago, trancexx said:

Oh, and to get small exes you'll need to change default settings for both compiler and linker. Otherwise you end up with plenty of BS.

Can you tell me if it's not too complicated to explain. Otherwise I can live also with 100kb. ;)

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

4 minutes ago, UEZ said:

Can you tell me if it's not too complicated to explain. Otherwise I can live also with 100kb. ;)

Sure. First of all delete all resource files from solution explorer window (right click on "Resource Files" -> delete. Then do the same for header files. Then remove #include #include "stdafx.h" line. Then go to "Project" and select bottom item (it should be "Some_Name Properties...", then expand "C/C++" and click "Precompiled Headers". On the right click "Use (/Yu)" and then select "Not Using Precompiled Headers".

Then on the left expand "Linker" and choose "Manifest File". Then on the right click "Yes /blah blah" and select "No ..."

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

5 minutes ago, trancexx said:

You should be on 6.5kb now. Few more tweaks and you're done.

Yep, exactly 6.5kb. :)

10.000 things to learn...

 

Thanks again.

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

On 17.10.2016 at 10:33 PM, UEZ said:

10.000 things to learn...

Or ask some of the 10.000 M$-Programmers why they are able to produce such a beautiful and outstanding piece of Software but not able to place a "Build smallest/shortest output possible" Button.

I am afraid, at our next joint beer, you will tell me that you added a "Build smallest/shortest output possible"-Button into the VS (by AutoIt of course! ). I am looking forward to...the beer, not the button:P

Link to comment
Share on other sites

  • 3 months later...

@Lakes: here a list of IDEs: https://www.freebasic-portal.de/downloads/ides-fuer-freebasic/

I personally using FbEdit.

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

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
 Share

×
×
  • Create New...