Debian Codegraph @

src/cdogsed/campaign_options.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2020-2021, 2023 Cong Xu
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "campaign_options.h"
29
30 #include "nk_window.h"
31
32 #define WIDTH 800
33 #define HEIGHT 350
34 #define ROW_HEIGHT 25
35
36 typedef struct
37 {
38 Campaign *c;
39 char Title[256];
40 char Author[256];
41 char Description[1024];
42 EditorResult result;
43 } CampaignOptionsData;
44
45 static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data);
46 static void CheckTextChanged(
47 char **dst, const char *src, EditorResult *result);
48 EditorResult EditCampaignOptions(EventHandlers *handlers, Campaign *c)
49 {
50 NKWindowConfig cfg;
51 memset(&cfg, 0, sizeof cfg);
52 cfg.Title = "Campaign Options";
53 cfg.Size = svec2i(WIDTH, HEIGHT);
54 color_t bg = {41, 26, 26, 255};
55 cfg.BG = bg;
56 cfg.Handlers = handlers;
57 cfg.Draw = Draw;
58
59 NKWindowInit(&cfg);
60
61 CampaignOptionsData data;
62 memset(&data, 0, sizeof data);
63 data.c = c;
64 strcpy(data.Title, c->Setting.Title);
65 strcpy(data.Author, c->Setting.Author);
66 strcpy(data.Description, c->Setting.Description);
67 cfg.DrawData = &data;
68
69 NKWindow(cfg);
70
71 CheckTextChanged(&c->Setting.Title, data.Title, &data.result);
72 CheckTextChanged(&c->Setting.Author, data.Author, &data.result);
73 CheckTextChanged(&c->Setting.Description, data.Description, &data.result);
74 return data.result;
75 }
76 static void CheckTextChanged(char **dst, const char *src, EditorResult *result)
77 {
78 if (strcmp(src, *dst) != 0)
79 {
80 CFREE(*dst);
81 CSTRDUP(*dst, src);
82 *result = EDITOR_RESULT_CHANGED;
83 }
84 }
85
86 static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data)
87 {
88 UNUSED(win);
89 bool changed = false;
90 CampaignOptionsData *cData = data;
91
92 if (nk_begin(
93 ctx, "Campaign Options", nk_rect(0, 0, WIDTH, HEIGHT),
94 NK_WINDOW_BORDER | NK_WINDOW_TITLE))
95 {
96 nk_layout_row_dynamic(ctx, ROW_HEIGHT, 1);
97 DrawTextbox(ctx, cData->Title, 256, "Title", NK_EDIT_FIELD);
98 DrawTextbox(ctx, cData->Author, 256, "Author", NK_EDIT_FIELD);
99 nk_layout_row_dynamic(ctx, ROW_HEIGHT * 3, 1);
100 DrawTextbox(ctx, cData->Description, 1024, "Description", NK_EDIT_BOX);
101 nk_layout_row_dynamic(ctx, ROW_HEIGHT, 1);
102 if (DrawCheckbox(
103 ctx, "Ammo",
104 "Enable ammo; if disabled all weapons have infinite ammo and "
105 "use up score instead",
106 &cData->c->Setting.Ammo))
107 {
108 changed = true;
109 }
110 if (DrawCheckbox(
111 ctx, "Skip weapon menu", "Skip weapon menu before missions",
112 &cData->c->Setting.SkipWeaponMenu))
113 {
114 changed = true;
115 if (cData->c->Setting.SkipWeaponMenu)
116 {
117 cData->c->Setting.BuyAndSell = false;
118 }
119 }
120 if (DrawCheckbox(
121 ctx, "Buy and sell",
122 "Enable buying/selling guns, ammo and equipment",
123 &cData->c->Setting.BuyAndSell))
124 {
125 changed = true;
126 if (cData->c->Setting.BuyAndSell)
127 {
128 cData->c->Setting.SkipWeaponMenu = false;
129 }
130 }
131 if (DrawCheckbox(
132 ctx, "Random pickups",
133 "Enable randomly spawned ammo/health pickups",
134 &cData->c->Setting.RandomPickups))
135 {
136 changed = true;
137 }
138 if (DrawNumberSlider(
139 ctx, "Door open ticks",
140 "Number of ticks that doors stay open (" TOSTRING(
141 FPS_FRAMELIMIT) " = 1 second)",
142 0, 700, 10, &cData->c->Setting.DoorOpenTicks))
143 {
144 changed = true;
145 }
146 if (DrawNumberSlider(
147 ctx, "Lives", "(0 = use game option)", 0, 5, 1,
148 &cData->c->Setting.MaxLives))
149 {
150 changed = true;
151 }
152 if (DrawNumberSlider(
153 ctx, "Max lives", "(0 = use game option)", 0, 5, 1,
154 &cData->c->Setting.MaxLives))
155 {
156 changed = true;
157 }
158 // Clamp Lives within MaxLives
159 if (changed && cData->c->Setting.MaxLives > 0)
160 {
161 cData->c->Setting.Lives =
162 MIN(cData->c->Setting.Lives, cData->c->Setting.MaxLives);
163 }
164 if (DrawNumberSlider(
165 ctx, "Player HP", "Starting player HP (0 = use max HP)", 0,
166 1000, 10, &cData->c->Setting.PlayerHP))
167 {
168 changed = true;
169 }
170 if (DrawNumberSlider(
171 ctx, "Player max HP", "(0 = use game option)", 0, 1000, 5,
172 &cData->c->Setting.PlayerMaxHP))
173 {
174 changed = true;
175 }
176 if (DrawNumberSlider(
177 ctx, "Player excess HP", "Max HP for items that heal over max HP", 0, 1000, 5,
178 &cData->c->Setting.PlayerExcessHP))
179 {
180 changed = true;
181 }
182 // Clamp PlayerHP, PlayerMaxHP and PlayerExcessHP
183 if (changed && cData->c->Setting.PlayerMaxHP > 0)
184 {
185 cData->c->Setting.PlayerHP =
186 MIN(cData->c->Setting.PlayerHP, cData->c->Setting.PlayerMaxHP);
187 cData->c->Setting.PlayerExcessHP =
188 MAX(cData->c->Setting.PlayerExcessHP, cData->c->Setting.PlayerMaxHP);
189 }
190 nk_end(ctx);
191 }
192 if (changed)
193 {
194 cData->result = EDITOR_RESULT_CHANGED;
195 }
196 return true;
197 }