Care to help a newbie?

Started by
14 comments, last by Utwo 22 years, 9 months ago
For people who don''t feel like reading through 10 paragraphs of information, I''ll state the question first: How do you program graphics with C++? Now, for a little background: I have found this website to be an incredible resource. I began programming exactly two weeks ago when I picked up a book called "Learn C++ in 21 Days." I''ve read most of the book. Some concepts are hard to grasp, like pointers and references, although I''ve been doing plenty of practicing which has helped a lot. I''ve skipped some of the more difficult concepts found later in the book until I''ve got what''s on my plate down pat. The book came with Borland C++ 3.1 for Windows, which I have been using to compile and link my simplistic programs. I''ve decided that I want to program games in my spare time. My search for information is what brought me here. I have found many of the articles here useful, and I''ve since downloaded the DirectX 8 SDK, a new compiler which supports Windows programming, and I''ve bookmarked many of the articles, including the interactive game development tutorial on this website. One piece of advice I intend to follow is that Tetris should be the first game I program. I feel like I have a basic idea of how to do it: how to set up the necessary loops and how to create and manipulate the data need to keep track of levels, points, etc. The problem is that I have absolutely NO idea how to plot a single damned pixel! I don''t want to use DirectDraw (I don''t think I''m ready yet), so I began looking for some libraries that would help. I downloaded some Tetris source code that uses a library called graphics.h, but A) I don''t know how to use it, and B) I get a compile-time error when I try to compile that source code anyway. Can someone point me in the right direction here?
---signature---" Actually, at the time, I didn't give a damn about the FoxNews issue... just that you would come swooping in with your Super Mith cape flapping behind you and debunk it just because it didn't happen in your living room." - InnocuousFox
Advertisement
well you should first get used to using the search engine on gamedev for -- familiar topics -- such as this thread. Next, since no one ever reads previous threads, you should pick up a copy of tricks of the windows game programming guru, which will walk you through all the basics, it will teach you how to get a windows skeleton up and running and eventually make 2d games using dsound, dinput, and ddraw. During this course, you should try working on a tetris clone, and by the end of the book I bet you will have one done. If you cant get the book, read the windows game programming articles on this site. good luck
bad!
That was my first question when I switched from Visual Basic to c++; "all I want to do is be able to plot just one little pixel!" I, being relatively new to c++ myself, am still figuring it all out, but here''s what I think is going on; Graphics programmers used to program in dos, where you could call system interrupts to basically take over every aspect of the computer. This included setting the video card mode and writing directly to video memory. Then there was windows. Now, if you try to call interrupts or write to memory, you get a "general protection fault," basically, the Blue Screen of Death. This is Windows "protecting" the system. So now''a days, your only option is to cooperate with Windows, which means the GDI (slow Windows graphics), DirectX, or OpenGL (I''m sure there are more ways, but again, what do I know?).

My ill-credited recomendation would be to learn OpenGL, OR Win32 programming, and from there to DX (again, an awsome book - tricks of the windows game programming guru). With OpenGL, you can use GLUT to take care of stuff like switching screen modes, creating windows, etc, so it''s easier on beginning programmers. The only reason I switched to DX later was simply because I went to Barnes and Nobles to find an OpenGL book, and there was only one... sitting next to a whole row of DX books. In other words, it generally seems more supported.

(BTW, I know I shouldn''t post this as it contains "DirectX" and "OpenGL" within the same paragraph - people seem a little edgy around here...)
Remember - Hard work pays off in the long run, but laziness pays off immediately.
First of all: you need pointers to do any complex programming. Learn them well before you start on real game programming. They become really easy to use, don''t worry about how hard they seem at first.

Next: Learn the Win32 API. You probably want to find your way around it (you don''t need to memorize all of it or anything) before using it in your games. Once you''re familiar with it, learn OpenGL or DirectX.

Some tutorials:
http://www.winprog.org (Win32 API)
http://nehe.gamedev.net (OpenGL)

[Resist Windows XP''s Invasive Production Activation Technology!]
Did anyone else notice the "...DirectX 8 SDK, a new compiler which supports Windows programming..." there? Utwo, something yuo should know: DX8 SDK is not a compiler, its a library. That means its sorta like a bunch of things you can use in your program to do the work for you. It is linked into your binary. This means the compiler uses it, sort of. Plus, its made for Visual C++, Microsoft''s compiler. *cough MONOPOLY cough* If you want to use DX then buy VC for about a hundred clams, or use OpenGL with any compiler you want.

