Commit 27d1d22c authored by Max Kobelt's avatar Max Kobelt
Browse files

Add basic input field

parent 258b7049
......@@ -6,19 +6,21 @@
#define min(a, b) ((a)<(b)? (a) : (b))
const int GAME_SCREEN_WIDTH = 400, GAME_SCREEN_HEIGHT = 400;
const int GAME_SCREEN_WIDTH = 400, GAME_SCREEN_HEIGHT = 400, FRAME_RATE = 60;
int main() {
InitWindow(GetScreenWidth(), GetScreenHeight(), "Space Intruders");
ToggleFullscreen();
SetTargetFPS(60);
SetTargetFPS(FRAME_RATE);
RenderTexture2D target = LoadRenderTexture(GAME_SCREEN_WIDTH, GAME_SCREEN_HEIGHT);
SetTextureFilter(target.texture, FILTER_BILINEAR);
enum { MENU, GAME } state = MENU;
initMenu();
while (!WindowShouldClose()) {
if (IsKeyPressed(KEY_F11)) {
ToggleFullscreen();
......@@ -26,6 +28,12 @@ int main() {
float scale = min((float) GetScreenWidth() / GAME_SCREEN_WIDTH, (float) GetScreenHeight() / GAME_SCREEN_HEIGHT);
if (state == MENU) {
calcMenu();
} else if (state == GAME) {
// todo
}
BeginDrawing();
ClearBackground(BLACK);
BeginTextureMode(target);
......@@ -36,8 +44,8 @@ int main() {
}
EndTextureMode();
DrawTexturePro(target.texture, (Rectangle){ 0.0f, 0.0f, (float) target.texture.width, (float) -target.texture.height },
(Rectangle){ (GetScreenWidth() - ((float) GAME_SCREEN_WIDTH * scale)) * 0.5, (GetScreenHeight() - ((float) GAME_SCREEN_HEIGHT * scale)) * 0.5,
DrawTexturePro(target.texture, (Rectangle) { 0.0f, 0.0f, (float) target.texture.width, (float) -target.texture.height },
(Rectangle) { (GetScreenWidth() - ((float) GAME_SCREEN_WIDTH * scale)) * 0.5, (GetScreenHeight() - ((float) GAME_SCREEN_HEIGHT * scale)) * 0.5,
(float) GAME_SCREEN_WIDTH * scale, (float) GAME_SCREEN_HEIGHT * scale }, (Vector2){ 0, 0 }, 0.0f, WHITE);
EndDrawing();
}
......
#include "raylib.h"
extern const int GAME_SCREEN_WIDTH, GAME_SCREEN_HEIGHT;
#include <stddef.h>
#define MAX_INPUT_CHARS 10
extern const int GAME_SCREEN_WIDTH, GAME_SCREEN_HEIGHT, FRAME_RATE;
const size_t PLAY_FONT_SIZE = 50, NAME_FONT_SIZE = 30;
char name[MAX_INPUT_CHARS + 1] = "\0";
size_t letterCount, framesCount;
Rectangle inputBox;
Vector2 playBtnPos;
void initMenu() {
letterCount = framesCount = 0;
int inputBoxWidth = GAME_SCREEN_WIDTH / 3;
inputBox = (Rectangle) { (GAME_SCREEN_WIDTH - inputBoxWidth) / 2, GAME_SCREEN_HEIGHT / 2, inputBoxWidth, GAME_SCREEN_HEIGHT / 7 };
playBtnPos = (Vector2) { (GAME_SCREEN_WIDTH - MeasureText("PLAY", PLAY_FONT_SIZE)) / 2, GAME_SCREEN_HEIGHT / 5 };
}
void calcMenu() {
int key = GetKeyPressed();
while (key > 0) {
// 0-9 and A-Z
if (((key >= KEY_ZERO && key <= KEY_NINE) || (key >= KEY_A && key <= KEY_Z)) && letterCount < MAX_INPUT_CHARS) {
if (!IsKeyPressed(KEY_LEFT_SHIFT) && !IsKeyPressed(KEY_RIGHT_SHIFT)) {
key += 32; // Uncapitalize
}
name[letterCount++] = (char) key;
} else if (IsKeyPressed(KEY_BACKSPACE) && letterCount > 0) {
name[--letterCount] = '\0';
}
key = GetKeyPressed();
}
}
void drawMenu() {
DrawText("PLAY", (GAME_SCREEN_WIDTH + - MeasureText("PLAY", 50)) / 2, GAME_SCREEN_HEIGHT / 5, 50, RED);
DrawText("PLAY", playBtnPos.x, playBtnPos.y, PLAY_FONT_SIZE, RED);
DrawRectangleRec(inputBox, LIGHTGRAY);
DrawRectangleLines(inputBox.x, inputBox.y, inputBox.width, inputBox.height, RED);
if ((++framesCount / (FRAME_RATE / 2)) % 2) {
DrawText("_", inputBox.x + 8 + MeasureText(name, NAME_FONT_SIZE), inputBox.y + inputBox.height - NAME_FONT_SIZE, NAME_FONT_SIZE, RED);
}
}
#pragma once
void initMenu();
void calcMenu();
void drawMenu();
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment