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