How can I implement custom maps with provinces in Android? - java

Ok, so I have this map and I have various sliders on the right. After changing slider values and pressing 'Execute' button, some provinces in the map below should change colour.
However, I don't know how to implement the map below. I have used 33 png drawable for each province. I have set them all to have a same big rectangle dimension so that they'd align themselves.
I am getting an 'Out of memory on byte allocation' error.
I assume this is because of all the large drawables I have.
I'm new to android and I want to ask, is there a way to implement this without the error?
Also the map should always be displayed on the left side of the screen so the images always have to be visible.

I would recommend making a SVG and changing the colors programmatically.
Graphics are hard to scale and are heavy space users, scalable graphics are slim, look great everywhere (device size and dpi) and easy to manage (single file instead of 33).

Related

Confused with image scaling and positioning in libgdx

I'm having quite a bit of difficulty wrapping my head around the actual display side of things with libgdx. That is, it just seems fairly jumbled in terms of what needs to be done in order to actually put something up onto the screen. I guess my confusion can sort of be separated into two parts:
What exactly needs to be done in terms of creating an image? There's
Texture, TextureRegion, TextureAtlas, Sprite, Batch, and probably a
few other art related assets that I'm missing. How do these all
relate and tie into each other? What's the "production chain" among
these I guess would be a way of putting it.
In terms of putting
whatever is created from the stuff above onto the monitor or
display, how do the different coordinate and sizing measures relate
and translate to and from each other? Say there's some image X that
I want to put on the screen. IT's got it's own set of dimensions and
coordinates, but then there's also a viewport size (is there a
viewport position?) and a camera position (is there a camera size?).
On top of all that, there's also the overall dispaly size that's
from Gdx.graphics. A few examples of things I might want to do could
be as follow:
X is my "global map" that is bigger than my screen
size. I want to be able to scroll/pan across it. What are the
coordinates/positions I should use when displaying it?
Y is bigger
than my screen size. I want to scale it down and have it always be
in the center of the screen/display. What scaling factor do I use
here, and which coordinates/positions?
Z is smaller than my screen
size. I want to stick it in the upper left corner of my screen and
have it "stick" to the global map I mentioned earlier. Which
positioning system do I use?
Sorry if that was a bunch of stuff... I guess the tl;dr of that second part is just which set of positions/coordinates, sizes, and scales am I supposed to do everything in terms of?
I know this might be a lot to ask at once, and I also know that most of this stuff can be found online, but after sifting through tutorial after tutorial, I can't seem to get a straight answer as to how these things all relate to each other. Any help would be appreciated.
Texture is essentially the raw image data.
TextureRegion allows you to grab smaller areas from a larger texture. For example, it is common practice to pack all of the images for your game/app into a single large texture (the LibGDX “TexturePacker” is a separate program that does this) and then use regions of the larger texture for your individual graphics. This is done because switching textures is a heavy and slow operation and you want to minimize this process.
When you pack your images into a single large image with the TexturePacker it creates a “.atlas” file which stores the names and locations of your individual images. TextureAtlas allows you to load the .atlas file and then extract your original images to use in your program.
Sprite adds position and color capabilities to the texture. Notice that the Texture API has no methods for setting/getting position or color. Sprites will be your characters and other objects that you can actually move around and position on the screen.
Batch/SpriteBatch is an efficient way of drawing multiple sprites to the screen. Instead of making drawing calls for each sprite one at a time the Batch does multiple drawing calls at once.
And hopefully I’m not adding to the confusion, but another I option I really like is using the “Actor” and “Stage” classes over the “Sprite” and “SpriteBatch” classes. Actor is similar to Sprite but adds additional functionality for moving/animating, via the act method. The Stage replaces the SpriteBatch as it uses its own internal SpriteBatch so you do not need to use the SpriteBatch explicitly.
There is also an entire set of UI components (table, button, textfield, slider, progress bar, etc) which are all based off of Actor and work with the Stage.
I can’t really help with question 2. I stick to UI-based apps, so I don’t know the best practices for working with large game worlds. But hopefully someone more knowledgeable in that area can help you with that.
This was to long to reply as a comment so I’m responding as another answer...
I think both Sprite/SpriteBatch and Actor/Stage are equally powerful as you can still animate and move with Sprite/SpriteBatch, but Actor/Stage is easier to work with. The stage has two methods called “act” and “draw” which allows the stage to update and draw every actor it contains very easily. You override the act method for each of your actors to specify what kind of action you want it to do. Look up a few different tutorials for Stage/Actor with sample code and it should become clear how to use it.
Also, I was slightly incorrect before that “Actor” is equivalent to Sprite, because Sprite includes a texture, but Actor by itself does not have any kind of graphical component. There is an extension of Actor called “Image” that includes a Drawable, so the Image class is actually the equivalent to Sprite. Actor is the base class that provides the methods for acting (or “updating”), but it doesn’t have to be graphical. I've used Actors for other purposes such as triggering audio sounds at specific times.
Atlas creates the large Texture containing all of your png files and then allows you to get regions from it for individual png's. So the pipeline for getting a specific png graphic would be Atlas > Region > Sprite/Image. Both Image and Sprite classes have constructors that take a region.

Storing ImageIcons

