• Create Account

# Mellkor

Member Since 04 Nov 2009
Offline Last Active Feb 16 2014 05:33 AM

### Pointer, array and struct problem?..

10 February 2014 - 05:36 PM

Hi,

Experimenting more with c++, but have encountered a problem i cant solve. (driving me mad lol)

The program runs perfectly with this code, Drawing a Triangle.

```struct Vertex
{
unsigned int color;
float x, y, z;
};

struct Vertex vertices[3]=
{
{0xFF0000FF, -0.0f, -0.0f, 0.0f},
{0xffffff00, 0.0f, 50.0f, 0.0f},
{0xFF00FF00, 50.0f, 0.0f, 0.0f},

//{0xffffff00, 0.0f, 50.0f, 0.0f},
//{0xffffff00, 50.0f, 50.0f, 0.0f},
//{0xffffff00, 50.0f, 00.0f, 0.0f}
};
```

vertices are passed off to the drawcall at the end, Everything works fine.

But i'm trying to "build" the verticies in a loop. The goal is to eventually build a tilegrid out of the vertices, and push them all at the drawcall.

I Can get the solution to compile but, alas no triangle.

```struct Vertex
{
unsigned int color;
float x, y, z;
};

struct Vertex vertices[3];
struct Vertex *pVerticies = &vertices[0];

void BuildVerts(struct Vertex *pVerticies)
{
int i;
for (i = 0; i < 1; i++)
{

(pVerticies+i)->color = 0xFF0000FF;
(pVerticies+i)->x = 0.0f;
(pVerticies+i)->y = 0.0f;
(pVerticies+i)->z = 0.0f;

(pVerticies+i)->color = 0xFF0000FF;
(pVerticies+i)->x = 0.0f;
(pVerticies+i)->y = 50.0f;
(pVerticies)->z = 0.0f;

(pVerticies+i)->color = 0xFF0000FF;
(pVerticies+i)->x = 50.0f;
(pVerticies+i)->y = 0.0f;
(pVerticies+i)->z = 0.0f;
}
}

int main(int argc, char* argv[])
{

BuildVerts(pVerticies);
...```

I suspect that i'm not using pointers correctly or i need to initialize array elements 'inline' like ie verticies = {..,}; ?

### 2D tilemaps "Chunk Theory".

06 February 2014 - 05:46 PM

Hello!

I'm looking to get as much theory as possible on this subject, I have a hobby project i'm working on: a 2D orthographic game which i have used the standard tile grid array technique to render in XNA. But would like to implement dynamic loading of groups of tiles (chunks) so i can implement larger worlds then a standard grid.

The part of the theory i most understand is when the camera, or object gets close to the edge of a chunk the new one is loaded in the direction the camera is traveling and the chunk behind is unloaded. I guess the theory is to have an array that tracks chunks, then another array inside the chunk to track tiles?

Things I don't understand:

• Best way to "signal" a new chunk should be loaded.
• General Implementation details

I would like to heavy research this topic as i want it to be a center point of any other 2d games i make in the future.

So i welcome any and all recommended:

• Books
• Tutorials
• Videos

And of course code snippets and comments would be very valuable to me. (most proficient with XNA)

Thanks.

Edit: (Also had another question on general tile maps, should i ask here or start a new thread)?

### From C# to C++ concept questions..

18 December 2013 - 09:11 AM

Hi,

I Know my way around the C# language fairly well and am looking to try and 'break into' doing some c++ but have a few conceptual questions to ask.

My main questions are about pointers and memory management, I believe i have researched and understood what pointers are, and how to use and manipulate them but i do not clearly understand why to use them.

C# Example:

class Program
{
static void Main(string[] args)
{
List<int> numberArray = new List<int>();
Random rnd = new Random();

for (int i = 0; i < 10000000; i++)
{
}
}
}

Now, this code should fill a dynamic array allocating memory as needed but when the variable goes out of scope it should be sent to the GC and eventually destroyed correct?

Now, if the same program were to be written in c++ using 'identical code' (ie not pointers) then that array would fill up the stack and the program should crash right?

That's why you need pointers? to send large or persistent data to the heap and not overload the stack?

### 2D octagonal tile map?

13 October 2013 - 08:43 AM

Looking at different types of tile arrays, Seems like an octagonal array would be the most flexible for a 2d rpg but i cant see any articles on people implementing them. Why is this?

### Theory: Vertices from file?

21 September 2013 - 01:35 PM

Hi,

New to 3d concepts, but have been experimenting with SharpDX (i have been using XNA in a 2d capacity previously) and wondering about loading Vertices from an external source..

Looking at the Sample code from the SharpDX sdk, It hard codes the vertices of a triangle, Now its my understanding that a mesh is just a list of vertices contained within in a file(among other things) So my hypothesis is that i could draw that triangle in blender, and load it from file, then draw it?

IF this is the case, whats a good way to go about doing this? primary thoughts would be to create a class to hold that data...

Here's the SharpDX template for reference:

```namespace MiniTri
{
[StructLayout(LayoutKind.Sequential)]
struct Vertex
{
public Vector4 Position;
public SharpDX.Color Color;
}

/// <summary>
///   SlimDX2 port of SlimDX-MiniTri Direct3D9 Sample
/// </summary>
static class Program
{
static void Main()
{
var form = new RenderForm("SlimDX2 - MiniTri Direct3D9 Sample");
var device = new Device(new Direct3D(), 0, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing, new PresentParameters(form.ClientSize.Width,form.ClientSize.Height));

var vertices = new VertexBuffer(device, 3 * 20, Usage.WriteOnly, VertexFormat.None, Pool.Managed);
vertices.Lock(0, 0, LockFlags.None).WriteRange(new[] {
new Vertex() { Color = Color.Red, Position = new Vector4(400.0f, 100.0f, 0.5f, 1.0f) },
new Vertex() { Color = Color.Blue, Position = new Vector4(650.0f, 500.0f, 0.5f, 1.0f) },
new Vertex() { Color = Color.Green, Position = new Vector4(150.0f, 500.0f, 0.5f, 1.0f) }
});
vertices.Unlock();

var vertexElems = new[] {
new VertexElement(0, 0, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.PositionTransformed, 0),
new VertexElement(0, 16, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0),
VertexElement.VertexDeclarationEnd
};

var vertexDecl = new VertexDeclaration(device, vertexElems);

RenderLoop.Run(form, () =>
{
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
device.BeginScene();

device.SetStreamSource(0, vertices, 0, 20);
device.VertexDeclaration = vertexDecl;
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);

device.EndScene();
device.Present();
});
}
}
}
```

PARTNERS