1 | /******************************************************************************************* |
2 | * |
3 | * raylib [textures] example - Image loading and drawing on it |
4 | * |
5 | * NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM) |
6 | * |
7 | * This example has been created using raylib 1.4 (www.raylib.com) |
8 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
9 | * |
10 | * Copyright (c) 2016 Ramon Santamaria (@raysan5) |
11 | * |
12 | ********************************************************************************************/ |
13 | |
14 | #include "raylib.h" |
15 | |
16 | int main(void) |
17 | { |
18 | // Initialization |
19 | //-------------------------------------------------------------------------------------- |
20 | const int screenWidth = 800; |
21 | const int screenHeight = 450; |
22 | |
23 | InitWindow(screenWidth, screenHeight, "raylib [textures] example - image drawing" ); |
24 | |
25 | // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) |
26 | |
27 | Image cat = LoadImage("resources/cat.png" ); // Load image in CPU memory (RAM) |
28 | ImageCrop(&cat, (Rectangle){ 100, 10, 280, 380 }); // Crop an image piece |
29 | ImageFlipHorizontal(&cat); // Flip cropped image horizontally |
30 | ImageResize(&cat, 150, 200); // Resize flipped-cropped image |
31 | |
32 | Image parrots = LoadImage("resources/parrots.png" ); // Load image in CPU memory (RAM) |
33 | |
34 | // Draw one image over the other with a scaling of 1.5f |
35 | ImageDraw(&parrots, cat, (Rectangle){ 0, 0, cat.width, cat.height }, (Rectangle){ 30, 40, cat.width*1.5f, cat.height*1.5f }, WHITE); |
36 | ImageCrop(&parrots, (Rectangle){ 0, 50, parrots.width, parrots.height - 100 }); // Crop resulting image |
37 | |
38 | // Draw on the image with a few image draw methods |
39 | ImageDrawPixel(&parrots, 10, 10, RAYWHITE); |
40 | ImageDrawCircle(&parrots, 10, 10, 5, RAYWHITE); |
41 | ImageDrawRectangle(&parrots, 5, 20, 10, 10, RAYWHITE); |
42 | |
43 | UnloadImage(cat); // Unload image from RAM |
44 | |
45 | // Load custom font for frawing on image |
46 | Font font = LoadFont("resources/custom_jupiter_crash.png" ); |
47 | |
48 | // Draw over image using custom font |
49 | ImageDrawTextEx(&parrots, font, "PARROTS & CAT" , (Vector2){ 300, 230 }, font.baseSize, -2, WHITE); |
50 | |
51 | UnloadFont(font); // Unload custom spritefont (already drawn used on image) |
52 | |
53 | Texture2D texture = LoadTextureFromImage(parrots); // Image converted to texture, uploaded to GPU memory (VRAM) |
54 | UnloadImage(parrots); // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM |
55 | |
56 | SetTargetFPS(60); |
57 | //--------------------------------------------------------------------------------------- |
58 | |
59 | // Main game loop |
60 | while (!WindowShouldClose()) // Detect window close button or ESC key |
61 | { |
62 | // Update |
63 | //---------------------------------------------------------------------------------- |
64 | // TODO: Update your variables here |
65 | //---------------------------------------------------------------------------------- |
66 | |
67 | // Draw |
68 | //---------------------------------------------------------------------------------- |
69 | BeginDrawing(); |
70 | |
71 | ClearBackground(RAYWHITE); |
72 | |
73 | DrawTexture(texture, screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2 - 40, WHITE); |
74 | DrawRectangleLines(screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2 - 40, texture.width, texture.height, DARKGRAY); |
75 | |
76 | DrawText("We are drawing only one texture from various images composed!" , 240, 350, 10, DARKGRAY); |
77 | DrawText("Source images have been cropped, scaled, flipped and copied one over the other." , 190, 370, 10, DARKGRAY); |
78 | |
79 | EndDrawing(); |
80 | //---------------------------------------------------------------------------------- |
81 | } |
82 | |
83 | // De-Initialization |
84 | //-------------------------------------------------------------------------------------- |
85 | UnloadTexture(texture); // Texture unloading |
86 | |
87 | CloseWindow(); // Close window and OpenGL context |
88 | //-------------------------------------------------------------------------------------- |
89 | |
90 | return 0; |
91 | } |