I got me 90 odd images that form an explosion animation. When I come to 'play' the animation, I use a Swing Timer to set a new Icon for a JLabel, and cycle through until I reach the last image.
My question is this: is it better to have loaded and stored all the ImageIcons in some static form somewhere, or load them from file every time I need them?
Explosions are quite common in the game, and I have two sizes of them (scaling each image for every size of explosion seemed rather inefficient when I want them to swap very quickly) which makes for 200 images (roundabout).
Store or load?
For a game, using Swing components to represent game objects (Sprites) is probably not the best way. I would use a single JPanel as the rendering component and override its paintComponent() method to do rendering for my own game objects.
This way you avoid dealing with Swing components intricacies (e.g. they may add unwanted gaps / offsets where they really paint, as well they make it harder to control the painting order - important when two objects overlap).
I wouldn't use any type of Icon, just use java.awt.Image, or you own type to represent an image (e.g. to render part of an underlying image - as sprite sheet requires). Icon doesn't really add any value for a game.
If the image data fits easily into memory, I would load all the images before even starting. If there are too many images to preload them all, I would use some sort of cache that retains as many images as possible - the cache may use an LRU based scheme to decide which images to retain when it gets full.
I use a SwingTimer to set a new Icon for a JLabel, and cycle through until I reach the last image.
You may want to consider using an Animated Icon. It can be used anywhere an Icon can be used.
The AnimatedIcon class uses loaded Icons, so I guess my answer would be to pre-load for ease of use and CPU efficiency.

Libgdx Pixellated GUI Effect

In LibGDX, I am currently trying to achieve the effect of a pixellated GUI, meaning the buttons are pixellated, like an 8-bit theme.
When I make my GUI elements, such as a TextButton, I am using images that are small, say 34x16 pixels, but I am rendering them on a large resolution like 1920x1080. What is the best way to render such a small image at a high resolution?
My thoughts were to use stage.setViewport(WIDTH,HEIGHT), and set the width and height to a scaled down resolution, so the gui elements wouldn't be so big. This works, however when adding elements things go wrong.
This my stage/table currently, using a scaled down resolution. I am adding widgets using to the table like this:
table.add(playButton);
table.row();
table.add(title);
but as you can see, the table.row() makes a row that is much too large, perhaps a row fit for a larger resolution. Is there any way to change the gap that is made by the row? I would have thought that the gap would be zero, and by using table.add().pad() you could change the gap, but I am not doing any of that.
Any help would be greatly appreciated.
Ninepatch
This is a common issue in all apps. To overcome this, images which can be safely resized in parts are used. they are called ninepatches.
Here is a nice Tutorial about using them in libgdx.
Distance Field Fonts
Although you haven't mentioned it here, you'd also find font sizing (pixellated fonts) as an issue. For that Distance Field Fonts are used.
Hope this helps.
I would say don't worry about scaling them up and making the virtual resolution bigger. If you want to see picture still pixelated when you scale it use Texture filter. For your case you want to use Nearest filter. use it like this:
yourTexture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
where yourTexture is the texture that you have all your bitmaps and skin elements on. If you are using texturePacker than you can specify the filter before packing too. Or just open the .pack file and on the top you will see filtering options, and edit those to Nearest.

how to get a region out of an image?

Lets say I use a black key jpg image as my layout in Java Code. Is there an easy way to get the region out of an image so I can use region.Contains() for my onTouchListener?
I've looked for them and there isn't any. I ask this because Im making a piano app and would like to get the regions of the images I use for the black/white keys.
Rather than use an image, try rendering the piano keys yourself, and check if input is in the black key regions you render, rather than using an image.
If you really want to use the image, define the black regions within the image by hand.
Detecting regions in an image is a difficult problem, one you probably don't want to get into for such a simple purpose. If you had many images, or had to do this frequently it might be worth the headache but as you presumably don't... don't worry about it.

android opengl game

I'm trying to develop a 2D game to android using opengl.
I know how to print images on the screen and animate them. But in my game I have a map and a want to zoom in and out and scroll the map. But I can't figure out the best way of doing it.
Can anybody help me?
I don't have any api examples but I did games design at college so I'll give my two bits.
The method you use will depend on your map style, size, functionality and format.
For example if you are looking for a very static non changing map, use a simple picture image. You can use the API frame for a picture view, enabling you to zoom in and out as you do in the gallery and to scroll on zoomed images, or in this case, zoom locations on your map.
Alternatively, if your map is based off a tiling system, a good example of this is the original Pokémon and Legend of Zelda games from the old game boy, then each area stores a tile 'thumbnail' for itself as a bitmap. These are then put into their appropriate locations on a grid depending on what areas are discovered.
This is the probably the most flexible way to build your map as you are not relying on a set bitmap for the entirety your map meaning it can change its look efficiently; you can build it as desired to show areas of choice (useful for if the map only reveals places the gamer has covered) and it also means you can do tile based overlay:
ie - if a certain area should contain treasure, theres a treasure icon overlayed on that tiles x,y position on the map grid.
I used the tiling option in my game projects from college and it made everything else map related easier. It also made the map side of things smaller storage wise.
The simplest approach would be to just call glTranslatef(-scrollX,-scrollY,0) followed by glScalef(zoom,zoom,zoom) before you render your map.

Categories

Resources