Windows programming

Moderators: None (Apply to moderate this forum)
Number of threads: 3670
Number of posts: 9122

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Setting values in arrays... Posted by Sephiroth on 8 Jul 2001 at 6:07 PM
OK, I have created a structure for polygons in my 3D engine, but can NOT figure out how to set the value of one. Here is basically what I am trying to do:
typedef struct
	{
	int x, y, z;
	int sx, sy;
	} VERTEX;

typedef struct
	{
	int num_vertex, color;
	VERTEX *vert;
	} POLYGON;

POLYGON test;

test.vert[0].x = 100;
test.vert[0].y = 100;
test.vert[0].z = 100;
test.vert[1].x = 50;
test.vert[1].y = 150;
test.vert[1].z = 150;
test.vert[2].x = 50;
test.vert[2].y = 50;
test.vert[2].z = 150;
test.vert[3].x = 50;
test.vert[3].y = 100;
test.vert[3].z = 50;

Now, when I comment out the code to set those values, it compiles AoK, but when I try leaving them in to setup the polygon (a 3D traingle), it crashes. How do I set something like that which could have between three and a thousand vertices? I am trying to set those in the callback under WM_CREATE handling, but it crashes no matter where I put it when it tries executing that code. Thanks in advance to anybody who can help me.
<br>-Sephiroth

Report
Re: Setting values in arrays... Posted by adrianxw on 9 Jul 2001 at 4:05 AM
: OK, I have created a structure for polygons in my 3D engine, but can NOT figure out how to set the value of one. Here is basically what I am trying to do:
:
: typedef struct
: 	{
: 	int x, y, z;
: 	int sx, sy;
: 	} VERTEX;
: 
: typedef struct
: 	{
: 	int num_vertex, color;
: 	VERTEX *vert;
: 	} POLYGON;
: 
: POLYGON test;
: 
: test.vert[0].x = 100;
: test.vert[0].y = 100;
: test.vert[0].z = 100;
: test.vert[1].x = 50;
: test.vert[1].y = 150;
: test.vert[1].z = 150;
: test.vert[2].x = 50;
: test.vert[2].y = 50;
: test.vert[2].z = 150;
: test.vert[3].x = 50;
: test.vert[3].y = 100;
: test.vert[3].z = 50;
: 



I can see two immediate problems. Firstly, you are using an array subscript, but you do not seem to have declared any arrays. Second, you are allocating a pointer to your VERTEX struct in your POLYGON struct, but are not allocating the storage for the actual element.

What I suspect you want is an array of VERTEX's in your POLYGON, or if you don't know how many VERTEX's there will be, you could build a linked list of dynamically created VERTEX's. A further technique would be to use a vector from the template library as a dynamic array.


Med venlig hilsen,

Adrian...


Report
Re: Setting values in arrays... Posted by Sephiroth on 9 Jul 2001 at 10:25 AM
: I can see two immediate problems. Firstly, you are using an array subscript, but you do not seem to have declared any arrays. Second, you are allocating a pointer to your VERTEX struct in your POLYGON struct, but are not allocating the storage for the actual element.
:
: What I suspect you want is an array of VERTEX's in your POLYGON, or if you don't know how many VERTEX's there will be, you could build a linked list of dynamically created VERTEX's. A further technique would be to use a vector from the template library as a dynamic array.
:
:
: Med venlig hilsen,
:
: Adrian...
:

What I want to do exactly, is create a polygon that can have between three and a thousand VERTEX structures in it. It would be a total waste of memory to declare my vertices like "VERTEX vert[1000];" though, especially if you're only making a small object, like the 3D triangle I am attempting to use and display. How could I do this and have it automatically create or remove vertices for me? In an older DOS 3D program I looked at, it was declared this way and accessed with a for loop like this:
for(int x = 0; x < polygon.vert_num; x++)
  {
  polygon.vert[x].x = blah;
  polygon.vert[x].y = blah2;
  polygon.vert[x].z = blah3;
  }

Now how could I create my structure (the POLYGON structure) to allow me up to 1000 vertices without declaring it as "VERTEX vert[1000];"? Note: I have not used pointers much aside from file-accessing stuff. Thanks for the help thus far Adrian.

-Sephiroth


