• ### Announcements

• #### Wondering what's new and changed at GameDev.net?06/20/17

Check out the latest Staff Blog update that talks about what's changed, what's new, and what's up with these "Pixels".

#### Archived

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

# 03.01 - Q&A

## 120 posts in this topic

If there''s something about the C language that you don''t fully understand, then chances are you''re going to suffer for it somewhere down the line. Between myself and the rest of the members of this forum, no C language question will go unanswered for long. You''ll get the answer you need, and the forum will gain a nice collection of C language Q&As for the benefit of future readers. Like I''ve said before, "There''s no such thing as a stupid question". Teej
0

##### Share on other sites
You can get Gimp for win32 (which is about 90-99% stable depending on your video card) from here.

It is equal to Photoshop in many ways, better in some, and it does have it''s own faults. But generally much better than PaintShop Pro and of course free.
0

##### Share on other sites
Hi, I could use a little help with pointers, linked lists and FILE I/O.

d00dzs

I turn pixels into gold.
0

##### Share on other sites
I just finished a course on data structures where we did linkedlists via array/cursor, dynamic, recursive... shoot me a question and I might be able to answer it , as for pointers, here is a great tutorial!

http://pweb.netcom.com/~tjensen/ptr/pointers.htm

you got me on the file I/O though... I know dittily squat about that stuff , hope that helps.

"You won''t get wise with the sleep still in your eyes." - Neil Peart
0

##### Share on other sites
Heya,

I was wondering if anyone on ths forum has tried to use the free compiler: LCC Win32. I have recompiled the Quake 3 source with this but I do not think it compiles C++ stuff. Any yays or nays about it?

L8R

-Nark
0

##### Share on other sites
Alright, I''ve got a billion questions on *exactly* how C/C++ compilers work (and I understand some of my questions may be compiler specific).

I''m currently taking a class on assembly language for the Motorola 68HC12, and many of my questions will involve understanding memory. I''ve got a ton of them (though, I can only remember so many at once!) so, please be patient =)

Firstly, I was to make a few statements...and please correct them if I''m wrong!! These are some of the fundamentals which I judge situations on, and I''d like for them to be corrected first, if they need to be.

1) C/C++ passes all parameters by value by default. This involves allocating a new & separate memory address on the hardware stack (using a instruction similar to LEAS -2,SP (for say, and int) AFTER the calling routine. Afterwards, a value equal to the passed parameter is placed within that address on the stack. The stack is deallocated BEFORE the end of the subroutine, therefore destroying or deallocating the "variable" on the stack. The described above is a part of (if not all of, I''m not sure!) what we would call "function overhead" -- any, of course takes a few CPU cycles. If the function is called over & over, it would be a waste of CPU cycles -- which introduces inline, which I''ll ask about in a sec.

2) An exception to this rule is whenever C/C++ passes parameter of any type of object. This includes any non-C strings, prewritten classes. This is because an "object" is essentially a variable (of type long int? - for 16 bit addressing) that acts as a pointer to the first position in memory in which the object holds. In this case, does C/C++ allocate stack space for the pointer (ie, 2 bytes onto stack, LEAS -2,SP) and allow the value of the pointer to change within the subroutine - but *NOT* affecting the results after the close of the subroutine. Therefore, you have access to "permanently" change all values within the object; however, any changes to the pointer (changing "where the pointer points to") will not be affected after the function call? This runs into a lot of grey area for me, as my understanding of C/C++ is limited here. Please explain any misunderstandings I have of C/C++ as well as anything you can think of that adds to this discussion.

3) Finally, I''m wondering *exactly* what goes on as far as memory goes, CPU cycles, and differences between functions, in the following code...in the comment I put what *I* believe is going on...

Class Example {
...
};

Example SubA(Example ex1)
{
... // does not create a copy of entire object ex1, merely a copy of the pointer ex1, allows all access to public variables
}

Example SubB(const Example &ex1)
{
... // passes the object "by reference" and does not allow the change of the pointer ex1, however, still allows access to public variable INSIDE ex1
}

