summaryrefslogtreecommitdiffhomepage
path: root/Stars45/GameWinDX9.cpp
blob: d67035101242325fd3d916643bb936cb0baf2ca4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
    Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
    Copyright (c) 1997-2006, Destroyer Studios LLC.
*/

#include "GameWinDX9.h"

#include <stdio.h>
#include <string.h>

#include "Color.h"
#include "DataLoader.h"
#include "Game.h"
#include "MachineInfo.h"
#include "Panic.h"
#include "Types.h"
#include "Utils.h"


GameWinDX9* GameWinDX9::instance = nullptr;


GameWinDX9*
GameWinDX9::GetInstance()
{
    return instance;
}


GameWinDX9::GameWinDX9() :
    max_tex_size {2048},
    screen_color {Color::Black}
{
    if (instance != nullptr)
        Panic::Panic("Multiple instances of GameWinDX9");
    instance = this;
}


GameWinDX9::~GameWinDX9()
{
    if (instance != nullptr)
        instance = nullptr;
}


bool
GameWinDX9::Init(HINSTANCE hi, HINSTANCE hpi, LPSTR cmdline, int nCmdShow)
{
    status = OK;
    hInst  = hi;

    Print("  Initializing Game\n");

    stats.Clear();

    if (!InitApplication(hInst)) {  // Initialize shared things
        Panic::Panic("Could not initialize the application.");
        status = INIT_FAILED;
    }

    if (status == OK && !video_settings) {
        Panic::Panic("No video settings specified");
        status = INIT_FAILED;
    }

    if (status == OK) {
        static int os_version = MachineInfo::GetPlatform();

        if (os_version == MachineInfo::OS_WIN95 || os_version == MachineInfo::OS_WIN98) {
            Panic::Panic("  Windows 95 and 98 are no longer supported. Please update to Windows XP or higher.");
            status = INIT_FAILED;
        } else if (os_version == MachineInfo::OS_WINNT) {
            Panic::Panic("  D3D not available under WinNT 4");
            status = INIT_FAILED;
        } else if (MachineInfo::GetDirectXVersion() < MachineInfo::DX_9) {
            Panic::Panic("  Insufficient DirectX detected (Dx9 IS REQUIRED)");
            status = INIT_FAILED;
        }
    }

    if (status == OK) {
        Print("\n  Initializing instance...\n");
        // Perform initializations that apply to a specific instance
        if (!InitInstance(hInst, nCmdShow)) {
            Panic::Panic("Could not initialize the instance.");
            status = INIT_FAILED;
        }
    }

    if (status != OK)
        return false;
    return Game::Init(hi, hpi, cmdline, nCmdShow);
}


bool
GameWinDX9::InitApplication(HINSTANCE hInstance)
{
    WNDCLASS  wc;
    LOGBRUSH  brush = { BS_SOLID, RGB(0,0,0), 0 };

    if (server)
    brush.lbColor = RGB(255,255,255);

    // Fill in window class structure with parameters that
    // describe the main window.
    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc   = (WNDPROC) WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(100));
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);

    wc.hbrBackground = CreateBrushIndirect(&brush);
    wc.lpszMenuName  = app_name;
    wc.lpszClassName = app_name;

    // Register the window class and return success/failure code.
    if (RegisterClass(&wc) == 0) {
        DWORD err = GetLastError();

        if (err == 1410) // class already exists, this is OK
        return true;

        else
        Print("WARNING: Register Window Class: %08x\n", err);
    }

    return true;
}


