Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
194 lines (172 loc) · 5.2 KB

testgamma.c

File metadata and controls

194 lines (172 loc) · 5.2 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
3
4
5
6
7
8
9
10
/* Bring up a window and manipulate the gamma on it */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "SDL.h"
Sep 28, 2005
Sep 28, 2005
11
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
May 28, 2006
May 28, 2006
12
static void
May 29, 2006
May 29, 2006
13
quit(int rc)
Sep 28, 2005
Sep 28, 2005
14
{
May 29, 2006
May 29, 2006
15
16
SDL_Quit();
exit(rc);
Sep 28, 2005
Sep 28, 2005
17
18
}
Apr 26, 2001
Apr 26, 2001
19
/* Turn a normal gamma value into an appropriate gamma ramp */
May 28, 2006
May 28, 2006
20
void
May 29, 2006
May 29, 2006
21
CalculateGamma(double gamma, Uint16 * ramp)
Apr 26, 2001
Apr 26, 2001
22
{
May 28, 2006
May 28, 2006
23
24
25
26
int i, value;
gamma = 1.0 / gamma;
for (i = 0; i < 256; ++i) {
May 29, 2006
May 29, 2006
27
value = (int) (pow((double) i / 256.0, gamma) * 65535.0 + 0.5);
May 28, 2006
May 28, 2006
28
29
30
31
32
if (value > 65535) {
value = 65535;
}
ramp[i] = (Uint16) value;
}
Apr 26, 2001
Apr 26, 2001
33
34
35
}
/* This can be used as a general routine for all of the test programs */
May 28, 2006
May 28, 2006
36
int
May 29, 2006
May 29, 2006
37
get_video_args(char *argv[], int *w, int *h, int *bpp, Uint32 * flags)
Apr 26, 2001
Apr 26, 2001
38
{
May 28, 2006
May 28, 2006
39
40
41
42
43
44
45
46
int i;
*w = 640;
*h = 480;
*bpp = 0;
*flags = SDL_SWSURFACE;
for (i = 1; argv[i]; ++i) {
May 29, 2006
May 29, 2006
47
if (strcmp(argv[i], "-width") == 0) {
May 28, 2006
May 28, 2006
48
if (argv[i + 1]) {
May 29, 2006
May 29, 2006
49
*w = atoi(argv[++i]);
May 28, 2006
May 28, 2006
50
}
May 29, 2006
May 29, 2006
51
} else if (strcmp(argv[i], "-height") == 0) {
May 28, 2006
May 28, 2006
52
if (argv[i + 1]) {
May 29, 2006
May 29, 2006
53
*h = atoi(argv[++i]);
May 28, 2006
May 28, 2006
54
}
May 29, 2006
May 29, 2006
55
} else if (strcmp(argv[i], "-bpp") == 0) {
May 28, 2006
May 28, 2006
56
if (argv[i + 1]) {
May 29, 2006
May 29, 2006
57
*bpp = atoi(argv[++i]);
May 28, 2006
May 28, 2006
58
}
May 29, 2006
May 29, 2006
59
} else if (strcmp(argv[i], "-fullscreen") == 0) {
May 28, 2006
May 28, 2006
60
*flags |= SDL_FULLSCREEN;
May 29, 2006
May 29, 2006
61
} else if (strcmp(argv[i], "-hw") == 0) {
May 28, 2006
May 28, 2006
62
*flags |= SDL_HWSURFACE;
May 29, 2006
May 29, 2006
63
} else if (strcmp(argv[i], "-hwpalette") == 0) {
May 28, 2006
May 28, 2006
64
65
66
67
68
*flags |= SDL_HWPALETTE;
} else
break;
}
return i;
Apr 26, 2001
Apr 26, 2001
69
70
}
May 28, 2006
May 28, 2006
71
int
May 29, 2006
May 29, 2006
72
main(int argc, char *argv[])
Apr 26, 2001
Apr 26, 2001
73
{
May 28, 2006
May 28, 2006
74
75
76
77
78
79
80
81
82
83
84
SDL_Surface *screen;
SDL_Surface *image;
float gamma;
int i;
int w, h, bpp;
Uint32 flags;
Uint16 ramp[256];
Uint16 red_ramp[256];
Uint32 then, timeout;
/* Check command line arguments */
May 29, 2006
May 29, 2006
85
argv += get_video_args(argv, &w, &h, &bpp, &flags);
May 28, 2006
May 28, 2006
86
87
/* Initialize SDL */
May 29, 2006
May 29, 2006
88
89
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
May 28, 2006
May 28, 2006
90
91
92
93
return (1);
}
/* Initialize the display, always use hardware palette */
May 29, 2006
May 29, 2006
94
screen = SDL_SetVideoMode(w, h, bpp, flags | SDL_HWPALETTE);
May 28, 2006
May 28, 2006
95
if (screen == NULL) {
May 29, 2006
May 29, 2006
96
97
98
fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
w, h, SDL_GetError());
quit(1);
May 28, 2006
May 28, 2006
99
100
101
}
/* Set the window manager title bar */
May 29, 2006
May 29, 2006
102
SDL_WM_SetCaption("SDL gamma test", "testgamma");
May 28, 2006
May 28, 2006
103
104
105
106
/* Set the desired gamma, if any */
gamma = 1.0f;
if (*argv) {
May 29, 2006
May 29, 2006
107
gamma = (float) atof(*argv);
May 28, 2006
May 28, 2006
108
}
May 29, 2006
May 29, 2006
109
110
111
if (SDL_SetGamma(gamma, gamma, gamma) < 0) {
fprintf(stderr, "Unable to set gamma: %s\n", SDL_GetError());
quit(1);
May 28, 2006
May 28, 2006
112
113
114
115
}
#if 0 /* This isn't supported. Integrating the gamma ramps isn't exact */
/* See what gamma was actually set */
float real[3];
May 29, 2006
May 29, 2006
116
117
if (SDL_GetGamma(&real[0], &real[1], &real[2]) < 0) {
printf("Couldn't get gamma: %s\n", SDL_GetError());
May 28, 2006
May 28, 2006
118
} else {
May 29, 2006
May 29, 2006
119
120
printf("Set gamma values: R=%2.2f, G=%2.2f, B=%2.2f\n",
real[0], real[1], real[2]);
May 28, 2006
May 28, 2006
121
}
Apr 26, 2001
Apr 26, 2001
122
123
#endif
May 28, 2006
May 28, 2006
124
/* Do all the drawing work */
May 29, 2006
May 29, 2006
125
image = SDL_LoadBMP("sample.bmp");
May 28, 2006
May 28, 2006
126
127
128
129
130
131
132
if (image) {
SDL_Rect dst;
dst.x = (screen->w - image->w) / 2;
dst.y = (screen->h - image->h) / 2;
dst.w = image->w;
dst.h = image->h;
May 29, 2006
May 29, 2006
133
134
SDL_BlitSurface(image, NULL, screen, &dst);
SDL_UpdateRects(screen, 1, &dst);
May 28, 2006
May 28, 2006
135
136
137
}
/* Wait a bit, handling events */
May 29, 2006
May 29, 2006
138
then = SDL_GetTicks();
May 28, 2006
May 28, 2006
139
timeout = (5 * 1000);
May 29, 2006
May 29, 2006
140
while ((SDL_GetTicks() - then) < timeout) {
May 28, 2006
May 28, 2006
141
142
SDL_Event event;
May 29, 2006
May 29, 2006
143
while (SDL_PollEvent(&event)) {
May 28, 2006
May 28, 2006
144
145
146
147
148
149
150
151
152
153
154
switch (event.type) {
case SDL_QUIT: /* Quit now */
timeout = 0;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_SPACE: /* Go longer.. */
timeout += (5 * 1000);
break;
case SDLK_UP:
gamma += 0.2f;
May 29, 2006
May 29, 2006
155
SDL_SetGamma(gamma, gamma, gamma);
May 28, 2006
May 28, 2006
156
157
158
break;
case SDLK_DOWN:
gamma -= 0.2f;
May 29, 2006
May 29, 2006
159
SDL_SetGamma(gamma, gamma, gamma);
May 28, 2006
May 28, 2006
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
break;
case SDLK_ESCAPE:
timeout = 0;
break;
default:
break;
}
break;
}
}
}
/* Perform a gamma flash to red using color ramps */
while (gamma < 10.0) {
/* Increase the red gamma and decrease everything else... */
gamma += 0.1f;
May 29, 2006
May 29, 2006
176
177
178
CalculateGamma(gamma, red_ramp);
CalculateGamma(1.0 / gamma, ramp);
SDL_SetGammaRamp(red_ramp, ramp, ramp);
May 28, 2006
May 28, 2006
179
180
}
/* Finish completely red */
May 29, 2006
May 29, 2006
181
182
183
memset(red_ramp, 255, sizeof(red_ramp));
memset(ramp, 0, sizeof(ramp));
SDL_SetGammaRamp(red_ramp, ramp, ramp);
May 28, 2006
May 28, 2006
184
185
186
/* Now fade out to black */
for (i = (red_ramp[0] >> 8); i >= 0; --i) {
May 29, 2006
May 29, 2006
187
188
memset(red_ramp, i, sizeof(red_ramp));
SDL_SetGammaRamp(red_ramp, NULL, NULL);
May 28, 2006
May 28, 2006
189
}
May 29, 2006
May 29, 2006
190
SDL_Delay(1 * 1000);
May 28, 2006
May 28, 2006
191
May 29, 2006
May 29, 2006
192
SDL_Quit();
May 28, 2006
May 28, 2006
193
return (0);
Apr 26, 2001
Apr 26, 2001
194
}