Archived

This topic is now archived and is closed to further replies.

Utwo

Care to help a newbie?

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...)

Share this post


Link to post
Share on other sites
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!]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
<>

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
I think that when he''s just started to learn C++ he shouldn''t try the DX, its too compilcated. He should try out Allegro (I myself am using it and im very happy with it). Allegro is very portable. It works under Windoze, Dos, Unix, Linux and BeOs

Example of a small Allegro program

#include

int main( int argc, cha *argv) {
allegro_init();
set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
textout(screen, font, "Hello World", 100, 100, makecol(255, 255,255);
readkey();
return 0;
}

though you may have problems building Allegro for Dev-C++,
The best thing about Allegro s that you dont have to worry about the windows stuff. You even can use OpenGl with Allegro, so that Allegro manages all the sound and input while OpenGl does all the 3D stuff. Allegro has some GUI functions too and there are some good GUI libraryes for Allegro too...

hmmm.... this post of mine seems to be an ad for Allegro :rolleyes:

Share this post


Link to post
Share on other sites
I saw that there''s a MoveTo and LineTo procedure in the GDI, above. I didn''t know that - I just ported my Bresenham procedure from DJGPP. Wasn''t too hard, but hey! Now I''m happy - no more bulky Bresenham algorithm

coSyn

Share this post


Link to post
Share on other sites
No one seemed to respond to the one person asking how to draw a single pixel If memory serves (been a while since i''ve used this), its

SetPixel(hdc, x, y, color);

GDI has just about everything for (2D) graphics, its just SLLLOOOOOWWWWW.

-Dan

Share this post


Link to post
Share on other sites
quote:
Original post by Verso
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...)



You can use system interrupts in windows 9x, but I''m not sure about NT. Ever played old dos games in a dos window? Some of them use standard VGA interrupts, others use other standards. You don''t need to use DX to write to the gfx card-it''s just needed to hit those high resolutions w/ high color depths. (Also it''s needed to use those neat hardware acceleration features of DDraw)

I see none of you have any idea of the Borland versioning...I have a copy of the Borland 4.5 and it''s for windows 3.1(!) and this guy has version 3.1... My suggestion is if you like borland better than dev-c++ then you can download v5.0 of the borland c++ compiler free. But I''ve never used dev-c++, do I don''t know which is better.

Share this post


Link to post
Share on other sites
Why do you want to plot pixels? What kind of game use pixel graphic? I think you should ask how to read and plot a bitmap instead.

Good Luck with your learning!



Zeblar Nagrim, Lord of Chaos

Share this post


Link to post
Share on other sites
Thanks for all of the help guys. I''m currently looking at Win32 programming, and I''ve begun to create some windows (with menus, icons and everything!). I still haven''t poured any actual "content" into those windows yet, but I''ll get there soon enough.

I''ve decided my first serious project will be to design a professional-looking, PC version of Mad Libs. You know, those books where you fill in blanks with words corresponding to different word types (nouns, pronouns, verbs, etc.), which complete a story. You then read the story, and laugh at the result.

The game will have a nice interface, tons of stories to choose from, and the ability to save and/or print the results.

I figure this should be low enough for me to aim right now.

Again, thanks for all of your help, people.

Share this post


Link to post
Share on other sites