Quantcast
Channel: Community | MonoGame - Latest topics
Viewing all articles
Browse latest Browse all 6821

Having a problem with drawing a tiled map

$
0
0

@Steyrix wrote:

Hello again. I'm facing trouble with drawing a tiled map. The map is simply not drawn.
At first, I thought the problem is in my map, but when I tried to draw the map from platformer demo
the result was the same. Now I'm trying to use the code examples from platformer demo, but still can't succeed.

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using TheCastle0._1.GameStates;
using MonoGame.Extended.Content;
using MonoGame.Extended;
using MonoGame.Extended.Tiled;
using MonoGame.Extended.Graphics;
using MonoGame.Extended.ViewportAdapters;

namespace TheCastle0._1
{

public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    State CurrentState;

    //these all are for the test of tiled maps
    TiledMapRenderer renderer;
    TiledMap test;
    Camera2D cam;

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

    void SetState(State state)
    {
        
        CurrentState = state;
        CurrentState.Init(Content);
        
    }

    protected override void Initialize()
    {
        SetState(new MenuState());
        graphics.PreferredBackBufferWidth = 1920;
        graphics.PreferredBackBufferHeight = 1080;
        graphics.IsFullScreen = false;
        graphics.ApplyChanges();
        base.Initialize();
    }

    protected override void LoadContent()
    {
        var viewPortAdapter = new BoxingViewportAdapter(Window, GraphicsDevice, 800, 800);
        renderer = new TiledMapRenderer(GraphicsDevice);
        spriteBatch = new SpriteBatch(GraphicsDevice);
        test = Content.Load<TiledMap>("level-1");
        cam = new Camera2D(viewPortAdapter);
    }

    protected override void UnloadContent()
    {

    }

    KeyboardState oldState;

    protected override void Update(GameTime gameTime)
    {
        KeyboardState newState = Keyboard.GetState();

        var delta = (float)gameTime.ElapsedGameTime.TotalSeconds;

        CurrentState.Update(delta);

        if (CurrentState is MenuState && (newState.IsKeyDown(Keys.F) && oldState.IsKeyUp(Keys.F)))
            SetState(new Level1(renderer, GraphicsDevice));
        if (CurrentState is MenuState && (newState.IsKeyDown(Keys.Escape) && oldState.IsKeyUp(Keys.Escape)))
            this.Exit();
        if (CurrentState is Level1 && (newState.IsKeyDown(Keys.Escape) && oldState.IsKeyUp(Keys.Escape)))
            this.Exit();

        base.Update(gameTime);
        
        oldState = newState;
    }

    
    protected override void Draw(GameTime gameTime)
    {
        

        GraphicsDevice.Clear(Color.White);
       
        var viewMatrix = cam.GetViewMatrix();
        var projectionMatrix = Matrix.CreateOrthographicOffCenter(0, GraphicsDevice.Viewport.Width,
           GraphicsDevice.Viewport.Height, 0, 0f, -1f);
        var toDraw = test.GetLayer("solids"); //I tried drawing another layers too
        renderer.Draw(toDraw, ref viewMatrix, ref projectionMatrix, null, 1);
        //The code below doesn't work as well :(
        //renderer.Draw(test, ref viewMatrix, ref projectionMatrix, null, 0);
        //renderer.Draw(test, null, null, null, 0);
        
        //CurrentState.Draw(spriteBatch, graphics);
        
        base.Draw(gameTime);
    }
    
}
}`

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 6821

Trending Articles