Author Topic: Newbie at C / C++ and getting small OGL exe < 4 KB  (Read 7033 times)

0 Members and 1 Guest are viewing this topic.

Offline va!n

  • Pentium
  • *****
  • Posts: 1435
  • Karma: 109
    • View Profile
    • http://www.secretly.de
hello... i am new here and i want try to learn C coding with Win32 API based stuff. I am very interesting in starting with my first own 4k intro for windows. I found on the web a 1k OGL framework for C/C++ that fits packed into something like around 500 bytes... However until now i dont any experience with C nor C++... My first question is where to start?

I downloaded and installed PelleC but i had a lot problems to compile simple programs. So i tried Dev-C++ and i can compile the auto created startupcode (window) for Win32 appz without any problems. :) Now i tried to create a new project... cutted off all the auto created source and pasted the framework for 1k OGL... when i create an executeable, this is around 6 KB... so how can i reduce the size without packing to something around 1,5 KB?

Btw... when creating a new Win32 App project (also a window based app), and compiling this, an empty window is just ober 20 KB i think O.O - can someone help me where to start, also whats better and more easy to use? (Dev-C++, VisualC++) and how to create very small executeables. Many thanks in advance.

Seems this is a great comunity!
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

Offline taj

  • Bytes hurt
  • DBF Aficionado
  • ******
  • Posts: 4810
  • Karma: 189
  • Scene there, done that.
    • View Profile
Re: Newbie at C / C++ and getting small OGL exe < 4 KB
« Reply #1 on: January 22, 2007 »
va!n welcome on board.

The tricks for making small code are really quite simple and built intoi the framework you downloaded. Do not try to make your own projects as the smart stuff is built into the makefile (I'm assuming you are referring to aulds framework).

In essence what size coders do is:
1. Miss out the standard C library
2. Set lots of compiler flags to the right values
3. Pack the excutable.

In your case by building your own project you included the standard library and set different flags. Automatically this will make your code much bigger. The actual C code after that is a subtlety.

You ask which is easier to work with to make small prods. Rbraz has proved that the microsoft compiler included Visual C++ Express can produce smaller code than GCC and in the end its a lot easier to link to Microsoft directx - the key to most 4k products today.

http://www.rbraz.com/source.htm

This is rbrazs framework. It gets aulds code to around 420 bytes after packing, GCC only gets to about 480. So use rbrazs framework.

After that by excluding stdlib, you find lots of stuff has disappeared from your normal C programs (you cant yuse them). So now doing a good 4k or 1k is a question of creativity. When you have something up and running and need to size compress it you could post it here and three or four of us can help you!

Good luck!

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Newbie at C / C++ and getting small OGL exe < 4 KB
« Reply #2 on: January 22, 2007 »
Hi Va!n, welcome to the forum!  As Taj says, if you have any problems with the C code as well as the tiny framework, just drop a post in here and we'll have a look for you.

Jim
Challenge Trophies Won:

Offline va!n

  • Pentium
  • *****
  • Posts: 1435
  • Karma: 109
    • View Profile
    • http://www.secretly.de
Re: Newbie at C / C++ and getting small OGL exe < 4 KB
« Reply #3 on: January 22, 2007 »
Wow, many thanks for the fast answer and for the very friendly welcoming on this great board! Thanxx!  :cheers:

Ok, i have following source (1k OGL framework)... due fact i am new to C/C++ i dont know the different of a C and C++ version of this source  ::)  Is there a big different to write the source for C or in C++? Afaik, one different is when working with COM like DX...    in C++ i think, its something like  ->   ... and in c the codeline seems more complex without using ->

Now, when i find a source like this 1k OGL framework on the net and trying to work with Dev-C++ atm... What are the steps to get source (copy/paste) work? Open Dev-C++, creating a new Win32App Project (?) ... remove the auto created code and overwrite it with the copied source (?) ...    (for the 1K OGL framework this seems to work for me in this case... but i am not sure if this way is correct)

Quote
1. Miss out the standard C library
2. Set lots of compiler flags to the right values
3. Pack the excutable.


