Well, this is my first tut, so nobody get hung up on any typos or grammar
mistakes, OK? ;-) First of all I want to warn all newbies to first get to
grips with C and C++ before even attempting Windows 32-bit (Win32)
programming. It might also help to get a decent compiler like M$ Visual
Studio 6 or Borland C (or even Cygnus or LCC-win32 if you're broke
The mission of this tutorial is to give the DOS coder some insight into the
world of Windows, not to teach you everything you need to know about this OS.
In my opinion, I'd rather use Linux - but my current set-up won't allow
it. ;-) Oh yeah, before I forget, go to http://www.gaffer.org/ptc for
Prometheus Truecolor Library, the definitive graphics library for Windows.
(Heck, with PTC you can even port it to the OS of your choice! :-)
So, have u ever seen Win32 code? If not, here's some:
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrev, LPSTR lpCmdLine, int Cmd) { MSG msg; ... // other initialization code; while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage (&msg); } return msg.wParam; }
Huh, what? Well, don't be afraid.. be VERY afraid - lol! No, once you learn
to read between the "lines" and don't worry about the Hungarian notation (can
u believe it, M$ coders dedicating the way they code to some dead guy ;-)
The part of your program shown above is kinda like the Main function in your
normal C program.
Ever heard of event-driven programming? That's when your program hangs around
(like a skateboarder ;-) until something happens, and then reacts to it. This
creates some problems when programming demo's, because you have to share the
CPU with a few other programs, and you have to wait until Windows sends you a
message, so you can react to it. That's what happen in the GetMessage part..
after that, your program translates and dispatches the message to your
WinProc function, which deals with each message and tells it what to do (draw
a triangle, play a tune, or crash the OS :-)
The big problem with Windows coding is the major amount of overhead you have
to work with even when creating the smallest of programs. But, guess what?!
There's ppl who've actually written a 4kb intro in Win32!* Compared to the
normal size of 100KB for even smaller programs, that's quite an achievement.
* Note to Rawhed - The intro was written in full assembler (TASM32), so
there's hope for u! ;-)
Ready for some more code? OK, let's shock ya:
// Setup and register the window class; WNDCLASS wClass; wClass.style =CS_HREDRAW|CS_VREDRAW; wClass.lpfnWndProc =WindowProcedure; // callback function; wClass.cbClsExtra =0; wClass.cbWndExtra =0; wClass.hInstance =hInstance; wClass.hIcon =LoadIcon(hInstance,IDI_APPLICATION); wClass.hCursor =LoadCursor(NULL,IDC_ARROW); wClass.hbrBackground =(HBRUSH)(COLOR_WINDOW+3); wClass.lpszMenuName =NULL; wClass.lpszClassName =WINDOWNAME; RegisterClass(&wClass);
All this code just to set up one window? Yeah, and that's not even all the
parameters! What actually happens is that you set up a class for your window,
register it, create it with some more code and then show it on screen.
Remember to update it too! :-)
If you ask me that's quite a lot of code for one small window! OK, let's see
some PTC code.. Remember that this is a full program designed to put a pixel
on the screen..
#include "ptc.h" inline void putpixel(Surface &surface,int x,int y,char8 r,char8 g,char8 b) { // lock surface pixels int32 *pixels = (int32*) surface.lock(); // pack the color integer from r,g,b components const int32 color = (int32(r)<<16) | (int32(g)<<8) | int32(b); // plot the pixel on the surface pixels[x+y*surface.width()] = color; // unlock surface surface.unlock(); } int APIENTRY WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nCmdShow) { try { // create console Console console; // create format Format format(32,0x00FF0000,0x0000FF00,0x000000FF); // open the console console.open("Pixel example",format); // create surface matching console dimensions Surface surface(console.width(),console.height(),format); // plot a white pixel in the middle of the surface putpixel(surface,surface.width()/2,surface.height()/2,255,255,255); // copy to console surface.copy(console); // update console console.update(); // read key console.read(); } catch (Error &error) { // report error error.report(); } // exit return 0; }
Remember that this code is DirectX optimised, and can be converted to any
platform just by switching the compiler! If u've ever looked at DirectX code,
u will realise that this is DEFINITELY much easier and shorter.. Yeah, you
read right, shorter! :-)
Windows may have more overheads and generate bigger code. But, you don't have
to worry about what videocard the guy uses, or even the soundcard he plays
with!
Hope you found some of this useful!
Gendor /Apocalypse
All comments/suggestions can be sent to gendor@mweb.co.za
Plz, I need feedback! :-)