Jump to content

  • Log In with Google      Sign In   
  • Create Account


Mellkor

Member Since 04 Nov 2009
Offline Last Active Jun 01 2014 06:10 AM
-----

Posts I've Made

In Topic: Pointer, array and struct problem?..

10 February 2014 - 07:57 PM



Sorry, i did not compile the code before posting, just replace "pVerticies[i]->" by "pVerticies[i]." and it should compile. Also, remove the "..." in main too, of course smile.png

 

Are you compiling in c++ or c?

 

Using c++ and gcc, It works and compiles now. Thanks a bunch 

 

After actually reading and understanding the code(learnt some new tricks), it wasn't quite what i was after i was hoping to define a triangle with 3 vertices and a color.

and use an iteration for loop to make more triangles, not make a triangle out of an iteration of vertices like you posted smile.png

 

Something like this maybe:

for (int i = 1; i < 6; i++)
{
         //Orthographic triangle
	 {0xFF0000FF, -0.0f*i, -0.0f*i, 0.0f}, 
	 {0xffffff00, 0.0f*i, 50.0f*i, 0.0f}, 
         {0xFF00FF00, 50.0f*i, 0.0f*i, 0.0f}, 
}

If you have time to offer some more assistance that would be excellent, if not thanks again smile.png


In Topic: Pointer, array and struct problem?..

10 February 2014 - 06:41 PM

 

Your for loop only execute once... so you're effectively assigning to vertices[0] 3 times.

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

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

float xVal[3] = {0.0f, 0.0f, 50.0f};
float yVal[3] = {0.0f, 50.0f, 0.0f};
float zVal[3] = {0.0f, 0.0f, 0.0f};

void BuildVerts(Vertex *pVerticies, int NumVerts)
{
	int i;
	for (i = 0; i < NumVerts; i++)
	{
		pVerticies[i]->color = 0xFF0000FF;
		pVerticies[i]->x = xVal[i];
		pVerticies[i]->y = yVal[i];
		pVerticies[i]->z = zVal[i];
	}
}

int main(int argc, char* argv[])
{
    
    BuildVerts(pVerticies, 3);
...
}

I removed the useless structs statements, fixed the loop counter and the pointers array indexes. I dont really like this code(by that i just mean it would be better to load the vertices from a file) but for a simple test it's perfectly fine. I noticed i should have used your Vertex structure to hard code the vertice but this work too.

 

EDIT: damn... ninja'ed! smile.png

 

EDIT2: That line

Vertex *pVerticies = &vertices[0];

is totally useless. Try this instead:

    BuildVerts(&vertices[0], 3);

 

 

Thanks for your reply!, Going through your code now. Getting some build errors atm.

 

"error : expected '=', ',', ';', 'asm' or '__attribute__' before 'vertices' "?

Edit: fixed the error by putting back in the "struct" - program now compiles and draws a single vertical line down the screen lol.

 

 

 

Question: Curious about the theory you used to here, They are defined as type INT, but the member uses a float arguement? - 

int xVal[3] = {0.0f, 0.0f, 50.0f};
int yVal[3] = {0.0f, 50.0f, 50.0f};
int zVal[3] = {0.0f, 0.0f, 50.0f};

In Topic: 2D tilemaps "Chunk Theory".

10 February 2014 - 12:03 AM

Hi Mellkor smile.png

A similar question popped up on the Microsoft XNA forums a while back, and I posted some graphics to visualize the theory and a code sample with class diagram.

(scroll down to "I found some time yesterday, and coded a framework for making infinite worlds.")

 

I hope it will prove useful to you smile.png

 

Kind regards - Jakob

 

Thanks mate! ill have take a look over the drawings and code.


In Topic: 2D tilemaps "Chunk Theory".

06 February 2014 - 11:09 PM

Assuming the map can expand in four directions, you might have a grid of 9 chunks, much like a Tic-Tac-Toe board, loaded into memory at once (if you are only moving in one direction, vertically or horizontally, then 3 or 5 would be a better  number). The chunk in which the player currently resides is always at the center. When the player crosses the boundary into another chunk, then you would unload one column or row and load a new one depending on the direction the player moved.

For example, say the player moves north into the chunk directly above the center one. Now, you unload the entire bottom row (three chunks). The center row now becomes the bottom row, the top row now becomes the center row, and you load three new chunks to fill in the top row. If diagonal movement is disallowed, you could simplify it to 5 chunks - the middle, N, S, E and W. You're still loading and unloading the same number of chunks, just in a different pattern. But I'd still go with 9 myself.

You'll want a decent chunk size for this, though. If the chunks are too small, you'll be loading more frequently unless you adapt the algorithm a bit.
 

 

Yes, I understand that in theory, and that's what i'm trying to accomplish but its the implementation theory that's my problem.

 

If I have a chunk that is 50x50 tiles big, The player/camera gets to local tile 30,0 and wants to load the next chunk(next set of 50x50 tiles), How do i know where to start drawing the next chunk?


In Topic: 2D tilemaps "Chunk Theory".

06 February 2014 - 08:44 PM

This is my very simple attempt at drawing a single chunk, Its probably horribly inefficient and "incorrect" lol

public class Game1 : Game
    {
        //Chunksize
        int width = 10;
        int height = 10;

        //tilesize
        int tSize = 64;

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public static Texture2D texture;

        //Chunks
        chunk[] ChunkArray = new chunk[5];

        //Tile
        public class tile
        {
            public Texture2D texture;
        }

        //chunk
        public class chunk
        {
            public tile[,] tiles;

            public chunk()
            {
                this.tiles = new tile[5, 5];
                BuildChunk();
            }

            public void BuildChunk()
            {
                for (int x = 0; x < 5; x++)
                {
                    for (int y = 0; y < 5; y++)
                    {
                        this.tiles[x, y] = new tile();
                        this.tiles[x, y].texture = Game1.texture;
                    }
                }

            }

        }
        private void addChunkToArray()
        {
            ChunkArray[0] = new chunk();
        }

        public Game1()
            : base()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }


        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }


        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            texture = Content.Load<Texture2D>("tile.png");
            addChunkToArray();
            // TODO: use this.Content to load your game content here
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            //foreach (var chunk in ChunkArray)
            for (int ci = 0; ci < ChunkArray.Length; ci++)
            {
                {
                    if (ChunkArray[ci] != null)
                    {
                        foreach (var tile in ChunkArray[ci].tiles)
                        {
                            for (int y = 0; y < height; y++)
                            {
                                for (int x = 0; x < width; x++)
                                {
                                    spriteBatch.Draw(tile.texture, new Rectangle(x * 64, y * 64, tSize, tSize), Color.White);
                                }
                            }
                        }
                    }
                }
                spriteBatch.End();
                base.Draw(gameTime);
            }
        }
    }
}

 My next goal was to set up another chunk to the right of that one using the array, but it seems from your post that i should pretty much reevaluate the whole theory?


PARTNERS