This commit is contained in:
usr 2025-06-04 21:26:11 +03:00
parent 2b06ef5d11
commit 3c79f41cb4
10 changed files with 114 additions and 80 deletions

2
build.sh Normal file → Executable file
View file

@ -5,4 +5,4 @@ cc -Wall -Wextra -I src -o obj/sdl.o -c src/sdl.c
cc -Wall -Wextra -I src -o obj/interface.o -c src/interface.c
cc -Wall -Wextra -I src -o obj/lpt_parser.o -c src/lpt_parser.c
cc -Wall -Wextra -I src -o obj/input.o -c src/input.c
cc -Wall -Wextra -I src -o game obj/*.o src/main.c -l SDL2 -l SDL2_image -l SDL2_ttf -g3
cc -Wall -Wextra -I src -o game obj/*.o src/main.c -l SDL2 -l m -l SDL2_image -l SDL2_ttf -g3

View file

@ -2,8 +2,8 @@ Background:
files/backgrounds/chem1.png
AddEntity:
files/sprites/u.png
-0.5 0.5
files/sprites/x.png
-0.5 1
AddEntity:
files/sprites/x.png
@ -11,7 +11,11 @@ AddEntity:
AddMessage:
ME
TEST
TEST djdodjsjdskdsksjdksdskdksdskdjksdjksdjsjdksjdksjdskjdksdjksdjksdjksjdksdksjdsjkdjskdskdjskdjskjdsjdsjdksjdksjdksjdsjdskjdskdskdjskdjskjdsjdsk
AddMessage:
ME
TEST djdodjsjdskdsksjdksdskdksdskdjksdjksd\njsjdksjdksjdskjdksdjksdjksdjksjdksdksjdsjkdjskdskdjskdjskjdsjdsjdks
AddMessage:
U

View file

@ -1,16 +0,0 @@
#Фон
Background:
files/backgrounds/chem1.png
#Добавляет сущность
AddEntity:
files/sprites/x.png
0.3 0.1
#Добавляет сообщение
AddMessage:
ME
TEST
#Очищает экран от сущностей
ClearEntity

View file

@ -1,8 +1,8 @@
fullscreen yes
fullscreen no
fullscreen_custom no
vsync yes
sound_volume 100
music_volume 22
screen_h 1080
screen_w 1900
saved_scene 1
music_volume 20
screen_h 900
screen_w 1400
saved_scene 0

View file

@ -2,7 +2,7 @@
#include "interface.h"
#include "input.h"
int Input(APP *app, char *run, FONTS *fonts, char flag) {
int Input(APP *app, char *run, char flag) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
@ -54,8 +54,6 @@ int Input(APP *app, char *run, FONTS *fonts, char flag) {
SDL_UpdateWindowSurface(app->window);
}
CloseFonts(fonts);
InitFonts(fonts, *app);
break;
default:

View file

@ -2,7 +2,6 @@
#define _INPUT_H
#include "sdl.h"
#include "interface.h"
/* Game modes */
enum {
@ -25,5 +24,5 @@ enum {
NEW_GAME
};
int Input(APP *app, char *run, FONTS *fonts, char flag);
int Input(APP *app, char *run, char flag);
#endif

View file

@ -1,4 +1,5 @@
#include <stdio.h>
#include <math.h>
#include "sdl.h"
#include "interface.h"
@ -16,8 +17,13 @@ int TextArray(TEXT text[], size_t n, char flag) {
return 0;
}
void PrintText(TTF_Font *font, APP *app, TEXT *dst, const char *text) {
SDL_Surface *temp = TTF_RenderUTF8_Solid(font, text, dst->color);
void PrintText(TTF_Font *font, APP *app, TEXT *dst, const char *text, int flag) {
SDL_Surface *temp;
if (flag)
temp = TTF_RenderUTF8_Solid_Wrapped(font, text, dst->color, flag);
else
temp = TTF_RenderUTF8_Solid(font, text, dst->color);
if (!temp) {
fprintf(stderr, "AddBanner Font: %s\n", TTF_GetError());
return;
@ -66,6 +72,40 @@ void AddPseudoButton(APP *app, TEXT text, const int bttn_num) {
AddBanner(app, text, rect);
}
void AddDialog(APP *app, TEXT author, TEXT text) {
int rect_x = APPROX2ORIG(app->scr_width, -0.9);
int rect_y = APPROX2ORIG(app->scr_height, 0.4);
SDL_Rect rect = {
.x = rect_x,
.y = rect_y,
.w = app->scr_width - (rect_x * 2),
.h = app->scr_height - rect_y
};
/* Box */
SDL_SetRenderDrawColor(app->render, 0, 0, 0, 0);
SDL_RenderFillRect(app->render, &rect);
/* Message */
rect.h = text.h;
rect.w = text.w;
SDL_SetRenderDrawColor(app->render, 255, 255, 255, 0);
SDL_RenderCopy(app->render, text.texture, NULL, &rect);
/* Author */
rect.y = APPROX2ORIG(app->scr_height, 0.34) - author.h;
rect.h = APPROX2ORIG(app->scr_height, 0.4) - APPROX2ORIG(app->scr_height, 0.35);
rect.w = author.w;
rect.h = author.h;
SDL_SetRenderDrawColor(app->render, 0, 0, 0, 0);
SDL_RenderCopy(app->render, author.texture, NULL, &rect);
}
void CloseFonts(FONTS *fonts) {
if (fonts->font1)
TTF_CloseFont(fonts->font1);
@ -76,14 +116,22 @@ void CloseFonts(FONTS *fonts) {
memset(fonts, 0, sizeof(FONTS));
}
int InitFonts(FONTS *fonts, const APP app) {
float GetDPIScale(SDL_Window *window) {
int w1, w2;
SDL_GetWindowSize(window, &w1, NULL);
SDL_GetWindowSizeInPixels(window, &w2, NULL);
return w2 / w1;
}
int InitFonts(FONTS *fonts, APP *app) {
memset(fonts, 0, sizeof(FONTS));
size_t font_size = app.scr_width * app.scr_height * 8 / 403200;
int font_size = round(24 * GetDPIScale(app->window));
fonts->font1 = TTF_OpenFont(FONT1, font_size);
font_size = app.scr_width * app.scr_height * 15 / 403200;
fonts->font2 = TTF_OpenFont(FONT1, font_size);
font_size = round(32 * GetDPIScale(app->window));
fonts->font2 = TTF_OpenFont(FONT2, font_size);
if (fonts->font1 == NULL || fonts->font2 == NULL) {
fprintf(stderr, "Can not open font: %s\n", TTF_GetError());

View file

@ -28,9 +28,10 @@ typedef struct {
} TEXT;
int TextArray(TEXT text[], size_t n, char flag);
void PrintText(TTF_Font *font, APP *app, TEXT *dst, const char *text);
void PrintText(TTF_Font *font, APP *app, TEXT *dst, const char *text, int flag);
void AddBanner(APP *app, TEXT text, SDL_Rect rect);
void AddPseudoButton(APP *app, TEXT text, const int bttn_num);
void AddDialog(APP *app, TEXT author, TEXT text);
void CloseFonts(FONTS *fonts);
int InitFonts(FONTS *fonts, const APP app);
int InitFonts(FONTS *fonts, APP *app);
#endif

View file

@ -40,11 +40,11 @@ void free_chain(struct ENTITY **ent) {
}
}
int DisplayScene(SDL_Texture *background, TEXT author, TEXT message, APP *app, FONTS *fonts, CONFIG cfg, struct ENTITY *ent) {
int DisplayScene(SDL_Texture *background, TEXT author, TEXT msg, APP *app, CONFIG cfg, struct ENTITY *ent) {
char run = LPTP_NORMAL;
while (run == LPTP_NORMAL) {
int status = Input(app, &run, fonts, IN_GAME);
int status = Input(app, &run, IN_GAME);
switch (status) {
case EXIT:
run = MENU;
@ -55,6 +55,7 @@ int DisplayScene(SDL_Texture *background, TEXT author, TEXT message, APP *app, F
break;
case KEY_RIGHT:
SDL_Delay(200);
run = NEXT;
break;
@ -90,8 +91,7 @@ int DisplayScene(SDL_Texture *background, TEXT author, TEXT message, APP *app, F
}
/* Dialog */
AddPseudoButton(app, message, 0);
AddPseudoButton(app, author, 1);
AddDialog(app, author, msg);
/* Render */
SDL_RenderPresent(app->render);
@ -154,10 +154,9 @@ int lpt_parser(APP *app, FONTS *fonts, CONFIG *cfg) {
else if (flag == ADD_MESSAGE) {
if (author.init)
SDL_DestroyTexture(author.texture);
memset(&author, 0, sizeof(author));
PrintText(fonts->font1, app, &author, str);
PrintText(fonts->font2, app, &author, str, 0);
if (author.init == 0)
goto LPT_PARSER_CLOSE_UP;
}
@ -165,14 +164,15 @@ int lpt_parser(APP *app, FONTS *fonts, CONFIG *cfg) {
else if (flag == ADD_MESSAGE_ARG2) {
if (msg.init)
SDL_DestroyTexture(msg.texture);
memset(&msg, 0, sizeof(msg));
PrintText(fonts->font1, app, &msg, str);
msg.color.b = msg.color.g = msg.color.r = 255;
PrintText(fonts->font1, app, &msg, str, app->scr_width - (APPROX2ORIG(app->scr_width, -0.75) * 2));
if (msg.init == 0)
goto LPT_PARSER_CLOSE_UP;
if (DisplayScene(background, author, msg, app, fonts, *cfg, entities) == MENU) {
if (DisplayScene(background, author, msg, app, *cfg, entities) == MENU) {
ret = MENU;
break;
}

View file

@ -27,7 +27,7 @@ void AddOption(TTF_Font *font, APP *app, TEXT text, const int bttn_num, const in
TEXT value;
memset(&value, 0, sizeof(value));
PrintText(font, app, &value, str);
PrintText(font, app, &value, str, 0);
if (value.init == 0)
return;
@ -35,10 +35,10 @@ void AddOption(TTF_Font *font, APP *app, TEXT text, const int bttn_num, const in
/* Print it */
int rect_x = APPROX2ORIG(app->scr_width, -0.8);
SDL_Rect rect = {
.x = app->scr_width - rect_x - value.w - 20,
.x = app->scr_width - rect_x - value.w,
.y = APPROX2ORIG(app->scr_height, (0.75 - (0.2 * bttn_num))),
.w = value.w + 20,
.h = 45
.h = 45,
.w = value.w
};
SDL_RenderCopy(app->render, value.texture, NULL, &rect);
@ -66,11 +66,11 @@ int main_menu(APP *app, FONTS *fonts, CONFIG *cfg) {
memset(&text_new_game, 0, sizeof(text_new_game));
text_continue = text_settings = text_quit = text_save = text_new_game;
PrintText(fonts->font1, app, &text_new_game, "[n] New Game");
PrintText(fonts->font1, app, &text_continue, "[c] Continue");
PrintText(fonts->font1, app, &text_settings, "[s] Settings");
PrintText(fonts->font1, app, &text_save, "[k] Save");
PrintText(fonts->font1, app, &text_quit, "[q] Quit");
PrintText(fonts->font1, app, &text_new_game, "[n] New Game", 0);
PrintText(fonts->font1, app, &text_continue, "[c] Continue", 0);
PrintText(fonts->font1, app, &text_settings, "[s] Settings", 0);
PrintText(fonts->font1, app, &text_save, "[k] Save", 0);
PrintText(fonts->font1, app, &text_quit, "[q] Quit", 0);
TEXT menu_msgs[] = {text_quit, text_settings, text_new_game, text_save, text_continue};
size_t menu_size = sizeof(menu_msgs) / sizeof(TEXT);
@ -84,16 +84,16 @@ int main_menu(APP *app, FONTS *fonts, CONFIG *cfg) {
memset(&settings, 0, sizeof(settings));
fullscreen = fullscreen_custom = vsync = sound_vol = music_vol = screen_h = screen_w = pointer = text_back = settings;
PrintText(fonts->font1, app, &fullscreen, "Fullscreen mode:");
PrintText(fonts->font1, app, &fullscreen_custom, "Custom resolution for fullscreen: Y");
PrintText(fonts->font1, app, &vsync, "VSync:");
PrintText(fonts->font1, app, &sound_vol, "Sound volume:");
PrintText(fonts->font1, app, &music_vol, "Music volume:");
PrintText(fonts->font1, app, &screen_h, "Screen height:");
PrintText(fonts->font1, app, &screen_w, "Screen width:");
PrintText(fonts->font1, app, &text_back, "[q] Back");
PrintText(fonts->font1, app, &settings, "[settings] Restart requied...");
PrintText(fonts->font2, app, &pointer, ">");
PrintText(fonts->font1, app, &fullscreen, "Fullscreen mode:", 0);
PrintText(fonts->font1, app, &fullscreen_custom, "Custom resolution for fullscreen:", 0);
PrintText(fonts->font1, app, &vsync, "VSync:", 0);
PrintText(fonts->font1, app, &sound_vol, "Sound volume:", 0);
PrintText(fonts->font1, app, &music_vol, "Music volume:", 0);
PrintText(fonts->font1, app, &screen_h, "Screen height:", 0);
PrintText(fonts->font1, app, &screen_w, "Screen width:", 0);
PrintText(fonts->font1, app, &text_back, "[q] Back", 0);
PrintText(fonts->font1, app, &settings, "[settings] Restart requied...", 0);
PrintText(fonts->font2, app, &pointer, ">", 0);
TEXT settings_msgs[] = {fullscreen, fullscreen_custom, vsync, sound_vol, music_vol, screen_h, screen_w, text_save, text_back, pointer, settings};
int *settings_opt[] = {&cfg->fullscreen, &cfg->fullscreen_custom, &cfg->vsync, &cfg->sound_volume, &cfg->music_volume, &cfg->screen_h, &cfg->screen_w};
@ -110,7 +110,7 @@ int main_menu(APP *app, FONTS *fonts, CONFIG *cfg) {
/* Input and navigation */
int status = Input(app, &run, fonts, mode);
int status = Input(app, &run, mode);
switch (status) {
case SETTINGS:
mode = IN_SETTINGS;
@ -202,10 +202,10 @@ int main_menu(APP *app, FONTS *fonts, CONFIG *cfg) {
if (mode == IN_MENU) {
/* Logo */
SDL_Rect rect = {
.x = APPROX2ORIG(app->scr_width, -0.5),
.y = APPROX2ORIG(app->scr_height, -0.5),
.w = 128 * 2,
.h = 64 * 2
.x = APPROX2ORIG(app->scr_width, 0) - 128,
.y = APPROX2ORIG(app->scr_height, -0.5) - 64,
.w = 256,
.h = 128
};
SDL_RenderCopy(app->render, logo, NULL, &rect);
@ -221,13 +221,13 @@ int main_menu(APP *app, FONTS *fonts, CONFIG *cfg) {
else if (mode == IN_SETTINGS) {
/* Menu */
AddOption(fonts->font1, app, fullscreen, 0, cfg->fullscreen, 1);
AddOption(fonts->font1, app, fullscreen_custom, 1, cfg->fullscreen_custom, 1);
AddOption(fonts->font1, app, vsync, 2, cfg->vsync, 1);
AddOption(fonts->font1, app, sound_vol, 3, cfg->sound_volume, 2);
AddOption(fonts->font1, app, music_vol, 4, cfg->music_volume, 2);
AddOption(fonts->font1, app, screen_h, 5, cfg->screen_h, 0);
AddOption(fonts->font1, app, screen_w, 6, cfg->screen_w, 0);
AddOption(fonts->font2, app, fullscreen, 0, cfg->fullscreen, 1);
AddOption(fonts->font2, app, fullscreen_custom, 1, cfg->fullscreen_custom, 1);
AddOption(fonts->font2, app, vsync, 2, cfg->vsync, 1);
AddOption(fonts->font2, app, sound_vol, 3, cfg->sound_volume, 2);
AddOption(fonts->font2, app, music_vol, 4, cfg->music_volume, 2);
AddOption(fonts->font2, app, screen_h, 5, cfg->screen_h, 0);
AddOption(fonts->font2, app, screen_w, 6, cfg->screen_w, 0);
AddPseudoButton(app, text_back, 7);
if (changed)
@ -236,7 +236,7 @@ int main_menu(APP *app, FONTS *fonts, CONFIG *cfg) {
/* Pointer */
SDL_Rect rect = {
.x = 0,
.y = APPROX2ORIG(app->scr_height, (0.75 - (0.2 * index))),
.y = APPROX2ORIG(app->scr_height, (0.77 - (0.2 * index))),
.w = pointer.w,
.h = pointer.h
};
@ -300,7 +300,7 @@ int main(void) {
int ret = 0;
FONTS fonts;
if (InitFonts(&fonts, app))
if (InitFonts(&fonts, &app))
ret = 1;
/* Game loop*/