|
| 1 | +""" |
| 2 | +
|
| 3 | +raylib [textures] example - Texture source and destination rectangles |
| 4 | +
|
| 5 | +""" |
| 6 | + |
| 7 | +# Import |
| 8 | +# ------------------------------------------------------------------------------------ |
| 9 | +from pyray import * |
| 10 | +# ------------------------------------------------------------------------------------ |
| 11 | + |
| 12 | +# ------------------------------------------------------------------------------------ |
| 13 | +# Program main entry point |
| 14 | +# ------------------------------------------------------------------------------------ |
| 15 | +def main(): |
| 16 | + # Initialization |
| 17 | + # ------------------------------------------------------------------------------------ |
| 18 | + SCREEN_WIDTH = 800 |
| 19 | + SCREEN_HEIGHT = 450 |
| 20 | + |
| 21 | + init_window(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [textures] examples - texture source and destination rectangles") |
| 22 | + |
| 23 | + # NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) |
| 24 | + |
| 25 | + scarfy = load_texture("resources/scarfy.png") # Texture loading |
| 26 | + |
| 27 | + frame_width = scarfy.width / 6 |
| 28 | + frame_height = scarfy.height |
| 29 | + |
| 30 | + # Source rectangle (part of the texture to use for drawing) |
| 31 | + source_rec = Rectangle(0, 0, frame_width, frame_height) |
| 32 | + |
| 33 | + # Destination rectangle (screen rectangle where drawing part of texture) |
| 34 | + dest_rec = Rectangle(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, frame_width*2, frame_height*2) |
| 35 | + |
| 36 | + # Origin of the texture (rotation/scale point), it's relative to destination rectangle size |
| 37 | + origin = Vector2(frame_width, frame_height) |
| 38 | + |
| 39 | + rotation = 0 |
| 40 | + |
| 41 | + set_target_fps(60) # Set our game to run at 60 frames-per-second |
| 42 | + # ------------------------------------------------------------------------------------ |
| 43 | + |
| 44 | + # Main game loop |
| 45 | + while not window_should_close(): # Detect window close button or ESC key |
| 46 | + # Update |
| 47 | + # ---------------------------------------------------------------------------------- |
| 48 | + rotation += 1 |
| 49 | + # ---------------------------------------------------------------------------------- |
| 50 | + |
| 51 | + # Draw |
| 52 | + # ---------------------------------------------------------------------------------- |
| 53 | + begin_drawing() |
| 54 | + |
| 55 | + clear_background(RAYWHITE) |
| 56 | + |
| 57 | + # NOTE: Using DrawTexturePro() we can easily rotate and scale the part of the texture we draw |
| 58 | + # sourceRec defines the part of the texture we use for drawing |
| 59 | + # destRec defines the rectangle where our texture part will fit (scaling it to fit) |
| 60 | + # origin defines the point of the texture used as reference for rotation and scaling |
| 61 | + # rotation defines the texture rotation (using origin as rotation point) |
| 62 | + |
| 63 | + draw_texture_pro(scarfy, source_rec, dest_rec, origin, rotation, WHITE) |
| 64 | + |
| 65 | + draw_line(int(dest_rec.x), 0, int(dest_rec.x), SCREEN_HEIGHT, GRAY) |
| 66 | + draw_line(0, int(dest_rec.y), SCREEN_WIDTH, int(dest_rec.y), GRAY) |
| 67 | + |
| 68 | + draw_text("(c) Scarfy sprite by Eiden Marsal", SCREEN_WIDTH - 200, SCREEN_HEIGHT - 20, 10, GRAY) |
| 69 | + |
| 70 | + end_drawing() |
| 71 | + # ---------------------------------------------------------------------------------- |
| 72 | + |
| 73 | + # De-Initialization |
| 74 | + # ---------------------------------------------------------------------------------- |
| 75 | + close_window() # Close window and OpenGL context |
| 76 | + # ---------------------------------------------------------------------------------- |
| 77 | + |
| 78 | + |
| 79 | +# Execute the main function |
| 80 | +if __name__ == '__main__': |
| 81 | + main() |
0 commit comments