Report
Re: Setting values in arrays... Posted by adrianxw on 10 Jul 2001 at 2:11 AM
: What I want to do exactly, is create a polygon that can have between three and a thousand VERTEX structures in it. It would be a total waste of memory to declare my vertices like "VERTEX vert[1000];" though, especially if you're only making a small object, like the 3D triangle I am attempting to use and display. How could I do this and have it automatically create or remove vertices for me? In an older DOS 3D program I looked at, it was declared this way and accessed with a for loop like this:
:
: for(int x = 0; x < polygon.vert_num; x++)
:   {
:   polygon.vert[x].x = blah;
:   polygon.vert[x].y = blah2;
:   polygon.vert[x].z = blah3;
:   }
: 

: Now how could I create my structure (the POLYGON structure) to allow me up to 1000 vertices without declaring it as "VERTEX vert[1000];"? Note: I have not used pointers much aside from file-accessing stuff. Thanks for the help thus far Adrian.
:

If you're not comfortable with pointers then use a vector. It's less efficient, but a little easier to grasp. Create an empty vector in your POLYGON like this...

vector<VERTEX> vert;

... this is now conceptually VERTEX vert[0];

Statically create a VERTEX...

VERTEX temp;

... then initialise the vertex and put it into the vector...

temp.x = blah1;
temp.y = blah2;
temp.z = blah3;

polygon.vert.push_back(temp);

Now your vector looks like VERTEX vert[1] with that element in it. Keep calling push_back(), (literally, push this on the back of whatever is there so far), until you have all your vertexs defined. Voila, an array of VERTEX struct's just the right size. You need to include the <vector> header file to use them.

You might want to start with a simpler example and get comfortable with vectors, they are really very easy to use. Look at this, and look in the help for other members of the vector class you can play with.
#include <iostream>
#include <vector>
using namespace std;

int main()
{
  vector<int> v;
  int i;

  for (i=0;i<10;i++)
    v.push_back(i);

  for (i=0;i<10;i++)
    cout << v[i] << endl;

  return 0;
}


Med venlig hilsen,

Adrian...


Report
Thanks Adrian! Posted by Sephiroth on 10 Jul 2001 at 7:47 AM
I have never heard of the "push_back" function call before, but that is EXACTLY what I need! Now i can declare it as "VERTEX vert[3];" since it needs three vertices minimum, and then use push_back whenever I have to add another one to make a more complex shape. As soon as I get the screen-drawing routines in the engine done you're welcome to a copy to see what it looks like if you want. Just last night I (finally) figured out angle computing and got the basic raytraciong routine setup and gave the player the ability to move ANYWHERE in ANY direction! Up until now it was just north, south, east, or west, hehe. Thanks again man, you've REALLY helped me. Just out of curiosity, is there another function that works the other way and removes one array from the variable? Like if I declared "VERTEX vert[4];" I could use something to make it three?

-Sephiroth


Report
Re: Thanks Adrian! Posted by adrianxw on 11 Jul 2001 at 1:03 AM
: I have never heard of the "push_back" function call before, but that is EXACTLY what I need! Now i can declare it as "VERTEX vert[3];" since it needs three vertices minimum, and then use push_back whenever I have to add another one to make a more complex shape. As soon as I get the screen-drawing routines in the engine done you're welcome to a copy to see what it looks like if you want. Just last night I (finally) figured out angle computing and got the basic raytraciong routine setup and gave the player the ability to move ANYWHERE in ANY direction! Up until now it was just north, south, east, or west, hehe. Thanks again man, you've REALLY helped me. Just out of curiosity, is there another function that works the other way and removes one array from the variable? Like if I declared "VERTEX vert[4];" I could use something to make it three?
:
:

:Now i can declare it as "VERTEX vert[3];"

No you can't. You can declare it "vector<VERTEX> vert(3);" though. Remember, a vector is not an array, it is a templated C++ class which looks and behaves like a dynamic array. For most practical purposes, you can think of it that way, but remember, you are actually using a very powerful, very flexible class here. I suggest you have a play with it before launching forth - the help will show you many things to do with vectors - it may give you some interesting ideas.

:another function that works the other way and removes one array from the variable?

These are not exactly functions, they a members of the vector class. If you look in the help, you will find many useful members to play with. "i = vert.size();" will tell you how many elements there are now, "vert.pop_back();" will delete the last element in the vector. You can insert/remove elements randomly into or out of the vector but that requires another technique known as an iterator, (kind of like a pointer). Let me know if you want to do this, it is not overly difficult.

