textures_raw_data.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Load textures from raw data (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. #include <stdlib.h> // Required for malloc() and free()
  15. #if defined(PLATFORM_WEB)
  16. #include <emscripten/emscripten.h>
  17. #endif
  18. //----------------------------------------------------------------------------------
  19. // Global Variables Definition
  20. //----------------------------------------------------------------------------------
  21. int screenWidth = 800;
  22. int screenHeight = 450;
  23. Texture2D fudesumi;
  24. Texture2D checked;
  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 from raw data");
  37. // Load RAW image data (512x512, 32bit RGBA, no file header)
  38. Image fudesumiRaw = LoadImageRaw("resources/fudesumi.raw", 384, 512, UNCOMPRESSED_R8G8B8A8, 0);
  39. fudesumi = LoadTextureFromImage(fudesumiRaw); // Upload CPU (RAM) image to GPU (VRAM)
  40. UnloadImage(fudesumiRaw); // Unload CPU (RAM) image data
  41. // Generate a checked texture by code (1024x1024 pixels)
  42. int width = 1024;
  43. int height = 1024;
  44. // Dynamic memory allocation to store pixels data (Color type)
  45. Color *pixels = (Color *)malloc(width*height*sizeof(Color));
  46. for (int y = 0; y < height; y++)
  47. {
  48. for (int x = 0; x < width; x++)
  49. {
  50. if (((x/32+y/32)/1)%2 == 0) pixels[y*height + x] = ORANGE;
  51. else pixels[y*height + x] = GOLD;
  52. }
  53. }
  54. // Load pixels data into an image structure and create texture
  55. Image checkedIm = LoadImageEx(pixels, width, height);
  56. checked = LoadTextureFromImage(checkedIm);
  57. UnloadImage(checkedIm); // Unload CPU (RAM) image data
  58. // Dynamic memory must be freed after using it
  59. free(pixels); // Unload CPU (RAM) pixels data
  60. #if defined(PLATFORM_WEB)
  61. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  62. #else
  63. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  64. //--------------------------------------------------------------------------------------
  65. // Main game loop
  66. while (!WindowShouldClose()) // Detect window close button or ESC key
  67. {
  68. UpdateDrawFrame();
  69. }
  70. #endif
  71. // De-Initialization
  72. //--------------------------------------------------------------------------------------
  73. UnloadTexture(fudesumi); // Texture unloading
  74. UnloadTexture(checked); // Texture unloading
  75. CloseWindow(); // Close window and OpenGL context
  76. //--------------------------------------------------------------------------------------
  77. return 0;
  78. }
  79. //----------------------------------------------------------------------------------
  80. // Module Functions Definition
  81. //----------------------------------------------------------------------------------
  82. void UpdateDrawFrame(void)
  83. {
  84. // Update
  85. //----------------------------------------------------------------------------------
  86. // TODO: Update your variables here
  87. //----------------------------------------------------------------------------------
  88. // Draw
  89. //----------------------------------------------------------------------------------
  90. BeginDrawing();
  91. ClearBackground(RAYWHITE);
  92. DrawTexture(checked, screenWidth/2 - checked.width/2, screenHeight/2 - checked.height/2, Fade(WHITE, 0.5f));
  93. DrawTexture(fudesumi, 430, -30, WHITE);
  94. DrawText("CHECKED TEXTURE ", 84, 100, 30, BROWN);
  95. DrawText("GENERATED by CODE", 72, 164, 30, BROWN);
  96. DrawText("and RAW IMAGE LOADING", 46, 226, 30, BROWN);
  97. DrawText("(c) Fudesumi sprite by Eiden Marsal", 310, screenHeight - 20, 10, BROWN);
  98. EndDrawing();
  99. //----------------------------------------------------------------------------------
  100. }