358
|
1 //video_init.c - SDL/Opengl Windowing Creation/Resizing Functions
|
|
2 //
|
|
3 //by Peter Sperl
|
|
4 //
|
|
5 //Opens an SDL Window and creates an OpenGL session
|
|
6 //also able to handle resizing and fullscreening of windows
|
|
7 //just call init_display again with differant variables
|
|
8
|
|
9 #include <SDL/SDL.h>
|
|
10 #include <GL/gl.h>
|
|
11 #include <GL/glu.h>
|
|
12
|
|
13 extern SDL_Surface *screen;
|
|
14 extern int texsize;
|
|
15 void setup_opengl( int w, int h );
|
|
16
|
|
17 void close_display() {
|
|
18 SDL_Quit();
|
|
19 }
|
|
20
|
|
21 void resize_display(int w, int h, int f) {
|
|
22 int flags;
|
|
23 if (f) flags = SDL_OPENGL|SDL_HWSURFACE|SDL_FULLSCREEN;
|
|
24 else flags = SDL_OPENGL|SDL_HWSURFACE|SDL_RESIZABLE;
|
|
25 // SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
|
|
26 screen = SDL_SetVideoMode( w, h, 0, flags ) ;
|
|
27 if(screen == 0 ) {
|
|
28 fprintf( stderr, "Video mode set failed: %s\n", SDL_GetError( ) );
|
|
29 return;
|
|
30 }
|
|
31 setup_opengl(w,h);
|
|
32 SDL_ShowCursor(f ? SDL_DISABLE : SDL_ENABLE);
|
|
33 }
|
|
34
|
|
35 //init_display
|
|
36 //
|
|
37 //Sets screen to new width and height (w,h)
|
|
38 //Also switches between fullscreen and windowed
|
|
39 //with the boolean f (fullscreen)
|
|
40 void init_display(int w, int h, int f)
|
|
41 {
|
|
42
|
|
43 /* Information about the current video settings. */
|
|
44 const SDL_VideoInfo* info = NULL;
|
|
45 int bpp = 0;
|
|
46 /* Flags we will pass into SDL_SetVideoMode. */
|
|
47 int flags = 0;
|
|
48 /* First, initialize SDL's video subsystem. */
|
|
49 if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER ) < 0 ) {
|
|
50 /* Failed, exit. */
|
|
51 fprintf( stderr, "Video initialization failed: %s\n",
|
|
52 SDL_GetError( ) );
|
|
53 //projectM_vtable.disable_plugin (&projectM_vtable);
|
|
54 return;
|
|
55
|
|
56 }
|
|
57 /* Let's get some video information. */
|
|
58 info = SDL_GetVideoInfo( );
|
|
59 if( !info ) {
|
|
60 /* This should probably never happen. */
|
|
61 fprintf( stderr, "Video query failed: %s\n",
|
|
62 SDL_GetError( ) );
|
|
63 // projectM_vtable.disable_plugin (&projectM_vtable);
|
|
64 return;
|
|
65 }
|
|
66
|
|
67 bpp = info->vfmt->BitsPerPixel;
|
|
68
|
|
69 //SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
|
|
70 //SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
|
|
71 //SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
|
|
72
|
|
73 // SDL_GL_SetAttribute( SDL_GL_ACCUM_RED_SIZE, 8 );
|
|
74 // SDL_GL_SetAttribute( SDL_GL_ACCUM_GREEN_SIZE, 8 );
|
|
75 // SDL_GL_SetAttribute( SDL_GL_ACCUM_BLUE_SIZE, 8 );
|
|
76 SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
|
|
77 SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
|
|
78 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
|
|
79
|
|
80 if (f==0)
|
|
81 flags = SDL_OPENGL|SDL_HWSURFACE|SDL_RESIZABLE;
|
|
82 else flags = SDL_OPENGL|SDL_HWSURFACE|SDL_FULLSCREEN;
|
|
83
|
|
84 screen= SDL_SetVideoMode( w, h, bpp, flags ) ;
|
|
85
|
|
86 if(screen == 0 ) {
|
|
87 /*
|
|
88 * This could happen for a variety of reasons,
|
|
89 * including DISPLAY not being set, the specified
|
|
90 * resolution not being available, etc.
|
|
91 */
|
|
92 fprintf( stderr, "Video mode set failed: %s\n",
|
|
93 SDL_GetError( ) );
|
|
94
|
|
95 // projectM_vtable.disable_plugin (&projectM_vtable);
|
|
96 return;
|
|
97
|
|
98 }
|
|
99
|
|
100
|
|
101 // setup_opengl(w,h);
|
|
102 //gluOrtho2D(0, w, 0, h);
|
|
103 }
|
|
104
|
|
105
|
|
106 void setup_opengl( int w, int h )
|
|
107 {
|
|
108
|
|
109 /* Our shading model--Gouraud (smooth). */
|
|
110 glShadeModel( GL_SMOOTH);
|
|
111 /* Culling. */
|
|
112 // glCullFace( GL_BACK );
|
|
113 // glFrontFace( GL_CCW );
|
|
114 // glEnable( GL_CULL_FACE );
|
|
115 /* Set the clear color. */
|
|
116 glClearColor( 0, 0, 0, 0 );
|
|
117 /* Setup our viewport. */
|
|
118 glViewport( 0, 0, w, h );
|
|
119 /*
|
|
120 * Change to the projection matrix and set
|
|
121 * our viewing volume.
|
|
122 */
|
|
123 glMatrixMode(GL_TEXTURE);
|
|
124 glLoadIdentity();
|
|
125
|
|
126 // gluOrtho2D(0.0, (GLfloat) width, 0.0, (GLfloat) height);
|
|
127 glMatrixMode(GL_PROJECTION);
|
|
128 glLoadIdentity();
|
|
129
|
|
130 // glFrustum(0.0, height, 0.0,width,10,40);
|
|
131 glMatrixMode(GL_MODELVIEW);
|
|
132 glLoadIdentity();
|
|
133
|
|
134 glDrawBuffer(GL_BACK);
|
|
135 glReadBuffer(GL_BACK);
|
|
136 glEnable(GL_BLEND);
|
|
137
|
|
138 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
139 // glBlendFunc(GL_SRC_ALPHA, GL_ONE);
|
|
140 glEnable(GL_LINE_SMOOTH);
|
|
141 glEnable(GL_POINT_SMOOTH);
|
|
142 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
|
143 glClear(GL_COLOR_BUFFER_BIT);
|
|
144
|
|
145 // glCopyTexImage2D(GL_TEXTURE_2D,0,GL_RGB,0,0,texsize,texsize,0);
|
|
146 //glCopyTexSubImage2D(GL_TEXTURE_2D,0,0,0,0,0,texsize,texsize);
|
|
147 glLineStipple(2, 0xAAAA);
|
|
148
|
|
149
|
|
150 }
|
|
151 #if 0
|
|
152 void CreateRenderTarget(int texsize,int *RenderTargetTextureID, int *RenderTarget )
|
|
153 {
|
|
154 /* Create the texture that will be bound to the render target */
|
|
155 glGenTextures(1, RenderTargetTextureID);
|
|
156 glBindTexture(GL_TEXTURE_2D, *RenderTargetTextureID);
|
|
157 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
158 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
159
|
|
160 /* Create the render target */
|
|
161 *RenderTarget = SDL_GL_CreateRenderTarget(texsize,texsize, NULL);
|
|
162 if ( *RenderTarget ) {
|
|
163
|
|
164 int value;
|
|
165 //printf("Created render target:\n");
|
|
166 SDL_GL_GetRenderTargetAttribute( *RenderTarget, SDL_GL_RED_SIZE, &value );
|
|
167 // printf( "SDL_GL_RED_SIZE: %d\n", value);
|
|
168 SDL_GL_GetRenderTargetAttribute( *RenderTarget, SDL_GL_GREEN_SIZE, &value );
|
|
169 // printf( "SDL_GL_GREEN_SIZE: %d\n", value);
|
|
170 SDL_GL_GetRenderTargetAttribute( *RenderTarget, SDL_GL_BLUE_SIZE, &value );
|
|
171 // printf( "SDL_GL_BLUE_SIZE: %d\n", value);
|
|
172 SDL_GL_GetRenderTargetAttribute( *RenderTarget, SDL_GL_ALPHA_SIZE, &value );
|
|
173 // printf( "SDL_GL_ALPHA_SIZE: %d\n", value);
|
|
174 SDL_GL_GetRenderTargetAttribute( *RenderTarget, SDL_GL_DEPTH_SIZE, &value );
|
|
175 // printf( "SDL_GL_DEPTH_SIZE: %d\n", value );
|
|
176
|
|
177 SDL_GL_BindRenderTarget(*RenderTarget, *RenderTargetTextureID);
|
|
178
|
|
179 } else {
|
|
180 /* We can fake a render target in this demo by rendering to the
|
|
181 * screen and copying to a texture before we do normal rendering.
|
|
182 */
|
|
183 printf("Failed to create render target, using screen buffer\n");
|
|
184
|
|
185 glBindTexture(GL_TEXTURE_2D, *RenderTargetTextureID);
|
|
186 glTexImage2D(GL_TEXTURE_2D,
|
|
187 0,
|
|
188 GL_RGB,
|
|
189 texsize, texsize,
|
|
190 0,
|
|
191 GL_RGB,
|
|
192 GL_UNSIGNED_BYTE,
|
|
193 NULL);
|
|
194 }
|
|
195
|
|
196
|
|
197 }
|
|
198 #endif
|