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.

98 lines
3.8 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - Load textures from raw data
  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. int main(void)
  16. {
  17. // Initialization
  18. //--------------------------------------------------------------------------------------
  19. const int screenWidth = 800;
  20. const int screenHeight = 450;
  21. InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture from raw data");
  22. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  23. // Load RAW image data (512x512, 32bit RGBA, no file header)
  24. Image fudesumiRaw = LoadImageRaw("resources/fudesumi.raw", 384, 512, UNCOMPRESSED_R8G8B8A8, 0);
  25. Texture2D fudesumi = LoadTextureFromImage(fudesumiRaw); // Upload CPU (RAM) image to GPU (VRAM)
  26. UnloadImage(fudesumiRaw); // Unload CPU (RAM) image data
  27. // Generate a checked texture by code
  28. int width = 960;
  29. int height = 480;
  30. // Dynamic memory allocation to store pixels data (Color type)
  31. Color *pixels = (Color *)malloc(width*height*sizeof(Color));
  32. for (int y = 0; y < height; y++)
  33. {
  34. for (int x = 0; x < width; x++)
  35. {
  36. if (((x/32+y/32)/1)%2 == 0) pixels[y*width + x] = ORANGE;
  37. else pixels[y*width + x] = GOLD;
  38. }
  39. }
  40. // Load pixels data into an image structure and create texture
  41. Image checkedIm = {
  42. .data = pixels, // We can assign pixels directly to data
  43. .width = width,
  44. .height = height,
  45. .format = UNCOMPRESSED_R8G8B8A8,
  46. .mipmaps = 1
  47. };
  48. Texture2D checked = LoadTextureFromImage(checkedIm);
  49. UnloadImage(checkedIm); // Unload CPU (RAM) image data (pixels)
  50. //---------------------------------------------------------------------------------------
  51. // Main game loop
  52. while (!WindowShouldClose()) // Detect window close button or ESC key
  53. {
  54. // Update
  55. //----------------------------------------------------------------------------------
  56. // TODO: Update your variables here
  57. //----------------------------------------------------------------------------------
  58. // Draw
  59. //----------------------------------------------------------------------------------
  60. BeginDrawing();
  61. ClearBackground(RAYWHITE);
  62. DrawTexture(checked, screenWidth/2 - checked.width/2, screenHeight/2 - checked.height/2, Fade(WHITE, 0.5f));
  63. DrawTexture(fudesumi, 430, -30, WHITE);
  64. DrawText("CHECKED TEXTURE ", 84, 85, 30, BROWN);
  65. DrawText("GENERATED by CODE", 72, 148, 30, BROWN);
  66. DrawText("and RAW IMAGE LOADING", 46, 210, 30, BROWN);
  67. DrawText("(c) Fudesumi sprite by Eiden Marsal", 310, screenHeight - 20, 10, BROWN);
  68. EndDrawing();
  69. //----------------------------------------------------------------------------------
  70. }
  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. }