aboutsummaryrefslogtreecommitdiff
path: root/src/parsec/vsdl_buffo.cpp
blob: 0b6ad5a9a2795f50b8d6442bcfbe2cc72317a2fe (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
/*
 * PARSEC - Frame Buffer Functions (OPENGL)
 *
 * $Author: uberlinuxguy $ - $Date: 2004/09/15 12:25:37 $
 *
 * Orginally written by:
 *   Copyright (c) Markus Hadwiger     <msh@parsec.org>   1999
 *   Copyright (c) Clemens Beer        <cbx@parsec.org>   1999
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */ 
#include "config.h"

// C library
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// compilation flags/debug support
#include "config.h"
#include "debug.h"

// general definitions
#include "general.h"
#include "objstruc.h"

// global externals
#include "globals.h"

// subsystem headers
#include "vid_defs.h"

// local module header
#include "vsdl_buffo.h"

// opengl headers
#include "r_gl.h"

// proprietary module headers
#include "con_aux.h"
#include "ro_api.h"
#include "ro_supp.h"
#include "vsdl_ogl.h"


// clear entire rendering buffer ----------------------------------------------
//
void VIDs_ClearRenderBuffer()
{
	glClearColor( 0, 0, 0, 0 );
	glClearDepth( 0.0 );

	//NOTE:
	// when clearing the depth buffer, write access has
	// to be enabled which is disabled on function entry.
	// interestingly enough, on TNT drivers this doesn't
	// matter, since the mask is completely ignored by
	// the driver when clearing the buffer. however, this
	// is not the case on a Rage128, for instance.

	if ( !AUX_DISABLE_BUFFER_CLEAR && !AUX_DISABLE_ZBUFFER_CLEAR ) {

		// simultaneously clear color and depth buffers
		RO_DepthMask( GL_TRUE );
		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
		RO_DepthMask( GL_FALSE );

	} else if ( !AUX_DISABLE_BUFFER_CLEAR ) {

		// clear color buffer only
		glClear( GL_COLOR_BUFFER_BIT );

	} else if ( !AUX_DISABLE_ZBUFFER_CLEAR ) {

		// clear depth buffer only
		RO_DepthMask( GL_TRUE );
		glClear( GL_DEPTH_BUFFER_BIT );
		RO_DepthMask( GL_FALSE );
	}
}


// commit render buffer (make backbuffer visible) -----------------------------
//
void VIDs_CommitRenderBuffer()
{
	// swap buffers
	VSDL_CommitOGLBuff();
}


// create/deinitialize screenshot buffer --------------------------------------
//
char *VIDs_ScreenshotBuffer( int create, int *size )
{
	//NOTE:
	// this function must be called twice in order to
	// make a screenshot.
	// first, to get a pointer to the screenshot buffer
	// and its size (create==TRUE). like:
	// shotbuff = VIDs_ScreenshotBuffer( TRUE, &bufsiz );
	// second, to deallocate the buffer (create==FALSE).
	// like: VIDs_ScreenshotBuffer( FALSE, NULL );

	// temporary screenshot buffer
	static char *buffer = NULL;

	// validate parameters with respect to buffer allocation
	ASSERT( (  create && ( buffer == NULL ) && ( size != NULL ) ) ||
			( !create && ( buffer != NULL ) && ( size == NULL ) ) );

	if ( create ) {

		// alloc screenshot buffer
		*size  = Screen_Width * Screen_Height * 3;
		buffer = (char *) ALLOCMEM( *size );
		if ( buffer == NULL ) {
			return NULL;
		}

		// alloc temp readpixels buffer
		char *readbuffer = NULL;
		readbuffer = (char *) ALLOCMEM( *size );
		if ( readbuffer == NULL ) {
			return NULL;
		}

		// assert that screenwidth is even
		ASSERT( ( Screen_Width & 0x01 ) == 0 );

		// read from back buffer
		glReadBuffer( GL_BACK );

		// read RGB data from the backbuffer
		glReadPixels( 0, 0, Screen_Width, Screen_Height, GL_RGB, GL_UNSIGNED_BYTE, readbuffer );

		size_t scanlinelen = (Screen_Width * 3);

		char *readpo  = readbuffer + ((Screen_Height-1) * scanlinelen);
		char *writepo = buffer;

		// flip the image upside down
		for ( int i = 0; i < Screen_Height; i++ ) {

			memcpy( writepo, readpo, scanlinelen );

			writepo += scanlinelen;
			readpo  -= scanlinelen;
		}

		FREEMEM( readbuffer );

	} else if ( buffer != NULL ) {

		// free screenshot buffer
		FREEMEM( buffer );
		buffer = NULL;
	}

	return buffer;
}