Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1075927 Posts in 44152 Topics- by 36120 Members - Latest Member: Royalhandstudios

December 29, 2014, 03:55:25 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Isometric engine problems
Pages: [1]
Print
Author Topic: Isometric engine problems  (Read 492 times)
TerrorLT
Level 0
*


View Profile
« on: March 16, 2012, 05:29:02 AM »

Hello. Recently, I started developing a strategy game(in C# and XNA) similar to games like Rise of Nations and Age of Empires(and adding some RPG elements). I've got the games' functions and ideas written down, but I digress.
There is currently one problem and 2 things that I can't figure out.

1. The Problem (SOLVED!)
I've created a basic map drawing method and soon after, the camera controls. The problem
is that at the speed of 1p/10ms (pixel/milliseconds), while scrolling to the right, left and bottom, everything's fine, but when I scroll upwards, lines(from the black background) appear on the ground. The size of the line depends on the speed of scrolling(The interval is always 10ms). Scrolling to the sides and bottom also "produce" lines, but it's only if the
scrolling speed is 2p/10ms or higher.


Game1.cs:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Threading;

namespace WarOfGlory
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D mouse;
        MouseState ms;

        Tile thetile;
        Tile tile = new Tile();

        Texture2D tileTexture;

        public int cameraX = 400;
        public int cameraY = 20;
        public int scrollSpd = 2;
        Thread scrolling;
        Rectangle Left;
        Rectangle Right;
        Rectangle Top;
        Rectangle Bottom;
        MouseCursor mc = new MouseCursor();

        SpriteFont simple;

        public byte[,] testmap;
        Map map = new Map();

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


        }

        protected override void Initialize()
        {

            testmap = map.GenMap();

            

            Left = new Rectangle(0, 0, 20, graphics.PreferredBackBufferHeight);
            Right = new Rectangle(graphics.PreferredBackBufferWidth - 20, 0, 20, graphics.PreferredBackBufferHeight);
            Top = new Rectangle(0, 0, graphics.PreferredBackBufferWidth, 20);
            Bottom = new Rectangle(0, graphics.PreferredBackBufferHeight - 20, graphics.PreferredBackBufferWidth, 20);

            scrolling = new Thread(new ThreadStart(ScrollMap));
            scrolling.Start();

            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            mouse = Content.Load<Texture2D>("Mouse");
            simple = Content.Load<SpriteFont>("Simple");

            // TODO: use this.Content to load your game content here
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
            scrolling.Abort();//put here because of the proccess not turning off, after the red X is pressed
        }
        protected override void Update(GameTime gameTime)
        {
            mc.newState = Mouse.GetState();//get the state of the mouse

            
            mc.oldState = mc.newState;//sets the old state of the mouse
            base.Update(gameTime);
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            spriteBatch.Begin();
            DrawMap(testmap, spriteBatch);
            DrawMouse(spriteBatch);
            DrawCameraCoords();
            spriteBatch.End();

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }

        private void DrawMouse(SpriteBatch sb)
        {
            ms = Mouse.GetState();
            sb.Draw(mouse, new Vector2(ms.X, ms.Y), Color.Blue);
        }



        public void DrawMap(byte[,] map, SpriteBatch sb)
        {
            byte lastTile = 0;
            Rectangle source = new Rectangle();
            for (int x = 0; x < map.GetLength(0); x++)
            {
                for (int y = 0; y < map.GetLength(1); y++)
                {
                    if(map[x,y] != lastTile)
                    {
                        thetile = tile.GetTileById(map[x, y]);//gets the tile info
                        tileTexture = Content.Load<Texture2D>(thetile.texture);//loads the tile's texture
                        int tilesInRow = tileTexture.Width / thetile.tileSizeX;//gets the tiles in row
                        //gets the tile into a rectagle for drawing
                        source = new Rectangle(thetile.tileSizeX * (map[x, y] - 1) - ((thetile.location / tilesInRow) * tileTexture.Width),
                            (thetile.location / tilesInRow) * thetile.tileSizeY, thetile.tileSizeX, thetile.tileSizeY);
                    }

                    //Problem's sowhere in here
                    //tileSizeX(Width) = 34
                    //tileSizeY(Height) = 17
                    //Camera X/Y is the camera position

                    Vector2 drawPos = new Vector2((x * (tile.tileSizeX / 2 ) - y * (tile.tileSizeX / 2 )) + cameraX,
                        (x * ((tile.tileSizeY-1) / 2) + y * ((tile.tileSizeY) / 2)) + cameraY);
                    sb.Draw(tileTexture, drawPos, source, Color.White);
                }
            }
        }

        private void ScrollMap()
        {
            

            if (mc.isMouseWithin(Left))
                cameraX+= scrollSpd;
            if (mc.isMouseWithin(Right))
                cameraX -= scrollSpd;
            if (mc.isMouseWithin(Top))
                cameraY+= scrollSpd;
            if (mc.isMouseWithin(Bottom))
                cameraY -= scrollSpd;
            Thread.Sleep(10);
            ScrollMap();
        }

        private void DrawCameraCoords()
        {
            spriteBatch.DrawString(simple, "X: "+cameraX, new Vector2(5,5),Color.DarkCyan);
            spriteBatch.DrawString(simple, "Y: " + cameraY, new Vector2(5, 20), Color.DarkCyan);
        }

    }
}

2. Finding the tile coordinates.
I was thinking of a way to find the tile's array counterpart with the mouse's and camera's positions. I couldn't figure it out. Maybe you have an idea for this?  Undecided

3.How do I make the scrolling rectangles work when the game is fullscreen?  Undecided


Thanks in advance!  Smiley
« Last Edit: March 16, 2012, 08:46:02 AM by TerrorLT » Logged
Geeze
Level 5
*****


Totally.


View Profile
« Reply #1 on: March 16, 2012, 05:49:08 AM »

My bet is on multithreading.
Looks like camera position changes during rendering of the map, thus causing black lines.

[EDIT]
Simple fix should be that you store camera position in separate local varibles before render routine and use them in it instead using camera position directly. Boom. Problem solved(???)
Logged

TerrorLT
Level 0
*


View Profile
« Reply #2 on: March 16, 2012, 06:00:16 AM »

My bet is on multithreading.
Looks like camera position changes during rendering of the map, thus causing black lines.

[EDIT]
Simple fix should be that you store camera position in separate local varibles before render routine and use them in it instead using camera position directly. Boom. Problem solved(Huh?)

Thanks!
Creating new camera variables that update after rendering worked perfectly.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic