Hello. I have used your program Realm Factory (thanks for making it free :) ) to make a level for my game, and have been able to draw it on the screen. i was wondering if it is possible to stretch the cells uniformly to fit the screen? I'd like to do this instead of making the screen resolution the size of the image. Thank you very much.
If you just want to stretch the images, and you're not concerned with maintaining aspect ratio of your cells (so that squares remain squares, instead of becoming rectangles, etc.), or if the aspect ratio of your window will always match the aspect ratio of your level, then this boils down to the following code.
(Note that this uses the calculated, full size of the level, that you asked for in your second question.)
protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); float width = level.Columns * level.CellSize.X; float height = level.Rows * level.CellSize.Y; float columnScale = level.CellSize.X * graphics.PreferredBackBufferWidth / width; float rowScale = level.CellSize.Y * graphics.PreferredBackBufferHeight / height; for (int row = 0; row < level.Rows; row++) { for (int column = 0; column < level.Columns; column++) { Tile tile = (Tile)level.GetTile(row, column); if (tile == null) { continue; } Texture2D texture = tile.Texture; spriteBatch.Draw(texture, new Rectangle( (int)(column * columnScale), (int)(row * rowScale), (int)(columnScale), (int)(rowScale)), Color.White); } } spriteBatch.End(); base.Draw(gameTime); }
I know that might be a lot to chew on. Give it a try and see if it works for you. If you have other questions about what this does or why, feel free to ask!
OP here. Forgot I already had an account…
I'd like to add, is there a way to determine the size of the level in pixels after loading it into the game?
EDIT: I think I figured out how to do that. I noticed the .realm file has the default tile size, which I could use to determine the level size. Still not sure hoe I could strecth the cells without editing the .realm file though
Assuming you've already got an instance of your Level that you're trying to draw, the total size of the level could be calculated like this:
float width = level.Columns * level.CellSize.X; float height = level.Rows * level.CellSize.Y;
That's the full width and height, in pixels, of the level by default. (You can of course, scale it to be larger or smaller when you draw it, but that's the default size.)
Thank you very much! This is very helpful.
Is there a way I could use 2 levels, and have one as the background for my game, and the other as the playable level? What I mean is, I'd like to have the background's cell sizes to be different from the playable level.