Optimal referential 2d array? - arrays

So, I'm creating a fairly basic overhead 2d game where users can "draw" a map. (Actually, they dont draw it, the manually input a list of x/y's, but the design aspect isnt important just now.)
When a new tile is added, that tile goes into an array of all tiles ingame.
The centrepoint is 0,0. Tiles can be added in all directions, so may be at 1,1 or 100,100 or -50,-50.
Sometimes I want to determine what tile is at a location. One (imho bad) way of doing this would be to get the x/y and loop through all tiles and check if they are that location.
The way I'm currently doing it is to have a seperate 2d array of null elements, and when a tile is added, its set at that array. (ie tilemap[10][10] = tile[100])
Of course, because the values can go negative, tilemap [0][0] is actually the -1000/-1000 tile. (chosen as an arbitrary limit)
Is there a better way of doing this? I feel like using a massive array of mostly empty objects could be more optimal.
Many thanks.

One possible solution would be to keep an Object or Dictionary where the key is the x/y location.
So if you're adding a tile at 10,20, you can store it in the Object like:
this.m_objs[tileX + "_" + tileY] = new Tile;
So if you want to check if something's at position 10,20 you can by using something like:
public function checkIfExists( x:int, y:int ):void
{
return ( this.m_objs[x + "_" + y] != undefined );
}

I recomend using Dictionary, as searching in it much more faster then searching array. Judhing by possible coords -1000/-1000 it would be a great advantage!

Related

replacing CALayer arrays with CAMetalLayer arrays

I have a painting app which at any given time interactively shows content from an array of 200 or so CALayers via an UIImageView. I get reasonable performance, but I'm wondering if there could be any performance benefits with using CAMetalLayers instead. In particular, I'm curious if I could benefit from blitting textures directly to each CAMetalLayer, and would there be any hardware considerations with stacking/displaying so many CAMetalLayers at once.
Are there any gotchas I should consider before implementing, and should I continue using an UIImageView (or other) to host these newly Metal-backed sublayers? Any thoughts would be appreciated.
That’s not going to work. You should be keeping track of your stroke’s data. For example an array of points would be a single stroke and then you should have an array of those strokes. It could be only points (x, y) or more probably also containing color, size and other variables. You should know what do you need to describe your stroke.
Then use that to draw (stamp at those locations). When you want to undo, just start drawing from the beginning all the strokes in the array until n-1, n-2, etc...

Swift 3 - Function to create n number of sprites with random x/y coordinates

I am trying to create multiple SKSpriteNodes that each have their own independent variables that I can change/modify. I would like to be able to run a function when the app starts, for example "createSprites(5)" which would create 5 sprites with the image/texture "shape.png" at random x and y coordinates and add all 5 Sprites to an array that I can access and edit different Sprite's positioning based on the index value. I would then like to be able to have another function "addSprite()" which, each time it is called, create a new Sprite with the same "shape.png" texture, place it at another random X and Y coordinate and also add it to the array of all Sprites to, again, be able to access later and change coordinates etc.
I have been looking through so many other Stack Overflow pages and can not seem to find a solution. My ideal solution would simply be the two functions I stated earlier. One to create an "n" number of Sprites and another function to create and add one more sprite to the array each time it is called.
Hope that makes sense, I'm fairly new to Swift and all this Sprite stuff, so simple informative answers would be very much appreciated.
You're not going to find an ideal solution from the past because nobody has likely had exactly the same desire with both Swift and SpriteKit. Having said that, there's likely partial answers you can blend together, and get the result you want or, at least, an understanding of how to do it.
Sprite Positioning in SK is probably the first thing to read up on:
https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Sprites/Sprites.html
having gotten that figured out, you can move to random positions.
Random positioning of Sprites:
Duplicate Sprite in Random Positions with SpriteKit
Sprite Kit random positions
Both use earlier versions of randomisation that aren't as powerful as what's available now, in GameplayKit. So... Generating random numbers in Swift with GameplayKit:
https://www.hackingwithswift.com/read/35/overview
It's hard to overstate the importance of understanding the various possibilities of game design implications of varying types of randomisation, so probably wise to read this, from Apple:
https://developer.apple.com/library/content/documentation/General/Conceptual/GameplayKit_Guide/RandomSources.html
After that, it's a case of needing to determine what constitutes a time or event at which to create more sprites at more random positions, and how fussy you want to be about proximity to other sprites, and overlaps.

