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.

94 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()
  16. {
  17. // Initialization
  18. //--------------------------------------------------------------------------------------
  19. int screenWidth = 800;
  20. 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 (1024x1024 pixels)
  28. int width = 1024;
  29. int height = 1024;
  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*height + x] = ORANGE;
  37. else pixels[y*height + x] = GOLD;
  38. }
  39. }
  40. // Load pixels data into an image structure and create texture
  41. Image checkedIm = LoadImageEx(pixels, width, height);
  42. Texture2D checked = LoadTextureFromImage(checkedIm);
  43. UnloadImage(checkedIm); // Unload CPU (RAM) image data
  44. // Dynamic memory must be freed after using it
  45. free(pixels); // Unload CPU (RAM) pixels data
  46. //---------------------------------------------------------------------------------------
  47. // Main game loop
  48. while (!WindowShouldClose()) // Detect window close button or ESC key
  49. {
  50. // Update
  51. //----------------------------------------------------------------------------------
  52. // TODO: Update your variables here
  53. //----------------------------------------------------------------------------------
  54. // Draw
  55. //----------------------------------------------------------------------------------
  56. BeginDrawing();
  57. ClearBackground(RAYWHITE);
  58. DrawTexture(checked, screenWidth/2 - checked.width/2, screenHeight/2 - checked.height/2, Fade(WHITE, 0.5f));
  59. DrawTexture(fudesumi, 430, -30, WHITE);
  60. DrawText("CHECKED TEXTURE ", 84, 100, 30, BROWN);
  61. DrawText("GENERATED by CODE", 72, 164, 30, BROWN);
  62. DrawText("and RAW IMAGE LOADING", 46, 226, 30, BROWN);
  63. DrawText("(c) Fudesumi sprite by Eiden Marsal", 310, screenHeight - 20, 10, BROWN);
  64. EndDrawing();
  65. //----------------------------------------------------------------------------------
  66. }
  67. // De-Initialization
  68. //--------------------------------------------------------------------------------------
  69. UnloadTexture(fudesumi); // Texture unloading
  70. UnloadTexture(checked); // Texture unloading
  71. CloseWindow(); // Close window and OpenGL context
  72. //--------------------------------------------------------------------------------------
  73. return 0;
  74. }