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.

82 lines
3.4 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.6 (www.raylib.com)
  8. -- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  9. --
  10. -- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5)
  11. --
  12. -------------------------------------------------------------------------------------------
  13. --#include <stdlib.h> -- Required for malloc() and free()
  14. -- Initialization
  15. -------------------------------------------------------------------------------------------
  16. local screenWidth = 800
  17. local screenHeight = 450
  18. InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture from raw data")
  19. -- NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  20. -- Load RAW image data (512x512, 32bit RGBA, no file header)
  21. local sonicRaw = LoadImageRaw("resources/texture_formats/sonic_R8G8B8A8.raw", 512, 512, TextureFormat.UNCOMPRESSED_R8G8B8A8, 0)
  22. local sonic = LoadTextureFromImage(sonicRaw) -- Upload CPU (RAM) image to GPU (VRAM)
  23. UnloadImage(sonicRaw) -- Unload CPU (RAM) image data
  24. -- Generate a checked texture by code (1024x1024 pixels)
  25. local width = 1024
  26. local height = 1024
  27. -- Dynamic memory allocation to store pixels data (Color type)
  28. local pixels = {}
  29. for y = 1, height do
  30. for x = 1, width do
  31. if ((((x - 1)/32+(y - 1)//32)//1)%2 == 0) then pixels[(y - 1)*height + x] = DARKBLUE
  32. else pixels[(y - 1)*height + x] = SKYBLUE end
  33. end
  34. end
  35. -- Load pixels data into an image structure and create texture
  36. local checkedIm = LoadImageEx(pixels, width, height)
  37. local checked = LoadTextureFromImage(checkedIm)
  38. UnloadImage(checkedIm) -- Unload CPU (RAM) image data
  39. -- Dynamic memory must be freed after using it
  40. --free(pixels) -- Unload CPU (RAM) pixels data
  41. -------------------------------------------------------------------------------------------
  42. -- Main game loop
  43. while not WindowShouldClose() do -- Detect window close button or ESC key
  44. -- Update
  45. ---------------------------------------------------------------------------------------
  46. -- TODO: Update your variables here
  47. ---------------------------------------------------------------------------------------
  48. -- Draw
  49. ---------------------------------------------------------------------------------------
  50. BeginDrawing()
  51. ClearBackground(RAYWHITE)
  52. DrawTexture(checked, screenWidth/2 - checked.width/2, screenHeight/2 - checked.height/2, Fade(WHITE, 0.3))
  53. DrawTexture(sonic, 330, -20, WHITE)
  54. DrawText("CHECKED TEXTURE ", 84, 100, 30, DARKBLUE)
  55. DrawText("GENERATED by CODE", 72, 164, 30, DARKBLUE)
  56. DrawText("and RAW IMAGE LOADING", 46, 226, 30, DARKBLUE)
  57. EndDrawing()
  58. ---------------------------------------------------------------------------------------
  59. end
  60. -- De-Initialization
  61. -------------------------------------------------------------------------------------------
  62. UnloadTexture(sonic) -- Texture unloading
  63. UnloadTexture(checked) -- Texture unloading
  64. CloseWindow() -- Close window and OpenGL context
  65. -------------------------------------------------------------------------------------------