Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

105 wiersze
4.2 KiB

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