Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   STL: Using vector to manage 3D vectors (3D games) (http://www.go4expert.com/forums/stl-using-vector-manage-3d-vectors-3d-t28415/)

 david_BS 20May2012 20:08

STL: Using vector to manage 3D vectors (3D games)

Credits: BS, OGC

A small demostration of the usage of std::vector to create a simple management system to manage 3D vectors. I used this in a game hack for CS

Code:

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
// UTN FRGP TSP
// BS
// mail: david_bs@live.com
// web: Etalking.Com.Ar
// 2012
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <windows.h>
#include <stdio.h>
#include <fstream.h>
#include <vector>

using namespace std;

struct vec
{
float h;
float f;
float r;
};

#define VEC_INDEX_MAX 32
vector<vec> Vecs[VEC_INDEX_MAX];

#define VECS 6

Functions to add, list and erase, as in every management system
Code:

void func_vec_add(int index, float x, float y, float z)
{
vec v;
v.h = z;
v.f = x;
v.r = y;        *
if(index<=VECS){
Vecs[index].push_back(v);
}
}

void func_vec_clear(int index)
{
* *if(index == -1){
* * * for(int i=0;i<=VECS;i++)
* Vecs[i].clear();
* *}
else if(index>=0&&index<=VECS){
* * * * * *Vecs[index].clear();
* *}
}

void func_vec_list(int index)
{
cout << "\n";

if(index == -1){

for(int i=0;i<=VECS;i++)
for(vector<vec>::iterator si = Vecs[i].begin(); si != Vecs[i].end();++si)
{
printf("Height: %f Forward: %f Right: %f\n",si->h,si->f,si->r);
}

cout << "There are no elements !" << endl;
}

else
{
if(index<=VECS){
* for(vector<vec>::iterator si = Vecs[index].begin(); si != Vecs[index].end();++si)
* {
printf("Height: %f Forward: %f Right: %f\n",si->h,si->f,si->r);
* }
}
}
}

Main function, used for testing purposes
Code:

int main(){

* * * *func_vec_add(5, 100, 500, 1.00f);

//func_vec_list(0);
func_vec_list(-1);

func_vec_clear(-1);

func_vec_list(-1);

cin.get();
return 0;
}

 All times are GMT +5.5. The time now is 09:29.