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.

74 lines
3.3 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - Retrieve image data from texture: LoadImageFromTexture()
  4. *
  5. * NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
  6. *
  7. * Example originally created with raylib 1.3, last time updated with raylib 4.0
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2015-2024 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. //------------------------------------------------------------------------------------
  17. // Program main entry point
  18. //------------------------------------------------------------------------------------
  19. int main(void)
  20. {
  21. // Initialization
  22. //--------------------------------------------------------------------------------------
  23. const int screenWidth = 800;
  24. const int screenHeight = 450;
  25. InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture to image");
  26. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  27. Image image = LoadImage("resources/raylib_logo.png"); // Load image data into CPU memory (RAM)
  28. Texture2D texture = LoadTextureFromImage(image); // Image converted to texture, GPU memory (RAM -> VRAM)
  29. UnloadImage(image); // Unload image data from CPU memory (RAM)
  30. image = LoadImageFromTexture(texture); // Load image from GPU texture (VRAM -> RAM)
  31. UnloadTexture(texture); // Unload texture from GPU memory (VRAM)
  32. texture = LoadTextureFromImage(image); // Recreate texture from retrieved image data (RAM -> VRAM)
  33. UnloadImage(image); // Unload retrieved image data from CPU memory (RAM)
  34. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  35. //---------------------------------------------------------------------------------------
  36. // Main game loop
  37. while (!WindowShouldClose()) // Detect window close button or ESC key
  38. {
  39. // Update
  40. //----------------------------------------------------------------------------------
  41. // TODO: Update your variables here
  42. //----------------------------------------------------------------------------------
  43. // Draw
  44. //----------------------------------------------------------------------------------
  45. BeginDrawing();
  46. ClearBackground(RAYWHITE);
  47. DrawTexture(texture, screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2, WHITE);
  48. DrawText("this IS a texture loaded from an image!", 300, 370, 10, GRAY);
  49. EndDrawing();
  50. //----------------------------------------------------------------------------------
  51. }
  52. // De-Initialization
  53. //--------------------------------------------------------------------------------------
  54. UnloadTexture(texture); // Texture unloading
  55. CloseWindow(); // Close window and OpenGL context
  56. //--------------------------------------------------------------------------------------
  57. return 0;
  58. }