1) What is this and how to do this?
2) Ok, the question is, what values are correct for Dev-C++ to get a ~2k exe? ^^
3) Ok, i know about packers/droppers... this part is no problem for me ;)

Many thanks for trying to help me.... and sorry for some silly, possible basic questions ^^

Here is the source i use atm...

Code: [Select]
//
// Re-use granted as long as long as you give credit in
// either your demo or accompanying files please...(auld)
//
// Thanks go to Icehawk for WS_MAXIMIZE optimisation and the idea
// how to get rid of Peekmessage
// The following code sets up an Opengl window under win32
// It is double buffered, hides the mouse, has 32 bits of depth/Z.
// The main loop includes a clear for depth and color bits and
// a swapbuffers call for drawing.
// It exits when escape is pressed...
// Tested under XP.
//

/*
---------------------------------------------------

Visual C++ 2005 Express version

by Rbraz - 2006

1K FrameWork  -  427 bytes


---------------------------------------------------
*/

#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>


int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{             
PIXELFORMATDESCRIPTOR pfd; 
pfd.cColorBits = pfd.cDepthBits = 32;
pfd.dwFlags    = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
HDC hDC = GetDC ( CreateWindow("edit", 0,
WS_POPUP|WS_VISIBLE|WS_MAXIMIZE,
0, 0, 0 , 0, 0, 0, 0, 0) );         
SetPixelFormat ( hDC, ChoosePixelFormat ( hDC, &pfd) , &pfd );
wglMakeCurrent ( hDC, wglCreateContext(hDC) );

ShowCursor(FALSE);
 
do {

glClear ( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );


SwapBuffers ( hDC );

} while ( !GetAsyncKeyState(VK_ESCAPE) );


ExitProcess(0);
return 0;
   
}

- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

Offline taj

  • Bytes hurt
  • DBF Aficionado
  • ******
  • Posts: 4810
  • Karma: 189
  • Scene there, done that.
    • View Profile
Re: Newbie at C / C++ and getting small OGL exe < 4 KB
« Reply #4 on: January 22, 2007 »
To quickly answer you:

1. Take the entire directory in the package you downloaded and copy it
2. Open the devC++ project file in this new copy (double click)
3. Delete the C code from the main.c there and paste your own code into the main.c file
4. Try to compile

This may not work because likely as not your code uses calls from stdlib. Stdlib is the C library with basic everyday functions in it. Try doing a web search for it to understand more.

If it doesnt work,. now your hard work begins. You need to remove all the things the compiler complains about one by one. See how far you get.

As for COM and C/C++...well its C that uses -> and C++ uses ::. I wouldnt worry right now though. Use C, dont try to use COM at all if you can avoid it. For example directx9 does not need to use COM. Its too much overhead.


Challenge Trophies Won:

Offline va!n

  • Pentium
  • *****
  • Posts: 1435
  • Karma: 109
    • View Profile
    • http://www.secretly.de
Re: Newbie at C / C++ and getting small OGL exe < 4 KB
« Reply #5 on: January 23, 2007 »
okay... i am not really sure if i get managed to compile to something like 2 kb... but i will give it a try ;)
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Newbie at C / C++ and getting small OGL exe < 4 KB
« Reply #6 on: January 23, 2007 »
Good luck with it Va!n, let us know how you get along with it.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Newbie at C / C++ and getting small OGL exe < 4 KB
« Reply #7 on: January 23, 2007 »
->va!in
The standard library functions are things like fopen, printf (file handling), strcpy (string handling), memcpy, malloc, free (memory functions), sin, cos (math functions) and a few other bits and bobs.
To get your exe file down to a tiny size you absolutely can't use any of these, you need to write your own just for the bits you need.
For C++, there's always a basic startup library present which calls things like static initialisers before main() is called.  Because of that, Taj is recommending you don't use C++, you just use plain C.  It's perfectly possible to do COM in C and call all the D3D (etc) functions, the Windows header files actually make this easy.  We've even got some sample code that can call the Windows Speech API with COM from BASIC :)

Even after following the above steps you might still have a sizeable exe, so post back where you get to.  Eliminating crt0 (C startup), linker options, and exe packers are the next steps.