As both I and your namesake^2 have pointed out, ultimately, if you want to create a lightning fast game engine, you will have to learn how to use dynamic allocation and pointers, as this is much faster.

Med venlig hilsen,

Adrian...


Report
Re: Setting values in arrays... Posted by Sephiroth2 on 9 Jul 2001 at 4:29 PM
: OK, I have created a structure for polygons in my 3D engine, but can NOT figure out how to set the value of one. Here is basically what I am trying to do:
:
: typedef struct
: 	{
: 	int x, y, z;
: 	int sx, sy;
: 	} VERTEX;
: 
: typedef struct
: 	{
: 	int num_vertex, color;
: 	VERTEX *vert;
: 	} POLYGON;
: 
: POLYGON test;
: 
: test.vert[0].x = 100;
: test.vert[0].y = 100;
: test.vert[0].z = 100;
: test.vert[1].x = 50;
: test.vert[1].y = 150;
: test.vert[1].z = 150;
: test.vert[2].x = 50;
: test.vert[2].y = 50;
: test.vert[2].z = 150;
: test.vert[3].x = 50;
: test.vert[3].y = 100;
: test.vert[3].z = 50;
: 

: Now, when I comment out the code to set those values, it compiles AoK, but when I try leaving them in to setup the polygon (a 3D traingle), it crashes. How do I set something like that which could have between three and a thousand vertices? I am trying to set those in the callback under WM_CREATE handling, but it crashes no matter where I put it when it tries executing that code. Thanks in advance to anybody who can help me.
: -Sephiroth
:
Use test.vert=malloc(sizeof(VERTEX)*Insertnumberhere); to allocate memory.

Report
Re: Setting values in arrays... Posted by Sephiroth on 9 Jul 2001 at 6:03 PM
Yeah, I can easily allocate memory for it, but what I want is a variable that can cold between three and a thousand vertices, like "VERTEX vert[1000];", but without actually declaring it that way. If I declared it that way and then only used four or five vertices, I've wasted SO much ram for the object. How can I declare it so it will hold just the amount I add?

-Sephiroth


Report
Re: Setting values in arrays... Posted by Sephiroth2 on 10 Jul 2001 at 9:58 AM
: Yeah, I can easily allocate memory for it, but what I want is a variable that can cold between three and a thousand vertices, like "VERTEX vert[1000];", but without actually declaring it that way. If I declared it that way and then only used four or five vertices, I've wasted SO much ram for the object. How can I declare it so it will hold just the amount I add?
:
: -Sephiroth
:
:
Just declare it like you did, as VERTEX*vert;
Allocate memory with test.vert=malloc(sizeof(VERTEX)*Number);
Change size by test.vert=realloc(test.vert,sizeof(VERTEX)*Number);
It's faster than using the vector C++ class, which by the way is buggy according to what I've heard.
If you want to lessen the amount of reallocations, you may do it like this:
typedef
struct {
int num_vert,color;
VERTEX*vert;
int vsiz;
} POLYGON;
void Addvertices(POLYGON*p,int n)
{
p->num_vert+=n;
if(p->num_vert>p->vsiz) {p->vsiz+=64; p->vert=realloc(p->vert,sizeof(VERTEX)*p->vsiz); }
}

Report
Re: Setting values in arrays... Posted by adrianxw on 11 Jul 2001 at 12:37 AM
: Just declare it like you did, as VERTEX*vert;
: Allocate memory with test.vert=malloc(sizeof(VERTEX)*Number);
: Change size by test.vert=realloc(test.vert,sizeof(VERTEX)*Number);
: It's faster than using the vector C++ class, which by the way is buggy according to what I've heard.
: If you want to lessen the amount of reallocations, you may do it like this:
: typedef
: struct {
: int num_vert,color;
: VERTEX*vert;
: int vsiz;
: } POLYGON;
: void Addvertices(POLYGON*p,int n)
: {
: p->num_vert+=n;
: if(p->num_vert>p->vsiz) {p->vsiz+=64; p->vert=realloc(p->vert,sizeof(VERTEX)*p->vsiz); }
: }
:

Dynamically allocating heap objects is, of course faster, but I think the vector is conceptually more straightforward to someone who is not overly familiar with pointers.

I have never had a vector fail. I'm not familiar with everybodys implementation of the STL of course, but VC would not appear to be buggy.

Med venlig hilsen,

Adrian...





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.