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.

90 lines
4.1 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, (float)cat.width, (float)cat.height }, (Rectangle){ 30, 40, cat.width*1.5f, cat.height*1.5f }, WHITE);
  29. ImageCrop(&parrots, (Rectangle){ 0, 50, (float)parrots.width, (float)parrots.height - 100 }); // Crop resulting image
  30. // Draw on the image with a few image draw methods
  31. ImageDrawPixel(&parrots, 10, 10, RAYWHITE);
  32. ImageDrawCircle(&parrots, 10, 10, 5, RAYWHITE);
  33. ImageDrawRectangle(&parrots, 5, 20, 10, 10, RAYWHITE);
  34. UnloadImage(cat); // Unload image from RAM
  35. // Load custom font for frawing on image
  36. Font font = LoadFont("resources/custom_jupiter_crash.png");
  37. // Draw over image using custom font
  38. ImageDrawTextEx(&parrots, font, "PARROTS & CAT", (Vector2){ 300, 230 }, (float)font.baseSize, -2, WHITE);
  39. UnloadFont(font); // Unload custom spritefont (already drawn used on image)
  40. Texture2D texture = LoadTextureFromImage(parrots); // Image converted to texture, uploaded to GPU memory (VRAM)
  41. UnloadImage(parrots); // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
  42. SetTargetFPS(60);
  43. //---------------------------------------------------------------------------------------
  44. // Main game loop
  45. while (!WindowShouldClose()) // Detect window close button or ESC key
  46. {
  47. // Update
  48. //----------------------------------------------------------------------------------
  49. // TODO: Update your variables here
  50. //----------------------------------------------------------------------------------
  51. // Draw
  52. //----------------------------------------------------------------------------------
  53. BeginDrawing();
  54. ClearBackground(RAYWHITE);
  55. DrawTexture(texture, screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2 - 40, WHITE);
  56. DrawRectangleLines(screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2 - 40, texture.width, texture.height, DARKGRAY);
  57. DrawText("We are drawing only one texture from various images composed!", 240, 350, 10, DARKGRAY);
  58. DrawText("Source images have been cropped, scaled, flipped and copied one over the other.", 190, 370, 10, DARKGRAY);
  59. EndDrawing();
  60. //----------------------------------------------------------------------------------
  61. }
  62. // De-Initialization
  63. //--------------------------------------------------------------------------------------
  64. UnloadTexture(texture); // Texture unloading
  65. CloseWindow(); // Close window and OpenGL context
  66. //--------------------------------------------------------------------------------------
  67. return 0;
  68. }