25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
4.2 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - Load textures from raw data
  4. *
  5. * Example complexity rating: [] 3/4
  6. *
  7. * NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
  8. *
  9. * Example originally created with raylib 1.3, last time updated with raylib 3.5
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2015-2025 Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include <stdlib.h> // Required for: malloc() and free()
  19. //------------------------------------------------------------------------------------
  20. // Program main entry point
  21. //------------------------------------------------------------------------------------
  22. int main(void)
  23. {
  24. // Initialization
  25. //--------------------------------------------------------------------------------------
  26. const int screenWidth = 800;
  27. const int screenHeight = 450;
  28. InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture from raw data");
  29. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  30. // Load RAW image data (512x512, 32bit RGBA, no file header)
  31. Image fudesumiRaw = LoadImageRaw("resources/fudesumi.raw", 384, 512, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, 0);
  32. Texture2D fudesumi = LoadTextureFromImage(fudesumiRaw); // Upload CPU (RAM) image to GPU (VRAM)
  33. UnloadImage(fudesumiRaw); // Unload CPU (RAM) image data
  34. // Generate a checked texture by code
  35. int width = 960;
  36. int height = 480;
  37. // Dynamic memory allocation to store pixels data (Color type)
  38. Color *pixels = (Color *)malloc(width*height*sizeof(Color));
  39. for (int y = 0; y < height; y++)
  40. {
  41. for (int x = 0; x < width; x++)
  42. {
  43. if (((x/32+y/32)/1)%2 == 0) pixels[y*width + x] = ORANGE;
  44. else pixels[y*width + x] = GOLD;
  45. }
  46. }
  47. // Load pixels data into an image structure and create texture
  48. Image checkedIm = {
  49. .data = pixels, // We can assign pixels directly to data
  50. .width = width,
  51. .height = height,
  52. .format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
  53. .mipmaps = 1
  54. };
  55. Texture2D checked = LoadTextureFromImage(checkedIm);
  56. UnloadImage(checkedIm); // Unload CPU (RAM) image data (pixels)
  57. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  58. //---------------------------------------------------------------------------------------
  59. // Main game loop
  60. while (!WindowShouldClose()) // Detect window close button or ESC key
  61. {
  62. // Update
  63. //----------------------------------------------------------------------------------
  64. // TODO: Update your variables here
  65. //----------------------------------------------------------------------------------
  66. // Draw
  67. //----------------------------------------------------------------------------------
  68. BeginDrawing();
  69. ClearBackground(RAYWHITE);
  70. DrawTexture(checked, screenWidth/2 - checked.width/2, screenHeight/2 - checked.height/2, Fade(WHITE, 0.5f));
  71. DrawTexture(fudesumi, 430, -30, WHITE);
  72. DrawText("CHECKED TEXTURE ", 84, 85, 30, BROWN);
  73. DrawText("GENERATED by CODE", 72, 148, 30, BROWN);
  74. DrawText("and RAW IMAGE LOADING", 46, 210, 30, BROWN);
  75. DrawText("(c) Fudesumi sprite by Eiden Marsal", 310, screenHeight - 20, 10, BROWN);
  76. EndDrawing();
  77. //----------------------------------------------------------------------------------
  78. }
  79. // De-Initialization
  80. //--------------------------------------------------------------------------------------
  81. UnloadTexture(fudesumi); // Texture unloading
  82. UnloadTexture(checked); // Texture unloading
  83. CloseWindow(); // Close window and OpenGL context
  84. //--------------------------------------------------------------------------------------
  85. return 0;
  86. }