forked from SteveMaddison/cabrio
-
Notifications
You must be signed in to change notification settings - Fork 4
/
snap.c
256 lines (224 loc) · 6.56 KB
/
snap.c
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
#include "snap.h"
#include "game.h"
#include "config.h"
#include "ogl.h"
#include "sdl_ogl.h"
#include "media.h"
#include "video.h"
static const GLfloat DEPTH = -8;
static const GLfloat MAX_SIZE = 280;
static const GLfloat SCALE_FACTOR = 0.012;
static const GLfloat PLATFORM_SIZE = 0.8;
static struct texture *texture = NULL;
static struct texture *platform_texture = NULL;
#define NUM_NOISE 3
static struct texture *noise[NUM_NOISE];
static int frame = 0;
static int noise_skip = 10;
static const int MAX_STEPS = 50;
static int steps = 50;
static int step = 0;
static int hide_direction = 0;
static int visible = 0;
static GLfloat scale = 0.006;
static GLfloat platform_scale = 1;
static GLfloat hidden_offset = -4.0;
static int video = 0;
static int width, height;
int snap_init( void ) {
const struct config *config = config_get();
int i;
noise[0] = sdl_create_texture( DATA_DIR "/pixmaps/noise1.png" );
noise[1] = sdl_create_texture( DATA_DIR "/pixmaps/noise2.png" );
noise[2] = sdl_create_texture( DATA_DIR "/pixmaps/noise3.png" );
if( noise[0] == NULL || noise[1] == NULL || noise[2] == NULL ) {
fprintf( stderr, "Warning: Couldn't create texture for snap noise\n" );
return -1;
}
for( i = 0 ; i < NUM_NOISE ; i++ ) {
noise[i]->width = MAX_SIZE;
noise[i]->height = MAX_SIZE / ogl_aspect_ratio();
}
if( config->iface.frame_rate ) {
noise_skip = config->iface.frame_rate / 10;
steps = config->iface.frame_rate / 4;
}
else {
steps = MAX_STEPS;
}
scale = config->iface.theme.snap.size * SCALE_FACTOR;
platform_scale = config->iface.theme.snap.size * PLATFORM_SIZE;
if( config->iface.theme.snap.offset1 > 0 )
hidden_offset = -hidden_offset;
return 0;
}
void snap_free( void ) {
int i;
snap_clear();
for( i = 0 ; i < NUM_NOISE ; i++ ) {
ogl_free_texture( noise[i] );
noise[i] = NULL;
}
}
void snap_pause( void ) {
snap_free();
}
int snap_resume( void ) {
return snap_init();
}
int snap_set( struct game *game ) {
const struct config_snap *config = &config_get()->iface.theme.snap;
char *filename;
snap_clear();
texture = NULL;
video = 0;
platform_texture = game->platform->texture;
filename = game_media_get( game, MEDIA_VIDEO, NULL );
if( filename && filename[0] ) {
if( video_open( filename ) == 0 ) {
video = 1;
texture = video_texture();
}
}
// no video ? ok screenshot //
if( !texture ) {
filename = game_media_get( game, MEDIA_IMAGE, image_type_name(IMAGE_SCREENSHOT) );
if( filename && filename[0] ) {
texture = sdl_create_texture( filename );
}
else {
return -1;
}
}
if( texture ) {
if( config->fix_aspect_ratio ) {
if( texture->width > texture->height ) {
/* Landscape */
width = MAX_SIZE;
height = MAX_SIZE / ogl_aspect_ratio();
}
else {
/* Portrait */
height = MAX_SIZE;
width = MAX_SIZE / ogl_aspect_ratio();
}
}
else {
if( texture->width > texture->height ) {
/* Landscape */
height = (int)(float)texture->height/((float)texture->width/MAX_SIZE);
width = MAX_SIZE;
}
else {
/* Portrait */
width = (int)(float)texture->width/((float)texture->height/MAX_SIZE);
height = MAX_SIZE;
}
}
return 0;
}
return -1;
}
void snap_clear( void ) {
if( video ) {
video_close();
video = 0;
}
else {
if( texture ) {
ogl_free_texture( texture );
}
}
texture = NULL;
platform_texture = NULL;
}
void snap_show( void ) {
if( !visible ) {
visible = 1;
hide_direction = 1;
step = steps;
}
}
void snap_hide( void ) {
if( visible ) {
hide_direction = -1;
step = steps;
}
}
void snap_draw( void ) {
const struct config_snap *config = &config_get()->iface.theme.snap;
if( visible ) {
struct texture *t = texture;
GLfloat xfactor = ogl_xfactor();
GLfloat yfactor = ogl_yfactor();
GLfloat xsize, ysize, hide_offset;
if( video ){
t = video_get_frame();
}
if( t == NULL )
t = noise[frame/noise_skip];
xsize = (width/2) * scale * xfactor;
ysize = (height/2) * scale * xfactor;
hide_offset = (((hidden_offset - config->offset1) / (GLfloat)steps) * (GLfloat)step);
ogl_load_alterego();
if( hide_direction == -1 ) {
if( config_get()->iface.theme.game_sel.orientation == CONFIG_PORTRAIT )
glTranslatef( (hidden_offset - hide_offset) * xfactor, config->offset2 * yfactor, DEPTH );
else
glTranslatef( config->offset2 * xfactor, (hidden_offset - hide_offset) * yfactor, DEPTH );
}
else {
if( config_get()->iface.theme.game_sel.orientation == CONFIG_PORTRAIT )
glTranslatef( (config->offset1 + hide_offset) * xfactor, config->offset2 * yfactor, DEPTH );
else
glTranslatef( config->offset2 * xfactor, (config->offset1 + hide_offset) * yfactor, DEPTH );
}
glRotatef( config->angle_x, 1.0, 0.0, 0.0 );
glRotatef( config->angle_y, 0.0, 1.0, 0.0 );
glRotatef( config->angle_z, 0.0, 0.0, 1.0 );
glColor4f( 1.0, 1.0, 1.0, 1.0 );
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glBindTexture( GL_TEXTURE_2D, t->id );
glBegin( GL_QUADS );
glTexCoord2f(0.0, 0.0); glVertex3f(-xsize, ysize, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-xsize, -ysize, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f( xsize, -ysize, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f( xsize, ysize, 0.0);
glEnd();
if( config->platform_icons && platform_count() > 1 && platform_texture ) {
GLfloat platform_xsize = platform_texture->width;
GLfloat platform_ysize = platform_texture->height;
if( platform_xsize > platform_ysize ) {
platform_ysize = platform_scale * platform_ysize/platform_xsize;
platform_xsize = platform_scale;
}
else {
platform_xsize = platform_scale * platform_xsize/platform_ysize;
platform_ysize = platform_scale;
}
glTranslatef( xsize * 0.8, -ysize * 0.9, 0.1 );
glRotatef( -config->angle_z, 0.0, 0.0, 1.0 );
glBindTexture( GL_TEXTURE_2D, platform_texture->id );
glBegin( GL_QUADS );
glTexCoord2f(0.0, 0.0); glVertex3f(-platform_xsize, platform_ysize, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-platform_xsize, -platform_ysize, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f( platform_xsize, -platform_ysize, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f( platform_xsize, platform_ysize, 0.0);
glEnd();
}
if( ++frame >= NUM_NOISE * noise_skip )
frame = 0;
if( step && --step == 0 ) {
if( hide_direction < 0 ) {
visible = 0;
snap_clear();
}
else {
visible = 1;
}
hide_direction = 0;
}
}
}