Jim
Challenge Trophies Won:

Offline va!n

  • Pentium
  • *****
  • Posts: 1435
  • Karma: 109
    • View Profile
    • http://www.secretly.de
Re: Newbie at C / C++ and getting small OGL exe < 4 KB
« Reply #8 on: January 25, 2007 »
sorry but seems i am to stupid?  :o
I am using latest Dev-C++ and i tried the source i posted before... whatever i try, the exe is everytime ~6,6 kb

I tried to create a new folder and unpacked the zip and copied all files of the C Version (instead c++) of the framwork. Inside the folder are files with the suffix   .c  .win ... nut not .dev file like when creating an own project... However, when opening the .c file by doubleclick it... Dev-C++ will be open with the selected file... but seems its just only the source and not as a project... ? << i cant compile it this way...

is embarrassing me... sorry guys...

i tried this C version...    btw, is there any big size different in the created executeable when using C or C++ ? However, i will try all future stuff using C... ^^

Code: [Select]
#include <windows.h>
#include <GL/gl.h>

// if using under VC++ make the following changes...
// VC++: change int-> void
int WINAPI WinMainCRTStartup()
{             
   PIXELFORMATDESCRIPTOR pfd; 
   pfd.cColorBits = pfd.cDepthBits = 32;
   pfd.dwFlags    = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;

//VC++ PVOID -> HDC
   PVOID hDC = GetDC ( CreateWindow("edit", 0,
                        WS_POPUP|WS_VISIBLE|WS_MAXIMIZE,
                        0, 0, 0 , 0, 0, 0, 0, 0) );         
   SetPixelFormat ( hDC, ChoosePixelFormat ( hDC, &pfd) , &pfd );
   wglMakeCurrent ( hDC, wglCreateContext(hDC) );

   ShowCursor(FALSE); 

   do {
       glClear ( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
       // insert Breakpoint winning 4kdemo here
       SwapBuffers ( hDC );   
   } while ( !GetAsyncKeyState(VK_ESCAPE) );
}

Btw... the erros i get...

Quote
  [Warning] cannot find entry symbol _mainCRTStartup; defaulting to 00401000
  [Linker error] undefined reference to `CreateWindowExA@48'
  [Linker error] undefined reference to `GetDC@4'
  [Linker error] undefined reference to `ChoosePixelFormat@8'
  [Linker error] undefined reference to `SetPixelFormat@12'
  ...
« Last Edit: January 25, 2007 by va!n »
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Newbie at C / C++ and getting small OGL exe < 4 KB
« Reply #9 on: January 25, 2007 »
You need to add the import libraries
kernel32.lib
user32.lib
gdi32.lib
opengl32.lib

And I think you're getting mainCRTStartup instead of WinMainCRTStartup because you're building it as a command line application rather than a Windows application.  Taj might correct me on that though.

Jim
Challenge Trophies Won:

Offline taj

  • Bytes hurt
  • DBF Aficionado
  • ******
  • Posts: 4810
  • Karma: 189
  • Scene there, done that.
    • View Profile
Re: Newbie at C / C++ and getting small OGL exe < 4 KB
« Reply #10 on: January 26, 2007 »
Im really confused why aulds package doesnt work for you. For a start it should just work...not linking errors. Jims right about what you need to link in...but aulds package does it for you. Are you sure you have done something like create a new project?

6.6k exe? Is that before or after using the makesmall script auld provides?

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Newbie at C / C++ and getting small OGL exe < 4 KB
« Reply #11 on: January 26, 2007 »
If I'm looking at this right, you can use devc++ to do all the editing and whatnot, but you must use Auld's makefile.win to build it
make -f makefile.win
And then you need to run the makesmall script (or a Windows equivalent) to pack it all down.

Jim
Challenge Trophies Won:

Offline taj

  • Bytes hurt
  • DBF Aficionado
  • ******
  • Posts: 4810
  • Karma: 189
  • Scene there, done that.
    • View Profile
Re: Newbie at C / C++ and getting small OGL exe < 4 KB
« Reply #12 on: January 26, 2007 »
Jim,

absolutely right.
Challenge Trophies Won: