#### Archived

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

# 03.01 - Q&A

This topic is 5223 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

marcl : just to add a little bit on what WildWest told you. The while (true) loop is indeed endless and when you use it you must make sure you include an exit condition in the body of the loop with break.
For example :

int x = 0;
while(true)
{
x++;
if x == 10 then break;
}

Of course, in this example, the sam behavior could be reproduced a number of ways. Like :

int x = 0;
while (x < 10)
{
x++;
}

Or even:

For (int x = 0; x < 10; x++)
{

All these code snippets do the same thing (if i didn''t make any typos that is) but the point is:

If you use While(true) without a Break in the body of the loop, then the loop will be endless, because as WildWest said, True is always True.

##### Share on other sites
When I first saw this I figured it was an endless loop but then I saw more and more code examples like this and figured it must be something else.

Is this done for speed reasons? In other words, would checking a variables value (e.g. When(exit==false)) take more time than an endless look that you break out of when needed?

I just tossed the books out window… endless loops are good now?

##### Share on other sites
kahl: i know you''re not going to like this answer, but i would recommend adding another layer of abstraction between the functions that care about these flags and the flags themselves. right now, it sounds like functions are aware of particular bit positions and determine if the bits are set by calling macros.

i would recommend your functions call macros that determine which bits to care about and, in turn, call your macros to see if the bits are set. you might have a "CAN_FLY" macro that checks "IS_SET" with your "fly bit," for example.

this will allow you to change the way you store the data in the future (more variables, perhaps -- though a struct or class might work nicely) without having to modify each individual function that uses the data.

it''s a lot of work to change your existing code, but it''ll make your life a LOT easier -- especially if you have to change the way you store the data again.

of course, being the OOP fan that i am i would also recommend providing a class that internalizes all the detials of your character. your class could encapsulate the data, and you would access it through methods. you''d never have to care about individual bits again.

hope this helps!

##### Share on other sites
submonkey: a quick search on yahoo or google for "hexagonal grid programming" seems to bring up useful results. hexagonal grids are the bane of wargame developers . luckily there are all sorts of established techniques for dealing with them. i hope this helps you find some useful information!

##### Share on other sites
silent_jesus:

hhmm...perhaps some specific examples would help:

in code, each bit is defined, as:
#define A 1
#define B 2
#define C 3
...
#define aa (some grossly huge power of 2)
...

now, the bits are given names (so we dont have to memorize them), like:

#define PLR_CAN_FLY A
#define PLR_CAN_SWIM B
...etc...

the actual variable that holds the data is:

struct CHAR_DATA{
...
long act; // <-- this is the bitvector
}

again..the macros are:
#define IS_SET(flag, bit) ( (flag) & (bit) )
#define SET_BIT(flag, bit) ( (flag) |= (bit) )
#define REMOVE_BIT(flag, bit) ( (flag) &= ~(bit) )

now...checking if the player can fly or not, is simply:

void SomeFun(CHAR_DATA *ch) // passes in character
{
if(IS_SET(ch->act, PLR_CAN_FLY)
// so, player can fly
else
// player can''t fly
}

so...checking the bits are pretty simple, since each flag is defined as a certain bit. if we''d like to change which bit mean which flag, we simply reassign the #define PLR_CAN_FLY to a different alphabetical letter (which correspondes to a certain bit).

the problem arises, however, when i want to use more than 32 bits for flags. since long is only 32 bits long, i''m restricted to only 32 different flags. most fixes online just addes a second bit vector ch->act2 as another long. but this seems like a temporary fix to me, and i''m really wondering why we cannot just chance the long to something, for example, string[10]. since, in truth, we don''t care what the value of string[10] -- only the bits. any further input is most appreciated =)

---
~khal

##### Share on other sites
Hi everybody!

I have a question about precompiled header files. I''m using VC++ 6 and when I create a new project it always uses stdafx.h (or something like that) as a precompiled header file. And if I add other files, I have to change their settings to: "not using precompiled header" to make them work or include stdafx.h.
So what exactly is a precompiled header file and why should I use it (or not)? And what sort of source files should use that precompiled header file?

Thanks,
Mox

##### Share on other sites
kahl:

yeah, i see what you want to do... unfortunately i''m not sure of how you do that. one would assume there is a way, though. i''d guess it has something to do with manually allocating a block of memory the size you want and just setting/checking bits (if you find a good way of doing this please post it here). i''m not sure how you would do this with strings.

again, all i can suggest is to provide another layer of abstraction... not just between the code and the meaning of each bit, but between the bits and the code itself. or, though i agree it would be something of a pain, creating another long to store more bits with.

hope you find a solution!

##### Share on other sites
silicon jesus;

hey, i worked something out -- much similar to that "layer of abstraction." i don''t know if this is the best or the smartest solution -- but its A solution...

here''s the snippet:
#include
#include

#define aa 0x1
#define ab 0x2
#define ac 0x4
#define ae 0x10
...
#define a1 0x4000000
#define a2 0x8000000
// so on and so foth. this is just to give values for all 32 bits

// convenient consts
#define MAX_BITVECTOR 2
#define MBV MAX_BITVECTOR

// lookup vector
const long bit_table [32] = { aa, ab, ac, ad, ae, af, ag, ah, ai, aj, ak, al, am, an, ao, ap, aq, ar, as, at, au, av, aw, ax, ay, az, a1, a2, a3, a4, a5, a6 };

// the old macros
#define IS_SET_SINGLE(flag, bit) ((flag) & (bit))
#define SET_BIT_SINGLE(var,bit) ((var) |= (bit))
#define REMOVE_BIT_SINGLE(var, bit) ((var) &= ~(bit))

bool IS_SET( long *b, long v, ... )
{
va_list args;
int i;
if (v!=0 && !IS_SET_SINGLE( b[0], v )) return false;
if(MBV>1)
{
long v2;
va_start(args, v);
for(i=0; i {
v2 = va_args(args, long)
if(!IS_SET_SINGLE(b, v2)) return false;
}
va_end(args);
}
return true;
}

void SET_BIT( long *b, long v, ... )
{
va_args args;
int i;
SET_BIT_SINGLE( b[0], v );
if(MBV>1)
{
long v2;
va_start(args, v);
for(i=1;i {
v2=va_start(args,long);
SET_BIT_SINGLE(b[i], v2);
}
va_end(args);
}
}

void REMOVE_BIT( long *b, long v, ... )
{
va_list args;
int i;

REMOVE_BIT_SINGLE( b[0], v ); // sets low

if(MBV>1)
{
long v2;
va_start( args, v );
for(i=1; i {
v2 = va_arg( args, long );
REMOVE_BIT_SINGLE( b[i], v2 );
}
va_end(args);
}
}

void CLR_BITVECTOR( long *b )
{
int i;
for(i=0;i b[i]=0;
}

...basically, i''ve used a long[] for my bitvector instead. then, when i check for certain bits using IS_SET, or whatever,
I use a variable argument list, ex:
#define A_CAN_FLY a2;
#define B_CAN_SWIM b3;
// bc would be the same as ac (the number 4)
if(!IS_SET(ch->bits, A_CAN_FLY, B_CAN_SWIM) return true;

...something along that line....

i haven''t fully tested the code yet...doin'' that right now.
i''ll post if there are bugs

---
~khal

##### Share on other sites
okay, that was the dumb way...
this is the smart way...

you still have all your #defines, such as..
#define aa 0x1
#define ab 0x2
...
#define a5 0x40000000
#define a6 0x80000000
#define ba 0x100000000
#define bb 0x200000000
...
#define b5 0x4000000000000000
#define b6 0x8000000000000000
/* tested up to 64 bits so far */

struct bitvector
{
long low; // a's
long high; // b's
};

//then, simply rewrite the macros:
#define IS_SET(flag, bit) ( ( (bit)>a6 ) ? ( ((flag).high) & ((unsigned long)((bit)>>32)) ) : ( ((flag).low ) & ((unsigned long) (bit) ) ) )

#define SET_BIT(var, bit) ( ( (bit)>a6 ) ? ( ((var ).high) |= ((unsigned long)((bit)>>32)) ) : ( ((var ).low ) |= ((unsigned long) (bit) ) ) )

#define REMOVE_BIT(var, bit) ( ( (bit)>a6 ) ? ( ((var ).high) &= ~((unsigned long)((bit)>>32)) ) : ( ((var ).high) &= ~((unsigned long) (bit) ) ) )

typedef bitvector BITVECTOR;
typedef bitvector128 BITVECTOR128;

void main()
{
cout<<"64-bit vector test"< BITVECTOR b;

SET_BIT(b, a6);
SET_BIT(b, b6);

if( IS_SET( b, a6 ) )
cout<<"true"< else cout<<"false"<
if( IS_SET( b, b6 ) )
cout<<"true"< else cout<<"false"<
REMOVE_BIT(b, a6);
REMOVE_BIT(b, b6);

if( IS_SET( b, a6 ) )
cout<<"true"< else cout<<"false"<
if( IS_SET( b, b6 ) )
cout<<"true"< else cout<<"false"<}

The highs check against the highs, lows against lows. I've looked through the code & we only set 1 bit at a time, but the macros can easily be altered to set/check/remove several at a time...

The problem was in the data types. If you pass it into a function, you're forced to pass it as a data-typed variable (at least, i don't know of another way). The problem was, there wasn't a integer type variable large enough, so it always gets truncated. The macros are easily changed for more bits as long as the compiler can handle larger constants. I've tested it as far 64 bits. I've also written macro definitions for up to 128 bits. If someone knows how to pass values into a function, w/o data-typing them, please let me know! (it would simply the code)
The advantage to macros is that they save a little cpu time. Due this being executed several hundred times every second (and the fact that i have to pay for my cpu usage), I really can't afford to waste cpu time doing bitvectors as strings & parsing. Also, since the damned thing is written in C, I don't have many of the C++ options available to me. If anyone can think of a better way, please let me know!

...ack, just tested it for 128 bit. get a const too large error. does anyone know if there is a a compiler directive that'll enable larger consts? (i'm using VC6.0, but its gotta compile for linux/unix's gcc as well) thx!

---
~khal

Edited by - khal on April 30, 2001 8:47:08 AM

##### Share on other sites
Every Time I compile my Direct X programs after I installed DirectX 8.0a, I get the following Error Messages in my Compiler:

--------------------Configuration: DirectX - Win32 Debug--------------------
File_01.obj : error LNK2001: unresolved external symbol _IID_IDirectDraw7
Debug/DirectX.exe : fatal error LNK1120: 1 unresolved externals

DirectX.exe - 2 error(s), 0 warning(s)

I am using Microsoft Visual C++ Studio Introductory Edition Compiler. When I looked at the DDraw.h file I noticed that to run against a different version of Direct X, I would have to #define the version. I tried that, but I did not work. Does anyone have an idea on what I should do?

##### Share on other sites
Anonymous Poster:

dxguid.lib ddraw.lib dinput.lib dsound.lib winmm.lib

Also, in globals.h, make sure that you have the following line near the top of the file:

#define DIRECTDRAW_VERSION 0X0700

##### Share on other sites
Can anyone explain what functions are being used to map the bitmapped alphabet, so that you can type in words and they print from the bitmap? Just like Basecode1 does. I''m sorry, but I''m a little lost on this one.

##### Share on other sites
Actually, it appears right now that basecode1 isn''t even using the font that''s in the resource.bmp file. However, the Utils_LoadBitmap function loads it into a DirectX surface, which is why it''s needed for the code. To print out the text, he uses the TextOut function, which is a Windows function, I believe.

##### Share on other sites
Thanks Piotyr, I thought it was a windows function but when it wouldn''t compile without the bitmap it threw me.

##### Share on other sites
I have a question for anybody who, like me, has been forging ahead while Teej''s comp was down.

I have managed to get DirectMusic to play a wav file, (a cool tune that I converted from an mp3) during teej''s base program, and stop playing when you press "M"

Thing is, the mp3 was a reasonable 3.5megs and when converted became a stonking 40meg wav. This, apparently, is normal when you convert mp3 to wav.

Here''s the thing, I would like to make the program play this tune. 40 megs is too large, though. I would like it to be playable, but be no larger than 5 megs, so that if I send this little prog to someone it isn''t too large. Any ideas how it can be done?

Someone did mention that DirectShow can help, but I haven''t gotten that far yet, and would rather not have to while we are dealing with DX7 on the forum. BTW, I am using DX8 and the DX8 DXmusic stuff.

##### Share on other sites
Ok. I have been playing around with DX show.

I have managed to get my mp3 playing, but it turns out that DX Show alters the way events are handled. Basicly, DXShow overrides everything else in the program. I could certainly change it to what I need if I continued working on it, but it isn''t worth it right now.

What it boils down to is, I want this mp3 to play using DXmusic. But how? is the question. Maybe we can convert it to a mid file, or to one of the other file types that DXmusic accomodates. But what will do it for us?

Any ideas?

##### Share on other sites
Hi, I have got 3 Q''s for someone nice to answer:

1. How do i get a random number? I know there''s a rand() function, but I''m not sure how it works..

2. What are all those macro things good for? How do they work?

3. Bitshifting, Hex values, and all that other weird stuff - Are there any good tutorials or something for this?

Thanks!

-Lord Maz-

##### Share on other sites

3/ I don''t know about tutorials for these. You wont be needing them for this game project of Teej''s, I shouldn''t think. What you do need, Teej will cover. But seriously, you won''t be needing these things for general game development.

2/ Macros are very useful little things. You normally make a macro by using #define. For example:

#define PI 3.14159265 - this statement means now that if you want to devide something by PI you wouldn''t need to do this everytime:

x = a/3.14159265; - but you could simply use

x = a/PI;

This same macro notion works with variable, as above, or can be used to represent a (small) function. Basically, whenever you later type in the name you defined (like PI above) that text is
replaced in the compiled program with the code you replaced it with.

#define identifier replacement is the syntax.

1/this code will return a random number:

srand (time(NULL));
rand();
int whtinj = rand();

The integar whtinj is now a random number.

2 things to note. C, and C++ cannot generate a truly random number. If you call rand, then call it again after restarting the program, you will receive the same number, everytime. So what we do is use the srand function first, to give rand() a different starting point each time it is called, or at the start of the program. Then call rand() once and disregard it, because the first call to rand is always a bit dodgy.

To all users of this forum, I strongly suggest you get yourself a C (preferably C++) reference book. You cannot build anything independantly without one, guaranteed. You could follow Teej''s examples, but in the end he is going to be teaching you DX and and some graphics basics, and game design methods and ideas. He isnt teaching you C. And you do need to be able to use C or C++ effectively in order to make anything useful, be it a game or an app.

##### Share on other sites
"Thinking in C++" 2nd edition (volumes 1 and 2) by Bruce Eckel.
These books are available from www.mindview.net (click on "books").

I guarantee that you won''t find a better value than this.

##### Share on other sites
Hi, i have programmed in c++ for the last 2 years!!! Have a litlle experience and i want to figure out how to acess bits in a byte!!! for example, just an stupid question, in the pcx function to read an pcx file, it have an rle that verifies the last two bits of the byte.. How can i set just on bit of the byte, how can i choose what bit i want to set, and how can i read just one bit from the byte and the bit i want.. ex.: bit 2, 3 or four of 8-bit!!! Any help will be welcome!

##### Share on other sites
Corrosive: We use the & operator to mask bits. For a single bit, recall the value of the column it''s in, e.g.

12 6 3 18 4 2 6 8 4 2 1---------------7 6 5 4 3 2 1 0

So the value of bit 5, let''s say, is 32. Here''s some examples on putting this to use:

  BYTE val;val = 42; // test valueif (val & 1) printf("Bit 0 is ON");if (val & 2) printf("Bit 1 is ON");if (val & 4) printf("Bit 2 is ON");if (val & 8) printf("Bit 3 is ON");if (val & 16) printf("Bit 4 is ON");if (val & 32) printf("Bit 5 is ON");if (val & 64) printf("Bit 6 is ON");if (val & 128) printf("Bit 7 is ON");

There ya go...

Teej

##### Share on other sites
Thank´s Teej!!! you are doing an Exceptional job here!!!

Just one more question... How can i treat an 32 bit number using this method you have described me? or an 16 bit number, etc!!!

I will have to treat byte per byte? like 8bits numbers separated?

##### Share on other sites
I have a simple C question, that relates to the game. How do I setup a structure in, say, game_initalise() in the InitTerm.cpp and then use it in game_main() in the GameMain.cpp?
Do I have to define it somehow in Globals, or do I use pointers, or should I just go watch TV? (Please note, I would rather poke out my eyes with a spoon than watch TV - so your help would be appreciated!)

##### Share on other sites
One more silly question...

What does the (0 ? 1 : 0) opearotr do?
I''ve seen it in defines but never knew what it does exactly...

Thanks

Biere et punk

##### Share on other sites
Ahh, the ternary operator. Basically, it''s a shorthand if-then-else statement. The format works like so:

(condition) ? (then statement) : (else statement)

For example, if you wanted to do a

if (x > y)
{
x = 0;
}
else
{
x++;
}

the syntax for the ternary operator would be:

(x > y) ? (x = 0) : (x++);