1 | /******************************************************************************************* |
2 | * |
3 | * raylib [textures] example - Texture source and destination rectangles |
4 | * |
5 | * This example has been created using raylib 1.3 (www.raylib.com) |
6 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
7 | * |
8 | * Copyright (c) 2015 Ramon Santamaria (@raysan5) |
9 | * |
10 | ********************************************************************************************/ |
11 | |
12 | #include "raylib.h" |
13 | |
14 | int main(void) |
15 | { |
16 | // Initialization |
17 | //-------------------------------------------------------------------------------------- |
18 | const int screenWidth = 800; |
19 | const int screenHeight = 450; |
20 | |
21 | InitWindow(screenWidth, screenHeight, "raylib [textures] examples - texture source and destination rectangles" ); |
22 | |
23 | // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) |
24 | |
25 | Texture2D scarfy = LoadTexture("resources/scarfy.png" ); // Texture loading |
26 | |
27 | int frameWidth = scarfy.width/6; |
28 | int frameHeight = scarfy.height; |
29 | |
30 | // Source rectangle (part of the texture to use for drawing) |
31 | Rectangle sourceRec = { 0.0f, 0.0f, frameWidth, frameHeight }; |
32 | |
33 | // Destination rectangle (screen rectangle where drawing part of texture) |
34 | Rectangle destRec = { screenWidth/2, screenHeight/2, frameWidth*2, frameHeight*2 }; |
35 | |
36 | // Origin of the texture (rotation/scale point), it's relative to destination rectangle size |
37 | Vector2 origin = { frameWidth, frameHeight }; |
38 | |
39 | int rotation = 0; |
40 | |
41 | SetTargetFPS(60); |
42 | //-------------------------------------------------------------------------------------- |
43 | |
44 | // Main game loop |
45 | while (!WindowShouldClose()) // Detect window close button or ESC key |
46 | { |
47 | // Update |
48 | //---------------------------------------------------------------------------------- |
49 | rotation++; |
50 | //---------------------------------------------------------------------------------- |
51 | |
52 | // Draw |
53 | //---------------------------------------------------------------------------------- |
54 | BeginDrawing(); |
55 | |
56 | ClearBackground(RAYWHITE); |
57 | |
58 | // NOTE: Using DrawTexturePro() we can easily rotate and scale the part of the texture we draw |
59 | // sourceRec defines the part of the texture we use for drawing |
60 | // destRec defines the rectangle where our texture part will fit (scaling it to fit) |
61 | // origin defines the point of the texture used as reference for rotation and scaling |
62 | // rotation defines the texture rotation (using origin as rotation point) |
63 | DrawTexturePro(scarfy, sourceRec, destRec, origin, (float)rotation, WHITE); |
64 | |
65 | DrawLine((int)destRec.x, 0, (int)destRec.x, screenHeight, GRAY); |
66 | DrawLine(0, (int)destRec.y, screenWidth, (int)destRec.y, GRAY); |
67 | |
68 | DrawText("(c) Scarfy sprite by Eiden Marsal" , screenWidth - 200, screenHeight - 20, 10, GRAY); |
69 | |
70 | EndDrawing(); |
71 | //---------------------------------------------------------------------------------- |
72 | } |
73 | |
74 | // De-Initialization |
75 | //-------------------------------------------------------------------------------------- |
76 | UnloadTexture(scarfy); // Texture unloading |
77 | |
78 | CloseWindow(); // Close window and OpenGL context |
79 | //-------------------------------------------------------------------------------------- |
80 | |
81 | return 0; |
82 | } |