This repository has been archived by the owner on Apr 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
317 lines (264 loc) · 7.01 KB
/
main.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <stdbool.h>
#include <stdarg.h>
#include "tigr.h"
/* Raygui are not working as standalone version without raylib. We need fake
* raylib.h */
#include "raylib.h"
#define RAYGUI_IMPLEMENTATION
// #define RAYGUI_STANDALONE
#include "raygui.h"
Tigr *screen;
bool quit;
Vector2 mouseCoords;
int mouseX;
int mouseY;
int mouseButtonsMask;
int mouseButtons[3];
int previousMouseButtons[3];
bool checkboxChecked;
int KeyTigrToRaylib(int key)
{
if ((key >= 32) && (key <= 125))
return key;
else
switch (key) {
case TK_PAD0:
case TK_PAD1:
case TK_PAD2:
case TK_PAD3:
case TK_PAD4:
case TK_PAD5:
case TK_PAD6:
case TK_PAD7:
case TK_PAD8:
case TK_PAD9:
return key - 80;
case TK_LEFT:
return KEY_LEFT;
case TK_RIGHT:
return KEY_RIGHT;
case TK_UP:
return KEY_UP;
case TK_DOWN:
return KEY_DOWN;
case TK_BACKSPACE:
return KEY_BACKSPACE_TEXT;
case TK_PADMUL:
return '*';
case TK_PADADD:
return '+';
case TK_MINUS:
case TK_PADSUB:
return '-';
case TK_DOT:
case TK_PADDOT:
return '.';
case TK_SLASH:
case TK_PADDIV:
return '/';
case TK_SEMICOLON:
return ';';
case TK_EQUALS:
return '=';
case TK_COMMA:
return ',';
case TK_TICK:
case TK_BACKTICK:
return '\'';
case TK_LSQUARE:
return '[';
case TK_RSQUARE:
return ']';
case TK_BACKSLASH:
return '\\';
default:
return 0;
}
}
int KeyRaylibToTigr(int key)
{
if ((key >= 32) && (key <= 125))
return key;
else
switch (key) {
case KEY_LEFT:
return TK_LEFT;
case KEY_RIGHT:
return TK_RIGHT;
case KEY_UP:
return TK_UP;
case KEY_DOWN:
return TK_DOWN;
case KEY_BACKSPACE_TEXT:
return TK_BACKSPACE;
default:
return 0;
}
}
static inline TPixel ColorRaylibToTigr(Color color)
{
TPixel resultColor;
resultColor.r = color.r;
resultColor.g = color.g;
resultColor.b = color.b;
resultColor.a = color.a;
return resultColor;
}
int MeasureText(const char *text, int fontSize)
{
return tigrTextWidth(tfont, text);
}
void DrawText(const char *text, int posX, int posY, int fontSize, Color color)
{
tigrPrint(screen, tfont, posX, posY, ColorRaylibToTigr(color), "%s", text);
}
void DrawRectangle(int x, int y, int width, int height, Color color)
{
tigrFill(screen, x, y, width, height, ColorRaylibToTigr(color));
}
void DrawRectangleRec(Rectangle rec, Color color)
{
DrawRectangle(rec.x, rec.y, rec.width, rec.height, color);
}
Vector2 GetMousePosition(void)
{
return mouseCoords;
}
bool IsMouseButtonDown(int button)
{
return mouseButtons[button];
}
bool IsMouseButtonUp(int button)
{
return !mouseButtons[button];
}
bool IsMouseButtonReleased(int button)
{
return ((mouseButtons[button] == false) &&
(previousMouseButtons[button] == true));
}
int GetMouseWheelMove(void)
{
/* Not provided by tigr */
return 0;
}
int GetKeyPressed(void)
{
return KeyTigrToRaylib(tigrReadChar(screen));
}
bool IsKeyPressed(int key)
{
return tigrKeyDown(screen, KeyRaylibToTigr(key));
}
bool IsKeyDown(int key)
{
return tigrKeyHeld(screen, KeyRaylibToTigr(key));
}
void DrawRectangleLines(int x, int y, int width, int height, Color color)
{
tigrRect(screen, x, y, width, height, ColorRaylibToTigr(color));
}
void DrawRectangleLinesEx(Rectangle rec, int lineThick, Color color)
{
tigrRect(screen, rec.x, rec.y, rec.width, rec.height,
ColorRaylibToTigr(color));
}
void DrawRectangleGradientV(int posX, int posY, int width, int height,
Color color1, Color color2)
{
/* TODO */
}
void DrawRectangleGradientH(int posX, int posY, int width, int height,
Color color1, Color color2)
{
/* TODO */
}
void DrawRectangleGradientEx(Rectangle rec, Color col1, Color col2, Color col3,
Color col4)
{
/* TODO */
}
void DrawTexture(Texture2D texture, int posX, int posY, Color tint)
{
/* TODO */
}
void DrawTextureRec(Texture2D texture, Rectangle sourceRec, Vector2 position,
Color tint)
{
/* TODO */
}
// Returns a Color struct from hexadecimal value
Color GetColor(int hexValue)
{
Color color;
color.r = (unsigned char)(hexValue >> 24) & 0xFF;
color.g = (unsigned char)(hexValue >> 16) & 0xFF;
color.b = (unsigned char)(hexValue >> 8) & 0xFF;
color.a = (unsigned char)hexValue & 0xFF;
return color;
}
// Returns hexadecimal value for a Color
int ColorToInt(Color color)
{
return (((int)color.r << 24) | ((int)color.g << 16) | ((int)color.b << 8) |
(int)color.a);
}
// Check if point is inside rectangle
bool CheckCollisionPointRec(Vector2 point, Rectangle rec)
{
if ((point.x >= rec.x) && (point.x <= (rec.x + rec.width)) &&
(point.y >= rec.y) && (point.y <= (rec.y + rec.height)))
return true;
return false;
}
// Color fade-in or fade-out, alpha goes from 0.0f to 1.0f
Color Fade(Color color, float alpha)
{
if (alpha < 0.0f) alpha = 0.0f;
else if (alpha > 1.0f) alpha = 1.0f;
return (Color){color.r, color.g, color.b, (unsigned char)(255.0f * alpha)};
}
// Formatting of text with variables to 'embed'
const char *FormatText(const char *text, ...)
{
#define MAX_FORMATTEXT_LENGTH 64
static char buffer[MAX_FORMATTEXT_LENGTH];
va_list args;
va_start(args, text);
vsprintf(buffer, text, args);
va_end(args);
return buffer;
}
void MouseUpdate(void)
{
int i;
memcpy(previousMouseButtons, mouseButtons, sizeof(int) * 3U);
tigrMouse(screen, &mouseX, &mouseY, &mouseButtonsMask);
for (i = 0; i < 3; i++)
mouseButtons[i] = ((mouseButtonsMask & (1 << i)) == (1 << i));
mouseCoords.x = mouseX;
mouseCoords.y = mouseY;
}
int main(int argc, char **argv)
{
checkboxChecked = false;
quit = false;
screen = tigrWindow(640, 480, "Raygui Helloworld", TIGR_FIXED);
GuiEnable();
while (!quit) {
MouseUpdate();
if (tigrClosed(screen)) {
quit = true;
}
/* Render */
tigrClear(screen, tigrRGB(0x00, 0x00, 0x00));
GuiLabel((Rectangle){32, 32, 64, 64}, "Hello");
checkboxChecked = GuiCheckBox((Rectangle){32, 64, 16, 16},
checkboxChecked);
/* Your other widgets here */
tigrUpdate(screen);
}
GuiDisable();
tigrFree(screen);
return 0;
}