You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
3.9 KiB

  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. #include "raylib.h"
  14. int main(void)
  15. {
  16. // Initialization
  17. //--------------------------------------------------------------------------------------
  18. const int screenWidth = 800;
  19. const int screenHeight = 450;
  20. InitWindow(screenWidth, screenHeight, "raylib [textures] example - image drawing");
  21. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  22. Image cat = LoadImage("resources/cat.png"); // Load image in CPU memory (RAM)
  23. ImageCrop(&cat, (Rectangle){ 100, 10, 280, 380 }); // Crop an image piece
  24. ImageFlipHorizontal(&cat); // Flip cropped image horizontally
  25. ImageResize(&cat, 150, 200); // Resize flipped-cropped image
  26. Image parrots = LoadImage("resources/parrots.png"); // Load image in CPU memory (RAM)
  27. // Draw one image over the other with a scaling of 1.5f
  28. ImageDraw(&parrots, cat, (Rectangle){ 0, 0, cat.width, cat.height }, (Rectangle){ 30, 40, cat.width*1.5f, cat.height*1.5f });
  29. ImageCrop(&parrots, (Rectangle){ 0, 50, parrots.width, parrots.height - 100 }); // Crop resulting image
  30. UnloadImage(cat); // Unload image from RAM
  31. // Load custom font for frawing on image
  32. Font font = LoadFont("resources/custom_jupiter_crash.png");
  33. // Draw over image using custom font
  34. ImageDrawTextEx(&parrots, (Vector2){ 300, 230 }, font, "PARROTS & CAT", font.baseSize, -2, WHITE);
  35. UnloadFont(font); // Unload custom spritefont (already drawn used on image)
  36. Texture2D texture = LoadTextureFromImage(parrots); // Image converted to texture, uploaded to GPU memory (VRAM)
  37. UnloadImage(parrots); // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
  38. SetTargetFPS(60);
  39. //---------------------------------------------------------------------------------------
  40. // Main game loop
  41. while (!WindowShouldClose()) // Detect window close button or ESC key
  42. {
  43. // Update
  44. //----------------------------------------------------------------------------------
  45. // TODO: Update your variables here
  46. //----------------------------------------------------------------------------------
  47. // Draw
  48. //----------------------------------------------------------------------------------
  49. BeginDrawing();
  50. ClearBackground(RAYWHITE);
  51. DrawTexture(texture, screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2 - 40, WHITE);
  52. DrawRectangleLines(screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2 - 40, texture.width, texture.height, DARKGRAY);
  53. DrawText("We are drawing only one texture from various images composed!", 240, 350, 10, DARKGRAY);
  54. DrawText("Source images have been cropped, scaled, flipped and copied one over the other.", 190, 370, 10, DARKGRAY);
  55. EndDrawing();
  56. //----------------------------------------------------------------------------------
  57. }
  58. // De-Initialization
  59. //--------------------------------------------------------------------------------------
  60. UnloadTexture(texture); // Texture unloading
  61. CloseWindow(); // Close window and OpenGL context
  62. //--------------------------------------------------------------------------------------
  63. return 0;
  64. }