I'm having some trouble understanding arrays in c++. I'm new to the language and come from a c# background.
I'm trying to take an array, containing objects. These objects have a property called "ShaderId".
Example array: { 1, 2, 3, 1, 3, 3 }
I want to sort that into the following...
{ { 1, 1 }, { 2 }, { 3, 3, 3 } } (Doesn't have to go 1, 2, 3 - So long as all common Id's are grouped).
When I pass in an array of GameObjects, the array within the function only contains the first element?
// MasterRenderer.h
1 2 3 4 5 6 7 8 9 10 11 12
|
#pragma once
#include <vector>
#include "GameObject.h"
class MasterRenderer
{
public:
MasterRenderer();
void RenderScene(GameObject Objects[]); // Renders the given game objects
};
|
// MasterRenderer.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include "MasterRenderer.h"
#include <iostream>
MasterRenderer::MasterRenderer()
{
}
void MasterRenderer::RenderScene(GameObject Objects[])
{
std::vector<std::vector<GameObject>> Batches;
// Grouping gameObjects into groups based on shader (set shader, draw group).
int length = sizeof(Objects) / sizeof(Objects[0]);
for (int i = 0; i < length; i++)
{
if (Batches.size() == 0)
{
Batches.push_back(std::vector<GameObject>());
Batches.back().push_back(Objects[i]);
continue;
}
for (int j = 0; j < Batches.size(); j++)
{
if (Objects[i].ShaderId == Batches[j].back().ShaderId)
{
Batches[j].push_back(Objects[i]);
continue;
}
if (j == Batches.size() - 1)
{
Batches.push_back(std::vector<GameObject>());
Batches.back().push_back(Objects[i]);
break;
}
}
}
std::cout << "Batches" << Batches.size() << std::endl;
}
|
// Main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
GameObject obj1(1);
GameObject obj2(2);
GameObject obj3(3);
GameObject obj4(1);
GameObject obj5(3);
GameObject objects[] = {
obj1,
obj2,
obj3,
obj4,
obj5
};
MasterRenderer renderer;
renderer.RenderScene(objects);
|