Having a problem with drawing a tiled map

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);
    }
    
}
}`

Set the mutable render pipeline state you want before drawing.

GraphicsDevice.BlendState = BlendState.AlphaBlend

@LithiumToast It doesn’t help. I set the state in Initialize method, but still can’t draw the map

Should be done immediately before drawing the layer map itself. SpriteBatch or other rendering code can change the state.

@LithiumToast
I’m trying it right now, but still I haven’t succeed.

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 backgr = test.GetLayer("background");
        var solids = test.GetLayer("solids");
        GraphicsDevice.BlendState = BlendState.AlphaBlend;
        renderer.Draw(backgr, ref viewMatrix, ref projectionMatrix, null, 0);
        renderer.Draw(solids, ref viewMatrix, ref projectionMatrix, null, 0);
        //CurrentState.Draw(spriteBatch, graphics); 
}

Take a look at the demo to see how things should work.

@LithiumToast i tried code from demo, but still the map is not drawn. I’m using monogame 3.6 and Monogame.Extended from github repository (built from source).
Clearly, can’t figure out the root of the issue.

What version of Tiled do you have installed? What version of the repository are you using? Are you develop branch? Can you run the demo? Does the demo maps render correctly?

@LithiumToast I currently have Tiled 0.18.2 installed. I’m using 1d55e72 version of github repository, develop branch.
I can run the Demo.Features.Windows. The Tiled Map demo seems to be rendering tiled map correctly.
But, when I try to run the Demo.Platformer, it throws an unexpected exception.

The Platformer is temporarily broken because it used the old ECS. It has very little to do with this problem of rendering Tiled maps anyways; the Tiled maps demo is enough to isolate the problem.

Which you say do render correctly, so I’m not sure why your map doesn’t work if you use the same pattern for rendering found in the demo. Could you post the map you are working with here?

@LithiumToast currently I’m trying to render the map from tiled maps demo “level01”, so I don’t think that there is much point to post it. It seems like there are some problems in code that I can’t see, eventhough I’m using the code samples from demo.

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


namespace TheCastle0._1
{

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

    private Effect _customEffect;
    //these all are for the test of tiled maps
    private TiledMapRenderer renderer;
    private TiledMap test;
    private 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());
     
       
        var viewPortAdapter = new BoxingViewportAdapter(Window, GraphicsDevice, 1024, 768);
        cam = new Camera2D(viewPortAdapter);
        renderer = new TiledMapRenderer(GraphicsDevice);

        Window.AllowUserResizing = true;
        base.Initialize();
    }

    protected override void LoadContent()
    {
        
        
        spriteBatch = new SpriteBatch(GraphicsDevice);
        
        test = Content.Load<TiledMap>("level01");

        cam.LookAt(new Vector2(test.WidthInPixels, test.HeightInPixels) * 0.5f);

        var effect = new CustomEffect(GraphicsDevice)
        {
            Alpha = 0.5f,
            TextureEnabled = true,
            VertexColorEnabled = false
        };

        _customEffect = effect;
    }


    protected override void UnloadContent()
    {

    }

    KeyboardState oldState;

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

        var delta = (float)gameTime.ElapsedGameTime.TotalSeconds;
        renderer.Update(test, gameTime);
        CurrentState.Update(delta);

        if (CurrentState is MenuState && (newState.IsKeyDown(Keys.F) && oldState.IsKeyUp(Keys.F)))
            SetState(new Level1(renderer));
        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.CornflowerBlue);

        GraphicsDevice.BlendState = BlendState.AlphaBlend;
        GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
        GraphicsDevice.RasterizerState = RasterizerState.CullNone;

        var viewMatrix = cam.GetViewMatrix();
        var projectionMatrix = Matrix.CreateOrthographicOffCenter(0, GraphicsDevice.Viewport.Width,
           GraphicsDevice.Viewport.Height, 0, 0f, -1f);
         renderer.Draw(test, ref viewMatrix, ref projectionMatrix, _customEffect, 0);

        //CurrentState.Draw(spriteBatch, graphics);
    }
    
}

public class CustomEffect : DefaultEffect, ITiledMapEffect
{
    public CustomEffect(GraphicsDevice graphicsDevice)
        : base(graphicsDevice)
    {
    }

    public CustomEffect(GraphicsDevice graphicsDevice, byte[] byteCode)
        : base(graphicsDevice, byteCode)
    {

    }

    public CustomEffect(Effect cloneSource)
        : base(cloneSource)
    {
    }
}
}

I can’t really see the problem. Try setting Alpha to 1 just be sure. Also try removing the camera maybe to see if it just rendered beyond the viewport. You can use Matrix.Identity for the view matrix.
Other than that I would need to get a copy of the project and load it up myself to see where the problem is. If you want to do that you can send me a private message.

So digging deeper into this, it seems to be a problem with DirectX. OpenGL works as expected. Looking into it. If you want to continue on your game meanwhile you can always use the DesktopGL dependencies of MonoGame.

@Steyrix So turns out there was a small mistake in the shader file that was causing problems for DirectX. I opened a [pull request that fixes this problem] (https://github.com/craftworkgames/MonoGame.Extended/pull/384). While waiting for merge I suggest using OpenGL which does work correctly. Thanks for reporting this!

@LithiumToast Thanks! So now I pulled git repository and rereferenced Monogame.Extended, but still haven’t got the wanted result (Maybe I’m doing something wrong again). I think it’s better for me to use OpenGL, how do I do this in monogame? (I’m quite a newbie in this field)
UPD: I found out how to use GL. But it would be nice if you could tell me what changes i need to make to get my current project working (just for general knowledge, OpenGL version of my project just works fine)