summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/defs.h51
-rw-r--r--src/help.c97
-rw-r--r--src/local.mk1
-rw-r--r--src/main-menu.c48
-rw-r--r--src/main.c5
5 files changed, 144 insertions, 58 deletions
diff --git a/src/defs.h b/src/defs.h
new file mode 100644
index 0000000..eefe893
--- /dev/null
+++ b/src/defs.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2021 P. J. McDermott
+ *
+ * This file is part of Dodge Balls
+ *
+ * Dodge Balls 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.
+ *
+ * Dodge Balls 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 Dodge Balls. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef DB_DEFS_H_
+#define DB_DEFS_H_
+
+#define DB_WINDOW_W 640 /* Window width */
+#define DB_WINDOW_H 480 /* Window height */
+#define DB_WINDOW_P 16 /* Window padding */
+#define DB_MARGIN 16 /* Margin between elements */
+#define DB_SCROLL_W 16 /* Scroll button width */
+#define DB_SCROLL_H DB_SCROLL_W / 2 /* Scroll button height */
+#define DB_SCROLL_S 16 /* Scrolling step */
+
+#define DB_FONT_TITLE_SIZE 48 /* Title font size */
+#define DB_FONT_TEXT_SIZE 16 /* Regular text font size */
+
+#define DB_COLOR_BACK_R 0x7F /* Background color */
+#define DB_COLOR_BACK_G 0x7F
+#define DB_COLOR_BACK_B 0x7F
+#define DB_COLOR_BACK_A 0xFF
+#define DB_COLOR_FORE_R 0x00 /* Text/button color */
+#define DB_COLOR_FORE_G 0x00
+#define DB_COLOR_FORE_B 0xFF
+#define DB_COLOR_FORE_A 0xFF
+#define DB_COLOR_ACTV_R 0xFF /* Active/hovered color */
+#define DB_COLOR_ACTV_G 0xFF
+#define DB_COLOR_ACTV_B 0xFF
+#define DB_COLOR_ACTV_A 0xFF
+#define DB_COLOR_DISA_R 0x3F /* Disabled color */
+#define DB_COLOR_DISA_G 0x3F
+#define DB_COLOR_DISA_B 0x7F
+#define DB_COLOR_DISA_A 0xFF
+
+#endif /* DB_DEFS_H_ */
diff --git a/src/help.c b/src/help.c
index 5cdd40f..3e3d365 100644
--- a/src/help.c
+++ b/src/help.c
@@ -21,6 +21,7 @@
#include <SDL_ttf.h>
#include <config.h>
#include "collision.h"
+#include "defs.h"
#include "help.h"
#include "main.h"
#include "output.h"
@@ -68,16 +69,16 @@ _db_help_triangle(SDL_Renderer *renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a,
int i;
int y;
int x;
- SDL_Point points[(16 * (16 + 2)) / 4];
+ SDL_Point points[(DB_SCROLL_W * (DB_SCROLL_W + 2)) / 4];
i = 0;
- for (y = 0; y < (16 / 2); ++y) {
- for (x = y; x < (16 - y); ++x) {
+ for (y = 0; y < DB_SCROLL_H; ++y) {
+ for (x = y; x < (DB_SCROLL_W - y); ++x) {
points[i].x = x_min + x;
if (dir > 0) {
points[i].y = y_min + y;
} else {
- points[i].y = y_min + (16 / 2) - 1 - y;
+ points[i].y = y_min + DB_SCROLL_H - 1 - y;
}
++i;
}
@@ -107,20 +108,20 @@ db_help(void)
renderer = db_get_renderer();
- font = TTF_OpenFont(font_path, 16);
+ font = TTF_OpenFont(font_path, DB_FONT_TEXT_SIZE);
if (font == NULL) {
db_err("Failed to open font (%s)", TTF_GetError());
free(font_path);
return -1;
}
- color.r = 0x00;
- color.g = 0x00;
- color.b = 0xFF;
- color.a = 0xFF;
+ color.r = DB_COLOR_FORE_R;
+ color.g = DB_COLOR_FORE_G;
+ color.b = DB_COLOR_FORE_B;
+ color.a = DB_COLOR_FORE_A;
surface = TTF_RenderText_Blended_Wrapped(font, _db_help_text, color,
- 592);
+ DB_WINDOW_W - DB_WINDOW_P * 2 - DB_SCROLL_W);
if (surface == NULL) {
db_err("Failed to create surface (%s)", TTF_GetError());
return -1;
@@ -136,23 +137,23 @@ db_help(void)
text_src_rect.x = 0;
text_src_rect.y = 0;
- text_src_rect.w = 592;
- text_src_rect.h = 448;
+ text_src_rect.w = DB_WINDOW_W - DB_WINDOW_P * 2 - DB_SCROLL_W;
+ text_src_rect.h = DB_WINDOW_H - DB_WINDOW_P * 2;
- text_dst_rect.x = 16;
- text_dst_rect.y = 16;
- text_dst_rect.w = 592;
- text_dst_rect.h = 448;
+ text_dst_rect.x = DB_WINDOW_P;
+ text_dst_rect.y = DB_WINDOW_P;
+ text_dst_rect.w = DB_WINDOW_W - DB_WINDOW_P * 2 - DB_SCROLL_W;
+ text_dst_rect.h = DB_WINDOW_H - DB_WINDOW_P * 2;
- up_rect.x = 608;
- up_rect.y = 16;
- up_rect.w = 16;
- up_rect.h = 8;
+ up_rect.x = DB_WINDOW_W - DB_WINDOW_P - DB_SCROLL_W;
+ up_rect.y = DB_WINDOW_P;
+ up_rect.w = DB_SCROLL_W;
+ up_rect.h = DB_SCROLL_H;
- dn_rect.x = 608;
- dn_rect.y = 456;
- dn_rect.w = 16;
- dn_rect.h = 8;
+ dn_rect.x = DB_WINDOW_W - DB_WINDOW_P - DB_SCROLL_W;
+ dn_rect.y = DB_WINDOW_H - DB_WINDOW_P - DB_SCROLL_H;
+ dn_rect.w = DB_SCROLL_W;
+ dn_rect.h = DB_SCROLL_H;
up_over = 0;
dn_over = 0;
@@ -165,13 +166,13 @@ db_help(void)
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_UP:
- text_src_rect.y -= 16;
+ text_src_rect.y -= DB_SCROLL_S;
if (text_src_rect.y < 0) {
text_src_rect.y = 0;
}
break;
case SDLK_DOWN:
- text_src_rect.y += 16;
+ text_src_rect.y += DB_SCROLL_S;
if (text_src_rect.y > surface->h
-
text_src_rect.h)
@@ -225,14 +226,14 @@ db_help(void)
if (db_pt_in_rect(event.button.x,
event.button.y,
&up_rect)) {
- text_src_rect.y -= 16;
+ text_src_rect.y -= DB_SCROLL_S;
if (text_src_rect.y < 0) {
text_src_rect.y = 0;
}
} else if (db_pt_in_rect(event.button.x,
event.button.y,
&dn_rect)) {
- text_src_rect.y += 16;
+ text_src_rect.y += DB_SCROLL_S;
if (text_src_rect.y > surface->h -
text_src_rect.h) {
text_src_rect.y = surface->h -
@@ -246,29 +247,55 @@ db_help(void)
if (_db_help_quit > 0) {
break;
}
- SDL_SetRenderDrawColor(renderer, 0x7F, 0x7F, 0x7F, 0xFF);
+ SDL_SetRenderDrawColor(renderer,
+ DB_COLOR_BACK_R, DB_COLOR_BACK_G,
+ DB_COLOR_BACK_B, DB_COLOR_BACK_A);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture,
&text_src_rect, &text_dst_rect);
if (text_src_rect.y <= 0) {
_db_help_triangle(renderer,
- 0x3F, 0x3F, 0x7F, 0xFF, 608, 16, -1);
+ DB_COLOR_DISA_R, DB_COLOR_DISA_G,
+ DB_COLOR_DISA_B, DB_COLOR_DISA_A,
+ DB_WINDOW_W - DB_WINDOW_P * 2,
+ DB_WINDOW_P,
+ -1);
} else if (up_over == 1) {
_db_help_triangle(renderer,
- 0xFF, 0xFF, 0xFF, 0xFF, 608, 16, -1);
+ DB_COLOR_ACTV_R, DB_COLOR_ACTV_G,
+ DB_COLOR_ACTV_B, DB_COLOR_ACTV_A,
+ DB_WINDOW_W - DB_WINDOW_P * 2,
+ DB_WINDOW_P,
+ -1);
} else {
_db_help_triangle(renderer,
- 0x00, 0x00, 0xFF, 0xFF, 608, 16, -1);
+ DB_COLOR_FORE_R, DB_COLOR_FORE_G,
+ DB_COLOR_FORE_B, DB_COLOR_FORE_A,
+ DB_WINDOW_W - DB_WINDOW_P * 2,
+ DB_WINDOW_P,
+ -1);
}
if (text_src_rect.y >= surface->h - text_src_rect.h) {
_db_help_triangle(renderer,
- 0x3F, 0x3F, 0x7F, 0xFF, 608, 456, 1);
+ DB_COLOR_DISA_R, DB_COLOR_DISA_G,
+ DB_COLOR_DISA_B, DB_COLOR_DISA_A,
+ DB_WINDOW_W - DB_WINDOW_P * 2,
+ DB_WINDOW_H - DB_WINDOW_P - DB_SCROLL_H,
+ 1);
} else if (dn_over == 1) {
_db_help_triangle(renderer,
- 0xFF, 0xFF, 0xFF, 0xFF, 608, 456, 1);
+ DB_COLOR_ACTV_R, DB_COLOR_ACTV_G,
+ DB_COLOR_ACTV_B, DB_COLOR_ACTV_A,
+ DB_WINDOW_W - DB_WINDOW_P * 2,
+ DB_WINDOW_H - DB_WINDOW_P - DB_SCROLL_H,
+ 1);
} else {
_db_help_triangle(renderer,
- 0x00, 0x00, 0xFF, 0xFF, 608, 456, 1);
+ DB_COLOR_FORE_R, DB_COLOR_FORE_G,
+ DB_COLOR_FORE_B, DB_COLOR_FORE_A,
+ DB_WINDOW_W - DB_WINDOW_P * 2,
+ DB_WINDOW_H - DB_WINDOW_P - DB_SCROLL_H,
+ 1);
}
SDL_RenderPresent(renderer);
}
diff --git a/src/local.mk b/src/local.mk
index 199a9c4..34594ab 100644
--- a/src/local.mk
+++ b/src/local.mk
@@ -1,6 +1,7 @@
dodge_balls_SOURCES += \
%reldir%/collision.c \
%reldir%/collision.h \
+ %reldir%/defs.h \
%reldir%/game.c \
%reldir%/game.h \
%reldir%/help.c \
diff --git a/src/main-menu.c b/src/main-menu.c
index 919b047..0bd135d 100644
--- a/src/main-menu.c
+++ b/src/main-menu.c
@@ -21,6 +21,7 @@
#include <SDL_ttf.h>
#include <stdlib.h>
#include "collision.h"
+#include "defs.h"
#include "game.h"
#include "help.h"
#include "main-menu.h"
@@ -121,18 +122,18 @@ db_main_menu(void)
renderer = db_get_renderer();
- text_color.r = 0x00;
- text_color.g = 0x00;
- text_color.b = 0xFF;
- text_color.a = 0xFF;
+ text_color.r = DB_COLOR_FORE_R;
+ text_color.g = DB_COLOR_FORE_G;
+ text_color.b = DB_COLOR_FORE_B;
+ text_color.a = DB_COLOR_FORE_A;
- over_color.r = 0xFF;
- over_color.g = 0xFF;
- over_color.b = 0xFF;
- over_color.a = 0xFF;
+ over_color.r = DB_COLOR_ACTV_R;
+ over_color.g = DB_COLOR_ACTV_G;
+ over_color.b = DB_COLOR_ACTV_B;
+ over_color.a = DB_COLOR_ACTV_A;
/* Render title text */
- font = TTF_OpenFont(font_path, 48);
+ font = TTF_OpenFont(font_path, DB_FONT_TITLE_SIZE);
if (font == NULL) {
db_err("Failed to open font (%s)", TTF_GetError());
free(font_path);
@@ -143,11 +144,11 @@ db_main_menu(void)
if (texture_title == NULL) {
goto err;
}
- dest_rect.x = 16;
- dest_rect.y = 16;
+ dest_rect.x = DB_WINDOW_P;
+ dest_rect.y = DB_WINDOW_P;
TTF_CloseFont(font);
- font = TTF_OpenFont(font_path, 16);
+ font = TTF_OpenFont(font_path, DB_FONT_TEXT_SIZE);
if (font == NULL) {
db_err("Failed to open font (%s)", TTF_GetError());
free(font_path);
@@ -179,8 +180,8 @@ db_main_menu(void)
if (buttons[0]->texture_over == NULL) {
goto err;
}
- buttons[0]->rect.x = 640 - 16 - buttons[0]->rect.w;
- buttons[0]->rect.y = 16;
+ buttons[0]->rect.x = DB_WINDOW_W - DB_WINDOW_P - buttons[0]->rect.w;
+ buttons[0]->rect.y = DB_WINDOW_P;
buttons[0]->action = &_db_main_menu_action_help;
buttons[0]->user_data = NULL;
@@ -200,8 +201,8 @@ db_main_menu(void)
if (buttons[1]->texture_over == NULL) {
goto err;
}
- buttons[1]->rect.x = 640 - 16 - buttons[1]->rect.w;
- buttons[1]->rect.y = 48;
+ buttons[1]->rect.x = DB_WINDOW_W - DB_WINDOW_P - buttons[1]->rect.w;
+ buttons[1]->rect.y = DB_WINDOW_P + DB_FONT_TEXT_SIZE + DB_MARGIN;
buttons[1]->action = &_db_main_menu_action_quit;
buttons[1]->user_data = NULL;
@@ -222,17 +223,20 @@ db_main_menu(void)
}
sprintf(name_desc, "%s\n%s", db_game_get_name(games[i]),
db_game_get_desc(games[i]));
- buttons[i + 2]->rect.x = 16;
- buttons[i + 2]->rect.y = 80 + 48 * i + 2;
+ buttons[i + 2]->rect.x = DB_WINDOW_P;
+ buttons[i + 2]->rect.y = DB_WINDOW_P + DB_FONT_TITLE_SIZE +
+ DB_MARGIN + (DB_FONT_TEXT_SIZE * 2 + DB_MARGIN) * i;
buttons[i + 2]->texture_text = _db_main_menu_text(font,
- name_desc, &text_color, 608, renderer,
+ name_desc, &text_color,
+ DB_WINDOW_W - DB_WINDOW_P * 2, renderer,
&buttons[i + 2]->rect);
if (buttons[i + 2]->texture_text == NULL) {
free(name_desc);
goto err;
}
buttons[i + 2]->texture_over = _db_main_menu_text(font,
- name_desc, &over_color, 608, renderer,
+ name_desc, &over_color,
+ DB_WINDOW_W - DB_WINDOW_P * 2, renderer,
&buttons[i + 2]->rect);
free(name_desc);
if (buttons[i + 2]->texture_over == NULL) {
@@ -372,7 +376,9 @@ db_main_menu(void)
if (_db_main_menu_quit > 0) {
break;
}
- SDL_SetRenderDrawColor(renderer, 0x7F, 0x7F, 0x7F, 0xFF);
+ SDL_SetRenderDrawColor(renderer,
+ DB_COLOR_BACK_R, DB_COLOR_BACK_G,
+ DB_COLOR_BACK_B, DB_COLOR_BACK_A);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture_title, NULL, &dest_rect);
for (i = 0; i < n + 2; ++i) {
diff --git a/src/main.c b/src/main.c
index c6da24a..daa37f4 100644
--- a/src/main.c
+++ b/src/main.c
@@ -23,6 +23,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include "defs.h"
#include "main.h"
#include "output.h"
@@ -66,7 +67,7 @@ _db_init(void)
_db_window = SDL_CreateWindow("Dodge Balls",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
- 640, 480, 0);
+ DB_WINDOW_W, DB_WINDOW_H, 0);
if (_db_window == NULL) {
db_err("Failed to create window (%s)", SDL_GetError());
SDL_Quit();
@@ -82,7 +83,7 @@ _db_init(void)
}
_db_texture = SDL_CreateTexture(_db_renderer, SDL_PIXELFORMAT_ARGB8888,
- SDL_TEXTUREACCESS_STATIC, 640, 480);
+ SDL_TEXTUREACCESS_STATIC, DB_WINDOW_W, DB_WINDOW_H);
if (_db_texture == NULL) {
db_err("Failed to create texture (%s)", SDL_GetError());
SDL_DestroyRenderer(_db_renderer);