Beginners_-_GetImageData.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "raylib.h"
  2. #include <stdlib.h> // Required for: free()
  3. //
  4. // How to draw into a image and then read the image data and use that.
  5. //
  6. int main(void)
  7. {
  8. // Initialization
  9. //--------------------------------------------------------------------------------------
  10. const int screenWidth = 800;
  11. const int screenHeight = 450;
  12. InitWindow(screenWidth, screenHeight, "raylib example.");
  13. // Create a Perlin Image
  14. Image tempImage = GenImageColor(32,32,BLACK);
  15. ImageDrawCircle(&tempImage,16,16,10,RED);
  16. Color *mapPixels = GetImageData(tempImage);
  17. // We will put the image data from the rectangle in this map.
  18. int map[32][32] = {0};
  19. for (int y = 0; y < tempImage.height; y++)
  20. {
  21. for (int x = 0; x < tempImage.width; x++)
  22. {
  23. if ((mapPixels[y*tempImage.width + x].r != 0)){ // Collision: if other than 0.
  24. map[x][y] = 1;
  25. }
  26. }
  27. }
  28. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  29. //--------------------------------------------------------------------------------------
  30. // Main game loop
  31. while (!WindowShouldClose()) // Detect window close button or ESC key
  32. {
  33. // Update
  34. //----------------------------------------------------------------------------------
  35. //----------------------------------------------------------------------------------
  36. // Draw
  37. //----------------------------------------------------------------------------------
  38. BeginDrawing();
  39. ClearBackground(RAYWHITE);
  40. for(int y=0;y<32;y++){
  41. for(int x=0;x<32;x++){
  42. if(map[x][y]==1)DrawRectangle(x*16,y*8,16,8,RED);
  43. }
  44. }
  45. EndDrawing();
  46. //----------------------------------------------------------------------------------
  47. }
  48. // De-Initialization
  49. //--------------------------------------------------------------------------------------
  50. free(mapPixels); // Unload color array
  51. UnloadImage(tempImage); // Unload image from RAM
  52. CloseWindow(); // Close window and OpenGL context
  53. //--------------------------------------------------------------------------------------
  54. return 0;
  55. }