-
Notifications
You must be signed in to change notification settings - Fork 1
/
ogl.c
executable file
·137 lines (112 loc) · 2.99 KB
/
ogl.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
#include <stdlib.h>
#include "ogl.h"
#include "config.h"
static int width = 0;
static int height = 0;
static int rotation = 0;
static int nopt_textures = 0;
static GLfloat xscale = 1.0;
static GLfloat yscale = 1.0;
int ogl_init( void ) {
const struct config *config = config_get();
GLenum error = GL_NO_ERROR;
glLoadIdentity();
glClearColor( 0, 0, 0, 1.0 );
glEnable( GL_TEXTURE_2D );
glShadeModel( GL_SMOOTH );
glClearDepth( 1.0 );
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LEQUAL );
glEnable( GL_BLEND );
if( config->iface.gfx_quality > CONFIG_LOW )
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0, (GLfloat)config->iface.screen_width/(GLfloat)config->iface.screen_height, 0.1, 100.0 );
glMatrixMode( GL_MODELVIEW );
width = config->iface.screen_width;
height = config->iface.screen_height;
if( config->iface.screen_hflip )
yscale = -yscale;
if( config->iface.screen_vflip )
xscale = -xscale;
ogl_screen_rotate( config->iface.screen_rotation );
ogl_load_alterego();
if( strstr( (char*)glGetString(GL_EXTENSIONS), "GL_ARB_texture_non_power_of_two" ) != NULL )
nopt_textures = 1;
error = glGetError();
if( error == GL_NO_ERROR ) {
return 0;
}
else {
fprintf(stderr, "Error: couldn't initialise OpenGL: %s\n", gluErrorString(error) );
return -1;
}
}
int ogl_screen_width( void ) {
return width;
}
int ogl_screen_height( void ) {
return height;
}
GLfloat ogl_xfactor( void ) {
return (GLfloat)width/(GLfloat)config_get()->iface.screen_width;
}
GLfloat ogl_yfactor( void ) {
return (GLfloat)height/(GLfloat)config_get()->iface.screen_height;
}
GLfloat ogl_aspect_ratio( void ) {
return (GLfloat)config_get()->iface.screen_width/(GLfloat)config_get()->iface.screen_height;
}
int ogl_screen_orientation( void ) {
if( height > width )
return CONFIG_PORTRAIT;
else
return CONFIG_LANDSCAPE;
}
void ogl_screen_rotate( int angle ) {
int tmp = -angle; /* Switch clockwise to anti-clockwise */
if( tmp % 90 != 0 ) {
tmp -= (tmp % 90);
fprintf( stderr, "Warning: Screen rotation rounded down to nearest 90 degrees\n" );
}
if( (rotation % 180 == 0 && tmp % 180 != 0)
|| (rotation % 180 != 0 && tmp % 180 == 0) ) {
/* Swap height and width */
int swap = width;
width = height;
height = swap;
}
rotation = tmp;
}
void ogl_load_alterego( void ) {
glLoadIdentity();
glRotatef( (GLfloat)rotation, 0.0, 0.0, 1.0 );
glScalef( xscale, yscale, 1.0 );
}
struct texture *ogl_create_empty_texture( void ) {
struct texture *t = malloc( sizeof(struct texture) );
if( t ) {
memset( t, 0, sizeof(struct texture) );
glGenTextures( 1, &t->id );
}
return t;
}
void ogl_free_texture( struct texture *t ) {
if( t ) {
if( t->id )
glDeleteTextures( 1, &t->id );
free( t );
t = NULL;
}
}
void ogl_clear( void ) {
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
void ogl_load_alterego();
}
void ogl_flush( void ) {
glFlush();
}
int ogl_nopt_textures( void ) {
return nopt_textures;
}