textures_to_image.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Retrieve image data from texture: GetTextureData() (adapted for HTML5 platform)
  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.3 (www.raylib.com)
  8. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  9. *
  10. * Copyright (c) 2015 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #if defined(PLATFORM_WEB)
  15. #include <emscripten/emscripten.h>
  16. #endif
  17. //----------------------------------------------------------------------------------
  18. // Global Variables Definition
  19. //----------------------------------------------------------------------------------
  20. int screenWidth = 800;
  21. int screenHeight = 450;
  22. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  23. Image image;
  24. Texture2D texture;
  25. //----------------------------------------------------------------------------------
  26. // Module Functions Declaration
  27. //----------------------------------------------------------------------------------
  28. void UpdateDrawFrame(void); // Update and Draw one frame
  29. //----------------------------------------------------------------------------------
  30. // Main Enry Point
  31. //----------------------------------------------------------------------------------
  32. int main()
  33. {
  34. // Initialization
  35. //--------------------------------------------------------------------------------------
  36. InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture to image");
  37. image = LoadImage("resources/raylib_logo.png"); // Load image data into CPU memory (RAM)
  38. texture = LoadTextureFromImage(image); // Image converted to texture, GPU memory (RAM -> VRAM)
  39. UnloadImage(image); // Unload image data from CPU memory (RAM)
  40. image = GetTextureData(texture); // Retrieve image data from GPU memory (VRAM -> RAM)
  41. UnloadTexture(texture); // Unload texture from GPU memory (VRAM)
  42. texture = LoadTextureFromImage(image); // Recreate texture from retrieved image data (RAM -> VRAM)
  43. UnloadImage(image); // Unload retrieved image data from CPU memory (RAM)
  44. #if defined(PLATFORM_WEB)
  45. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  46. #else
  47. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  48. //--------------------------------------------------------------------------------------
  49. // Main game loop
  50. while (!WindowShouldClose()) // Detect window close button or ESC key
  51. {
  52. UpdateDrawFrame();
  53. }
  54. #endif
  55. // De-Initialization
  56. //--------------------------------------------------------------------------------------
  57. UnloadTexture(texture); // Texture unloading
  58. CloseWindow(); // Close window and OpenGL context
  59. //--------------------------------------------------------------------------------------
  60. return 0;
  61. }
  62. //----------------------------------------------------------------------------------
  63. // Module Functions Definition
  64. //----------------------------------------------------------------------------------
  65. void UpdateDrawFrame(void)
  66. {
  67. // Update
  68. //----------------------------------------------------------------------------------
  69. // TODO: Update your variables here
  70. //----------------------------------------------------------------------------------
  71. // Draw
  72. //----------------------------------------------------------------------------------
  73. BeginDrawing();
  74. ClearBackground(RAYWHITE);
  75. DrawTexture(texture, screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2, WHITE);
  76. DrawText("this IS a texture loaded from an image!", 300, 370, 10, GRAY);
  77. EndDrawing();
  78. //----------------------------------------------------------------------------------
  79. }