bool
GameWinDX9::InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    hInst = hInstance;

    // center window on display:
    int   screenx  = GetSystemMetrics(SM_CXSCREEN);
    int   screeny  = GetSystemMetrics(SM_CYSCREEN);
    int   x_offset = 0;
    int   y_offset = 0;
    int   s_width  = 800;
    int   s_height = 600;

    if (server) {
        s_width  = 320;
        s_height = 200;
    }

    else if (video_settings) {
        s_width  = video_settings->window_width;
        s_height = video_settings->window_height;
    }

    if (s_width < screenx)
    x_offset = (screenx - s_width) / 2;

    if (s_height < screeny)
    y_offset = (screeny - s_height) / 2;

    // Create a main window for this application instance
    RECT  rctmp;
    rctmp.left   = x_offset;
    rctmp.top    = y_offset;
    rctmp.right  = x_offset + s_width;
    rctmp.bottom = y_offset + s_height;

    window_style =
        WS_OVERLAPPED  | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME |
        WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_VISIBLE;

    AdjustWindowRect(&rctmp, window_style, 1);

    hwnd = CreateWindow(
        app_name,                  // Class name
        app_name,                  // Caption
        window_style,
        x_offset,                  // Position
        y_offset,
        rctmp.right - rctmp.left,  // Size
        rctmp.bottom - rctmp.top,
        0,                         // Parent window (no parent)
        0,                         // use class menu
        hInst,                     // handle to window instance
        0);                        // no params to pass on

    // If window could not be created, return "failure"
    if (!hwnd) {
        Panic::Panic("Could not create window\n");
        return false;
    }

    Print("  Window created.\n");

    // Make the window visible and draw it
    ShowWindow(hwnd, nCmdShow); // Show the window
    UpdateWindow(hwnd);         // Sends WM_PAINT message

    // Save window properties
    window_style = GetWindowLong(hwnd, GWL_STYLE);
    GetWindowRect(hwnd, &bounds_rect);
    GetClientRect(hwnd, &client_rect);

    // Use client area to set video window size
    video_settings->window_width  = client_rect.right - client_rect.left;
    video_settings->window_height = client_rect.bottom - client_rect.top;

    Print("  Instance initialized.\n");
    return true;
}


bool
GameWinDX9::InitGame()
{
    if (server) return true;
    if (!SetupPalette()) {
        Panic::Panic("Could not set up the palette.");
        return false;
    }
    Print("  Palette laoded\n");
    return Game::InitGame();
}


bool
GameWinDX9::SetupPalette()
{
    if (LoadPalette(standard_palette, inverse_palette)) {
        Color::SetPalette(standard_palette, 256, inverse_palette);
        return true;
    }

    return false;
}


bool
GameWinDX9::LoadPalette(PALETTEENTRY* pal, BYTE* inv)
{
    char palheader[32];
    struct {
        WORD Version;
        WORD NumberOfEntries;
        PALETTEENTRY Entries[256];
    } Palette = { 0x300, 256 };

    DataLoader* loader = DataLoader::GetLoader();
    BYTE* block;
    char fname[256];
    sprintf_s(fname, "%s.pal", palette_name);

    if (!loader->LoadBuffer(fname, block)) {
        Print("  Could not open file '%s'\n", fname);
        return false;
    }

    memcpy(&palheader, block, 24);
    memcpy((char*) Palette.Entries, (block+24), 256*4);

    for (int i = 0; i < 256; i++) {
        *pal++ = Palette.Entries[i];
    }

    loader->ReleaseBuffer(block);

    sprintf_s(fname, "%s.ipl", palette_name);
    int size = loader->LoadBuffer(fname, block);
    if (size < 32768) {
        Print("  Could not open file '%s'\n", fname);
        return false;
    }

    memcpy(inv, block, 32768);
    loader->ReleaseBuffer(block);

    return true;
}


int
GameWinDX9::MaxTexSize() const
{
    if (video) {
        int max_from_video = video->MaxTexSize();
        if (max_from_video < max_tex_size)
            return max_from_video;
        return max_tex_size;
    }
    else if (Video* video = Video::GetInstance()) {
        return video->MaxTexSize();
    }
    return 256;
}


int
GameWinDX9::MaxTexAspect() const
{
    if (video)
        return video->MaxTexAspect();
    else if (Video* video = Video::GetInstance())
        return video->MaxTexAspect();
    return 1;
}


Color
GameWinDX9::GetScreenColor() const
{
    return screen_color;
}


int
GameWinDX9::GetScreenWidth() const
{
    if (video)
        return video->Width();
    return 0;
}


int
GameWinDX9::GetScreenHeight() const
{
    if (video)
        return video->Height();
    return 0;
}


void
GameWinDX9::SetMaxTexSize(int n)
{
    if (n >= 64 && n <= 4096)
        max_tex_size = n;
}


void
GameWinDX9::SetScreenColor(Color c)
{
    screen_color = c;
    if (screen)
        screen->SetBackgroundColor(c);
}