Created
June 25, 2020 15:58
-
-
Save HParker/7fca7a3da6e8641782d32a18a599b5d8 to your computer and use it in GitHub Desktop.
little raylib example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <raylib.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
struct Target { | |
Vector2 position; | |
int size | |
}; | |
int main(void) | |
{ | |
// Initialization | |
const int screenWidth = 800; | |
const int screenHeight = 450; | |
int timer = 100; | |
int reactionTime = 0; | |
int lastReactionTime = 0; | |
InitWindow(screenWidth, screenHeight, "raylib [core] example - keyboard input"); | |
Vector2 targetPosition = { (float)screenWidth/2 - 25, (float)screenHeight/2 - 25}; | |
struct Target target; | |
char* reactionMessage; | |
target.position = targetPosition; | |
target.size = 50; | |
Color targetColorPre = DARKBLUE; | |
Color targetColorPost = RED; | |
SetTargetFPS(60); | |
// Main game loop | |
while (!WindowShouldClose()) | |
{ | |
// Update | |
if (timer > 0) { | |
timer = timer - 1; | |
} | |
if (timer <= 0) { | |
reactionTime = reactionTime + 1; | |
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) { | |
lastReactionTime = reactionTime; | |
timer = 100; | |
reactionTime = 0; | |
} | |
} | |
BeginDrawing(); | |
ClearBackground(RAYWHITE); | |
if (timer > 0) { | |
DrawRectangle(target.position.x - (target.size / 2), | |
target.position.y - (target.size / 2), | |
target.size, | |
target.size, | |
targetColorPre); | |
} else { | |
DrawRectangle(target.position.x - (target.size / 2), | |
target.position.y - (target.size / 2), | |
target.size, | |
target.size, | |
targetColorPost); | |
} | |
if (lastReactionTime > 0) { | |
if (0 < asprintf(&reactionMessage, "reaction time %i frames", lastReactionTime)) | |
DrawText(reactionMessage, 100, 300, 20, LIGHTGRAY); | |
} | |
DrawText("Reaction Time tester", 100, 200, 20, LIGHTGRAY); | |
EndDrawing(); | |
} | |
CloseWindow(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment