Currently I have 4 different images that I am loading into 4 seperate BitmapTextureAtlas's.
What I would like to do is load 4 different images inside 1 BitMapTextureAtlas to try and optimize speed. I have edited the code to what I believe should have made it work however when this (Live wallpaper) loads the texture, the screen is just white. Here is what I have done so far:
onLoadResources()
Original BitmapTextureAtlas:
this.StarsAtlas = new BitmapTextureAtlas(1024, 1024, TextureOptions.DEFAULT);
this.SpaceAtlas = new BitmapTextureAtlas(1024, 1024, TextureOptions.DEFAULT);
this.CloudsAtlas = new BitmapTextureAtlas(1024, 1024, TextureOptions.DEFAULT);
this.SunetAtlas = new BitmapTextureAtlas(1024, 512, TextureOptions.DEFAULT);
I have condensed to:
this.skyAtlas = new BitmapTextureAtlas(1024, 4096, TextureOptions.BILINEAR);
Original Texture Regions:
this.LayerStars = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(this.StarsAtlas, this,
"stars.jpg", 0, 0); //568x518
this.LayerSpace = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(this.SpaceAtlas, this,
"space.jpg", 0, 0); //500x800
this.LayerClouds = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(this.CloudsAtlas, this,
"clouds.jpg", 0, 0); //600x478
this.LayerSunset = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(this.SunsetAtlas, this,
"sunset.jpg", 0, 0); //997x460
I have changed to:
this.LayerStars = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(this.skyAtlas, this,
"stars.jpg", 0, 0); //568x518
this.LayerSpace = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(this.skyAtlas, this,
"space.jpg", 0, 518); //500x800
this.LayerClouds = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(this.skyAtlas, this,
"clouds.jpg", 0, 1318); //600x478
this.LayerSunset = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(this.skyAtlas, this,
"sunset.jpg", 0, 1796); //997x460
Loading the texture:
this.mEngine.getTextureManager().loadTexture(
this.skyAtlas);
...Later on in the onLoadScene()
This stays the same:
Sprite starSprite = new Sprite(0, 0, 568, 518, this.LayerStars);
Sprite cloudSprite = new Sprite(0, 0, 600, 478, this.LayerClouds);
Sprite spaceSprite = new Sprite(0, 0, 500, 800, this.LayerSpace);
Sprite sunsetSprite = new Sprite(0, 0, 997, 460, this.LayerSunset);
Any idea what could be causing the white screen?
On a side note, it loaded fine when I had 4 different images in 4 separate BitmapTextureAtlas's.
The only thing I can see is that you are using different texture options. Have you tried TextureOptions.DEFAULT instead of TextureOptions.BILINEAR? Theturtleboy on the AndEngine forums had some problems with JPEGs using the same settings. See http://www.andengine.org/forums/development/jpeg-support-t26.html
Related
So I have a HUGE problem. I can't solve it for years so please anybody who can help - it would be amazing.
So the problem is with ColorMatrix. As you can see in the picture when I add the effect on the photo and save it to my memory I get that kind of picture with a little white line in the left. The funny fact is that when I increase contrast for example to 7 this line became more and more bigger. So the problem as I guess is with contrast and brightness. Anyway it works great and with anything else I'm satisfied...just only this one bug which will kill me someday I guess. Any help?
My code:
public void effect(View view) {
float contrast = 1;
float brightness = 0;
Bitmap.Config config = bmp.getConfig();
if (config == null) {
config = Bitmap.Config.ARGB_8888;
}
operation = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), config);
ColorMatrix cm = new ColorMatrix(new float[]
{
contrast, 0, 0, 0, brightness,
0, contrast, 0, 0, brightness,
0, 0, contrast, 0, brightness,
0, 0, 0, 1, 0,
0, 0, 0, 0, 1
});
Canvas canvas = new Canvas(operation);
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bmp, contrast, brightness, paint);
imageview.setImageBitmap(operation);
}
IMAGE OF BUG. The white line in the left
Anyone who will help with this. THANK YOU!
So I want to post the answer to my problem.
I was so dumb..- x and y have to be 0 0 instead of writing contrast and brightness.
Solved line:
canvas.drawBitmap(bmp, 0, 0, paint);
int img_w = 100;
int img_h = 50;
TextureRegion region = new TextureRegion(texture, 0, 0, img_w, img_h);
batch.draw(region, 0, 0);
Wouldn't it draw the full image and
TextureRegion region = new TextureRegion(texture, img_w / 2, 0, img_w / 2, img_h);
batch.draw(region, 0, 0);
the half of it?
Where I am wrong? Or there is a better solution to draw a half of image?
The problem was that I forgot about scaling.
The original image size was 100x50, but on display it should be 50x25 and after drawing a portion of a texture I need to recount new display size.
I am new at Canvas and drawing in Android and I just took the code from this website I managed to tweak it however, I am finding it difficult to change the border style and the border color.
I want the border to just be a simple black square border, no fancy waves and color spectrums.
This is the code in the onCreate function, I already managed to minimize the wavyness of the border but there's still tiny bumps and turns.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
float[] outerR = new float[] { 12, 12, 12, 12, 0, 0, 0, 0 };
RectF inset = new RectF(6, 6, 6, 6);
float[] innerR = new float[] { 12, 12, 0, 0, 12, 12, 0, 0 };
mDrawables = new ShapeDrawable[2];
mDrawables[0] = new ShapeDrawable(new RoundRectShape(outerR, inset, null));
//mDrawables[0].getPaint().setShader(makeSweep());
mDrawables[1] = new ShapeDrawable(new RoundRectShape(outerR, inset, innerR));
//mDrawables[1].getPaint().setShader(makeLinear());
PathEffect mPathEffect1 = new DiscretePathEffect(1, 1);
PathEffect mPathEffect2 = new CornerPathEffect(1);
mDrawables[0].getPaint().setPathEffect(new ComposePathEffect(mPathEffect2, mPathEffect1));
RelativeLayout layout = (RelativeLayout) findViewById(R.id.myDrawing);
layout.setBackgroundDrawable(mDrawables[0]);
activity = this;
//mView = new DrawingView(this);
mView = new SignatureView(activity, null);
layout.addView(mView, new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
init();
mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);
mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
}
Sorry I know this may turn out to be a simple task but I'm completely stumped.
EDIT - Okay I managed to make it plain black, I just removed the mDrawables[0/1].getPaint() lines and it turned to black, my only problem right now is to make it perfectly straight.
Figured it out.
For the color of the border, comment out the following lines:
mDrawables[0].getPaint().setShader(makeSweep()); and mDrawables[1].getPaint().setShader(makeLinear());
After that, the problem is the DiscretePathEffect. Change that line of code to:
PathEffect mPathEffect1 = new DiscretePathEffect(1, 0);
Apparently, the two arguments for the DiscretePathEffect determine how "squiggly" the line gets.
I am working in some game idea with Android and AndEngine, but I can't find a good tiling approach.
Some part of the game will consist on a rectangular grid. Three "styles" are possible, for each square side of the grid or the inner square. For simplicity we can think about gray, blue and red.
The problem is, when I think about making the sprite sheet, I'm not sure how to do it.
This is a quick (and bad drawing) of my first thoughts, being black the grid and green the cuts. Problem with this one is I would need to have up to 512 versions of the line crossing.
Is there a better approach? Can I do that without sprite sheets, just drawing lines and filling rectangles?
Sorry, I can't follow your thoughts completely. But, I understand you are handling a lot of squares and lines in different styles. And that's where you are right, you don't need any Sprites for that, AndEngine has some classes to draw simple things and it is way faster than Sprites.
basic example that reproduces your graphic with lines
// first the green lines (they are under the black ones)
Line[] greenLines = new Line[8];
// from (x0 ,y0) to (x1,y1) with lineWidth=5px
// the outer square
greenLines[0] = new Line(0, 0, 100, 0, 5, vertexBufferObjectManager); // top line
greenLines[1] = new Line(100, 0, 100, 100, 5, vertexBufferObjectManager); // right line
greenLines[2] = new Line(100, 100, 0, 100, 5, vertexBufferObjectManager); // bottom line
greenLines[3] = new Line(0, 100, 0, 0, 5, vertexBufferObjectManager); // left line
// inner horizontal lines
greenLines[4] = new Line(0, 33, 100, 33, 5, vertexBufferObjectManager);
greenLines[5] = new Line(0, 66, 100, 66, 5, vertexBufferObjectManager);
// inner vertical lines
greenLines[6] = new Line(33, 0, 33, 100, 5, vertexBufferObjectManager);
greenLines[7] = new Line(66, 0, 66, 100, 5, vertexBufferObjectManager);
// now the black lines
Line[] blackLines = new Line[4];
blackLines[0] = new Line(0, 15, 100, 15, 5, vertexBufferObjectManager);
blackLines[1] = new Line(0, 81, 100, 81, 5, vertexBufferObjectManager);
blackLines[2] = new Line(15, 0, 15, 100, 5, vertexBufferObjectManager);
blackLines[3] = new Line(81, 0, 81, 100, 5, vertexBufferObjectManager);
// now set the color and attach the lines to the scene (green)
for(Line line: greenLines){
line.setColor(0f,1f,0f);
myScene.attachChild(line);
}
// now set the color and attach the lines to the scene (black)
for(Line line: blackLines){
line.setColor(0f,0f,0f);
myScene.attachChild(line);
}
this above example should actually work. Now you only have to change it and adjust it to your needs. If you want to change a line you could call myLine.setPosition(fromX, fromY, toX, toY); oh and a rectangle is quite simple as well: Rectangle rectangle = new Rectangle(50,50,100,100, 5, vertexBufferObjectManager); for a rectangle that starts at (50,50) and is 100 pixels wide and 100 pixels high. And has a line width of 5 pixels. You can set the color of the Rectangle as you can for the lines. The only problem is, that the rectangle is always filled. If you want an empty rectangle you have to draw it with lines.
public Line buildGrid(int pWidth, int pHeight, float pRed, float pGreen, float pBlue){
Line grid = new Line(0, 0, 0, pHeight);
grid.setColor(0.5f, 0.5f, 0.5f);
int cont = 0;
while(cont < pWidth){
cont += 10;
grid.attachChild(new Line(cont, 0, cont, pHeight));
grid.getLastChild().setColor(pRed, pGreen, pBlue);
}
cont = 0;
while (cont < pHeight){
cont += 10;
grid.attachChild(new Line(0, cont, pWidth, cont));
grid.getLastChild().setColor(pRed, pGreen, pBlue);
}
return grid;
}
I'm using AndEngine to create a live wallpaper. However, in my onLoadResources() method I am currently loading 3 different textures:
#Override
public void onLoadResources() {
prefs = PhysicsWallpaperActivity.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
prefs.registerOnSharedPreferenceChangeListener(this);
this.mAutoParallaxBackgroundTexture = new BitmapTextureAtlas(2048, 2048, TextureOptions.DEFAULT);
this.mAutoParallaxImage1Texture = new BitmapTextureAtlas(2048, 2048, TextureOptions.DEFAULT);
this.mAutoParallaxImage2Texture = new BitmapTextureAtlas(2048, 2048, TextureOptions.DEFAULT);
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mParallaxLayerBackground = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "background.jpg", 0, 0);
this.mParallaxLayerImage1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxImage1Texture, this, "image1.jpg", 0, 0);
this.mParallaxLayerImage2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxImage2Texture, this, "image2.png", 0, 800);
this.mEngine.getTextureManager().loadTexture(this.mAutoParallaxBackgroundTexture);
this.mEngine.getTextureManager().loadTexture(this.mAutoParallaxImage1Texture);
this.mEngine.getTextureManager().loadTexture(this.mAutoParallaxImag2Texture);
}
I feel like this is not the most efficient way to do this. But if I have all my BitmapTextureAtlasTextureRegionFactorys 'images' pointing to just a single BitmapTextureAtlas, and those 'images' are at the same coordinates, the images will load together on top of each other even if I only call 1 of them.
An example of the problem is here:
#Override
public void onLoadResources() {
prefs = PhysicsWallpaperActivity.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
prefs.registerOnSharedPreferenceChangeListener(this);
this.mAutoParallaxBackgroundTexture = new BitmapTextureAtlas(2048, 2048, TextureOptions.DEFAULT);
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mParallaxLayerBackground = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "background.jpg", 0, 0);
this.mParallaxLayerImage1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "image1.jpg", 0, 0;
this.mParallaxLayerImage2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "image2.png", 0, 800);
this.mEngine.getTextureManager().loadTexture(this.mAutoParallaxBackgroundTexture);
}
^ I would have liked to use the code like this because it seems more efficient, but 'image1' and 'image2' automatically load on top of each other even if I only call 1 of them.
Am I doing this the right way? Or is there a more efficient way?
Your textures can contain more than one resource. Think of it like a sprite sheet. or like gluing photos to a big white sheet of paper. you do not need a new texture for each texture region. For example:
Say you have a star image, that is 250x250 pixels and a spaceship image that is 100x100 pixels. You could put both of these on a texture that measure 512x256.
You create a texture region for the star(250x250) at 0, 0.
Then create a second texture region for the spaceship at 251, 0.
In that scenario there is still some leftover area for other images blow the spaceship, if they are small enough to fit. The Image below shows how your texture would look if you could see it in memory.
Hope this helps.
Your Textures are huge! Are you very sure you need them that big?