How to resize initialized image with allegro5 in initializition part of game?
ALLEGRO_BITMAP *player = NULL;
player = al_load_bitmap("st.png");
//something
al_draw_bitmap(player, player_position_x, player_position_y, 0);
Instead of al_draw_bitmap() use:
void al_draw_scaled_bitmap(ALLEGRO_BITMAP *bitmap,
float sx, float sy, float sw, float sh,
float dx, float dy, float dw, float dh, int flags);
Related
I am having a hard time figuring out how to find correct rect that represents a ui element area. I get the Ui element RectTransform. I tried to use it directly, using a function and RectTransformUtility, but nothing seems to wok.
Here is the code.
RenderTexture.active = scr;
//adjust rect position
//Rect rect = RectUtils.RectTransformToScreenSpace (rectTransform);
int width = System.Convert.ToInt32 (rect.width);
int height = System.Convert.ToInt32 (rect.height);
// Create a texture the size of the screen, RGB24 format
Texture2D tex = new Texture2D(width, height, TextureFormat.RGBA32, false);
RenderTexture.active = scr;
// Read screen contents into the texture
tex.ReadPixels(rect, 0, 0);
tex.Apply();
RenderTexture.active = null;
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();
//todo destroy the texture
Object.Destroy (tex);
// For testing purposes, also write to a file in the project folder
File.WriteAllBytes(path, bytes);
I tried to create rect in different ways like:
Methood 1:
Vector2 size = Vector2.Scale(transform.rect.size, transform.lossyScale);
Rect rect = new Rect(transform.position.x, Screen.height - transform.position.y, size.x, size.y);
rect.x -= (transform.pivot.x * size.x);
rect.y -= ((1.0f - transform.pivot.y) * size.y);
return rect;
Method 2:
Vector2 temp = rectT.transform.position;
var startX = temp.x - width/2;
var startY = temp.y - height/2;
var tex = new Texture2D (width, height, TextureFormat.RGB24, false);
tex.ReadPixels (new Rect(startX, startY, width, height), 0, 0);
I am using Unity 5.5 Mac version.
when i pass to ReadPixels new Rect (0, 0, Screen.width, Screen.height) i see the whole RenderTexture in its defined dimensions 1920x1080
I want to use this as a baker of UI elements as I am having performance issue, and definitively no other solution to implement the required behavior.
I also struggled with this problem. Here's how I solved it.
First we need the position of RectTransform in screen and for this I used RectTransformUtility. Then since the rect x, y is the starting point at left bottom (note: in GUI x, y is at left top) we subtract respectively the half of width and height.
Vector2 rectPos = RectTransformUtility.WorldToScreenPoint (Camera.main, rectTransform.position);
float startX = rectPos.x - width / 2;
float startY = rectPos.y - height / 2;
Rect screenRect = new Rect (startX, startY, width, height);
Note: width height should be integer.
Edit: The above solution when we use Screen overlay and change resolution from standard to any other do not work. Even if I used the lossyscale I could not get consistent behavior.
One method of RecTransformUtility is really helpful for this situation (WorldToScreenPoint).
Vector3[] bounds = new Vector3[4];
rectTransform.GetWorldCorners (bounds);
Vector2 rectPos = RectTransformUtility.WorldToScreenPoint (camera, rectTransform.position);
Vector2 minPosition = RectTransformUtility.WorldToScreenPoint (camera, bounds[0]);
Vector2 maxPosition = RectTransformUtility.WorldToScreenPoint (camera, bounds[2]);
Vector2 size = maxPosition - minPosition;
float startX = rectPos.x - size.x / 2;
float startY = rectPos.y - size.y / 2;
return new Rect (startX, startY, size.x, size.y);
I have several object rotating by itself in FixedUpdate().
Now I need to track rotation of one object, lets call it objX. Rotation goes only from 0 to 360 when I retrieve it. Is it possible to get rotation after 360 degrees?
For example when I use something like
float x = objX.transform.rotation.z;
variable x should be 560 degrees.
Is something like that possible?
Here is the way to track rotation.
Vector3 mouseClickPos;
float angle;
float lastAngle;
int fullRotations;
public float realAngle;
void FixedUpdate(){
mouseClickPos = Input.mousePosition;
Vector3 dir = mouseClickPos - Camera.main.WorldToScreenPoint(transform.position);
angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
angle-=90;
if(lastAngle - angle > 270){
fullRotations ++;
}else if(angle - lastAngle > 270){
fullRotations --;
}
Debug.Log(360*fullRotations + angle);
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.RotateTowards(transform.rotation, q, 6);
lastAngle = angle;
}
I'm trying to calculate the angle of the click i am making in relationship to the middle of the screen. But maybe i am confused on how atanf is suppsoed to work.
CGPoint pt = [self convertTouchToNodeSpace:[touches anyObject]];
float adj = pt.x - 512;
float opposite = pt.y - 384;
float combined = opposite / adj;
float tan = atanf(combined);
but when i try to NSLog Tan, i just get some giant number like 0.1253649
thoughts?
The right way to convert vector to angle is through atan2 function:
float angle = atan2f (pt.y - 384, pt.x - 512) * 180 / PI;
PS: Are you using cocos2d engine? It has ccpToAngle(...) function.
i am following a tutorial on a simple cocos2d game.
however on that tutorial the bullets that the user fires is only on one direction
what can i do to make it fire on all directions not just one sided?
here is the code of the direction.
int offX = location.x - projectile.position.x;
int offY = location.y - projectile.position.y;
[self addChild:projectile];
int realX = winSize.width + (projectile.contentSize.width/2);
float ratio = (float) offY / (float) offX;
int realY = (realX *ratio) + projectile.position.y;
CGPoint realDest = ccp(realX, realY);
int offRealX = realX - projectile.position.x;
int offRealY = realY - projectile.position.y;
float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
float velocity = 480/1;
float realMoveDuration = length/velocity;
[projectile runAction:[Sequence actions:[MoveTo actionWithDuration:realMoveDuration position:realDest],
[CallFuncN actionWithTarget:self selector:#selector(spriteMoveFinished:)], nil]];
all help will be greatly appreciated. Thanks
Assuming you are creating the projectile at the location of your character, you just need to figure out the direction before calculating the end point.
After adding the projectile:
[self addChild:projectile];
Add a scalar float:
float scalarX = 1.0f;
And make it negative if the touch is left of the character:
if (offX < 0.0f) scalar = -1.0f;
Then just multiply the realX by this scalar to make it point the correct way
int realX = scalar * (winSize.width + (projectile.contentSize.width/2));
I am trying to convert a double to radians using the Objective c function toRadians like this:
double oldLat = toRadians(oldLocation.coordinate.latitude);
but I keep getting a compiler warning and error or implicit declaration of toRadians and toDegrees
I included #import <math.h> but that didn't solve the problem for some reason.
any help would be greatly appreciated.
There is no toRadians in the Foundation classes. You can just convert it manually
degrees * M_PI / 180.0
Foundation imports math.h so you'll have M_PI otherwise you'll have to
#include <math.h>
try this coding:- ( radian to degree)
location = [[CCDirector sharedDirector] convertToGL:location];
// Determine offset of location to projectile
int offX = location.x - PROJECTILE_POSITION_X;
int offY = location.y - PROJECTILE_POSITION_Y;
int realX = winSize.width + (PROJECTILE_WIDTH/2);
float ratio = (float) offY / (float) offX;
int realY = (realX * ratio) + PROJECTILE_POSITION_Y;
int offRealX = realX - PROJECTILE_POSITION_X;
int offRealY = realY - PROJECTILE_POSITION_Y;
// Determine angle to face
float angleRadians = atanf((float)offRealY / (float)offRealX);
float angleDegrees = (__angleRadians__)/((__angleRadians__) * 57.29577951f);