hanafuda/intro.c

69 lines
2.5 KiB
C

// Copyright 2025 Bill Rossi
//
// This file is part of Hanafuda Hachi-Hachi.
//
// Hanafuda Hachi-Hachi is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
// Hanafuda Hachi-Hachi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with Hanafuda Hachi-Hachi. If not, see <https://www.gnu.org/licenses/>.
#include <stdlib.h>
#include "intro.h"
#define FONT_SIZE 36
void display_loading(void) {
int window_width = GetRenderWidth();
int window_height = GetRenderHeight();
int loading_width = MeasureText("Loading...", FONT_SIZE);
BeginDrawing();
DrawText("Loading...", window_width - loading_width, window_height - FONT_SIZE, FONT_SIZE, RAYWHITE);
EndDrawing();
}
Intro *load_intro(void) {
Intro *i = malloc(sizeof(Intro));
i->textures = malloc(sizeof(Texture) * 2);
Image bgb = LoadImage("img/made_by_bgb.png");
i->textures[0] = LoadTextureFromImage(bgb);
UnloadImage(bgb);
Image ray = LoadImage("img/made_w_raylib.png");
i->textures[1] = LoadTextureFromImage(ray);
UnloadImage(ray);
i->texture_count = 2;
i->timer = 0.;
return i;
}
#define SLIDE_TIME 3.0
#define FADE_IN_TIME 0.5
#define FADE_OUT_TIME 0.5
void intro_display(Intro *intro) {
float frame_time, elapsed_time, alpha;
for (int i = 0; i < intro->texture_count; i++) {
intro->timer = GetTime() + SLIDE_TIME;
while((frame_time = GetTime()) < intro->timer) {
if (WindowShouldClose()) exit(0);
elapsed_time = SLIDE_TIME - (intro->timer - frame_time);
if (elapsed_time < FADE_IN_TIME)
alpha = ((FADE_IN_TIME - elapsed_time) / FADE_IN_TIME) * 255.;
else if (elapsed_time > SLIDE_TIME - FADE_OUT_TIME)
alpha = ((elapsed_time - (SLIDE_TIME - FADE_OUT_TIME)) / FADE_OUT_TIME) * 255.;
else
alpha = 0;
BeginDrawing();
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), (Color) { 34, 32, 52, 255 });
DrawTexture(intro->textures[i],
(GetScreenWidth() / 2) - (intro->textures->width / 2),
(GetScreenHeight() / 2) - (intro->textures->height / 2),
WHITE);
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), (Color) { 0, 0, 0, alpha });
EndDrawing();
}
}
}