summaryrefslogtreecommitdiff
path: root/src/windows/swimp.c
blob: 0f102d0a3fd14cfe9f5ab96840e80b806e4159a7 (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
/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

/*
RW_IMP.C

This file contains ALL Win32 specific stuff having to do with the
software refresh.  When a port is being made the following functions
must be implemented by the port:

SWimp_EndFrame
SWimp_Init
SWimp_SetPalette
SWimp_Shutdown
*/

#include "client.h"

typedef struct {
    HDC         dibdc;      // DC compatible with DIB section
    HBITMAP     dibsect;    // DIB section
    byte        *pixels;    // DIB base pointer, NOT used directly for rendering!
    HGDIOBJ     prevobj;
} sww_t;

static sww_t sww;

/*
SWimp_Shutdown

System specific graphics subsystem shutdown routine.
Destroys DIB surfaces as appropriate.
*/
void VID_Shutdown(void)
{
    if (sww.dibdc) {
        SelectObject(sww.dibdc, sww.prevobj);
        DeleteDC(sww.dibdc);
    }

    if (sww.dibsect) {
        DeleteObject(sww.dibsect);
    }

    memset(&sww, 0, sizeof(sww));

    Win_Shutdown();
}

void SWimp_ModeChanged(void)
{
    BITMAPINFO info;

    if (!sww.dibdc) {
        return;
    }

    // destroy previous DIB section
    if (sww.dibsect) {
        SelectObject(sww.dibdc, sww.prevobj);
        DeleteObject(sww.dibsect);
    }

    // fill in the BITMAPINFO struct
    memset(&info, 0, sizeof(info));

    info.bmiHeader.biSize           = sizeof(BITMAPINFOHEADER);
    info.bmiHeader.biWidth          = win.rc.width;
    info.bmiHeader.biHeight         = win.rc.height;
    info.bmiHeader.biPlanes         = 1;
    info.bmiHeader.biCompression    = BI_RGB;
    info.bmiHeader.biBitCount       = 32;

    // create the DIB section
    sww.dibsect = CreateDIBSection(win.dc,
                                   &info,
                                   DIB_RGB_COLORS,
                                   (void **)&sww.pixels,
                                   NULL,
                                   0);

    if (!sww.dibsect) {
        Com_Error(ERR_FATAL, "DIB_Init: CreateDIBSection failed");
    }

    if (info.bmiHeader.biHeight > 0) {
        // bottom up
        win.buffer  = sww.pixels + (win.rc.height - 1) * win.rc.width * 4;
        win.pitch   = -win.rc.width * 4;
    } else {
        // top down
        win.buffer  = sww.pixels;
        win.pitch   = win.rc.width * 4;
    }

    // clear the DIB memory buffer
    memset(sww.pixels, 0, win.rc.width * win.rc.height * 4);

    sww.prevobj = SelectObject(sww.dibdc, sww.dibsect);
    if (!sww.prevobj) {
        Com_Error(ERR_FATAL, "DIB_Init: SelectObject failed\n");
    }
}


/*
SWimp_Init

This routine is responsible for initializing the implementation
specific stuff in a software rendering subsystem.
*/
qboolean VID_Init(void)
{
    // create the window
    Win_Init();

    // set display mode
    Win_SetMode();

    // create logical DC
    sww.dibdc = CreateCompatibleDC(win.dc);
    if (!sww.dibdc) {
        Com_EPrintf("DIB_Init: CreateCompatibleDC failed with error %lu\n", GetLastError());
        Win_Shutdown();
        return qfalse;
    }

    // call SWimp_ModeChanged and friends
    Win_ModeChanged();
    return qtrue;
}

void VID_VideoWait(void)
{
}

qboolean VID_VideoSync(void)
{
    return qtrue;
}

void VID_BeginFrame(void)
{
}

/*
SWimp_EndFrame

This does an implementation specific copy from the backbuffer to the
front buffer.  In the Win32 case it uses BitBlt if we're using DIB sections/GDI.
*/
void VID_EndFrame(void)
{
    BitBlt(win.dc, 0, 0, win.rc.width, win.rc.height, sww.dibdc, 0, 0, SRCCOPY);
}