How do load an image into a 2D array element?

I'm currently doing my A-Level Computing project for which I am making my own version of the classic game Space Invaders.
To create the wave of space invaders I want to use a 2D array of images, where the images are loaded from a disk and then displayed on the form but I am unsure of how load the images into the array and then display the array on a form.
The current arrays are:
ImagePaths:array [1..3] of string =('SpaceInvader1.jpg', 'SpaceInvader2.jpg', 'SpaceInvader3.jpg');
Wave:array[1..11, 1..5] of TImage; x,y:integer;
What I would like to know is: how would I load an image into an array element? eg how would I load 'SpaceInvader1.jpg' to array element [1,1]?
Any help would be greatly appreciated.
If you're going to be coding graphical animations, you probably won't want to do it directly on the form. Trying to move things around can be tricky, especially if you want to animate smoothly and not get a lot of flicker and graphical artifacts.
It would be better to use a rendering library. The workflow goes like this:
Create a rendering context on the form. This is a control that provides a surface for graphical animations to run on.
Load your three images into memory.
Create an array of objects to represent the game data associated with your monsters. (Position, movement direction and speed, etc.) It can be flat or 2D, whichever works better for you.
Set up a render loop that goes like this:
For each monster in the array, draw it at its position.
Draw the player's ship and all projectiles.
Check for collisions and handle appropriately.
Check for player input and handle appropriately.
You can find plenty of information on rendering libraries for Delphi in the forums at Pascal Game Development.
You have the following declarations:
ImagePaths:array [1..3] of string =(
'SpaceInvader1.jpg',
'SpaceInvader2.jpg',
'SpaceInvader3.jpg'
);
Wave: array[1..11, 1..5] of TImage;
You want to know how to populate the array of images. It is quite wasteful to create 55 images when 3 will suffice. So instead of that, use indirection. Store references to the images. And TImage is a visual component, and so not appropriate for a sprite.
I would hold the images in an array like this:
Sprites: array [1..3] of TBitmap;
And populate it
JPEGImage := TJPEGImage.Create;
try
for i := 1 to 3 do
begin
JPEGImage.LoadFromFile(ImagePaths[i]);
Sprites[i] := TBitmap.Create;
Sprites[i].Assign(JPEGImage);
end;
finally
JPEGImage.Free;
end;
Then populate your Wave array like this, for example:
Wave: array[1..11, 1..5] of TBitmap;
....
for i := 1 to 11 do
for j := 1 to 5 do
Wave[i,j] := Sprites[1];// or whichever sprite you want
Of course your sprites may be better with a real name rather than in an array.
Some other comments:
JPEG is a bad format for a game sprite. It is a lossy format. A plain Windows bitmap would be fine, as would a GIF or PNG.
I'd much rather see the images as embedded resources. Then your executable can stand alone.
I'd also far rather see your Waves array holding the state of each invader. And then you would create a function that would render that state onto a canvas.

Referring to a two dimensional array - rows and columns

