You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Shows examples of how the origin and destination rectangle interact when used with DrawTexturePro.
If your origin is not at 0,0, the destination rectangle you give will NOT be in screen space, as the origin will offset it. The X and Y of the rectangle end up being where on the screen you want to put the texture origin.
This is important if you are using the same destination rectangle for things that don't have an origin offset, like Collision checks. You will have to use a screen space rectangle for collisions.
You can see in this example that even though the second bunny has a destination rectangle X of 100, the left side is not at 100 like the first one that has no origin offset. The origin offset for the second bunny is subtracted from it's rectangle so that the bunny is centered on the coordinates specified (100,200).
/*******************************************************************************************
*
* raylib example - texture origin
*
* Welcome to raylib!
*
* To test examples, just press F6 and execute raylib_compile_execute script
* Note that compiled executable is placed in the same folder as .c file
*
* You can find all basic examples on C:\raylib\raylib\examples folder or
* raylib official webpage: www.raylib.com
*
* Enjoy using raylib. :)
*
* This example has been created using raylib 3.8 by Jeffery Myers (@JeffM)
*
* Copyright (c) 2013-2021 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
int main(void)
{
// Init
const int screenWidth = 800;
const int screenHeight = 600;
SetConfigFlags(FLAG_VSYNC_HINT);
InitWindow(screenWidth, screenHeight, "raylib example - texture origin");
// load a texture
Texture bunny = LoadTexture("resources/wabbit_alpha.png");
// cache some bunny constants
const Vector2 bunnyCenter = { bunny.width / 2.0f, bunny.height / 2.0f };
const Rectangle bunnyRect = { 0,0, (float)bunny.width, (float)bunny.height };
SetTargetFPS(144);
// Main game loop
while (!WindowShouldClose())
{
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
// grid lines
DrawLine(100, 0, 100, screenHeight, RED);
DrawLine(0, 100, screenWidth, 100, RED);
DrawLine(0, 200, screenWidth, 200, RED);
DrawLine(0, 300, screenWidth, 300, RED);
DrawLine(0, 400, screenWidth, 400, RED);
// notes
DrawText("100", 100, 0, 20, RED);
DrawText("Upper Left Origin", 200, 100, 20, RED);
DrawText("Center Origin", 200, 200, 20, RED);
DrawText("Center Origin Rotating", 200, 300, 20, RED);
DrawText("Upper Left Origin Rotating", 200, 400, 20, RED);
// upper left origin (0,0), drawn at 100,100
DrawTexturePro(bunny, bunnyRect, (Rectangle) { 100, 100, bunnyRect.width, bunnyRect.height }, (Vector2) { 0, 0 }, 0, WHITE);
// upper center origin (bunnyCenter), drawn at 100, 200
DrawTexturePro(bunny, bunnyRect, (Rectangle) { 100, 200, bunnyRect.width, bunnyRect.height }, bunnyCenter, 0, WHITE);
// center origin (bunnyCenter), drawn at 100, 300, spinning
DrawTexturePro(bunny, bunnyRect, (Rectangle) { 100, 300, bunnyRect.width, bunnyRect.height }, bunnyCenter, (float)GetTime() * 90, WHITE);
// upper left origin (0,0), drawn at 100, 400, spinning
DrawTexturePro(bunny, bunnyRect, (Rectangle) { 100, 400, bunnyRect.width, bunnyRect.height }, (Vector2) { 0, 0 }, (float)GetTime() * 90, WHITE);
EndDrawing();
}
// cleanup
UnloadTexture(bunny);
CloseWindow();
return 0;
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Shows examples of how the origin and destination rectangle interact when used with DrawTexturePro.
If your origin is not at 0,0, the destination rectangle you give will NOT be in screen space, as the origin will offset it. The X and Y of the rectangle end up being where on the screen you want to put the texture origin.
This is important if you are using the same destination rectangle for things that don't have an origin offset, like Collision checks. You will have to use a screen space rectangle for collisions.
You can see in this example that even though the second bunny has a destination rectangle X of 100, the left side is not at 100 like the first one that has no origin offset. The origin offset for the second bunny is subtracted from it's rectangle so that the bunny is centered on the coordinates specified (100,200).
Beta Was this translation helpful? Give feedback.
All reactions