1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4
5 Copyright (c) 2013-2016, 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 "menu_utils.h"
30
31 #include <assert.h>
32
33 #include <cdogs/draw/draw.h>
34 #include <cdogs/draw/draw_actor.h>
35 #include <cdogs/font.h>
36
37
38 // Display a character and the player name above it, with the character
39 // centered around the target position
DisplayCharacterAndName(struct vec2i pos,const Character * c,const direction_e d,const char * name,const color_t color,const WeaponClass * gun)40 void DisplayCharacterAndName(
41 struct vec2i pos, const Character *c, const direction_e d,
42 const char *name, const color_t color, const WeaponClass *gun)
43 {
44 // Move the point down a bit since the default character draw point is at
45 // its feet
46 pos.y += 8;
47 struct vec2i namePos = svec2i_add(pos, svec2i(-FontStrW(name) / 2, -30));
48 DrawCharacterSimple(c, pos, d, false, false, gun);
49 FontStrMask(name, namePos, color);
50 }
51
MenuDisplayPlayer(const menu_t * menu,GraphicsDevice * g,const struct vec2i pos,const struct vec2i size,const void * data)52 void MenuDisplayPlayer(
53 const menu_t *menu, GraphicsDevice *g,
54 const struct vec2i pos, const struct vec2i size, const void *data)
55 {
56 UNUSED(g);
57 const MenuDisplayPlayerData *d = data;
58 struct vec2i playerPos;
59 char s[22];
60 UNUSED(menu);
61 struct vec2i dPos = pos;
62 dPos.x -= size.x; // move to left half of screen
63 playerPos = svec2i(
64 dPos.x + size.x * 3 / 4 - 12 / 2, CENTER_Y(dPos, size, 0) - 12);
65
66 PlayerData *pData = PlayerDataGetByUID(d->PlayerUID);
67 if (d->currentMenu && strcmp((*d->currentMenu)->name, "Name") == 0)
68 {
69 sprintf(s, "%c%s%c", '>', pData->name, '<');
70 }
71 else
72 {
73 strcpy(s, pData->name);
74 }
75 const WeaponClass *gun = NULL;
76 if (d->GunIdx >= 0 && d->GunIdx < MAX_WEAPONS)
77 {
78 gun = pData->guns[d->GunIdx];
79 }
80
81 DisplayCharacterAndName(playerPos, &pData->Char, d->Dir, s, colorWhite, gun);
82 }
83
MenuDisplayPlayerControls(const menu_t * menu,GraphicsDevice * g,const struct vec2i pos,const struct vec2i size,const void * data)84 void MenuDisplayPlayerControls(
85 const menu_t *menu, GraphicsDevice *g,
86 const struct vec2i pos, const struct vec2i size, const void *data)
87 {
88 UNUSED(g);
89 char s[256];
90 const int *playerUID = data;
91 const int y = pos.y + size.y - FontH();
92
93 UNUSED(menu);
94
95 const PlayerData *pData = PlayerDataGetByUID(*playerUID);
96 char directionNames[256];
97 InputGetDirectionNames(
98 directionNames, pData->inputDevice, pData->deviceIndex);
99 switch (pData->inputDevice)
100 {
101 case INPUT_DEVICE_KEYBOARD:
102 {
103 char button1[256], button2[256];
104 InputGetButtonName(
105 pData->inputDevice, pData->deviceIndex, CMD_BUTTON1, button1);
106 InputGetButtonName(
107 pData->inputDevice, pData->deviceIndex, CMD_BUTTON2, button2);
108 sprintf(s, "(%s,\n%s and %s)", directionNames, button1, button2);
109 FontStr(s, svec2i(pos.x - FontStrW(s) / 2, y - FontH()));
110 }
111 break;
112 case INPUT_DEVICE_MOUSE:
113 sprintf(s, "(%s to scroll,\nleft and right click)", directionNames);
114 FontStr(s, svec2i(pos.x - FontStrW(s) / 2, y - FontH()));
115 break;
116 case INPUT_DEVICE_JOYSTICK:
117 {
118 sprintf(s, "(%s,",
119 InputDeviceName(pData->inputDevice, pData->deviceIndex));
120 struct vec2i textPos = svec2i(pos.x - FontStrW(s) / 2, y - FontH());
121 FontStr(s, textPos);
122 textPos.y += FontH();
123 color_t c = colorWhite;
124 InputGetButtonNameColor(
125 pData->inputDevice, pData->deviceIndex, CMD_BUTTON1, s, &c);
126 textPos = FontStrMask(s, textPos, c);
127 textPos = FontStr(" and ", textPos);
128 c = colorWhite;
129 InputGetButtonNameColor(
130 pData->inputDevice, pData->deviceIndex, CMD_BUTTON2, s, &c);
131 textPos = FontStrMask(s, textPos, c);
132 FontStr(")", textPos);
133 }
134 break;
135 case INPUT_DEVICE_AI:
136 sprintf(s, "(%s)",
137 InputDeviceName(pData->inputDevice, pData->deviceIndex));
138 FontStr(s, svec2i(pos.x - FontStrW(s) / 2, y));
139 break;
140 case INPUT_DEVICE_UNSET:
141 strcpy(s, "(no device; plug in a controller)");
142 FontStr(s, svec2i(pos.x - FontStrW(s) / 2, y));
143 break;
144 default:
145 CASSERT(false, "unknown device");
146 break;
147 }
148 }
149