I have a two dimensional array and I want to know how I can refer to the rows and columns in it. Do I use [row,column] or [column,row]? I also have some graphics. Do I calculate the (x,y) coordinate set of each graphic with (row*size,col*size) or with (col*size,row*size)?
The whole two dimensional array is the building instructions for the grid.
Each element in the array refers to a piece of the grid.
I know how to construct this grid and its pieces and I know how to access and manipulate the array's elements.
The problem is that when I construct the grid I have to calculate the x and y coordinate of each piece, but I just don't know if my variable curRow should be used for the x or y coordinate. It's similarly with the variable curCol.
My code is working, but it confuses me.
I think of it like the rows control the y coordinates and the columns control the x coordinates, because I just learned of the way matrices are referred to.
I ask, because it came to my mind that I am unsure of how to do this.
In the past I have used [row,column] to loop and (row*size,col*size) to position.
The code so far is:
function buildGrid(gridInfo:Array):Sprite {
var displaySprite:Sprite = new Sprite();
for(var curRow:uint=0;curRow<gridInfo.length;curRow++) {
for(var curCol:uint=0;curCol<gridInfo[curRow].length;curCol++) {
var infoRef:Object = gridInfo[curRow][curCol];//create reference for fast access
var pieceGraphic:Shape = new Shape();
pieceGraphic.graphics.beginFill(infoRef.fillColor);
pieceGraphic.graphics.lineStyle(infoRef.borderThickness,infoRef.theBorderColor);
pieceGraphic.graphics.drawRect(0,0,infoRef.sideLength,infoRef.sideLength);
pieceGraphic.graphics.endFill();
pieceGraphic.x = curRow*(infoRef.sideLength+infoRef.spaceX);//later use of graphic requires known x
pieceGraphic.y = curCol*(infoRef.sideLength+infoRef.spaceY);//later use of graphic requires known y
displaySprite.addChild(pieceGraphic);
}
}
return displaySprite;
}
It's correct to refer to a two dimensional array with [row,column], but it's easier with other variable names like curX and curY. It's important to be consistent of what the indexes mean throughout the project and what indexes are used. To access the first elements in the array create a loop for rows or for curX and for the elements in the second dimension create a loop for columns or for curY.
You can calculate coordinate sets (x,y) for graphics in a grid with (row*size,col*size) or (curX*size,curY*size), so your code is correct and doesn't need to be changed.
Remember that rows are horizontal and columns are vertical.

Recognizing tetris pieces in C

I have to make an application that recognizes inside an black and white image a piece of tetris given by the user. I read the image to analyze into an array.
How can I do something like this using C?
Assuming that you already loaded the images into arrays, what about using regular expressions?
You don't need exact shape matching but approximately, so why not give it a try!
Edit: I downloaded your doc file. You must identify a random pattern among random figures on a 2D array so regex isn't suitable for this problem, lets say that's the bad news. The good news is that your homework is not exactly image processing, and it's much easier.
It's your homework so I won't create the code for you but I can give you directions.
You need a routine that can create a new piece from the original pattern/piece rotated. (note: with piece I mean the 4x4 square - all the cells of it)
You need a routine that checks if a piece matches an area from the 2D image at position x,y - the matching area would have corners (x-2, y-2, x+1, y+1).
You search by checking every image position (x,y) for a match.
Since you must use parallelism you can create 4 threads and assign to each thread a different rotation to search.
You might not want to implement that from scratch (unless required, of course) ... I'd recommend looking for a suitable library. I've heard that OpenCV is good, but never done any work with machine vision myself so I haven't tested it.
Search for connected components (i.e. using depth-first search; you might want to avoid recursion if efficiency is an issue; use your own stack instead). The largest connected component should be your tetris piece. You can then further analyze it (using the shape, the size or some kind of border description)
Looking at the shapes given for tetris pieces in Wikipedia, called "I,J,L,O,S,T,Z", it seems that the ratios of the sides of the bounding box (easy to find given a binary image and C) reveal whether you have I (4:1) or O (1:1); the other shapes are 2:3.
To detect which of the remaining shapes you have (J,L,S,T, or Z), it looks like you could collect the length and position of the shape's edges that fall on the bounding box's edges. Thus, T would show 3 and 1 along the 3-sides, and 1 and 1 along the 2 sides. Keeping track of the positions helps distinguish J from L, S from Z.

Resources