| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/ddgr_win32/ddgrWin32Init.cpp $ |
| 21 | * $Revision: 6 $ |
| 22 | * $Date: 11/01/98 1:58a $ |
| 23 | * $Author: Jeff $ |
| 24 | * |
| 25 | * DDGR v2.0 Win32 Implementation. |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/ddgr_win32/ddgrWin32Init.cpp $ |
| 28 | * |
| 29 | * 6 11/01/98 1:58a Jeff |
| 30 | * converted the vsprintf calls to use the Pvsprintf, which is a safe |
| 31 | * vsprintf, no buffer overflows allowed |
| 32 | * |
| 33 | * 5 3/18/98 8:03p Samir |
| 34 | * if library aint initialized don't try to close! |
| 35 | * |
| 36 | * 4 12/23/97 6:16p Samir |
| 37 | * Moved ddgr_Close to header. |
| 38 | * |
| 39 | * 3 9/12/97 4:13p Samir |
| 40 | * Added some private data access functions and more DirectX |
| 41 | * functionality. |
| 42 | * |
| 43 | * 2 6/16/97 4:46p Samir |
| 44 | * OpenGL works in window too. |
| 45 | * |
| 46 | * 1 6/12/97 6:33p Samir |
| 47 | * Initial revision |
| 48 | * |
| 49 | * $NoKeywords: $ |
| 50 | */ |
| 51 | |
| 52 | #include <cstdarg> |
| 53 | #include <cstdio> |
| 54 | |
| 55 | #include "ddgrWin32.h" |
| 56 | #include "ddgrWin32GDI.h" |
| 57 | #include "ddgrWin32DX.h" |
| 58 | #include "pserror.h" |
| 59 | |
| 60 | #include <stdlib.h> |
| 61 | #include <string.h> |
| 62 | |
| 63 | /* DDGR_WIN32 Library |
| 64 | v2.0 enhancements = concept of graphic subsystem when initializing. |
| 65 | removal of blting routines. |
| 66 | reduced role in graphic implementation (video and bitmaps management) |
| 67 | |
| 68 | subsystems supported: |
| 69 | GDI32 (fullscreen/windowed) |
| 70 | */ |
| 71 | char *DDGR_subsystem_names[] = {"GDI32", // Standard Win32 GDI DIBs |
| 72 | "GDIX", // GDI and DirectDraw for mode settings. |
| 73 | "DX", // Direct X! |
| 74 | NULL}; |
| 75 | |
| 76 | int DDGR_subsystems[] = {DDGR_GDI_SUBSYSTEM, DDGR_GDIX_SUBSYSTEM, DDGR_DX_SUBSYSTEM, -1}; |
| 77 | |
| 78 | tDDGRInternalData DDGR_lib_data; // Library info. |
| 79 | // HACKS!! |
| 80 | |
| 81 | static int DDGR_subsysid = 0; |
| 82 | static bool DDGR_fullscreen; |
| 83 | |
| 84 | /* Functions internal to the Init module |
| 85 | */ |
| 86 | |
| 87 | /* ddgr_Init |
| 88 | app = application object. |
| 89 | subsystem = subsystem name ('DirectDraw', 'GDI') |
| 90 | fullscreen = whether it's full screen or windowed |
| 91 | */ |
| 92 | bool ddgr_Init(oeApplication *app, char *subsystem, bool fullscreen) { |
| 93 | static bool first_time = true; |
| 94 | int subsys_id; |
| 95 | |
| 96 | // close old systems |
| 97 | if (first_time) { |
| 98 | atexit(ddgr_Close); |
| 99 | first_time = false; |
| 100 | } else if (LIB_DATA(init)) { |
| 101 | ddgr_Close(); |
| 102 | } |
| 103 | |
| 104 | // find subsystem id based off of subsystem requested. |
| 105 | for (subsys_id = 0; DDGR_subsystems[subsys_id] != -1; subsys_id++) { |
| 106 | if (strcmp(DDGR_subsystem_names[subsys_id], subsystem) == 0) |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | if (DDGR_subsystems[subsys_id] == -1) |
| 111 | ddgr_FatalError("Subsystem %s not found during startup.", subsystem); |
| 112 | |
| 113 | LIB_DATA(subsystem) = DDGR_subsystems[subsys_id]; |
| 114 | |
| 115 | // Initialize that subsystem |
| 116 | switch (DDGR_subsystems[subsys_id]) { |
| 117 | case DDGR_GDI_SUBSYSTEM: |
| 118 | ddgr_gdi_Init(app, fullscreen, false); |
| 119 | break; |
| 120 | case DDGR_GDIX_SUBSYSTEM: |
| 121 | ddgr_gdi_Init(app, fullscreen, true); |
| 122 | break; |
| 123 | case DDGR_DX_SUBSYSTEM: |
| 124 | ddgr_dx_Init(app); |
| 125 | break; |
| 126 | |
| 127 | default: |
| 128 | Int3(); |
| 129 | } |
| 130 | |
| 131 | LIB_DATA(init) = true; |
| 132 | DDGR_subsysid = subsys_id; |
| 133 | DDGR_fullscreen = fullscreen; |
| 134 | |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | void ddgr_Close() { |
| 139 | if (!LIB_DATA(init)) |
| 140 | return; |
| 141 | |
| 142 | switch (LIB_DATA(subsystem)) { |
| 143 | case DDGR_GDIX_SUBSYSTEM: |
| 144 | case DDGR_GDI_SUBSYSTEM: |
| 145 | ddgr_gdi_Close(); |
| 146 | break; |
| 147 | case DDGR_DX_SUBSYSTEM: |
| 148 | ddgr_dx_Close(); |
| 149 | break; |
| 150 | default: |
| 151 | Int3(); |
| 152 | } |
| 153 | |
| 154 | LIB_DATA(subsystem) = 0; |
| 155 | LIB_DATA(init) = false; |
| 156 | } |
| 157 | |
| 158 | void ddgr_GetSubsystem(char *name, bool *fullscreen) { |
| 159 | strcpy(name, DDGR_subsystem_names[DDGR_subsysid]); |
| 160 | *fullscreen = DDGR_fullscreen; |
| 161 | } |
| 162 | |
| 163 | /* DDGR Internal Error System |
| 164 | This error system will display an error message when a fatal graphics problem occurs. |
| 165 | To display error properly when video screen is in a special mode, we must close down all |
| 166 | graphics and show this error. We also handle multiple errors, so we keep a stack of |
| 167 | error messages, and show them when we're finished. |
| 168 | */ |
| 169 | |
| 170 | #define DDGR_MAX_ERRORS 10 |
| 171 | |
| 172 | static char *DDGR_error_msgs[DDGR_MAX_ERRORS]; |
| 173 | static int DDGR_num_msgs = 0; |
| 174 | |
| 175 | void ddgr_FatalError(const char *fmt, ...) { |
| 176 | // create our error list and flag a system error |
| 177 | char buf[768]; |
| 178 | |
| 179 | // get subsystem name. |
| 180 | int i; |
| 181 | for (i = 0; DDGR_subsystems[i] != -1; i++) { |
| 182 | if (DDGR_subsystems[i] == LIB_DATA(subsystem)) { |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // create whole error message |
| 188 | std::va_list arglist; |
| 189 | |
| 190 | va_start(arglist, fmt); |
| 191 | std::vsnprintf(buf, 768, fmt, arglist); |
| 192 | va_end(arglist); |
| 193 | |
| 194 | if (DDGR_subsystems[i] = !-1) { |
| 195 | strcat(buf, "\n\nSubsystem: "); |
| 196 | strcat(buf, DDGR_subsystem_names[i]); |
| 197 | } |
| 198 | strcat(buf, "\n\nError stack:"); |
| 199 | for (i = 0; i < DDGR_num_msgs; i++) { |
| 200 | strcat(buf, "\n"); |
| 201 | strcat(buf, DDGR_error_msgs[i]); |
| 202 | delete[] DDGR_error_msgs[i]; |
| 203 | } |
| 204 | |
| 205 | Error(buf); |
| 206 | } |
| 207 | |
| 208 | void ddgr_PushError(const char *fmt, ...) { |
| 209 | std::va_list arglist; |
| 210 | char buf[128]; |
| 211 | |
| 212 | va_start(arglist, fmt); |
| 213 | std::vsnprintf(buf, 128, fmt, arglist); |
| 214 | va_end(arglist); |
| 215 | |
| 216 | DDGR_error_msgs[DDGR_num_msgs] = new char[strlen(buf) + 1]; |
| 217 | strcpy(DDGR_error_msgs[DDGR_num_msgs], buf); |
| 218 | DDGR_num_msgs++; |
| 219 | } |