font and menu

This commit is contained in:
huaqo-games
2025-03-25 18:02:34 +01:00
parent 71660ba942
commit 8384967e55
29 changed files with 293 additions and 83 deletions
+74 -23
View File
@@ -2,47 +2,98 @@
#define GAMEGUI_H
#include "sprite.h"
#include <raylib.h>
typedef enum {
NOT_CLICKED,
CLICKED
} ButtonState;
typedef struct
{
int state;
Rectangle textureRecs[2];
Rectangle buttonBounds;
Texture2D texture;
Sprite sprite;
Vector2 notClicked;
Vector2 clicked;
} ImageButton;
void initImageButton(ImageButton *b, Rectangle textureRecs[2], Rectangle buttonBounds, Texture2D texture){
b->textureRecs[0] = textureRecs[0];
b->textureRecs[1] = textureRecs[1];
b->buttonBounds = buttonBounds;
b->texture = texture;
b->state = 0;
typedef struct
{
Sprite sprite;
} ImageElement;
ImageButton CreateImageButton(ButtonState state, Texture2D *texture, const TextureAsset *asset, Vector2 *source, Vector2 *clicked, Rectangle *destRec){
return (ImageButton){
.sprite = {
.texture = *texture,
.sourceRec = (Rectangle){ source->x, source->y, asset->frameWidth, asset->frameHeight},
.destRec = *destRec,
.frameSize = (Vector2){asset->frameWidth, asset->frameHeight},
.origin = (Vector2){0.0f, 0.0f},
.rotation = 0.0f,
.color = WHITE
},
.notClicked = (Vector2){source->x, source->y},
.clicked = *clicked
};
}
ImageElement CreateImageElement(Texture2D *texture, const TextureAsset *asset, Vector2 *source, Rectangle *destRec){
return (ImageElement){
.sprite = {
.texture = *texture,
.sourceRec = (Rectangle){ source->x, source->y, asset->frameWidth, asset->frameHeight},
.destRec = *destRec,
.frameSize = (Vector2){asset->frameWidth, asset->frameHeight},
.origin = (Vector2){0.0f, 0.0f},
.rotation = 0.0f,
.color = WHITE
}
};
}
void PrintImageButton(const ImageButton btn) {
printf("ImageButton:\n");
printf(" Sprite.texture: id = %d\n", btn.sprite.texture.id);
printf(" Sprite.sourceRec: x = %f, y = %f, width = %f, height = %f\n",
btn.sprite.sourceRec.x, btn.sprite.sourceRec.y,
btn.sprite.sourceRec.width, btn.sprite.sourceRec.height);
printf(" Sprite.destRec: x = %f, y = %f, width = %f, height = %f\n",
btn.sprite.destRec.x, btn.sprite.destRec.y,
btn.sprite.destRec.width, btn.sprite.destRec.height);
printf(" Sprite.frameSize: x = %f, y = %f\n",
btn.sprite.frameSize.x, btn.sprite.frameSize.y);
printf(" Sprite.origin: x = %f, y = %f\n",
btn.sprite.origin.x, btn.sprite.origin.y);
printf(" Sprite.rotation: %f\n", btn.sprite.rotation);
printf(" Sprite.color: r = %d, g = %d, b = %d, a = %d\n",
btn.sprite.color.r, btn.sprite.color.g,
btn.sprite.color.b, btn.sprite.color.a);
printf(" notClicked: x = %f, y = %f\n", btn.notClicked.x, btn.notClicked.y);
printf(" clicked: x = %f, y = %f\n", btn.clicked.x, btn.clicked.y);
}
int isImageButtonClicked(ImageButton *b){
Vector2 mousePoint = GetMousePosition();
if (CheckCollisionPointRec(mousePoint, b->buttonBounds)) {
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) b->state = 1;
if (CheckCollisionPointRec(GetMousePosition(), b->sprite.destRec)) {
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)){
UpdateSpriteSourceRec(&b->sprite, &b->clicked);
}
if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON))
{
b->state = 0;
UpdateSpriteSourceRec(&b->sprite, &b->notClicked);
return true;
}
} else {
b->state = 0;
UpdateSpriteSourceRec(&b->sprite, &b->notClicked);
}
return false;
}
void renderImageButton(ImageButton *b) {
Sprite sprite;
sprite.texture = b->texture;
sprite.sourceRec = b->textureRecs[b->state];
sprite.destRec = b->buttonBounds;
sprite.origin = (Vector2){ 0, 0 };
sprite.rotation = 0.0f;
sprite.color = WHITE;
RenderSprite(&sprite);
RenderSprite(&b->sprite);
}
void renderImageElement(ImageElement *e){
RenderSprite(&e->sprite);
}
+20
View File
@@ -0,0 +1,20 @@
// initButton(&m->startButton, "Start Game", GetScreenWidth() / 10, GetScreenHeight() / 20, GetScreenWidth() / 2, GetScreenHeight() / 2);
// initButton(&m->quitButton, "Quit Game", GetScreenWidth() / 10, GetScreenHeight() / 20, GetScreenWidth() / 2,
// GetScreenHeight() / 2 + (GetScreenHeight() / 20 * 2));
// if (isButtonClicked(&m->startButton)) {
// state->gameStarted = true;
// m->startButton.text = "Continue";
// state->currentScreen = GAMEPLAY;
// }
// if (isButtonClicked(&m->quitButton) || (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE))) {
// state->running = false;
// }
// if (state->gameStarted){
// if (IsKeyPressed(KEY_ESCAPE)){
// state->currentScreen = GAMEPLAY;
// }
// }
// renderButton(&m->startButton);
// renderButton(&m->quitButton);
+7
View File
@@ -0,0 +1,7 @@
#ifndef COLORS_H
#define COLORS_H
#define TEXT_COLOR_DARK (Color){ 182, 137, 98, 255}
#define TEXT_COLOR_LIGHT (Color){ 243, 229, 194, 255}
#endif // COLORS_H
+1 -1
View File
@@ -63,7 +63,7 @@ void InitGame(GameState* g, Display *display){
g->bee1 = CreateBee(FLYING, g->textures[BEE], g->assets.textureAssets[BEE].frameWidth, 4, 10, g->hive.position, 10.0f, &g->flowers, &g->hive);
// I/O
g->camera = CreateCamera(display->width, display->height, g->player.physics.position, 5.0f);
g->camera = CreateCamera(display->width, display->height, g->player.physics.position, 10.0f);
g->mouse = CreateMouse(0.10f, 5.0f, 10.0f);
}
+1 -1
View File
@@ -10,7 +10,7 @@ Object CreateHive(Texture2D texture, float frameWidth){
.destRec = (Rectangle){0.0f, 0.0f, frameWidth, frameWidth},
.origin = (Vector2){frameWidth / 2, frameWidth / 2},
.rotation = 0.0f,
.color = WHITE,
.color = WHITE
},
.position = (Vector2){0.0f, 0.0f}
};
+50 -50
View File
@@ -6,14 +6,14 @@
#include "../engine/asset.h"
#include "../engine/gamegui.h"
#include "menuassets.h"
#include "colors.h"
typedef struct {
Assets assets;
Texture2D textures[MENU_TEX_COUNT];
Button startButton;
Button quitButton;
ImageButton newStartButton;
ImageButton newQuitButton;
ImageElement board;
ImageButton playButton;
ImageButton quitButton;
} MenuState;
void InitMenu(MenuState *m){
@@ -23,75 +23,75 @@ void InitMenu(MenuState *m){
for (int i = 0; i < MENU_TEX_COUNT; i++){
m->textures[i] = LoadTexture(m->assets.textureAssets[i].path);
}
int menuScale = 4;
int screenWidth = GetScreenWidth();
int screenHeight = GetScreenHeight();
Vector2 screenCenter = {screenWidth / 2, screenHeight / 2};
Rectangle startButtonBounds = {
GetScreenWidth() / 2 - (GetScreenWidth() / 10) / 2,
GetScreenHeight() / 2 - (GetScreenHeight() / 20) / 2,
GetScreenWidth() / 10,
GetScreenHeight() / 20
int boardWidth = screenWidth / menuScale * 3.0f;
float boardScaleFactor = (float)boardWidth / m->assets.textureAssets[BOARD].frameWidth;
int boardHeight = (int)(m->assets.textureAssets[BOARD].frameHeight * boardScaleFactor);
Vector2 boardSource = {128.0f,0.0f};
Rectangle boardDestRec = {
.x = screenCenter.x - (boardWidth / 2),
.y = screenCenter.y - (boardHeight / 2),
.width = boardWidth,
.height = boardHeight
};
Rectangle quitButtonBounds = {
GetScreenWidth() / 2 - (GetScreenWidth() / 10) / 2,
GetScreenHeight() / 2 + (GetScreenHeight() / 10) / 2,
GetScreenWidth() / 10,
GetScreenHeight() / 20
m->board = CreateImageElement(&m->textures[BOARD], &m->assets.textureAssets[BOARD], &boardSource, &boardDestRec);
int buttonWidth = screenWidth / menuScale;
float buttonScaleFactor = (float)buttonWidth / m->assets.textureAssets[BIG_BUTTONS].frameWidth;
int buttonHeight = (int)(m->assets.textureAssets[BIG_BUTTONS].frameHeight * buttonScaleFactor);
int buttonSpacing = buttonHeight;
Vector2 playButtonNotPressed = { 0.0f, 96.0f};
Vector2 quitButtonNotPressed = { 0.0f, 0.0f};
Vector2 playButtonPressed = { 96.0f, 96.0f};
Vector2 quitButtonPressed = { 96.0f, 0.0f};
Rectangle playButtonDestRec = {
.x = screenCenter.x - (buttonWidth / 2),
.y = screenCenter.y - (buttonHeight / 2),
.width = buttonWidth,
.height = buttonHeight
};
Rectangle playTexRecs[2] = {
{ 0.0f, 96.0f, m->assets.textureAssets[BIG_BUTTONS].frameWidth, m->assets.textureAssets[BIG_BUTTONS].frameHeight },
{ m->assets.textureAssets[BIG_BUTTONS].frameWidth, 96.0f, m->assets.textureAssets[BIG_BUTTONS].frameWidth, m->assets.textureAssets[BIG_BUTTONS].frameHeight }
Rectangle quitButtonDestRec = {
.x = screenCenter.x - (buttonWidth / 2),
.y = screenCenter.y - (buttonHeight / 2) + buttonSpacing,
.width = buttonWidth,
.height = buttonHeight
};
Rectangle emptyTexRecs[2] = {
{ 0.0f, 0.0f, m->assets.textureAssets[BIG_BUTTONS].frameWidth, m->assets.textureAssets[BIG_BUTTONS].frameHeight },
{ m->assets.textureAssets[BIG_BUTTONS].frameWidth, 0.0f, m->assets.textureAssets[BIG_BUTTONS].frameWidth, m->assets.textureAssets[BIG_BUTTONS].frameHeight }
};
initImageButton(&m->newStartButton, playTexRecs, startButtonBounds, m->textures[BIG_BUTTONS]);
initImageButton(&m->newQuitButton, emptyTexRecs, quitButtonBounds, m->textures[BIG_BUTTONS]);
// initButton(&m->startButton, "Start Game", GetScreenWidth() / 10, GetScreenHeight() / 20, GetScreenWidth() / 2, GetScreenHeight() / 2);
// initButton(&m->quitButton, "Quit Game", GetScreenWidth() / 10, GetScreenHeight() / 20, GetScreenWidth() / 2,
// GetScreenHeight() / 2 + (GetScreenHeight() / 20 * 2));
m->playButton = CreateImageButton(NOT_CLICKED, &m->textures[BIG_BUTTONS], &m->assets.textureAssets[BIG_BUTTONS], &playButtonNotPressed, &playButtonPressed, &playButtonDestRec);
m->quitButton = CreateImageButton(NOT_CLICKED, &m->textures[BIG_BUTTONS], &m->assets.textureAssets[BIG_BUTTONS], &quitButtonNotPressed, &quitButtonPressed, &quitButtonDestRec);
}
void UpdateMenu(MenuState *m, State *state){
if (isImageButtonClicked(&m->newStartButton)){
if (isImageButtonClicked(&m->playButton)){
state->gameStarted = true;
m->startButton.text = "Continue";
state->currentScreen = GAMEPLAY;
}
if (isImageButtonClicked(&m->newQuitButton) || (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE))) {
if (isImageButtonClicked(&m->quitButton) || (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE))) {
state->running = false;
}
// if (isButtonClicked(&m->startButton)) {
// state->gameStarted = true;
// m->startButton.text = "Continue";
// state->currentScreen = GAMEPLAY;
// }
// if (isButtonClicked(&m->quitButton) || (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE))) {
// state->running = false;
// }
// if (state->gameStarted){
// if (IsKeyPressed(KEY_ESCAPE)){
// state->currentScreen = GAMEPLAY;
// }
// }
}
void RenderMenu(MenuState *m){
void RenderMenu(MenuState *m, Font *font, const char* name){
BeginDrawing();
ClearBackground(WHITE);
// renderButton(&m->startButton);
// renderButton(&m->quitButton);
renderImageButton(&m->newStartButton);
renderImageButton(&m->newQuitButton);
renderImageElement(&m->board);
renderImageButton(&m->playButton);
renderImageButton(&m->quitButton);
int textWidth = MeasureText(name, font->baseSize);
DrawTextEx(*font, name, (Vector2){ GetScreenWidth() / 2 - textWidth / 2, textWidth / 4}, (float)font->baseSize, 2, WHITE);
DrawFPS(10, 10);
EndDrawing();
}
+3 -1
View File
@@ -4,11 +4,13 @@
typedef enum {
BIG_BUTTONS,
BOARD,
MENU_TEX_COUNT
} MenuTextureID;
const TextureAsset menuTextureAssets[MENU_TEX_COUNT] = {
{"assets/SproutLandsUI/Spritesheets/UIBigPlayButton.png", 96.0f, 32.0f, 180.0f}
{"assets/SproutLandsUI/Spritesheets/UIBigPlayButton.png", 96.0f, 32.0f, 180.0f},
{"assets/SproutLandsUI/Spritesheets/Settingmenu.png", 128.0f, 144.0f, 180.0f},
};
#endif // MENUASSETS_H
+16 -7
View File
@@ -9,9 +9,11 @@
#include "game/menu.h"
#include "game/game.h"
typedef struct {
State state;
Display display;
Font font;
LogoState logo;
GameState game;
MenuState menu;
@@ -19,17 +21,23 @@ typedef struct {
int main(void)
{
Display display = {
.name = "The Last Hive",
.width = 1280,
.height = 720
};
InitWindow(display.width, display.height, display.name);
InitAudioDevice();
Application app;
app.state.currentScreen = MENU;
app.state.running = true;
app.state.gameStarted = false;
app.display.name = "The Last Hive";
app.display.width = 1280;
app.display.height = 720;
app.display = display;
app.font = LoadFont("assets/SproutLandsUI/fonts/pixelFont-7-8x14-sproutLands.fnt");
InitWindow(app.display.width, app.display.height, app.display.name);
InitAudioDevice();
SetTextureFilter(app.font.texture, TEXTURE_FILTER_POINT);
SetTraceLogLevel(LOG_NONE);
InitLogo(&app.logo);
@@ -48,7 +56,7 @@ int main(void)
case MENU:
{
UpdateMenu(&app.menu, &app.state);
RenderMenu(&app.menu);
RenderMenu(&app.menu, &app.font, app.display.name);
}break;
case GAMEPLAY:
{
@@ -59,6 +67,7 @@ int main(void)
}
}
CleanupGame(&app.game);
UnloadFont(app.font);
CloseWindow();
return 0;
}