Debian Codegraph @

src/credits.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4
5 Copyright (c) 2013-2014, 2020 Cong Xu
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
13 Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28 */
29 #include "credits.h"
30
31 #include <assert.h>
32 #include <stdio.h>
33 #include <string.h>
34
35 #include <cdogs/config.h>
36 #include <cdogs/files.h>
37 #include <cdogs/font.h>
38 #include <cdogs/utils.h>
39
40 void LoadCredits(
41 credits_displayer_t *displayer, color_t nameColor, color_t textColor)
42 {
43 char buf[CDOGS_PATH_MAX];
44 int nameOrMessageCounter = 0;
45 FILE *file;
46
47 GetDataFilePath(buf, "doc/CREDITS");
48 file = fopen(buf, "r");
49 if (file == NULL)
50 {
51 printf("Error: cannot load %s\n", buf);
52 return;
53 }
54
55 assert(displayer != NULL);
56
57 CArrayInit(&displayer->credits, sizeof(credit_t));
58 displayer->lastUpdateTime = time(NULL);
59 displayer->creditsIndex = 0;
60 displayer->nameColor = nameColor;
61 displayer->textColor = textColor;
62 char nameBuf[256];
63 nameBuf[0] = '\0';
64 while (fgets(buf, CDOGS_PATH_MAX, file) != NULL)
65 {
66 if (strlen(buf) > 0 && buf[strlen(buf) - 1] == '\n')
67 {
68 buf[strlen(buf) - 1] = '\0';
69 }
70 if (strlen(buf) == 0)
71 {
72 nameOrMessageCounter = 0;
73 continue;
74 }
75 if (nameOrMessageCounter == 0)
76 {
77 strcpy(nameBuf, buf);
78 nameOrMessageCounter = 1;
79 }
80 else
81 {
82 credit_t credit;
83 CSTRDUP(credit.name, nameBuf);
84 CSTRDUP(credit.message, buf + 1);
85 CArrayPushBack(&displayer->credits, &credit);
86 nameOrMessageCounter = 0;
87 }
88 }
89
90 fclose(file);
91 }
92
93 void UnloadCredits(credits_displayer_t *displayer)
94 {
95 if (displayer == NULL)
96 {
97 return;
98 }
99 CA_FOREACH(credit_t, credit, displayer->credits)
100 CFREE(credit->name);
101 CFREE(credit->message);
102 CA_FOREACH_END()
103 CArrayTerminate(&displayer->credits);
104 displayer->creditsIndex = 0;
105 }
106
107 #define CREDIT_DISPLAY_PERIOD_SECONDS 10.0
108 void ShowCredits(credits_displayer_t *displayer)
109 {
110 CASSERT(displayer != NULL, "null pointer");
111 if ((int)displayer->credits.size > 0)
112 {
113 time_t now = time(NULL);
114 credit_t *credits =
115 CArrayGet(&displayer->credits, displayer->creditsIndex);
116 int y = gGraphicsDevice.cachedConfig.Res.y - 50;
117
118 FontStrMask("Credits:", svec2i(16, y), displayer->textColor);
119 y += FontH() + 2;
120 FontStrMask(credits->name, svec2i(20, y), displayer->nameColor);
121 y += FontH();
122 FontStrMask(credits->message, svec2i(20, y), displayer->textColor);
123
124 if (difftime(now, displayer->lastUpdateTime) >
125 CREDIT_DISPLAY_PERIOD_SECONDS)
126 {
127 displayer->creditsIndex++;
128 if (displayer->creditsIndex >= (int)displayer->credits.size)
129 {
130 displayer->creditsIndex = 0;
131 }
132 displayer->lastUpdateTime = now;
133 }
134 }
135 }