• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Kurt-olsson

Count vertex array elements

11 posts in this topic

I have a problem with counting the elements in a vertex array.

I have a vertex array like this

Vertex temp = {
{ x,y,z,norm,u,v,color}
---||----

Here i can use sizeof to get the right number of elements to create my vertexbuffer.
But when i declare my xertex array like this:

Vertex* temp;
Temp = new vertex[count];
Foreach
.....

Then i cant use size of?

Any tips?
0

Share this post


Link to post
Share on other sites
[CODE]
size = count*sizeof(vertex)

for(int i=0; i<count<i++)
{
Temp[i].x = 1337;
}
[/CODE] Edited by Tispe
0

Share this post


Link to post
Share on other sites
Thanks, but i need to solve count from an array of vertex. I have the array but cant get the count of it.
0

Share this post


Link to post
Share on other sites
You need to either manually keep track of the array size or use sonething like std::vector. Is there any reason why you aren't using the latter?
0

Share this post


Link to post
Share on other sites
Dont know how i can sent data to my buffer otherwise?
this is the geometry i want to calculate count on.

VERTEX OurVertices[] =
{
{-1.0f, -1.0f, 1.0f, D3DXVECTOR3(0.0f, 0.0f, 1.0f), 0.0f, 0.0f}, // side 1
{1.0f, -1.0f, 1.0f, D3DXVECTOR3(0.0f, 0.0f, 1.0f), 0.0f, 1.0f},
{-1.0f, 1.0f, 1.0f, D3DXVECTOR3(0.0f, 0.0f, 1.0f), 1.0f, 0.0f},
{1.0f, 1.0f, 1.0f, D3DXVECTOR3(0.0f, 0.0f, 1.0f), 1.0f, 1.0f},
{-1.0f, -1.0f, -1.0f, D3DXVECTOR3(0.0f, 0.0f, -1.0f), 0.0f, 0.0f}, // side 2
{-1.0f, 1.0f, -1.0f, D3DXVECTOR3(0.0f, 0.0f, -1.0f), 0.0f, 1.0f},
{1.0f, -1.0f, -1.0f, D3DXVECTOR3(0.0f, 0.0f, -1.0f), 1.0f, 0.0f},
{1.0f, 1.0f, -1.0f, D3DXVECTOR3(0.0f, 0.0f, -1.0f), 1.0f, 1.0f},
{-1.0f, 1.0f, -1.0f, D3DXVECTOR3(0.0f, 1.0f, 0.0f), 0.0f, 0.0f}, // side 3
{-1.0f, 1.0f, 1.0f, D3DXVECTOR3(0.0f, 1.0f, 0.0f), 0.0f, 1.0f},
{1.0f, 1.0f, -1.0f, D3DXVECTOR3(0.0f, 1.0f, 0.0f), 1.0f, 0.0f},
{1.0f, 1.0f, 1.0f, D3DXVECTOR3(0.0f, 1.0f, 0.0f), 1.0f, 1.0f},
{-1.0f, -1.0f, -1.0f, D3DXVECTOR3(0.0f, -1.0f, 0.0f), 0.0f, 0.0f}, // side 4
{1.0f, -1.0f, -1.0f, D3DXVECTOR3(0.0f, -1.0f, 0.0f), 0.0f, 1.0f},
{-1.0f, -1.0f, 1.0f, D3DXVECTOR3(0.0f, -1.0f, 0.0f), 1.0f, 0.0f},
{1.0f, -1.0f, 1.0f, D3DXVECTOR3(0.0f, -1.0f, 0.0f), 1.0f, 1.0f},
{1.0f, -1.0f, -1.0f, D3DXVECTOR3(1.0f, 0.0f, 0.0f), 0.0f, 0.0f}, // side 5
{1.0f, 1.0f, -1.0f, D3DXVECTOR3(1.0f, 0.0f, 0.0f), 0.0f, 1.0f},
{1.0f, -1.0f, 1.0f, D3DXVECTOR3(1.0f, 0.0f, 0.0f), 1.0f, 0.0f},
{1.0f, 1.0f, 1.0f, D3DXVECTOR3(1.0f, 0.0f, 0.0f), 1.0f, 1.0f},
{-1.0f, -1.0f, -1.0f, D3DXVECTOR3(-1.0f, 0.0f, 0.0f), 0.0f, 0.0f}, // side 6
{-1.0f, -1.0f, 1.0f, D3DXVECTOR3(-1.0f, 0.0f, 0.0f), 0.0f, 1.0f},
{-1.0f, 1.0f, -1.0f, D3DXVECTOR3(-1.0f, 0.0f, 0.0f), 1.0f, 0.0f},
{-1.0f, 1.0f, 1.0f, D3DXVECTOR3(-1.0f, 0.0f, 0.0f), 1.0f, 1.0f},
};
DWORD OurIndices[] =
{
0, 1, 2, // side 1
2, 1, 3,
4, 5, 6, // side 2
6, 5, 7,
8, 9, 10, // side 3
10, 9, 11,
12, 13, 14, // side 4
14, 13, 15,
16, 17, 18, // side 5
18, 17, 19,
20, 21, 22, // side 6
22, 21, 23,
};
[source lang="cpp"][/source]
0

Share this post


Link to post
Share on other sites
If you're hard coding your geometry like that then you know how many vertices and indices you have, unless I'm missing something?
0

Share this post


Link to post
Share on other sites
[quote name='kubera' timestamp='1349622284' post='4987690']
I think, that this would be a method:

[CODE]
const ULONG_PTR ulSize = sizeof(OurVertices) / sizeof(VERTEX)
[/CODE]
[/quote]
Consider replacing the calculation with the following function:
[code]
template<typename T, size_t N>
size_t number_of_array_elements(T (&)[N])
{
return N;
}
[/code]
It will return the number of elements for an array, and will error out at compile time if you don't pass it an array. Your code, for example, will fail silently and calculate an incorrect size value if you try to use it with a pointer instead of an array.
2

Share this post


Link to post
Share on other sites
[quote name='Brother Bob' timestamp='1349622939' post='4987693']
template
size_t number_of_array_elements(T (&)[N])
{
return N;
}
[/quote]
Could you explain, please, how your template works?
(or add an example)
0

Share this post


Link to post
Share on other sites
[quote name='kubera' timestamp='1349693656' post='4987941']
[quote name='Brother Bob' timestamp='1349622939' post='4987693']
template
size_t number_of_array_elements(T (&)[N])
{
return N;
}
[/quote]
Could you explain, please, how your template works?
(or add an example)
[/quote]
I can interpret your question in two ways:[list=1]
[*]you want to know how to use it, or
[*]you want to know how it works and how it is able to determine the size of an array.
[/list]
I'll just go ahead and answer both, so pick your answer :)

For 1, it is just a function where you pass the array and it returns its size.
[code]
int foo[42];
int size = number_of_array_elements(foo);
[/code]
The variable [i]size[/i] is assigned the value 42, because the array [i]foo[/i] contains 42 elements.

For 2, you need to know a bit about references to arrays. Normally when you pass an array to a function, you pass a pointer to its the first value in the array along with the size of the array. This way, the function can take any array, but since the array is decayed into a pointer, the size information about the array is also lost which is why you also have to pass its size to the function. Such a function prototype typically looks like this:
[code]
void myFunction(int *p, int n);
[/code]
where [i]p[/i] is the pointer and [i]n[/i] is the size. The function now knows how many elements that is pointed to by [i]p[/i]. You can pass any pointer to any array of any size to this function, but you have to manually track the size of the array.

Another way to pass an array to a function is by reference. The syntax is a little strange if you're not used to it:
[code]
void myFunction(int (&a)[42]);
[/code]
The function now takes a reference to an array of 42 elements. This way, you can [i]only[/i] pass an array of 42 elements, but the type of the array (which includes its size) is preserved entirely. In the previous function, the type of the array is partially lost: the type of the elements is implied by the pointer's type, but the array's size is lost in the pointer decay.

Now, a template is introduced to allow the type to vary so that you can pass an array of any type. Furthermore, the size of the array can also be made a template parameter. So replace the type ([i]int[/i] in this case) and the size (42 in this case) with generic template parameters.
[code]
template<typename T, size_t N>
void myFunction(T (&a)[N]);
[/code]

Next step is to ask: what do we want the function to do with the array? In this case, we don't want to do anything with the array itself, we just want the function to return the size of the array.
[code]
template<typename T, size_t N>
void myFunction(T (&a)[N])
{ return N; }
[/code]

Last step to match the function I presented above is to remove the name of the parameter. Since we don't use the parameter [i]a[/i] anywhere in the function, there is no need to give it a name so we can leave it as an unnamed parameter.
[code]
template<typename T, size_t N>
void myFunction(T (&)[N])
{ return N; }
[/code]
And there it is, the function [i]number_of_array_elements[/i].
2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0