And finally...tell me why this code is valid:
Example SubC()
{
Example ex2 = new Example();
return ex2;
}
...with my understanding, the local variable ex2 should be deallocated prior to the subroutine actually "ending." For this reason, the address ex2 points to should be an unallocated, random place in memory. The only way that I can convince myself that this works is if return actually (a) allocates space on the hardware stack for retrieval after the subroutine ends, to hold the value of the object ex2. (b) acts somewhat like a RTS instruction in the 68HC12, where it physically *replaces* the machine code that the function call (from main, or parent routine) with a copy of ex2; however, this would require special preparation of the function call, so that there is enough space in the actual call & space from any parameters passes (usually placed after the mnemoic AS WELL AS any extra space needed, so that you would not rewrite or overwrite the next instruction. In such case, the return operator much calculate how many bytes to increment the PC counter (again, I''m refering to the Motorola chip when I should be trying to learn the Intel x86 chipset) by the proper amount to "skip" over this white space. This seems like an awful long winded way to do things, so some clarification is much MUCH appreciated!!

Anyway, sorry for the terribly long post...and thanks for your time...
~khal

---
~khal
0

##### Share on other sites
Help.. please this is driving me nuts.

Since the first time the board was up I have followed through all the posts and have had very little in the way of problems, but this has me stumped. If I compile one of the original files from Teej''s site I have no problems with the compile or running of the game. If I type the code from scratch using the same settings for the libraries and links etc. then the game compiles ok but when run comes up with (mostly) "DI_Init failed".

I have checked for spelling mistakes and all the usual stuff, I even thought I had corrupted something so I formatted the hard drive and re-installed everything with still the same results.

If anyone has any suggestions as I am at a loss and not very experienced at this, it would be much appreciated.

I am using Windows2000 and VC++ 6.0 with DX8

0

##### Share on other sites
I don''t know your problem for sure but i can give you a advice.
When you are at a critical point of the program(Creation of objs or devices...) put a MessageBox(HWND, LPCSTR, LPCSTR, UINT) call (it creates a messagebox with custom message) where teej does the error checking.This way you might have an idea what is wrong.

I turn pixels into gold!!
0

##### Share on other sites
Hi,

I love this forum and I''ve followed it since the beginning.
So here I am for the second edition and I hope we are all gonna enjoy it.

Thanks a lot for your time Teej,
bios10h.
0

##### Share on other sites
d00dzs: Pointers is a pretty popular topic, and for good reason -- it''s one of the most powerful assets of the C language. If you''re patient, I''m sure that you''ll find an article on pointers sooner rather than later in 03.03 - Selected Language Topics. I can almost guarantee the same treatment with linked-lists and file I/O; both hot topics.

khal: I happen to also do some OS development now and again, and I''ve found a nice little document that illustrates compiled code and actual ASM instructions:

http://nondot.org/sabre/os/S1Booting/CompilingBinaryFilesUsingACompiler.pdf

1) The PDF file I''ve mentioned will be of help here. It looks at functions, parameters and pointers, and how they''re treated by the compiler.

2) If memory serves, a function is given the pointer on the stack and is free to alter the contents of any memory accessible via the pointer, but cannot change the pointer itself. After all, the pointer is merely a variable (4-byte on Win32) value. When the caller regains control, the original pointer value is still on the stack from the original function call.

3) SubA() receives a copy of Example, and therefore can''t affect the object externally -- your typical ''pass by value''. On the other hand, SubB() receives a pointer to the object and masquerades it as an actual object (by reference), but can''t alter anything because of the const restraint. Finally, the last function, SubC() works because the return value of a function is passed back to the caller via the stack. Once again, the PDF file link I''ve provided will give you a look at function parameters and return values.

Whew...

Mano3D: You''re a little premature in worrying about the template code -- It''s part of the Ladder series that may be up by the time you read this. In the meantime, it''s hard to diagnose the problem from here, but one thing comes to mind: do you have the following #define?

#define DIRECTINPUT_VERSION 0x0700

One thing that was blatently absent from the template code the first time around was any form of debugging facility, but I can assure you that this time it won''t be overlooked. In your particular case, I''d rewrite DI_Init() to allow the HRESULT value from all of those DI calls to be returned to the caller (Game_Initialize()) so that you can see the error code and look it up in the documentation.

Keep ''em coming...

Teej

0

##### Share on other sites
Mano3D,

I''m not exactly sure how you have your stuff set up, but one possible cause of the problem is simply not having "resource.bmp" or whatever the file was called in the same directory as the .exe file. You might want to check this out since it was a common problem when dealing with "DI_Init Failed" messages. I know several others had this problem, myself included, and the solution was simply putting the .exe and "resource.bmp" in the same directory. Hope this helps. Peace.

Blazeroni
0

##### Share on other sites
That Works for the DD_INIT problem not for DirectInput.

d00dzs

0

##### Share on other sites
Oh, sorry. My mistake. =(
0

##### Share on other sites
Thanx to all for the response.

I have checked (double and triple) all of the #defines and #includes as well as the paths etc. I think the problem I have is more to do with my typing than anything else since all the example DX programs compile and run fine as do Teej''s, it''s just a case of tracking down the offending item. Since my last post I again tried inputting a new project from scratch (except the WinBase and Utils files) and this time everything was OK, but I still can''t find the problem in the previous attempts as they both appear to be identical, maybe it''s just a quirk of my system, I''m sure I''ll work it out somewhere down the line.

Thanx again.

P.S. The site''s great Teej, keep it coming.
0

##### Share on other sites
Teej,

I''ve written a simple calculator program in dos. It works and im thrilled about it I read your article about the standard source code in windows:

int apientry winmain (hinstance etc.)

But if I compile the same program with this code instead of int main() for dos. It gives me a syntax error on the first line of the code before the ''( sign. Am I missing something, besides the proper knowledge??? Maybe I need to include another *.h file than iostream????

here the complete first three lines of the code:

#include

int apientry winmain(hinstance hinstance, //error on this line
hinstance hprevinstance,
etc.

0

##### Share on other sites
Hoi Wulf,

Try including windows.h. It is most likely that your compiler chokes on the argument types (hinstance etc.).

Houdoe

Edited by - georgix on April 11, 2001 10:45:23 AM
0

##### Share on other sites
wulf: If your compiler is complaining, then it''s usually a syntactical error. Either you''re missing an #include (as georgix suggests -- you definitely need windows.h), or some other file. You''ll know if you''re missing an include if the compiler complains that it doesn''t understand a function, keyword, etc. Usually you can track this down by searching for the term in the online help, which directs you towards the proper #include file.

Now, if the linker is complaining, then you''re most likely missing a library file. Under Project|Settings|Link, you can see some information on what the IDE is using to prepare your program. My hunch is that you haven''t set up a project for the right type of program -- create a new Win32 Application Project and drop your source file(s) into the new directory. This way, the IDE knows how to build windows applications (i.e. what to link to).

One other comment: DOS and Windows programs aren''t compatible (except under special circumstances). Chances are that you''ll have to change the way your calculator program does its business by creating a window or dialog box and processing user events such as keyboard entry or mouse clicks. For example, you''re not going to have a DOS shell in a Windows application...

In a nutshell, Windows programs start as windows programs. They''re written differently because they have to behave differently. You''re going to need to learn Windows application programming, which is not in the scope of these tutorials...

Teej

0

##### Share on other sites
Background: I have some procedural "programming" experience in 4GL''s, like VB and MS Access, and a fair amount of bourne-shell scripting, and very (extremely) limited C...

Also this is (I think) more an algorithm than a syntax question.

In addition to following this forum, I''m trying to write a text-based adventure-type game, and I''ve run into my first stumbling block...

Picture a 2-dimensional hex map with 1000 hex''s. I''d like them numbered starting at 0 and spiraling outward from the center. Given the number of a hex, how do I find the numbers of the 6 hex''s adjoining it?

I''ve beat my head against the wall trying to figure this out, and have a feeling I''m going to kick myself when I find out...

Thanks
0

##### Share on other sites
Hi,

I just want to know ask thing:
Can you really go into the code line by line when we do stuff for the first time.

I''m a newly graduate programmer and to be able to use code, I really need to know what it does, means and implies. . .
That''s mainly why I''m here anyway, as DX7 help files are poorly documented in that sense.
Just for now, I get really mixed up with those new data types. . . (liek LPDIRECTDRAW, HINSTANCE and such)they seem liek they''ve been typefed but I can''t find info on them anywhere. . . to me these are the basic things I need to nail down before I even consider starting writing code. Same thing for functions and objects. . .I really need a clear explanation about what an object contains and does before I use it, detailed info on parameters and returns for functions. . . .

the gap between win32 and console programming (I only did the latter in school) looks huge to me.
The post on basic windows application was very nice, I need more of this.

On another note:
I bought me a DX7 intro book and started to read it, but I always get jammed at the same level: win32 programming. Thanks to whoever directed me to that tutorial it looks mighty fine and i''m gonna dive into it just now.

I hope thread was the right one to post this. . .
and many thanks Teej, you''re doing a great job!
I owe you a (B)

Ð!@ßØ£Ü§
0

##### Share on other sites
hey submonkey
i read your post and was thinking about your problem i think that you might look towards differant ways of numbering your hex map if your firm on your numbering route two definte things you would need to know would be the actual row number your hex resides in and its number in that row i am working on some code for you but i know there is a beter way to do it ill also try to come up with an alternative numbering scheme and some code for it i have also done some text adventures before and work on a mud curently feel free to email me darkdragon007607@msn.com
0

##### Share on other sites
Hey submonkey -

I definitely agree with asylum101...instead of wasting your time coming up with that very difficult algorithm, just change the way you''re making your game. A cartesian numbering system would be much easier to implement than your spiral system, despite the slight kink that using hexes instead of squares throws in to the problem.

A note on programming in general (and submonkey, this is certainly not aimed at you, it''s just a bit of wisdom I''ve learned the hard way over the years): it has been my experience that almost every time I''ve run up against a problem I couldn''t solve, it turned out that someone else could figure it out easily. The reason was simply that I was being close-minded in my approach - instead of trying to figure out a new way to work at the problem, I would hammer away for hours trying to make my first idea work. Most problems just aren''t that hard unless you go out of your way to make them that way - these days I try very hard not to reinvent the wheel unless I absolutely have to.

0

##### Share on other sites
Thanks asylum101 and elcabong, both. In a way, I''m relieved that it''s not a terribly simple solution, otherwise I''d have to wack myself in the forehead

quote:
Original post by ElCabong
Most problems just aren''t that hard unless you go out of your way to make them that way - these days I try very hard not to reinvent the wheel unless I absolutely have to.

I most certainly agree. Initially, it seemed it wouldn''t be difficult at the time, but the more I thought about it the more I couldn''t figure it out...

Time for a new direction...thanks again

0

##### Share on other sites
Alright, this forum is for questions in C/C++? Well, I''ve run across a little problem in some MUD coding I do on the side. There''s a variable...currently declared long...that controls the "status" of my character. For example, bit 0 might mean he''s invisible. bit 1 might mean he''s flying, etc, etc... There''s an instance of this variable for every character...and the damn thing is written in C (and compiled on a variety of OS & processors) so, I''m not sure what all is available for me to use or what not.

Anyway, the problem comes when I started needing more than 32 bits. You see, I really don''t care what the data type is, as long as it can be conviniently passed. I''ve given thoughts to how I should change this (and since this is used 500+ times in the code, I would like to change as little as possible).
The current macros that extract the bit data are:

#define IS_SET(flag,bit) ((flag) & (bit))
#define SET_BIT(var,bit) ((var) |= (bit))
#define REMOVE_BIT(var,bit) ((var) &= ~(bit))

If at all possible, I''d like to simply change its data type (and since its passed as a part of the CHAR_DATA object, I probably don''t need to redo function parameter lists), and perhaps these macros. I''ve given though to using data types such as long double (10 bytes, 80 bits), or string[10]. Or, perhaps...just alloc_perm() space in memory and set a pointer to it. Which is perhaps the best way to do things? How would these affect the macros? Will the compiler know how to do a logical and with a pointer to xyz number of bits (thats essentially what a string[10] is) and a number (that''s beyond the max int for long 2.14 billion, or something like that)? thanks for any help!!

---
~khal
0

##### Share on other sites
Ok, here is my silly question for the day.

I often see code loops that start like this: "when(true)". Well, when WHAT is true? This one has always had me stumped. I''m asuming its some kind of trick where the compiler understands an inplied variable to check but I don''t see how.
0

##### Share on other sites
marcl: the loops you mean are endless loops!
The loop only runs, if the statement in bracket is true. and true is true for ever. normally you have a loop like this:
while(x==1)
{
}
so this loops runs only, if x is equal to 1.