This repository was archived by the owner on Feb 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraylib_example_code.txt
More file actions
135 lines (105 loc) · 5.05 KB
/
raylib_example_code.txt
File metadata and controls
135 lines (105 loc) · 5.05 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Here's some code that I have so far for raylib, not sure how to get all of it set up with github at the moment,
// and variable names are bound to change, so I'm just leving it here for the moment
// here are instructions i used for setting up raylib on my mac,
// you can just copy/paste this into a new project to try it out.
// https://github.com/raysan5/raylib/wiki/Compile-for-OSX
#include <raylib.h>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "SliderAI");
Color tileColors[9] = { GRAY, GRAY, GRAY, GRAY, GRAY, GRAY, GRAY, GRAY, GRAY };
Color numButtonColors[9] = { GRAY, GRAY, GRAY, GRAY, GRAY, GRAY, GRAY, GRAY, GRAY };
Rectangle selectableButtons[9]; //tiles
Rectangle numberButtons[9]; //number buttons
int tileValues[9] = {0,0,0,0,0,0,0,0,0};
int selectedTileIndex = 0;
//board buttons
for (int i = 0; i < 9; i++)
{
selectableButtons[i].x = 20 + 50*(i%3) + 10*(i%3);
selectableButtons[i].y = 50 + 50*(i/3) + 10*(i/3);
selectableButtons[i].width = 50;
selectableButtons[i].height = 50;
}
//number buttons
for (int i = 0; i < 9; i++)
{
numberButtons[i].x = 20 + 30*(i);
numberButtons[i].y = 290;
numberButtons[i].width = 20;
numberButtons[i].height = 20;
}
bool selectedTile[9] = { false }; // Selected tiles indicator
Vector2 mousePoint;
SetTargetFPS(30);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
mousePoint = GetMousePosition();
for (int i = 0; i < 9; i++) // Iterate along all the tile rectangles
{
if (CheckCollisionPointRec(mousePoint, selectableButtons[i]))
{
tileColors[i].a = 40;
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
bool beforeSelectedTile = selectedTile[i];
for (int j=0; j<9; j++) {
selectedTile[j] = false;
}
selectedTile[i] = !beforeSelectedTile;
if(selectedTile[i]) selectedTileIndex = i;
else selectedTileIndex = 9; //invalid value, will do nothing becuase of conditional below (like 11 lines down)
}
}
else tileColors[i].a = 120;
}
for (int i = 0; i < 9; i++) // Iterate along all number button rectangles
{
if (CheckCollisionPointRec(mousePoint, numberButtons[i]))
{
numButtonColors[i].a = 40;
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && selectedTileIndex<9) { // <-- that conditional...
tileValues[selectedTileIndex] = i+1;
}
}
else numButtonColors[i].a = 120;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("8-Puzzle Solver", 20, 20, 20, BLUE);
for (int i = 0; i < 9; i++) // Draw tile buttons
{
if (selectedTile[i]) tileColors[i].a = 255;
DrawRectangleRec(selectableButtons[i], tileColors[i]);
DrawText((FormatText("%01i", tileValues[i])), selectableButtons[i].x + 21, selectableButtons[i].y + 17, 20, BLUE);
}
DrawText("Instructions : Click a box in the board above, \nthen click a number from the list below.", 20, 240, 15, BLUE);
for (int i = 0; i < 9; i++) // Draw num buttons
{
DrawRectangleRec(numberButtons[i], numButtonColors[i]);
DrawText(((i != 8) ? (FormatText("%01i", i+1)) : "D"), numberButtons[i].x + 7, numberButtons[i].y + 5, 5, BLUE);
}
DrawText("Click 'D' to remove a value in the board. \nif a value is a duplicate, its previous instance will be removed.", 20, 320, 10, BLUE); // this part doesn't work yet...
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}