textures_image_loading.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Image loading and texture creation (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. Texture2D texture; // Image converted to texture, GPU memory (VRAM)
  24. //----------------------------------------------------------------------------------
  25. // Module Functions Declaration
  26. //----------------------------------------------------------------------------------
  27. void UpdateDrawFrame(void); // Update and Draw one frame
  28. //----------------------------------------------------------------------------------
  29. // Main Enry Point
  30. //----------------------------------------------------------------------------------
  31. int main()
  32. {
  33. // Initialization
  34. //--------------------------------------------------------------------------------------
  35. InitWindow(screenWidth, screenHeight, "raylib [textures] example - image loading");
  36. Image image = LoadImage("resources/raylib_logo.png"); // Loaded in CPU memory (RAM)
  37. texture = LoadTextureFromImage(image); // Image converted to texture, GPU memory (VRAM)
  38. UnloadImage(image); // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
  39. #if defined(PLATFORM_WEB)
  40. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  41. #else
  42. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  43. //--------------------------------------------------------------------------------------
  44. // Main game loop
  45. while (!WindowShouldClose()) // Detect window close button or ESC key
  46. {
  47. UpdateDrawFrame();
  48. }
  49. #endif
  50. // De-Initialization
  51. //--------------------------------------------------------------------------------------
  52. UnloadTexture(texture); // Texture unloading
  53. CloseWindow(); // Close window and OpenGL context
  54. //--------------------------------------------------------------------------------------
  55. return 0;
  56. }
  57. //----------------------------------------------------------------------------------
  58. // Module Functions Definition
  59. //----------------------------------------------------------------------------------
  60. void UpdateDrawFrame(void)
  61. {
  62. // Update
  63. //----------------------------------------------------------------------------------
  64. // TODO: Update your variables here
  65. //----------------------------------------------------------------------------------
  66. // Draw
  67. //----------------------------------------------------------------------------------
  68. BeginDrawing();
  69. ClearBackground(RAYWHITE);
  70. DrawTexture(texture, screenWidth/2 - texture.width/2, screenHeight/2 - texture.height/2, WHITE);
  71. DrawText("this IS a texture loaded from an image!", 300, 370, 10, GRAY);
  72. EndDrawing();
  73. //----------------------------------------------------------------------------------
  74. }