Understanding a Line of Quake 2 Source Code

This is a discussion on Understanding a Line of Quake 2 Source Code within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a question about a line in the following code, which is part of the released source code ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    127

    Understanding a Line of Quake 2 Source Code

    Hi,

    I have a question about a line in the following code, which is part of the released source code for Quake 2. I'm trying to understand how it works. As I understand it, this is a declaration of the gitem_t struct, which describes objects in the game:

    Code:
    typedef struct gitem_s
    {
    	void		(*drop)(struct edict_s *ent, struct gitem_s *item);
    	void		(*weaponthink)(struct edict_s *ent);
    	char		*pickup_sound;
    	char		*world_model;
    	int			world_model_flags;
    	char		*view_model;
    } gitem_t;
    Later in the code, the following is part of the code used to test if an item can be dropped:

    Code:
    	gitem_t		*it;
    	if (!it->drop)
    What I'm wondering about is this line:

    Code:
    void	(*drop)(struct edict_s *ent, struct gitem_s *item);
    This doesn't look like any variable/ function declaration I've come across/ used before. It's not a function declaration because if I right click it in VS Express, and click goto definition it just stays on the same line. So presumably it's a declaration of a pointer to type "struct edict_s *ent", or "struct gitem_s *item". Does this sound right?

    Sorry if this sounds dumb, it just seems a far cry from anything I've seen in my C++ book. Thanks.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    This just screams C, but anyway:
    It's a function pointer that points to a function that whose prototype looks like this:
    void (edict_s*, gitem_s*)
    The
    if (!it->drop)
    line checks if it's a NON-valid pointer (ie NULL pointer).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    drop is a (pointer) variable that POINTS to a function with the prototype:
    Code:
    void somename(struct edict_s *ent, struct gitem_s *item);
    As it is a pointer it can be NULL. The
    Code:
    if(!item->drop)
    Does indeed check if the pointer is NULL or, in other words, that it doesn't point to a valid function. Apparently NULL is used here if an object can't be dropped. It could've been a pointer to a special function (drop_doNothing) as well, to name an example, but setting it to NULL is probably better and more readable.

  4. #4
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by EVOEx View Post
    but setting it to NULL is probably better and more readable.
    I'd rather use item states, for obvious reasons.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    This just screams C, but anyway:
    Yep, I believe its QuakeC .
    Spidey out!

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,657
    Quote Originally Posted by Spidey View Post
    Yep, I believe its QuakeC .
    I doubt it. From memory, QuakeC did (does?) not support struct types or typedefs.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Dae View Post
    I'd rather use item states, for obvious reasons.
    States is the wrong word, as it is not dependent on any state, it's dependent on whether an item can be dropped (or so I assume). True, we could use a droppable flag. But personally I find this fairly readable and expect this to give less trouble than such a flag. So I say it's just a matter of style; just that my style would match this code should it be mine.
    (That is, if I'd code it in C. In C++ it would be classes with virtual functions)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 01:48 PM
  2. ASM beginner gets 24 compiler errors on hello world...
    By Desolation in forum Tech Board
    Replies: 12
    Last Post: 06-16-2007, 11:21 PM
  3. Source code....
    By Darkcoder in forum Game Programming
    Replies: 8
    Last Post: 03-07-2005, 08:58 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. C source code for int25 or code help
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 09-26-2001, 03:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21