(http://www.ironfroggy.com/)(http://www.ironfroggy.com/pinch)
Ahhhh well I wasn''t sure if he was calling DX8 SDK a compiler or saying that he downloaded the SDK AND a new compiler... *shrug*

But in actuality you don''t need VC to program DirectX it will work on any 32-bit C++ compiler as long as you link to the proper libraries and include the proper headers. I think some older versions will work on a 16-bit compiler too. That is not part of the MS monopoly... I will stay out of an imminent flamewar by not stating my choice though... lol

Seeya
Krippy

<>

No. Heheh.

The compiler I got is called Dev-C++. I was saying that I got the DirectX SDK, as well as a 32-bit compiler.
---signature---" Actually, at the time, I didn't give a damn about the FoxNews issue... just that you would come swooping in with your Super Mith cape flapping behind you and debunk it just because it didn't happen in your living room." - InnocuousFox
How DOES one plot a pixel anyway with GDI? I''ve been fooling around with drawing functions, BitBlt, etc., but I can''t figure out how to do anything myself. I''ve been trying to use DIB Sections, but I don''t really know how. I''ve been reading the VC++ help, which actually is helpful, but still haven''t been able figure it out. So does anyone have any resources I could turn to to figure out how?
Utwo, try the allegro game programming library.It does the graphics for you and lots more.There are a lot of examples that come with the library. You can even write windows programs using the old dos coding style.Consider this example program for plotting a pixel :-).

#include //header for pixel-plotting,...
#include
int main()
{
allegro_init(); //initialize the library
set_gfx_mode(GFX_AUTODETECT,640,480,0,0);
/*Finally the most important thing here :-)*/
/*Fill your screen with different pixel-colors*/
for(int i=0;i<640;i++)
{
for(int k=0;k<480;k++)
{
putpixel(screen,i,k,rand()%255);
}
}
....
return 0;
}
END_OF_MAIN();

you can find this library and a lot of games at www.allegro.cc
Using Allegro, while a perfectly valid approach, doesn't answer his question or increase his knowledge. To plot a pixel using plain, vanilla Win32, you need to understand windows (not Windows).

Windows (the OS) represents interactive elements using windows (the constructs). The include file windows.h defines a ton of structures and data types (some included from other include files, and so on). One of the most important of them is a structure called an HWND. An HWND is th code "handle" to a graphical window. Another important one is an HDC, the handle to a Device Context. Every visible window has a device context, which is what you actually draw on.

I'm going to provide very skeletal function declarations here as you can check up the full documentation at msdn.microsoft.com for free. To create a Windows application (I'm sure you already know this, but bear with me), you include windows.h and define the already declared WinMain(...) function. Within your WinMain() you either directly or indirectly (such as through a call to another function of our own) declare and define a window class:

WNDCLASSEX wc;

Fill out the fields with appropriate values and then register the class with Windows. Note that I am omitting every single parameter to every single function!

RegisterClassEx(...);

If this is succesful, you can now create a window (the structure):

HWND hwnd = CreateWindow(...);

You then need to set up a message pump for your window. Windows (the OS) communicates to every running application's windows via messages which tell the app whether the user has clicked the mouse (and which button or buttons) in the window, and so on.

MSG msg;while( GetMessage(...) ){  TranslateMessage();  DispatchMessage();}   


You then end your WinMain() function with a return value, or error processing. This is just a skeletal WinMain(). You now have to define the already declared window procedure function, which you would have specified to the window class (see WNDCLASSEX). It is in this window procedure that you handle messages, such as the one that tell you to redraw your window - WM_PAINT.

LRESULT CALLBACK WndProc(...){  ...  if( msg == WM_PAINT )  {    // do your drawing here  }  ...}   


Since most people like to keep their programs modular, common practice is to do the actual drawing in another function that you'd call from within that if-block above (let's call it OnPaint, for MFC reasons). Within this function you need to get access to your window's device context, create a graphical drawing object and then plot your pixel.

HRESULT OnPaint(...){  // get the window's device context  static HDC paintdc = GetDC( hwnd );  // get a pen to draw with; we'll use a 'stock" pen  HPEN hpen = GetStockObject( WHITE_PEN );  // when we select our object as the current drawing object, it   // returns a pointer to the previous object, which we should  // save so we can restore it when we're done  HPEN hOldPen = SelectObject( hpen );  BeginPaint(...);    // plot your pixel here    MoveTo( &paintdc, x, y, NULL );    LineTo( &paintdc, x, y );  EndPaint(...);}   


That's a simplistic example. There are complete descriptions of all functions, explanations and samples at msdn.microsoft.com, and if you don't have the MSDN Library CD (which I guess you don't, since you se Dev-C++), that should be your best friend while you do Windows programming.


People who can't do shit, talk.
-Dwight Yorke, Manchester United

Edited by - Oluseyi on July 9, 2001 4:56:38 AM

Edited by - Oluseyi on July 9, 2001 4:58:17 AM

This topic is closed to new replies.

Advertisement